Просмотр исходного кода

minesweeper: update to canonical tests

Stuart Kent 9 лет назад
Родитель
Сommit
270336e2aa

+ 5
- 41
exercises/minesweeper/src/example/java/MinesweeperBoard.java Просмотреть файл

1
 import java.util.ArrayList;
1
 import java.util.ArrayList;
2
 import java.util.List;
2
 import java.util.List;
3
-import java.util.Set;
4
-import java.util.stream.Collectors;
5
 
3
 
6
 final class MinesweeperBoard {
4
 final class MinesweeperBoard {
7
 
5
 
16
     private final int numberOfColumns;
14
     private final int numberOfColumns;
17
 
15
 
18
     MinesweeperBoard(final List<String> rawRepresentation) {
16
     MinesweeperBoard(final List<String> rawRepresentation) {
19
-        validateInputBoard(rawRepresentation);
20
         this.rawRepresentation = rawRepresentation;
17
         this.rawRepresentation = rawRepresentation;
21
         this.numberOfRows = rawRepresentation.size();
18
         this.numberOfRows = rawRepresentation.size();
22
         this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
19
         this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
23
     }
20
     }
24
 
21
 
25
-    List<String> getAnnotatedRepresentation() throws IllegalArgumentException {
22
+    List<String> withNumbers() {
26
         final List<String> result = new ArrayList<>();
23
         final List<String> result = new ArrayList<>();
27
 
24
 
28
         for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
25
         for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
29
-            result.add(getAnnotatedRow(rowNumber));
26
+            result.add(getRowWithNumbers(rowNumber));
30
         }
27
         }
31
 
28
 
32
         return result;
29
         return result;
33
     }
30
     }
34
 
31
 
35
-    private String getAnnotatedRow(final int rowNumber) {
32
+    private String getRowWithNumbers(final int rowNumber) {
36
         String result = "";
33
         String result = "";
37
 
34
 
38
         for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
35
         for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
39
-            result += getCellAnnotation(rowNumber, columnNumber);
36
+            result += getCellNumber(rowNumber, columnNumber);
40
         }
37
         }
41
 
38
 
42
         return result;
39
         return result;
43
     }
40
     }
44
 
41
 
45
-    private char getCellAnnotation(final int rowNumber, final int columnNumber) {
42
+    private char getCellNumber(final int rowNumber, final int columnNumber) {
46
         // If (rowNumber, columnNumber) is a mine, we're done.
43
         // If (rowNumber, columnNumber) is a mine, we're done.
47
         if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
44
         if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
48
             return MINE_CHAR;
45
             return MINE_CHAR;
75
         return result;
72
         return result;
76
     }
73
     }
77
 
74
 
78
-    private void validateInputBoard(final List<String> inputBoard) throws IllegalArgumentException {
79
-        validateInputBoardIsNotNull(inputBoard);
80
-
81
-        if (inputBoard.isEmpty()) {
82
-            return;
83
-        }
84
-
85
-        validateInputBoardCharacters(inputBoard);
86
-        validateInputBoardColumnCounts(inputBoard);
87
-    }
88
-
89
-    private void validateInputBoardIsNotNull(final List<String> inputBoard) throws IllegalArgumentException {
90
-        if (inputBoard == null) {
91
-            throw new IllegalArgumentException("Input board may not be null.");
92
-        }
93
-    }
94
-
95
-    private void validateInputBoardCharacters(final List<String> inputBoard) throws IllegalArgumentException {
96
-        final String allBoardCharacters = String.join("", inputBoard);
97
-
98
-        if (!allBoardCharacters.matches("^[ *]*$")) {
99
-            throw new IllegalArgumentException("Input board can only contain the characters ' ' and '*'.");
100
-        }
101
-    }
102
-
103
-    private void validateInputBoardColumnCounts(final List<String> inputBoard) throws IllegalArgumentException {
104
-        final Set<Integer> setOfColumnCounts = inputBoard.stream().map(String::length).collect(Collectors.toSet());
105
-
106
-        if (setOfColumnCounts.size() > 1) {
107
-            throw new IllegalArgumentException("Input board rows must all have the same number of columns.");
108
-        }
109
-    }
110
-
111
 }
75
 }

+ 36
- 90
exercises/minesweeper/src/test/java/MinesweeperBoardTest.java Просмотреть файл

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
-import org.junit.Rule;
3
 import org.junit.Test;
2
 import org.junit.Test;
4
-import org.junit.rules.ExpectedException;
5
 
3
 
6
 import java.util.Arrays;
