1.1 --- a/minesweeper/src/test/java/org/apidesign/demo/minesweeper/MinesModelTest.java Sun Jun 28 07:14:21 2015 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,166 +0,0 @@
1.4 -/**
1.5 - * The MIT License (MIT)
1.6 - *
1.7 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.8 - *
1.9 - * Permission is hereby granted, free of charge, to any person obtaining a copy
1.10 - * of this software and associated documentation files (the "Software"), to deal
1.11 - * in the Software without restriction, including without limitation the rights
1.12 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1.13 - * copies of the Software, and to permit persons to whom the Software is
1.14 - * furnished to do so, subject to the following conditions:
1.15 - *
1.16 - * The above copyright notice and this permission notice shall be included in
1.17 - * all copies or substantial portions of the Software.
1.18 - *
1.19 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1.20 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1.21 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1.22 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.23 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1.24 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1.25 - * THE SOFTWARE.
1.26 - */
1.27 -package org.apidesign.demo.minesweeper;
1.28 -
1.29 -import org.apidesign.demo.minesweeper.MinesModel.SquareType;
1.30 -import static org.testng.Assert.*;
1.31 -import org.testng.annotations.Test;
1.32 -
1.33 -public class MinesModelTest {
1.34 - @Test public void tenTenTen() {
1.35 - Mines m = new Mines();
1.36 - m.init(10, 10, 10);
1.37 -
1.38 - assertEquals(m.getRows().size(), 10, "Ten rows");
1.39 - int cnt = 0;
1.40 - for (Row row : m.getRows()) {
1.41 - assertEquals(row.getColumns().size(), 10, "Ten columns in each row");
1.42 - for (Square square : row.getColumns()) {
1.43 - if (square.isMine()) {
1.44 - cnt++;
1.45 - }
1.46 - }
1.47 - }
1.48 - assertEquals(cnt, 10, "Ten mines");
1.49 - }
1.50 -
1.51 - @Test public void clickRemovesMarkedSign() {
1.52 - Mines m = new Mines();
1.53 - m.init(10, 10, 10);
1.54 -
1.55 - final Square sq = m.getRows().get(5).getColumns().get(5);
1.56 - MinesModel.markMine(m);
1.57 - MinesModel.click(m, sq);
1.58 -
1.59 - assertEquals(sq.getState(), SquareType.MARKED, "Changed to marked");
1.60 -
1.61 - MinesModel.click(m, sq);
1.62 -
1.63 - assertEquals(sq.getState(), SquareType.UNKNOWN, "Changed back to unknown");
1.64 - }
1.65 -
1.66 - @Test public void gameWonWhenAllMarked() {
1.67 - Mines m = new Mines();
1.68 - m.init(10, 10, 10);
1.69 -
1.70 - for (Row row : m.getRows()) {
1.71 - for (Square sq : row.getColumns()) {
1.72 - if (sq.isMine()) {
1.73 - MinesModel.markMine(m);
1.74 - MinesModel.click(m, sq);
1.75 - }
1.76 - }
1.77 - }
1.78 -
1.79 - assertEquals(m.getState(), MinesModel.GameState.WON, "All mines found. You have won!");
1.80 - }
1.81 -
1.82 - @Test public void gameNotWonWhenTooMuchIsMarked() {
1.83 - Mines m = new Mines();
1.84 - m.init(10, 10, 10);
1.85 -
1.86 - Square additional = null;
1.87 - for (Row row : m.getRows()) {
1.88 - for (Square sq : row.getColumns()) {
1.89 - if (sq.isMine()) {
1.90 - MinesModel.markMine(m);
1.91 - MinesModel.click(m, sq);
1.92 - } else if (additional == null) {
1.93 - MinesModel.markMine(m);
1.94 - MinesModel.click(m, additional = sq);
1.95 - }
1.96 - }
1.97 - }
1.98 -
1.99 - assertEquals(m.getState(), MinesModel.GameState.IN_PROGRESS, "One additional mine is marked!");
1.100 -
1.101 - // remove the mark
1.102 - MinesModel.click(m, additional);
1.103 -
1.104 - assertEquals(m.getState(), MinesModel.GameState.WON, "All mines found. You have won!");
1.105 -
1.106 -
1.107 - }
1.108 -
1.109 - @Test public void bombsSet() {
1.110 - Mines m = new Mines();
1.111 - m.init(10, 10, 0);
1.112 -
1.113 - set(m, 1, 1, SquareType.UNKNOWN, true);
1.114 - set(m, 0, 0, SquareType.N_0, false);
1.115 - set(m, 0, 1, SquareType.N_2, false);
1.116 - set(m, 0, 2, SquareType.N_3, false);
1.117 - set(m, 1, 0, SquareType.N_5, false);
1.118 - set(m, 2, 0, SquareType.N_8, false);
1.119 - set(m, 3, 0, SquareType.N_2, false);
1.120 -
1.121 - m.computeMines();
1.122 -
1.123 - assertSquare(m, 0, 0, SquareType.N_1);
1.124 - assertSquare(m, 1, 0, SquareType.N_1);
1.125 - assertSquare(m, 2, 0, SquareType.N_1);
1.126 - assertSquare(m, 3, 0, SquareType.N_0);
1.127 - assertSquare(m, 0, 1, SquareType.N_1);
1.128 - assertSquare(m, 0, 2, SquareType.N_1);
1.129 - assertSquare(m, 2, 2, SquareType.UNKNOWN);
1.130 - }
1.131 -
1.132 - @Test public void gameIsWonIfAllMinesDiscovered() {
1.133 - Mines m = new Mines();
1.134 - m.init(2, 1, 0);
1.135 - set(m, 0, 0, SquareType.UNKNOWN, true);
1.136 - m.computeMines();
1.137 - assertEquals(m.getState(), MinesModel.GameState.IN_PROGRESS);
1.138 - set(m, 1, 0, SquareType.N_0, false);
1.139 - m.computeMines();
1.140 - assertEquals(m.getState(), MinesModel.GameState.WON, "All non-bomb squares discovered");
1.141 - }
1.142 -
1.143 - @Test public void unhideNeibourghsOfEmptyPieces() {
1.144 - Mines m = new Mines();
1.145 - m.init(3, 3, 0);
1.146 - set(m, 0, 0, SquareType.UNKNOWN, true);
1.147 - MinesModel.click(m, m.getRows().get(2).getColumns().get(2));
1.148 -
1.149 - assertSquare(m, 0, 0, SquareType.DISCOVERED);
1.150 - assertSquare(m, 0, 1, SquareType.N_1);
1.151 - assertSquare(m, 1, 1, SquareType.N_1);
1.152 - assertSquare(m, 1, 0, SquareType.N_1);
1.153 - assertSquare(m, 2, 0, SquareType.N_0);
1.154 - assertSquare(m, 2, 1, SquareType.N_0);
1.155 - assertSquare(m, 2, 2, SquareType.N_0);
1.156 - assertSquare(m, 1, 2, SquareType.N_0);
1.157 - }
1.158 -
1.159 - private static void set(Mines m, int x, int y, SquareType squareType, boolean mine) {
1.160 - Square sq = m.getRows().get(y).getColumns().get(x);
1.161 - sq.setState(squareType);
1.162 - sq.setMine(mine);
1.163 - }
1.164 -
1.165 - private static void assertSquare(Mines m, int x, int y, SquareType t) {
1.166 - Square sq = m.getRows().get(y).getColumns().get(x);
1.167 - assertEquals(sq.getState(), t, "Expecting at " + x + ":" + y);
1.168 - }
1.169 -}