Sfoglia il codice sorgente

Merge pull request #750 from matthewstyler/master

Fixes #714 assertTrue -> assertEquals
Logan Stucki 9 anni fa
parent
commit
ef869fd9ae

+ 21
- 16
exercises/robot-simulator/src/example/java/GridPosition.java Vedi File

@@ -9,21 +9,26 @@ final class GridPosition {
9 9
         this.y = y;
10 10
     }
11 11
 
12
-    /*
13
-     * This equals method is of deliberately narrow scope (only allows comparison with another GridPosition) to increase
14
-     * readability. In general, one should provide a full implementation of Object.equals(Object obj) and a
15
-     * corresponding implementation of Object.hashCode(). See
16
-     *
17
-     * https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)
18
-     *
19
-     * and
20
-     *
21
-     * https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()
22
-     *
23
-     * for more information.
24
-     */
25
-    boolean equals(final GridPosition gridPosition) {
26
-        return this.x == gridPosition.x && this.y == gridPosition.y;
27
-    }
12
+    @Override
13
+    public int hashCode() {
14
+        final int prime = 31;
15
+        int result = 1;
16
+        result = prime * result + x;
17
+        result = prime * result + y;
18
+        return result;
19
+   }
20
+
21
+    @Override
22
+    public boolean equals(Object obj) {
23
+        if (this == obj) {
24
+            return true;
25
+        } else if (obj == null || getClass() != obj.getClass()) {
26
+            return false;
27
+        } else if (x != ((GridPosition) obj).x || y != ((GridPosition)obj).y) {
28
+            return false;
29
+        } else {
30
+            return true;
31
+        }
32
+   }
28 33
 
29 34
 }

+ 24
- 24
exercises/robot-simulator/src/test/java/RobotTest.java Vedi File

@@ -1,7 +1,7 @@
1 1
 import org.junit.Ignore;
2 2
 import org.junit.Test;
3 3
 
4
-import static org.junit.Assert.assertTrue;
4
+import static org.junit.Assert.assertEquals;
5 5
 
