# HG changeset patch # User Jaroslav Tulach # Date 1374765295 -7200 # Node ID b675be28fc49e10dac021b8d364b39d940ce9f46 # Parent b20104a99a6b428fedc93e8345a22f8427051309 Can move pieces and shows how to unit test the model diff -r b20104a99a6b -r b675be28fc49 chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 16:21:25 2013 +0200 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 17:14:55 2013 +0200 @@ -33,7 +33,44 @@ @Property(name = "rows", type = Row.class, array = true) }) public class BoardModel { - @Model(className="Row", properties = { + @Function static void selected(Board b, Square data) { + Square previoslySelected = findSelectedSquare(b); + if (previoslySelected == null) { + if (data.getPiece() != null) { + data.setSelected(true); + } + } else { + data.setPieceColor(previoslySelected.getPieceColor()); + data.setPiece(previoslySelected.getPiece()); + previoslySelected.setPiece(null); + previoslySelected.setPieceColor(null); + previoslySelected.setSelected(false); + } + } + + static Square findSquare(Board b, char column, int row) { + for (Row r : b.getRows()) { + for (Square square : r.getColumns()) { + if (square.getX() == column && square.getY() == row) { + return square; + } + } + } + return null; + } + + static Square findSelectedSquare(Board b) { + for (Row row : b.getRows()) { + for (Square square : row.getColumns()) { + if (square.isSelected()) { + return square; + } + } + } + return null; + } + + @Model(className="Row", properties = { @Property(name = "columns", type = Square.class, array = true) }) static class RowsImpl { @@ -72,12 +109,9 @@ @Property(name = "color", type = ColorType.class), @Property(name = "x", type = int.class), @Property(name = "y", type = int.class), + @Property(name = "selected", type = boolean.class) }) static class PieceImpl { - @Function static void changeToPawn(Square s) { - s.setPiece(PieceType.PAWN); - } - @ComputedProperty static String pieceEntity( PieceType piece, ColorType pieceColor ) { @@ -87,7 +121,11 @@ return piece.computeEntity(pieceColor); } - @ComputedProperty static String squareColor(ColorType color) { + @ComputedProperty static String squareColor(ColorType color, boolean selected) { + if (selected) { + return "selected"; + } + if (color == null) { return ""; } else { @@ -101,7 +139,7 @@ b.applyBindings(); } - private static Board createBoard() { + static Board createBoard() { Board b = new Board(); for (int i = 8; i > 0; i--) { Row r = new Row(); diff -r b20104a99a6b -r b675be28fc49 chess/src/main/webapp/pages/css/chess.css --- a/chess/src/main/webapp/pages/css/chess.css Thu Jul 25 16:21:25 2013 +0200 +++ b/chess/src/main/webapp/pages/css/chess.css Thu Jul 25 17:14:55 2013 +0200 @@ -52,6 +52,9 @@ table.board td.black { background-color: #B0B0B0; } +table.board td.selected { + background-color: #B000B0; +} .figure { cursor: pointer; } \ No newline at end of file diff -r b20104a99a6b -r b675be28fc49 chess/src/main/webapp/pages/index.html --- a/chess/src/main/webapp/pages/index.html Thu Jul 25 16:21:25 2013 +0200 +++ b/chess/src/main/webapp/pages/index.html Thu Jul 25 17:14:55 2013 +0200 @@ -60,7 +60,7 @@ - + diff -r b20104a99a6b -r b675be28fc49 chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java Thu Jul 25 17:14:55 2013 +0200 @@ -0,0 +1,65 @@ +/** + * The MIT License (MIT) + * + * Copyright (C) 2013 Jaroslav Tulach + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.apidesign.html.demo.chess; + +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class BoardModelTest { + + public BoardModelTest() { + } + + + @Test public void e2e4() { + Board b = BoardModel.createBoard(); + Square e2 = BoardModel.findSquare(b, 'E', 2); + assertNotNull(e2); + BoardModel.selected(b, e2); + assertEquals(BoardModel.findSelectedSquare(b), e2, "E2 is selected"); + + Square e4 = BoardModel.findSquare(b, 'E', 4); + assertNotNull(e4); + + BoardModel.selected(b, e4); + + assertNull(e2.getPiece(), "No pawn at e2"); + assertEquals(e4.getPiece(), BoardModel.PieceType.PAWN, "Pawn moved successfully"); + assertNull(BoardModel.findSelectedSquare(b), "No square selected"); + + } + + @Test public void cantSelectEmptySquare() { + Board b = BoardModel.createBoard(); + Square e3 = BoardModel.findSquare(b, 'E', 3); + assertNotNull(e3); + BoardModel.selected(b, e3); + assertNull(BoardModel.findSelectedSquare(b), "No square is selected"); + } + +}