Skip to content

Commit 270336e

Browse files
committed
minesweeper: update to canonical tests
1 parent 0369706 commit 270336e

2 files changed

Lines changed: 41 additions & 131 deletions

File tree

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import java.util.ArrayList;
22
import java.util.List;
3-
import java.util.Set;
4-
import java.util.stream.Collectors;
53

64
final class MinesweeperBoard {
75

@@ -16,33 +14,32 @@ final class MinesweeperBoard {
1614
private final int numberOfColumns;
1715

1816
MinesweeperBoard(final List<String> rawRepresentation) {
19-
validateInputBoard(rawRepresentation);
2017
this.rawRepresentation = rawRepresentation;
2118
this.numberOfRows = rawRepresentation.size();
2219
this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
2320
}
2421

25-
List<String> getAnnotatedRepresentation() throws IllegalArgumentException {
22+
List<String> withNumbers() {
2623
final List<String> result = new ArrayList<>();
2724

2825
for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
29-
result.add(getAnnotatedRow(rowNumber));
26+
result.add(getRowWithNumbers(rowNumber));
3027
}
3128

3229
return result;
3330
}
3431

35-
private String getAnnotatedRow(final int rowNumber) {
32+
private String getRowWithNumbers(final int rowNumber) {
3633
String result = "";
3734

3835
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
39-
result += getCellAnnotation(rowNumber, columnNumber);
36+
result += getCellNumber(rowNumber, columnNumber);
4037
}
4138

4239
return result;
4340
}
4441

45-
private char getCellAnnotation(final int rowNumber, final int columnNumber) {
42+
private char getCellNumber(final int rowNumber, final int columnNumber) {
4643
// If (rowNumber, columnNumber) is a mine, we're done.
4744
if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
4845
return MINE_CHAR;
@@ -75,37 +72,4 @@ private int computeMineCountAround(final int rowNumber, final int columnNumber)
7572
return result;
7673
}
7774

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-
11175
}
Lines changed: 36 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import org.junit.Ignore;
2-
import org.junit.Rule;
32
import org.junit.Test;
4-
import org.junit.rules.ExpectedException;
53

64
import java.util.Arrays;
75
import java.util.Collections;
@@ -14,32 +12,23 @@
1412
*/
1513
public class MinesweeperBoardTest {
1614

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-
2415
@Test
2516
public void testInputBoardWithNoRowsAndNoColumns() {
2617
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();
3020

31-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
21+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
3222
}
3323

3424
@Ignore("Remove to run test")
3525
@Test
3626
public void testInputBoardWithOneRowAndNoColumns() {
3727
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();
4130

42-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
31+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
4332
}
4433

4534
@Ignore("Remove to run test")
@@ -51,16 +40,15 @@ public void testInputBoardWithNoMines() {
5140
" "
5241
);
5342

54-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
43+
final List<String> expectedNumberedBoard = Arrays.asList(
5544
" ",
5645
" ",
5746
" "
5847
);
5948

60-
final List<String> actualAnnotatedRepresentation
61-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
49+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
6250

63-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
51+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
6452
}
6553

6654
@Ignore("Remove to run test")
@@ -72,16 +60,15 @@ public void testInputBoardWithOnlyMines() {
7260
"***"
7361
);
7462

75-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
63+
final List<String> expectedNumberedBoard = Arrays.asList(
7664
"***",
7765
"***",
7866
"***"
7967
);
8068

81-
final List<String> actualAnnotatedRepresentation
82-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
69+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
8370

84-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
71+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
8572
}
8673

8774
@Ignore("Remove to run test")
@@ -93,16 +80,15 @@ public void testInputBoardWithSingleMineAtCenter() {
9380
" "
9481
);
9582

96-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
83+
final List<String> expectedNumberedBoard = Arrays.asList(
9784
"111",
9885
"1*1",
9986
"111"
10087
);
10188

102-
final List<String> actualAnnotatedRepresentation
103-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
89+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
10490

105-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
91+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
10692
}
10793

10894
@Ignore("Remove to run test")
@@ -114,16 +100,15 @@ public void testInputBoardWithMinesAroundPerimeter() {
114100
"***"
115101
);
116102

117-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
103+
final List<String> expectedNumberedBoard = Arrays.asList(
118104
"***",
119105
"*8*",
120106
"***"
121107
);
122108

123-
final List<String> actualAnnotatedRepresentation
124-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
109+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
125110

126-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
111+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
127112
}
128113

129114
@Ignore("Remove to run test")
@@ -133,14 +118,13 @@ public void testInputBoardWithSingleRowAndTwoMines() {
133118
" * * "
134119
);
135120

136-
final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
121+
final List<String> expectedNumberedBoard = Collections.singletonList(
137122
"1*2*1"
138123
);
139124

140-
final List<String> actualAnnotatedRepresentation
141-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
125+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
142126

143-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
127+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
144128
}
145129

146130
@Ignore("Remove to run test")
@@ -150,14 +134,13 @@ public void testInputBoardWithSingleRowAndTwoMinesAtEdges() {
150134
"* *"
151135
);
152136

153-
final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
137+
final List<String> expectedNumberedBoard = Collections.singletonList(
154138
"*1 1*"
155139
);
156140

157-
final List<String> actualAnnotatedRepresentation
158-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
141+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
159142

160-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
143+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
161144
}
162145

163146
@Ignore("Remove to run test")
@@ -171,18 +154,17 @@ public void testInputBoardWithSingleColumnAndTwoMines() {
171154
" "
172155
);
173156

174-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
157+
final List<String> expectedNumberedBoard = Arrays.asList(
175158
"1",
176159
"*",
177160
"2",
178161
"*",
179162
"1"
180163
);
181164

182-
final List<String> actualAnnotatedRepresentation
183-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
165+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
184166

185-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
167+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
186168
}
187169

188170
@Ignore("Remove to run test")
@@ -196,18 +178,17 @@ public void testInputBoardWithSingleColumnAndTwoMinesAtEdges() {
196178
"*"
197179
);
198180

199-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
181+
final List<String> expectedNumberedBoard = Arrays.asList(
200182
"*",
201183
"1",
202184
" ",
203185
"1",
204186
"*"
205187
);
206188

207-
final List<String> actualAnnotatedRepresentation
208-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
189+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
209190

210-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
191+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
211192
}
212193

213194
@Ignore("Remove to run test")
@@ -221,18 +202,17 @@ public void testInputBoardWithMinesInCross() {
221202
" * "
222203
);
223204

224-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
205+
final List<String> expectedNumberedBoard = Arrays.asList(
225206
" 2*2 ",
226207
"25*52",
227208
"*****",
228209
"25*52",
229210
" 2*2 "
230211
);
231212

232-
final List<String> actualAnnotatedRepresentation
233-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
213+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
234214

235-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
215+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
236216
}
237217

238218
@Ignore("Remove to run test")
@@ -247,7 +227,7 @@ public void testLargeInputBoard() {
247227
" "
248228
);
249229

250-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
230+
final List<String> expectedNumberedBoard = Arrays.asList(
251231
"1*22*1",
252232
"12*322",
253233
" 123*2",
@@ -256,43 +236,9 @@ public void testLargeInputBoard() {
256236
"111111"
257237
);
258238

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();
273240

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);
296242
}
297243

298244
}

0 commit comments

Comments
 (0)