6 6
 public class RobotTest {
7 7
 
@@ -10,7 +10,7 @@ public class RobotTest {
10 10
         final GridPosition initialGridPosition = new GridPosition(0, 0);
11 11
         final Robot robot = new Robot(initialGridPosition, Orientation.NORTH);
12 12
 
13
-        assertTrue(robot.getGridPosition().equals(initialGridPosition));
13
+        assertEquals(robot.getGridPosition(), initialGridPosition);
14 14
     }
15 15
 
16 16
     @Ignore("Remove to run test")
@@ -19,7 +19,7 @@ public class RobotTest {
19 19
         final Orientation initialOrientation = Orientation.NORTH;
20 20
         final Robot robot = new Robot(new GridPosition(0, 0), initialOrientation);
21 21
 
22
-        assertTrue(robot.getOrientation().equals(initialOrientation));
22
+        assertEquals(robot.getOrientation(), initialOrientation);
23 23
     }
24 24
 
25 25
     @Ignore("Remove to run test")
@@ -30,7 +30,7 @@ public class RobotTest {
30 30
 
31 31
         robot.turnRight();
32 32
 
33
-        assertTrue(robot.getGridPosition().equals(initialGridPosition));
33
+        assertEquals(robot.getGridPosition(), initialGridPosition);
34 34
     }
35 35
 
36 36
     @Ignore("Remove to run test")
@@ -42,7 +42,7 @@ public class RobotTest {
42 42
 
43 43
         final Orientation expectedOrientation = Orientation.EAST;
44 44
 
45
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
45
+        assertEquals(robot.getOrientation(), expectedOrientation);
46 46
     }
47 47
 
48 48
     @Ignore("Remove to run test")
@@ -54,7 +54,7 @@ public class RobotTest {
54 54
 
55 55
         final Orientation expectedOrientation = Orientation.SOUTH;
56 56
 
57
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
57
+        assertEquals(robot.getOrientation(), expectedOrientation);
58 58
     }
59 59
 
60 60
     @Ignore("Remove to run test")
@@ -66,7 +66,7 @@ public class RobotTest {
66 66
 
67 67
         final Orientation expectedOrientation = Orientation.WEST;
68 68
 
69
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
69
+        assertEquals(robot.getOrientation(), expectedOrientation);
70 70
     }
71 71
 
72 72
     @Ignore("Remove to run test")
@@ -78,7 +78,7 @@ public class RobotTest {
78 78
 
79 79
         final Orientation expectedOrientation = Orientation.NORTH;
80 80
 
81
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
81
+        assertEquals(robot.getOrientation(), expectedOrientation);
82 82
     }
83 83
 
84 84
     @Ignore("Remove to run test")
@@ -89,7 +89,7 @@ public class RobotTest {
89 89
 
90 90
         robot.turnLeft();
91 91
 
92
-        assertTrue(robot.getGridPosition().equals(initialGridPosition));
92
+        assertEquals(robot.getGridPosition(), initialGridPosition);
93 93
     }
94 94
 
95 95
     @Ignore("Remove to run test")
@@ -101,7 +101,7 @@ public class RobotTest {
101 101
 
102 102
         final Orientation expectedOrientation = Orientation.WEST;
103 103
 
104
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
104
+        assertEquals(robot.getOrientation(), expectedOrientation);
105 105
     }
106 106
 
107 107
     @Ignore("Remove to run test")
@@ -113,7 +113,7 @@ public class RobotTest {
113 113
 
114 114
         final Orientation expectedOrientation = Orientation.SOUTH;
115 115
 
116
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
116
+        assertEquals(robot.getOrientation(), expectedOrientation);
117 117
     }
118 118
 
119 119
     @Ignore("Remove to run test")
@@ -125,7 +125,7 @@ public class RobotTest {
125 125
 
126 126
         final Orientation expectedOrientation = Orientation.EAST;
127 127
 
128
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
128
+        assertEquals(robot.getOrientation(), expectedOrientation);
129 129
     }
130 130
 
131 131
     @Ignore("Remove to run test")
@@ -137,7 +137,7 @@ public class RobotTest {
137 137
 
138 138
         final Orientation expectedOrientation = Orientation.NORTH;
139 139
 
140
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
140
+        assertEquals(robot.getOrientation(), expectedOrientation);
141 141
     }
142 142
 
143 143
     @Ignore("Remove to run test")
@@ -148,7 +148,7 @@ public class RobotTest {
148 148
 
149 149
         robot.advance();
150 150
 
151
-        assertTrue(robot.getOrientation().equals(initialOrientation));
151
+        assertEquals(robot.getOrientation(), initialOrientation);
152 152
     }
153 153
 
154 154
     @Ignore("Remove to run test")
@@ -160,7 +160,7 @@ public class RobotTest {
160 160
 
161 161
         final GridPosition expectedGridPosition = new GridPosition(0, 1);
162 162
 
163
-        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
163
+        assertEquals(robot.getGridPosition(), expectedGridPosition);
164 164
     }
165 165
 
166 166
     @Ignore("Remove to run test")
@@ -172,7 +172,7 @@ public class RobotTest {
172 172
 
173 173
         final GridPosition expectedGridPosition = new GridPosition(0, -1);
174 174
 
175
-        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
175
+        assertEquals(robot.getGridPosition(), expectedGridPosition);
176 176
     }
177 177
 
178 178
     @Ignore("Remove to run test")
@@ -184,7 +184,7 @@ public class RobotTest {
184 184
 
185 185
         final GridPosition expectedGridPosition = new GridPosition(1, 0);
186 186
 
187
-        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
187
+        assertEquals(robot.getGridPosition(), expectedGridPosition);
188 188
     }
189 189
 
190 190
     @Ignore("Remove to run test")
@@ -196,7 +196,7 @@ public class RobotTest {
196 196
 
197 197
         final GridPosition expectedGridPosition = new GridPosition(-1, 0);
198 198
 
199
-        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
199
+        assertEquals(robot.getGridPosition(), expectedGridPosition);
200 200
     }
201 201
 
202 202
     @Ignore("Remove to run test")
@@ -209,8 +209,8 @@ public class RobotTest {
209 209
         final GridPosition expectedGridPosition = new GridPosition(-4, 1);
210 210
         final Orientation expectedOrientation = Orientation.WEST;
211 211
 
212
-        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
213
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
212
+        assertEquals(robot.getGridPosition(), expectedGridPosition);
213
+        assertEquals(robot.getOrientation(), expectedOrientation);
214 214
     }
215 215
 
216 216
     @Ignore("Remove to run test")
@@ -223,8 +223,8 @@ public class RobotTest {
223 223
         final GridPosition expectedGridPosition = new GridPosition(-3, -8);
224 224
         final Orientation expectedOrientation = Orientation.SOUTH;
225 225
 
226
-        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
227
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
226
+        assertEquals(robot.getGridPosition(), expectedGridPosition);
227
+        assertEquals(robot.getOrientation(), expectedOrientation);
228 228
     }
229 229
 
230 230
     @Ignore("Remove to run test")
@@ -237,8 +237,8 @@ public class RobotTest {
237 237
         final GridPosition expectedGridPosition = new GridPosition(11, 5);
238 238
         final Orientation expectedOrientation = Orientation.NORTH;
239 239
 
240
-        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
241
-        assertTrue(robot.getOrientation().equals(expectedOrientation));
240
+        assertEquals(robot.getGridPosition(), expectedGridPosition);
241
+        assertEquals(robot.getOrientation(), expectedOrientation);
242 242
     }
243 243
 
244 244
 }