Last active
August 29, 2015 14:18
-
-
Save TheSecretSquad/c7ce1a12615b6f1dc25c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MovesReceiver { | |
void path(final MoveCount moveCount, | |
final Consumer<PathDirection> pathDirectionConsumer, | |
final Consumer<CollisionRule> collisionRuleConsumer); | |
void step(final Consumer<StepDirection> stepDirectionConsumer, | |
final Consumer<CollisionRule> collisionRuleConsumer); | |
} | |
public class MoveCount { | |
private final int number; | |
public MoveCount(final int number) { | |
if(number < 0) | |
throw new NegativeMoveCountException(); | |
this.number = number; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + number; | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
MoveCount other = (MoveCount) obj; | |
if (number != other.number) | |
return false; | |
return true; | |
} | |
} | |
public interface PathDirection extends RankPathDirection, FilePathDirection { | |
void forwardAnd(final Consumer<FilePathDirection> filePathDirectionConsumer); | |
void backwardAnd(final Consumer<FilePathDirection> filePathDirectionConsumer); | |
void leftAnd(final Consumer<RankPathDirection> rankPathDirectionConsumer); | |
void rightAnd(final Consumer<RankPathDirection> rankPathDirectionConsumer); | |
} | |
public interface RankPathDirection { | |
void forward(); | |
void backward(); | |
} | |
public interface FilePathDirection { | |
void left(); | |
void right(); | |
} | |
public interface StepDirection extends RankStepDirection, FileStepDirection { | |
void forwardAnd(final MoveCount moveCount, final Consumer<FileStepDirection> fileStepDirectionConsumer); | |
void backwardAnd(final MoveCount moveCount, final Consumer<FileStepDirection> fileStepDirectionConsumer); | |
void leftAnd(final MoveCount moveCount, final Consumer<RankStepDirection> rankStepDirectionConsumer); | |
void rightAnd(final MoveCount moveCount, final Consumer<RankStepDirection> rankStepDirectionConsumer); | |
} | |
public interface RankStepDirection { | |
void forward(final MoveCount moveCount); | |
void backward(final MoveCount moveCount); | |
} | |
public interface FileStepDirection { | |
void left(final MoveCount moveCount); | |
void right(final MoveCount moveCount); | |
} | |
public interface CollisionRule { | |
void avoidEnemiesOf(final Piece piece); | |
void attackEnemies(final Piece piece); | |
} | |
public class Pawn extends ChessPiece { | |
public Pawn(final Color color) { | |
super(ChessPieceType.PAWN, color); | |
} | |
@Override | |
public void describeMovesTo(final MovesReceiver movesReceiver) { | |
movesReceiver.path(new MoveCount(2), | |
(pathDirection) -> pathDirection.forward(), | |
(collisionRule) -> collisionRule.avoidEnemiesOf(this)); | |
movesReceiver.step( | |
(stepDirection) -> stepDirection.forwardAnd(new MoveCount(1), | |
(fileDirection) -> fileDirection.left(new MoveCount(1))), | |
(collisionRule) -> collisionRule.attackEnemies(this)); | |
movesReceiver.step( | |
(stepDirection) -> stepDirection.forwardAnd(new MoveCount(1), | |
(fileDirection) -> fileDirection.right(new MoveCount(1))), | |
(collisionRule) -> collisionRule.attackEnemies(this)); | |
} | |
} | |
// Test Sample | |
private Pawn pawn; | |
@Mock | |
private MovesReceiver movesReceiver; | |
@Captor | |
private ArgumentCaptor<Consumer<PathDirection>> pathDirectionConsumerCaptor; | |
@Captor | |
private ArgumentCaptor<Consumer<CollisionRule>> collisionRuleConsumerCaptor; | |
@Mock | |
private PathDirection pathDirection; | |
@Mock | |
private CollisionRule collisionRule; | |
private void verifyGoesForwardAndAvoidsEnemies(final Consumer<PathDirection> pathDirectionConsumer) { | |
pathDirectionConsumer.accept(pathDirection); | |
verify(pathDirection).forward(); | |
} | |
private void verifyAvoidsEnemies(final Consumer<CollisionRule> collisionRuleConsumer) { | |
collisionRuleConsumer.accept(collisionRule); | |
verify(collisionRule).avoidEnemiesOf(pawn); | |
} | |
@Before | |
public void setUp() throws Exception { | |
pawn = new Pawn(Color.LIGHT); | |
} | |
@Test | |
public void WhenDescribingMoves_IfHasNotMoved_ShouldMoveInAPathTwoForwardAvoidingEnemies() { | |
pawn.describeMovesTo(movesReceiver); | |
verify(movesReceiver).path(eq(new MoveCount(2)), pathDirectionConsumerCaptor.capture(), collisionRuleConsumerCaptor.capture()); | |
verifyGoesForwardAndAvoidsEnemies(pathDirectionConsumerCaptor.getValue()); | |
verifyAvoidsEnemies(collisionRuleConsumerCaptor.getValue()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment