Sunday, March 29, 2015

Unit-Testing Tutorial 2 Appendix: My Tests

package com.proquest.chess;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class PawnTest
{
   private Board board;
   private Pawn whitePawn;
   private Pawn blackPawn;
   private Knight whiteKnight;
   private Knight blackKnight;
   private Bishop whiteBishop;
   private Bishop blackBishop;

   @Before
   public void setUp() {
      board = new Board();
      whitePawn = new Pawn(Color.White);
      blackPawn = new Pawn(Color.Black);
      whiteKnight = new Knight(Color.White);
      blackKnight = new Knight(Color.Black);
      whiteBishop = new Bishop(Color.White);
      blackBishop = new Bishop(Color.Black);
   }

   @Test
   public void shouldAdvancePawnOneSquare_fromMiddleOfBoard() {
      board.placePiece(whitePawn, new Position(2, 3));
      assertPawnCanMoveTo (whitePawn, 2, 4);
      board.placePiece(blackPawn, new Position(2, 3));
      assertPawnCanMoveTo (blackPawn, 2, 2);
   }

   @Test
   public void shouldNotAdvancePawnIfBlocked() {
      board.placePiece(whitePawn, new Position(2, 3));
      board.placePiece(blackPawn, new Position(2, 4));
      assertPawnCanMoveTo (whitePawn);
      assertPawnCanMoveTo (blackPawn);
   }

   private void assertPawnCanMoveTo (Pawn pawn, Integer ... xyValues) {
      List<Move> moves = pawn.makeMoves(board);
      assertEquals (xyValues.length/2, moves.size());
      for (int i=0; i<xyValues.length; i+=2) {
         ChessTestUtils.findMoveToSquare(moves, xyValues[i],
         xyValues[i+1]);
      }
   }

   @Test
   public void shouldAdvancePawnTwoSquares_fromStartingPosition() {
      board.placePiece(whitePawn, new Position(2, 1));
      assertPawnCanMoveTo(whitePawn, 2,2, 2,3);
      board.placePiece(blackPawn, new Position(2, 6));
      assertPawnCanMoveTo(blackPawn, 2,5, 2,4);
   }

   @Test
   public void shouldNotAdvancePawnTwoSquares_fromStartingPosition_when2ndMoveBlocked() {
      board.placePiece(whitePawn, new Position(2, 1));
      board.placePiece(whiteKnight, new Position(2, 3));
      assertPawnCanMoveTo(whitePawn, 2,2);
      board.placePiece(blackPawn, new Position(2, 6));
      board.placePiece(blackKnight, new Position(2, 4));
      assertPawnCanMoveTo(blackPawn, 2,5);
   }

   @Test
   public void shouldCaptureForwardAndRightOrLeft_forWhite() {
      board.placePiece(whitePawn, new Position(4, 3));
      board.placePiece(blackKnight, new Position(5, 4));
      board.placePiece(blackBishop, new Position(3, 4));
      assertPawnCanCapture (whitePawn, blackKnight);
      assertPawnCanCapture (whitePawn, blackBishop);
   }

   @Test
   public void shouldCaptureForwardAndRightOrLeft_forBlack() {
      board.placePiece(blackPawn, new Position(5, 4));
      board.placePiece(whiteKnight, new Position(6, 3));
      board.placePiece(whiteBishop, new Position(4, 3));
      assertPawnCanCapture (blackPawn, whiteKnight);
      assertPawnCanCapture (blackPawn, whiteBishop);
   }

   @Test
   public void shouldCaptureForwardAndRight_atLeftEdgeOfBoard() {
      board.placePiece(whitePawn, new Position(0, 3));
      board.placePiece(blackKnight, new Position(1, 4));
      assertPawnCanCapture (whitePawn, blackKnight);
      assertEquals (1, countCaptures(whitePawn.makeMoves(board)));
   }

   @Test
   public void shouldNotCaptureForwardAndRightOrLeft_forWhitesOwnPieces() {
      board.placePiece(whitePawn, new Position(4, 3));
      board.placePiece(whiteKnight, new Position(5, 4));
      board.placePiece(whiteBishop, new Position(3, 4));
      assertEquals (0, countCaptures(whitePawn.makeMoves(board)));
   }

   private void assertPawnCanCapture (Pawn pawn, Piece piece) {
      List<Move> moves = pawn.makeMoves(board);
      Move capture = ChessTestUtils.findMoveToSquare(moves,
      piece.getPosition().getX(), piece.getPosition().getY());
      assertEquals (piece, capture.getCapture());
   }

   private int countCaptures (List moves) {
      int count = 0;
      for (Move move: moves) {
         if (move.getCapture() != null) ++count;
      }
      return count;
   }
}

No comments:

Post a Comment