chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 26 Jul 2013 09:28:49 +0200
branchchess
changeset 30 a46846115b83
parent 29 9fb64f6528b5
child 34 1ebd52ae8ccb
permissions -rw-r--r--
Ensure changing turns
     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 static org.testng.Assert.*;
    28 import org.testng.annotations.Test;
    29 
    30 /**
    31  *
    32  * @author Jaroslav Tulach <jtulach@netbeans.org>
    33  */
    34 public class BoardModelTest {
    35     
    36     public BoardModelTest() {
    37     }
    38 
    39 
    40     @Test public void e2e4e7e6() {
    41         Board b = BoardModel.createBoard();
    42         assertEquals(b.getTurn(), ColorType.WHITE);
    43         
    44         Square e2 = BoardModel.findSquare(b, 'E', 2);
    45         assertNotNull(e2);
    46         BoardModel.selected(b, e2);
    47         assertEquals(BoardModel.findSelectedSquare(b), e2, "E2 is selected");
    48         
    49         Square e4 = BoardModel.findSquare(b, 'E', 4);
    50         assertNotNull(e4);
    51         
    52         BoardModel.selected(b, e4);
    53         
    54         assertNull(e2.getPiece(), "No pawn at e2");
    55         assertEquals(e4.getPiece(), BoardModel.PieceType.PAWN, "Pawn moved successfully");
    56         assertNull(BoardModel.findSelectedSquare(b), "No square selected");
    57         
    58         BoardModel.selected(b, e4);
    59         assertNull(BoardModel.findSelectedSquare(b), "No square selected, it is blacks turn");
    60         
    61         assertTrue(b.isBlackTurn(), "black's turn");
    62         
    63         
    64         Square e7 = BoardModel.findSquare(b, 'E', 7);
    65         BoardModel.selected(b, e7);
    66         assertEquals(BoardModel.findSelectedSquare(b), e7);
    67         
    68         BoardModel.selected(b, BoardModel.findSquare(b, 'E', 6));
    69         assertNull(e7.getPiece(), "Piece has been moved");
    70     }
    71     
    72     @Test public void cantSelectEmptySquare() {
    73         Board b = BoardModel.createBoard();
    74         Square e3 = BoardModel.findSquare(b, 'E', 3);
    75         assertNotNull(e3);
    76         BoardModel.selected(b, e3);
    77         assertNull(BoardModel.findSelectedSquare(b), "No square is selected");
    78     }
    79 
    80     @Test public void cantTakeOwnPiece() {
    81         Board b = BoardModel.createBoard();
    82         Square e1 = BoardModel.findSquare(b, 'E', 1);
    83         assertNotNull(e1);
    84         BoardModel.selected(b, e1);
    85         assertEquals(BoardModel.findSelectedSquare(b), e1, "E1 is selected");
    86         
    87         Square e2 = BoardModel.findSquare(b, 'E', 2);
    88         assertNotNull(e2);
    89         
    90         BoardModel.selected(b, e2);
    91         
    92         assertNotNull(e1.getPiece(), "King remains at e1");
    93         assertEquals(e2.getPiece(), BoardModel.PieceType.PAWN, "Pawn remains");
    94         assertEquals(BoardModel.findSelectedSquare(b), e2, "e2 now selected");
    95         
    96     }
    97     
    98 }