Bläddra i källkod

queen-attack: update tests(#1357) (#1388)

* update queen-attack tests to version 2.1.0

* remove unchecked exceptions signature
michaelspets 8 år sedan
förälder
incheckning
5411aca298

+ 0
- 39
exercises/queen-attack/.meta/src/reference/java/BoardCoordinate.java Visa fil

@@ -1,39 +0,0 @@
1
-final class BoardCoordinate {
2
-
3
-    private final int row;
4
-
5
-    private final int column;
6
-
7
-    BoardCoordinate(final int row, final int column) throws IllegalArgumentException {
8
-        this.row = row;
9
-        this.column = column;
10
-
11
-        validateInputs();
12
-    }
13
-
14
-    int getRow() {
15
-        return row;
16
-    }
17
-
18
-    int getColumn() {
19
-        return column;
20
-    }
21
-
22
-    private void validateInputs() throws IllegalArgumentException {
23
-        validateCoordinateComponent(row, "row");
24
-        validateCoordinateComponent(column, "column");
25
-    }
26
-
27
-    private void validateCoordinateComponent(final int value, final String componentName)
28
-            throws IllegalArgumentException {
29
-
30
-        if (value < 0) {
31
-            throw new IllegalArgumentException("Coordinate must have positive " + componentName + ".");
32
-        }
33
-
34
-        if (value > 7) {
35
-            throw new IllegalArgumentException("Coordinate must have " + componentName + " <= 7.");
36
-        }
37
-    }
38
-
39
-}

+ 38
- 0
exercises/queen-attack/.meta/src/reference/java/Queen.java Visa fil

@@ -0,0 +1,38 @@
1
+final class Queen {
2
+
3
+    private final int row;
4
+
5
+    private final int column;
6
+
7
+    Queen(final int row, final int column) {
8
+        this.row = row;
9
+        this.column = column;
10
+
11
+        validatePosition();
12
+    }
13
+
14
+    int getRow() {
15
+        return row;
16
+    }
17
+
18
+    int getColumn() {
19
+        return column;
20
+    }
21
+
22
+    private void validatePosition() {
23
+        validatePositionComponent(row, "row");
24
+        validatePositionComponent(column, "column");
25
+    }
26
+
27
+    private void validatePositionComponent(final int value, final String componentName) {
28
+
29
+        if (value < 0) {
30
+            throw new IllegalArgumentException("Queen position must have positive " + componentName + ".");
31
+        }
32
+
33
+        if (value > 7) {
34
+            throw new IllegalArgumentException("Queen position must have " + componentName + " <= 7.");
35
+        }
36
+    }
37
+
38
+}

+ 11
- 12
exercises/queen-attack/.meta/src/reference/java/QueenAttackCalculator.java Visa fil

@@ -1,14 +1,13 @@
1 1
 final class QueenAttackCalculator {
2 2
 
3
-    private final BoardCoordinate whiteQueenCoordinate;
3
+    private final Queen whiteQueen;
4 4
 
5
-    private final BoardCoordinate blackQueenCoordinate;
5
+    private final Queen blackQueen;
6 6
 
7
-    QueenAttackCalculator(final BoardCoordinate whiteQueenCoordinate, final BoardCoordinate blackQueenCoordinate)
8
-            throws IllegalArgumentException {
7
+    QueenAttackCalculator(final Queen whiteQueen, final Queen blackQueen) {
9 8
 
10
-        this.whiteQueenCoordinate = whiteQueenCoordinate;
11
-        this.blackQueenCoordinate = blackQueenCoordinate;
9
+        this.whiteQueen = whiteQueen;
10
+        this.blackQueen = blackQueen;
12 11
 
13 12
         validateInputs();
14 13
     }
@@ -17,13 +16,13 @@ final class QueenAttackCalculator {
17 16
         return queensShareColumn() || queensShareRow() || queensShareDiagonal();
18 17
     }
19 18
 
20
-    private void validateInputs() throws IllegalArgumentException {
21
-        if (whiteQueenCoordinate == null || blackQueenCoordinate == null) {
22
-            throw new IllegalArgumentException("You must supply valid board coordinates for both Queens.");
19
+    private void validateInputs() {
20
+        if (whiteQueen == null || blackQueen == null) {
21
+            throw new IllegalArgumentException("You must supply valid positions for both Queens.");
23 22
         }
24 23
 
25 24
         if (queensShareBoardCoordinate()) {
26
-            throw new IllegalArgumentException("Queens may not occupy the same board coordinate.");
25
+            throw new IllegalArgumentException("Queens cannot occupy the same position.");
27 26
         }
28 27
     }
29 28
 
@@ -44,11 +43,11 @@ final class QueenAttackCalculator {
44 43
     }
45 44
 
46 45
     private int differenceBetweenRows() {
47
-        return Math.abs(whiteQueenCoordinate.getRow() - blackQueenCoordinate.getRow());
46
+        return Math.abs(whiteQueen.getRow() - blackQueen.getRow());
48 47
     }
49 48
 
50 49
     private int differenceBetweenColumns() {
51
-        return Math.abs(whiteQueenCoordinate.getColumn() - blackQueenCoordinate.getColumn());
50
+        return Math.abs(whiteQueen.getColumn() - blackQueen.getColumn());
52 51
     }
53 52
 
54 53
 }

+ 1
- 1
exercises/queen-attack/.meta/version Visa fil

@@ -1 +1 @@
1
-2.0.0
1
+2.1.0

+ 50
- 44
exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java Visa fil

@@ -12,119 +12,125 @@ public class QueenAttackCalculatorTest {
12 12
     public ExpectedException expectedException = ExpectedException.none();
13 13
 
14 14
     @Test
15
-    public void testQueensThatDoNotShareRowColumnOrDiagonalCannotAttack() {
16
-        final QueenAttackCalculator calculator
17
-                = new QueenAttackCalculator(new BoardCoordinate(2, 4), new BoardCoordinate(6, 6));
15
+    public void testCreateQueenWithAValidPosition() {
16
+        new Queen(2, 2);
17
+    }
18 18
 
19
-        assertFalse(calculator.canQueensAttackOneAnother());
19
+    @Ignore("Remove to run test")
20
+    @Test
21
+    public void testCreateQueenMustHavePositiveRow() {
22
+        expectedException.expect(IllegalArgumentException.class);
23
+        expectedException.expectMessage("Queen position must have positive row.");
24
+
25
+        new Queen(-2, 2);
20 26
     }
21 27
 
22 28
     @Ignore("Remove to run test")
23 29
     @Test
24
-    public void testQueensCanAttackOnTheSameRow() {
25
-        final QueenAttackCalculator calculator
26
-                = new QueenAttackCalculator(new BoardCoordinate(2, 4), new BoardCoordinate(2, 6));
30
+    public void testCreateQueenMustHaveRowOnBoard() {
31
+        expectedException.expect(IllegalArgumentException.class);
32
+        expectedException.expectMessage("Queen position must have row <= 7.");
27 33
 
28
-        assertTrue(calculator.canQueensAttackOneAnother());
34
+        new Queen(8, 4);
29 35
     }
30 36
 
31 37
     @Ignore("Remove to run test")
32 38
     @Test
33
-    public void testQueensCanAttackOnTheSameColumn() {
34
-        final QueenAttackCalculator calculator
35
-                = new QueenAttackCalculator(new BoardCoordinate(4, 5), new BoardCoordinate(2, 5));
39
+    public void testCreateQueenMustHavePositiveColumn() {
40
+        expectedException.expect(IllegalArgumentException.class);
41
+        expectedException.expectMessage("Queen position must have positive column.");
36 42
 
37
-        assertTrue(calculator.canQueensAttackOneAnother());
43
+        new Queen(2, -2);
38 44
     }
39 45
 
40 46
     @Ignore("Remove to run test")
41 47
     @Test
42
-    public void testQueensCanAttackOnFirstDiagonal() {
43
-        final QueenAttackCalculator calculator
44
-                = new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(0, 4));
48
+    public void testCreateQueenMustHaveColumnOnBoard() {
49
+        expectedException.expect(IllegalArgumentException.class);
50
+        expectedException.expectMessage("Queen position must have column <= 7.");
45 51
 
46
-        assertTrue(calculator.canQueensAttackOneAnother());
52
+        new Queen(4, 8);
47 53
     }
48 54
 
49 55
     @Ignore("Remove to run test")
50 56
     @Test
51
-    public void testQueensCanAttackOnSecondDiagonal() {
57
+    public void testQueensCannotAttack() {
52 58
         final QueenAttackCalculator calculator
53
-                = new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(3, 1));
59
+                = new QueenAttackCalculator(new Queen(2, 4), new Queen(6, 6));
54 60
 
55
-        assertTrue(calculator.canQueensAttackOneAnother());
61
+        assertFalse(calculator.canQueensAttackOneAnother());
56 62
     }
57 63
 
58 64
     @Ignore("Remove to run test")
59 65
     @Test
60
-    public void testQueensCanAttackOnThirdDiagonal() {
66
+    public void testQueensCanAttackOnTheSameRow() {
61 67
         final QueenAttackCalculator calculator
62
-                = new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(1, 1));
68
+                = new QueenAttackCalculator(new Queen(2, 4), new Queen(2, 6));
63 69
 
64 70
         assertTrue(calculator.canQueensAttackOneAnother());
65 71
     }
66 72
 
67 73
     @Ignore("Remove to run test")
68 74
     @Test
69
-    public void testQueensCanAttackOnFourthDiagonal() {
75
+    public void testQueensCanAttackOnTheSameColumn() {
70 76
         final QueenAttackCalculator calculator
71
-                = new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(5, 5));
77
+                = new QueenAttackCalculator(new Queen(4, 5), new Queen(2, 5));
72 78
 
73 79
         assertTrue(calculator.canQueensAttackOneAnother());
74 80
     }
75 81
 
76 82
     @Ignore("Remove to run test")
77 83
     @Test
78
-    public void testCoordinateWithNegativeRowNotAllowed() {
79
-        expectedException.expect(IllegalArgumentException.class);
80
-        expectedException.expectMessage("Coordinate must have positive row.");
84
+    public void testQueensCanAttackOnFirstDiagonal() {
85
+        final QueenAttackCalculator calculator
86
+                = new QueenAttackCalculator(new Queen(2, 2), new Queen(0, 4));
81 87
 
82
-        new BoardCoordinate(-2, 2);
88
+        assertTrue(calculator.canQueensAttackOneAnother());
83 89
     }
84 90
 
85 91
     @Ignore("Remove to run test")
86 92
     @Test
87
-    public void testCoordinateWithRowGreaterThan7NotAllowed() {
88
-        expectedException.expect(IllegalArgumentException.class);
89
-        expectedException.expectMessage("Coordinate must have row <= 7.");
93
+    public void testQueensCanAttackOnSecondDiagonal() {
94
+        final QueenAttackCalculator calculator
95
+                = new QueenAttackCalculator(new Queen(2, 2), new Queen(3, 1));
90 96
 
91
-        new BoardCoordinate(8, 4);
97
+        assertTrue(calculator.canQueensAttackOneAnother());
92 98
     }
93 99
 
94 100
     @Ignore("Remove to run test")
95 101
     @Test
96
-    public void testCoordinateWithNegativeColumnNotAllowed() {
97
-        expectedException.expect(IllegalArgumentException.class);
98
-        expectedException.expectMessage("Coordinate must have positive column.");
102
+    public void testQueensCanAttackOnThirdDiagonal() {
103
+        final QueenAttackCalculator calculator
104
+                = new QueenAttackCalculator(new Queen(2, 2), new Queen(1, 1));
99 105
 
100
-        new BoardCoordinate(2, -2);
106
+        assertTrue(calculator.canQueensAttackOneAnother());
101 107
     }
102 108
 
103 109
     @Ignore("Remove to run test")
104 110
     @Test
105
-    public void testCoordinateWithColumnGreaterThan7NotAllowed() {
106
-        expectedException.expect(IllegalArgumentException.class);
107
-        expectedException.expectMessage("Coordinate must have column <= 7.");
111
+    public void testQueensCanAttackOnFourthDiagonal() {
112
+        final QueenAttackCalculator calculator
113
+                = new QueenAttackCalculator(new Queen(2, 2), new Queen(5, 5));
108 114
 
109
-        new BoardCoordinate(4, 8);
115
+        assertTrue(calculator.canQueensAttackOneAnother());
110 116
     }
111 117
 
112 118
     @Ignore("Remove to run test")
113 119
     @Test
114
-    public void testNullCoordinateNotAllowed() {
120
+    public void testNullPositionsNotAllowed() {
115 121
         expectedException.expect(IllegalArgumentException.class);
116
-        expectedException.expectMessage("You must supply valid board coordinates for both Queens.");
122
+        expectedException.expectMessage("You must supply valid positions for both Queens.");
117 123
 
118
-        new QueenAttackCalculator(null, new BoardCoordinate(0, 7));
124
+        new QueenAttackCalculator(null, new Queen(0, 7));
119 125
     }
120 126
 
121 127
     @Ignore("Remove to run test")
122 128
     @Test
123 129
     public void testQueensMustNotOccupyTheSameSquare() {
124 130
         expectedException.expect(IllegalArgumentException.class);
125
-        expectedException.expectMessage("Queens may not occupy the same board coordinate.");
131
+        expectedException.expectMessage("Queens cannot occupy the same position.");
126 132
 
127
-        new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(2, 2));
133
+        new QueenAttackCalculator(new Queen(2, 2), new Queen(2, 2));
128 134
     }
129 135
 
130 136
 }