4
 import java.util.Arrays;
7
 import java.util.Collections;
5
 import java.util.Collections;
14
  */
12
  */
15
 public class MinesweeperBoardTest {
13
 public class MinesweeperBoardTest {
16
 
14
 
17
-    /*
18
-     * See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and
19
-     * ExpectedExceptions in particular.
20
-     */
21
-    @Rule
22
-    public ExpectedException expectedException = ExpectedException.none();
23
-
24
     @Test
15
     @Test
25
     public void testInputBoardWithNoRowsAndNoColumns() {
16
     public void testInputBoardWithNoRowsAndNoColumns() {
26
         final List<String> inputBoard = Collections.emptyList();
17
         final List<String> inputBoard = Collections.emptyList();
27
-        final List<String> expectedAnnotatedRepresentation = Collections.emptyList();
28
-        final List<String> actualAnnotatedRepresentation
29
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
18
+        final List<String> expectedNumberedBoard = Collections.emptyList();
19
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
30
 
20
 
31
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
21
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
32
     }
22
     }
33
 
23
 
34
     @Ignore("Remove to run test")
24
     @Ignore("Remove to run test")
35
     @Test
25
     @Test
36
     public void testInputBoardWithOneRowAndNoColumns() {
26
     public void testInputBoardWithOneRowAndNoColumns() {
37
         final List<String> inputBoard = Collections.singletonList("");
27
         final List<String> inputBoard = Collections.singletonList("");
38
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList("");
39
-        final List<String> actualAnnotatedRepresentation
40
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
28
+        final List<String> expectedNumberedBoard = Collections.singletonList("");
29
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
41
 
30
 
42
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
31
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
43
     }
32
     }
44
 
33
 
45
     @Ignore("Remove to run test")
34
     @Ignore("Remove to run test")
51
                 "   "
40
                 "   "
52
         );
41
         );
53
 
42
 
54
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
43
+        final List<String> expectedNumberedBoard = Arrays.asList(
55
                 "   ",
44
                 "   ",
56
                 "   ",
45
                 "   ",
57
                 "   "
46
                 "   "
58
         );
47
         );
59
 
48
 
60
-        final List<String> actualAnnotatedRepresentation
61
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
49
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
62
 
50
 
63
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
51
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
64
     }
52
     }
65
 
53
 
66
     @Ignore("Remove to run test")
54
     @Ignore("Remove to run test")
72
                 "***"
60
                 "***"
73
         );
61
         );
74
 
62
 
75
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
63
+        final List<String> expectedNumberedBoard = Arrays.asList(
76
                 "***",
64
                 "***",
77
                 "***",
65
                 "***",
78
                 "***"
66
                 "***"
79
         );
67
         );
80
 
68
 
81
-        final List<String> actualAnnotatedRepresentation
82
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
69
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
83
 
70
 
84
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
71
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
85
     }
72
     }
86
 
73
 
87
     @Ignore("Remove to run test")
74
     @Ignore("Remove to run test")
93
                 "   "
80
                 "   "
94
         );
81
         );
95
 
82
 
96
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
83
+        final List<String> expectedNumberedBoard = Arrays.asList(
97
                 "111",
84
                 "111",
98
                 "1*1",
85
                 "1*1",
99
                 "111"
86
                 "111"
100
         );
87
         );
101
 
88
 
102
-        final List<String> actualAnnotatedRepresentation
103
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
89
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
104
 
90
 
105
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
91
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
106
     }
92
     }
107
 
93
 
108
     @Ignore("Remove to run test")
94
     @Ignore("Remove to run test")
114
                 "***"
100
                 "***"
115
         );
101
         );
116
 
102
 
117
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
103
+        final List<String> expectedNumberedBoard = Arrays.asList(
118
                 "***",
104
                 "***",
119
                 "*8*",
105
                 "*8*",
120
                 "***"
106
                 "***"
121
         );
107
         );
122
 
108
 
123
-        final List<String> actualAnnotatedRepresentation
124
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
109
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
125
 
110
 
126
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
111
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
127
     }
112
     }
128
 
113
 
129
     @Ignore("Remove to run test")
114
     @Ignore("Remove to run test")
133
                 " * * "
118
                 " * * "
134
         );
119
         );
135
 
120
 
136
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
121
+        final List<String> expectedNumberedBoard = Collections.singletonList(
137
                 "1*2*1"
122
                 "1*2*1"
138
         );
123
         );
139
 
124
 
140
-        final List<String> actualAnnotatedRepresentation
141
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
125
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
142
 
