Commit cb3f5fad by Paktalin

Test refactoring

parent 4a9cc889
...@@ -32,16 +32,28 @@ public class Board { ...@@ -32,16 +32,28 @@ public class Board {
layout.placePiece(piece, Position.create(position)); layout.placePiece(piece, Position.create(position));
} }
public void placePiece(Piece piece, Position position) {
layout.placePiece(piece, position);
}
public void move(Position from, Position to) { public void move(Position from, Position to) {
Piece piece = layout.getPieceAtPosition(from); Piece piece = layout.getPieceAtPosition(from);
if (piece.isMovable(from, to)) if (piece.isMovable(from, to))
layout.move(from, to); layout.move(from, to);
} }
public void move(Piece piece, Position to) {
move(piece.getPosition(), to);
}
public Piece getPieceAtPosition(String position) { public Piece getPieceAtPosition(String position) {
return layout.getPieceAtPosition(Position.create(position)); return layout.getPieceAtPosition(Position.create(position));
} }
public Piece getPieceAtPosition(Position position) {
return layout.getPieceAtPosition(position);
}
String print() { String print() {
return layout.print(); return layout.print();
} }
......
package com.example.paktalin.agilejava_exercises.moves; package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Piece;
import com.example.paktalin.agilejava_exercises.Position; import com.example.paktalin.agilejava_exercises.Position;
/** /**
...@@ -9,6 +10,11 @@ import com.example.paktalin.agilejava_exercises.Position; ...@@ -9,6 +10,11 @@ import com.example.paktalin.agilejava_exercises.Position;
public class KingMoveStrategyTest extends MoveStrategyTest { public class KingMoveStrategyTest extends MoveStrategyTest {
@Override @Override
void putPieceOnBoard() {
board.placePiece(Piece.createKing(Piece.Color.Black), currentPosition);
}
@Override
void setAccessiblePosition() { void setAccessiblePosition() {
accessiblePosition = Position.create("a6"); accessiblePosition = Position.create("a6");
} }
......
...@@ -13,30 +13,30 @@ import junit.framework.TestCase; ...@@ -13,30 +13,30 @@ import junit.framework.TestCase;
public abstract class MoveStrategyTest extends TestCase { public abstract class MoveStrategyTest extends TestCase {
private Piece piece; private Piece piece;
private Board board; Board board;
Position accessiblePosition, notAccessiblePosition; Position accessiblePosition, notAccessiblePosition;
Position currentPosition = Position.create("b5");
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
board = Board.createEmpty(); board = Board.createEmpty();
board.placePiece(Piece.createKing(Piece.Color.Black), "b5"); putPieceOnBoard();
piece = board.getPieceAtPosition("b5"); piece = board.getPieceAtPosition(currentPosition);
setAccessiblePosition(); setAccessiblePosition();
setNotAccessiblePosition(); setNotAccessiblePosition();
} }
abstract void putPieceOnBoard();
abstract void setAccessiblePosition(); abstract void setAccessiblePosition();
abstract void setNotAccessiblePosition(); abstract void setNotAccessiblePosition();
public void testMove() { public void testMove() {
Position currentPosition = piece.getPosition(); board.move(piece, accessiblePosition);
board.move(currentPosition, accessiblePosition);
assertTrue(piece.isAtPosition(accessiblePosition)); assertTrue(piece.isAtPosition(accessiblePosition));
assertFalse(piece.isAtPosition(currentPosition)); assertFalse(piece.isAtPosition(currentPosition));
currentPosition = accessiblePosition; currentPosition = accessiblePosition;
board.move(currentPosition, notAccessiblePosition); board.move(piece, notAccessiblePosition);
assertTrue(piece.isAtPosition(currentPosition)); assertTrue(piece.isAtPosition(currentPosition));
assertFalse(piece.isAtPosition(notAccessiblePosition)); assertFalse(piece.isAtPosition(notAccessiblePosition));
} }
......
package com.example.paktalin.agilejava_exercises.moves; package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Piece;
import com.example.paktalin.agilejava_exercises.Position; import com.example.paktalin.agilejava_exercises.Position;
/** /**
...@@ -9,6 +10,11 @@ import com.example.paktalin.agilejava_exercises.Position; ...@@ -9,6 +10,11 @@ import com.example.paktalin.agilejava_exercises.Position;
public class QueenMoveStrategyTest extends MoveStrategyTest { public class QueenMoveStrategyTest extends MoveStrategyTest {
@Override @Override
void putPieceOnBoard() {
board.placePiece(Piece.createQueen(Piece.Color.Black), currentPosition);
}
@Override
void setAccessiblePosition() { void setAccessiblePosition() {
accessiblePosition = Position.create("d3"); accessiblePosition = Position.create("d3");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment