chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 26 Jul 2013 17:14:25 +0200
branchchess
changeset 34 1ebd52ae8ccb
parent 30 a46846115b83
child 35 7ae0125d57b2
permissions -rw-r--r--
Show accessible squares when a figure is selected
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     6  * Permission is hereby granted, free of charge, to any person obtaining a copy
     7  * of this software and associated documentation files (the "Software"), to deal
     8  * in the Software without restriction, including without limitation the rights
     9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  * copies of the Software, and to permit persons to whom the Software is
    11  * furnished to do so, subject to the following conditions:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  * THE SOFTWARE.
    23  */
    24 package org.apidesign.html.demo.chess;
    25 
    26 import org.apidesign.html.demo.chess.BoardModel.ColorType;
    27 import org.apidesign.html.demo.chess.BoardModel.PieceType;
    28 import static org.testng.Assert.*;
    29 import org.testng.annotations.Test;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public class BoardModelTest {
    36     
    37     public BoardModelTest() {
    38     }
    39 
    40 
    41     @Test public void e2e4e7e6() {
    42         Board b = BoardModel.createBoard();
    43         assertEquals(b.getTurn(), ColorType.WHITE);
    44         
    45         Square e2 = BoardModel.findSquare(b, 'E', 2);
    46         assertNotNull(e2);
    47         BoardModel.selected(b, e2);
    48         assertEquals(BoardModel.findSelectedSquare(b), e2, "E2 is selected");
    49         
    50         Square e4 = BoardModel.findSquare(b, 'E', 4);
    51         assertNotNull(e4);
    52         
    53         BoardModel.selected(b, e4);
    54         
    55         assertNull(e2.getPiece(), "No pawn at e2");
    56         assertEquals(e4.getPiece(), BoardModel.PieceType.PAWN, "Pawn moved successfully");
    57         assertNull(BoardModel.findSelectedSquare(b), "No square selected");
    58         
    59         BoardModel.selected(b, e4);
    60         assertNull(BoardModel.findSelectedSquare(b), "No square selected, it is blacks turn");
    61         
    62         assertTrue(b.isBlackTurn(), "black's turn");
    63         
    64         
    65         Square e7 = BoardModel.findSquare(b, 'E', 7);
    66         BoardModel.selected(b, e7);
    67         assertEquals(BoardModel.findSelectedSquare(b), e7);
    68         
    69         BoardModel.selected(b, BoardModel.findSquare(b, 'E', 6));
    70         assertNull(e7.getPiece(), "Piece has been moved");
    71     }
    72     
    73     @Test public void cantSelectEmptySquare() {
    74         Board b = BoardModel.createBoard();
    75         Square e3 = BoardModel.findSquare(b, 'E', 3);
    76         assertNotNull(e3);
    77         BoardModel.selected(b, e3);
    78         assertNull(BoardModel.findSelectedSquare(b), "No square is selected");
    79     }
    80 
    81     @Test public void cantTakeOwnPiece() {
    82         Board b = BoardModel.createBoard();
    83         Square e1 = BoardModel.findSquare(b, 'E', 1);
    84         assertNotNull(e1);
    85         BoardModel.selected(b, e1);
    86         assertEquals(BoardModel.findSelectedSquare(b), e1, "E1 is selected");
    87         
    88         Square e2 = BoardModel.findSquare(b, 'E', 2);
    89         assertNotNull(e2);
    90         
    91         BoardModel.selected(b, e2);
    92         
    93         assertNotNull(e1.getPiece(), "King remains at e1");
    94         assertEquals(e2.getPiece(), BoardModel.PieceType.PAWN, "Pawn remains");
    95         assertEquals(BoardModel.findSelectedSquare(b), e2, "e2 now selected");
    96         
    97     }
    98     
    99     @Test public void knightMustMoveToF3() {
   100         Board b = BoardModel.createBoard();
   101         Square g1 = BoardModel.findSquare(b, 'G', 1);
   102         BoardModel.selected(b, g1);
   103         
   104         Square f3 = BoardModel.findSquare(b, 'F', 3);
   105         assertTrue(f3.isAccessible(), "This is a field where knight can move");
   106 
   107         Square g3 = BoardModel.findSquare(b, 'G', 3);
   108         assertFalse(g3.isAccessible(), "Not a place for knight");
   109 
   110         Square e2 = BoardModel.findSquare(b, 'E', 2);
   111         assertFalse(e2.isAccessible(), "Not a place either, occupied");
   112 
   113         BoardModel.selected(b, g3);
   114         
   115         assertNull(g3.getPiece(), "No figure was moved");
   116         assertTrue(g1.isSelected(), "Original square still selected");
   117         
   118         BoardModel.selected(b, f3);
   119         
   120         assertEquals(f3.getPiece(), PieceType.KNIGHT, "Moved");
   121         assertFalse(g3.isSelected(), "No longer selected");
   122     }
   123 }