126
 
143
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
127
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
144
     }
128
     }
145
 
129
 
146
     @Ignore("Remove to run test")
130
     @Ignore("Remove to run test")
150
                 "*   *"
134
                 "*   *"
151
         );
135
         );
152
 
136
 
153
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
137
+        final List<String> expectedNumberedBoard = Collections.singletonList(
154
                 "*1 1*"
138
                 "*1 1*"
155
         );
139
         );
156
 
140
 
157
-        final List<String> actualAnnotatedRepresentation
158
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
141
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
159
 
142
 
160
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
143
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
161
     }
144
     }
162
 
145
 
163
     @Ignore("Remove to run test")
146
     @Ignore("Remove to run test")
171
                 " "
154
                 " "
172
         );
155
         );
173
 
156
 
174
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
157
+        final List<String> expectedNumberedBoard = Arrays.asList(
175
                 "1",
158
                 "1",
176
                 "*",
159
                 "*",
177
                 "2",
160
                 "2",
179
                 "1"
162
                 "1"
180
         );
163
         );
181
 
164
 
182
-        final List<String> actualAnnotatedRepresentation
183
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
165
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
184
 
166
 
185
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
167
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
186
     }
168
     }
187
 
169
 
188
     @Ignore("Remove to run test")
170
     @Ignore("Remove to run test")
196
                 "*"
178
                 "*"
197
         );
179
         );
198
 
180
 
199
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
181
+        final List<String> expectedNumberedBoard = Arrays.asList(
200
                 "*",
182
                 "*",
201
                 "1",
183
                 "1",
202
                 " ",
184
                 " ",
204
                 "*"
186
                 "*"
205
         );
187
         );
206
 
188
 
207
-        final List<String> actualAnnotatedRepresentation
208
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
189
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
209
 
190
 
210
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
191
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
211
     }
192
     }
212
 
193
 
213
     @Ignore("Remove to run test")
194
     @Ignore("Remove to run test")
221
                 "  *  "
202
                 "  *  "
222
         );
203
         );
223
 
204
 
224
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
205
+        final List<String> expectedNumberedBoard = Arrays.asList(
225
                 " 2*2 ",
206
                 " 2*2 ",
226
                 "25*52",
207
                 "25*52",
227
                 "*****",
208
                 "*****",
229
                 " 2*2 "
210
                 " 2*2 "
230
         );
211
         );
231
 
212
 
232
-        final List<String> actualAnnotatedRepresentation
233
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
213
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
234
 
214
 
235
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
215
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
236
     }
216
     }
237
 
217
 
238
     @Ignore("Remove to run test")
218
     @Ignore("Remove to run test")
247
                 "      "
227
                 "      "
248
         );
228
         );
249
 
229
 
250
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
230
+        final List<String> expectedNumberedBoard = Arrays.asList(
251
                 "1*22*1",
231
                 "1*22*1",
252
                 "12*322",
232
                 "12*322",
253
                 " 123*2",
233
                 " 123*2",
256
                 "111111"
236
                 "111111"
257
         );
237
         );
258
 
238
 
259
-        final List<String> actualAnnotatedRepresentation
260
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
261
-
262
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
263
-    }
264
-
265
-    @Ignore("Remove to run test")
266
-    @Test
267
-    public void testNullInputBoardIsRejected() {
268
-        expectedException.expect(IllegalArgumentException.class);
269
-        expectedException.expectMessage("Input board may not be null.");
270
-
271
-        new MinesweeperBoard(null);
272
-    }
239
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
273
 
240
 
274
-    @Ignore("Remove to run test")
275
-    @Test
276
-    public void testInputBoardWithInvalidSymbolsIsRejected() {
277
-        expectedException.expect(IllegalArgumentException.class);
278
-        expectedException.expectMessage("Input board can only contain the characters ' ' and '*'.");
279
-
280
-        new MinesweeperBoard(Collections.singletonList(" * & "));
281
-    }
282
-
283
-    @Ignore("Remove to run test")
284
-    @Test
285
-    public void testInputBoardWithInconsistentRowLengthsIsRejected() {
286
-        expectedException.expect(IllegalArgumentException.class);
287
-        expectedException.expectMessage("Input board rows must all have the same number of columns.");
288
-
289
-        new MinesweeperBoard(Arrays.asList(
290
-                "*",
291
-                "**",
292
-                "* *",
293
-                "*  *",
294
-                "*   *"
295
-        ));
241
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
296
     }
242
     }
297
 
243
 
298
 }
244
 }