OLD | NEW |
1 // Copyright 2014 Google Inc. All Rights Reserved. | 1 // Copyright 2014 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the 'License'); | 3 // Licensed under the Apache License, Version 2.0 (the 'License'); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an 'AS IS' BASIS, | 10 // distributed under the License is distributed on an 'AS IS' BASIS, |
(...skipping 24 matching lines...) Expand all Loading... |
35 try { | 35 try { |
36 expect(null, areEqualityGroups); | 36 expect(null, areEqualityGroups); |
37 fail('Should fail with null reference'); | 37 fail('Should fail with null reference'); |
38 } catch (e) { | 38 } catch (e) { |
39 expect(e.toString(), contains('Equality Group must not be null')); | 39 expect(e.toString(), contains('Equality Group must not be null')); |
40 } | 40 } |
41 }); | 41 }); |
42 | 42 |
43 test('Test null group name yields error', () { | 43 test('Test null group name yields error', () { |
44 try { | 44 try { |
45 expect({'null': [reference], null: [reference]}, areEqualityGroups); | 45 expect({ |
| 46 'null': [reference], |
| 47 null: [reference] |
| 48 }, areEqualityGroups); |
46 fail('Should fail with null group name'); | 49 fail('Should fail with null group name'); |
47 } catch (e) { | 50 } catch (e) { |
48 expect(e.toString(), contains('Group name must not be null')); | 51 expect(e.toString(), contains('Group name must not be null')); |
49 } | 52 } |
50 }); | 53 }); |
51 | 54 |
52 test('Test null group yields error', () { | 55 test('Test null group yields error', () { |
53 try { | 56 try { |
54 expect({'bad group': null}, areEqualityGroups); | 57 expect({'bad group': null}, areEqualityGroups); |
55 fail('Should fail with null group'); | 58 fail('Should fail with null group'); |
56 } catch (e) { | 59 } catch (e) { |
57 expect(e.toString(), contains('Group must not be null')); | 60 expect(e.toString(), contains('Group must not be null')); |
58 } | 61 } |
59 }); | 62 }); |
60 | 63 |
61 test('Test after adding multiple instances at once with a null', () { | 64 test('Test after adding multiple instances at once with a null', () { |
62 try { | 65 try { |
63 expect( | 66 expect({ |
64 {'bad group': [reference, equalObject1, null]}, areEqualityGroups); | 67 'bad group': [reference, equalObject1, null] |
| 68 }, areEqualityGroups); |
65 fail('Should fail with null group'); | 69 fail('Should fail with null group'); |
66 } catch (e) { | 70 } catch (e) { |
67 expect(e.toString(), contains("$reference [group 'bad group', item 1]" | 71 expect( |
68 " must be equal to null [group 'bad group', item 3]")); | 72 e.toString(), |
| 73 contains("$reference [group 'bad group', item 1]" |
| 74 " must be equal to null [group 'bad group', item 3]")); |
69 } | 75 } |
70 }); | 76 }); |
71 | 77 |
72 test('Test adding non-equal objects only in single group.', () { | 78 test('Test adding non-equal objects only in single group.', () { |
73 try { | 79 try { |
74 expect( | 80 expect({ |
75 {'not equal': [equalObject1, notEqualObject1]}, areEqualityGroups); | 81 'not equal': [equalObject1, notEqualObject1] |
| 82 }, areEqualityGroups); |
76 fail("Should get not equal to equal object error"); | 83 fail("Should get not equal to equal object error"); |
77 } catch (e) { | 84 } catch (e) { |
78 expect(e.toString(), contains("$equalObject1 [group 'not equal', item" | 85 expect( |
79 " 1] must be equal to $notEqualObject1 [group 'not equal'" | 86 e.toString(), |
80 ", item 2]")); | 87 contains("$equalObject1 [group 'not equal', item" |
| 88 " 1] must be equal to $notEqualObject1 [group 'not equal'" |
| 89 ", item 2]")); |
81 } | 90 } |
82 }); | 91 }); |
83 | 92 |
84 test('Test with no equals or not equals objects. This checks' | 93 test( |
| 94 'Test with no equals or not equals objects. This checks' |
85 ' proper handling of null, incompatible class and reflexive tests', () { | 95 ' proper handling of null, incompatible class and reflexive tests', () { |
86 expect({'single object': [reference]}, areEqualityGroups); | 96 expect({ |
| 97 'single object': [reference] |
| 98 }, areEqualityGroups); |
87 }); | 99 }); |
88 | 100 |
89 test('Test after populating equal objects. This checks proper' | 101 test( |
| 102 'Test after populating equal objects. This checks proper' |
90 ' handling of equality and verifies hashCode for valid objects', () { | 103 ' handling of equality and verifies hashCode for valid objects', () { |
91 expect({ | 104 expect({ |
92 'all equal': [reference, equalObject1, equalObject2] | 105 'all equal': [reference, equalObject1, equalObject2] |
93 }, areEqualityGroups); | 106 }, areEqualityGroups); |
94 }); | 107 }); |
95 | 108 |
96 test('Test proper handling of case where an object is not equal to itself', | 109 test('Test proper handling of case where an object is not equal to itself', |
97 () { | 110 () { |
98 Object obj = new _NonReflexiveObject(); | 111 Object obj = new _NonReflexiveObject(); |
99 try { | 112 try { |
100 expect({'non-reflexive': [obj]}, areEqualityGroups); | 113 expect({ |
| 114 'non-reflexive': [obj] |
| 115 }, areEqualityGroups); |
101 fail("Should get non-reflexive error"); | 116 fail("Should get non-reflexive error"); |
102 } catch (e) { | 117 } catch (e) { |
103 expect(e.toString(), contains("$obj must be equal to itself")); | 118 expect(e.toString(), contains("$obj must be equal to itself")); |
104 } | 119 } |
105 }); | 120 }); |
106 | 121 |
107 test('Test proper handling of case where hashcode is not idempotent', () { | 122 test('Test proper handling of case where hashcode is not idempotent', () { |
108 Object obj = new _InconsistentHashCodeObject(1, 2); | 123 Object obj = new _InconsistentHashCodeObject(1, 2); |
109 try { | 124 try { |
110 expect({'non-reflexive': [obj]}, areEqualityGroups); | 125 expect({ |
| 126 'non-reflexive': [obj] |
| 127 }, areEqualityGroups); |
111 fail("Should get non-reflexive error"); | 128 fail("Should get non-reflexive error"); |
112 } catch (e) { | 129 } catch (e) { |
113 expect(e.toString(), contains( | 130 expect( |
114 "the implementation of hashCode of $obj must be idempotent")); | 131 e.toString(), |
| 132 contains( |
| 133 "the implementation of hashCode of $obj must be idempotent")); |
115 } | 134 } |
116 }); | 135 }); |
117 | 136 |
118 test('Test proper handling where an object incorrectly tests for an ' | 137 test( |
| 138 'Test proper handling where an object incorrectly tests for an ' |
119 'incompatible class', () { | 139 'incompatible class', () { |
120 Object obj = new _InvalidEqualsIncompatibleClassObject(); | 140 Object obj = new _InvalidEqualsIncompatibleClassObject(); |
121 try { | 141 try { |
122 expect({'equals method broken': [obj]}, areEqualityGroups); | 142 expect({ |
| 143 'equals method broken': [obj] |
| 144 }, areEqualityGroups); |
123 fail("Should get equal to incompatible class error"); | 145 fail("Should get equal to incompatible class error"); |
124 } catch (e) { | 146 } catch (e) { |
125 expect(e.toString(), contains("$obj must not be equal to an " | 147 expect( |
126 "arbitrary object of another class")); | 148 e.toString(), |
| 149 contains("$obj must not be equal to an " |
| 150 "arbitrary object of another class")); |
127 } | 151 } |
128 }); | 152 }); |
129 | 153 |
130 test('Test proper handling where an object is not equal to one the user ' | 154 test( |
| 155 'Test proper handling where an object is not equal to one the user ' |
131 'has said should be equal', () { | 156 'has said should be equal', () { |
132 try { | 157 try { |
133 expect({'non-equal': [reference, notEqualObject1]}, areEqualityGroups); | 158 expect({ |
| 159 'non-equal': [reference, notEqualObject1] |
| 160 }, areEqualityGroups); |
134 fail("Should get not equal to equal object error"); | 161 fail("Should get not equal to equal object error"); |
135 } catch (e) { | 162 } catch (e) { |
136 expect( | 163 expect( |
137 e.toString(), contains("$reference [group 'non-equal', item 1]")); | 164 e.toString(), contains("$reference [group 'non-equal', item 1]")); |
138 expect(e.toString(), | 165 expect(e.toString(), |
139 contains("$notEqualObject1 [group 'non-equal', item 2]")); | 166 contains("$notEqualObject1 [group 'non-equal', item 2]")); |
140 } | 167 } |
141 }); | 168 }); |
142 | 169 |
143 test('Test for an invalid hashCode method, i.e., one that returns ' | 170 test( |
| 171 'Test for an invalid hashCode method, i.e., one that returns ' |
144 'different value for objects that are equal according to the equals ' | 172 'different value for objects that are equal according to the equals ' |
145 'method', () { | 173 'method', () { |
146 Object a = new _InvalidHashCodeObject(1, 2); | 174 Object a = new _InvalidHashCodeObject(1, 2); |
147 Object b = new _InvalidHashCodeObject(1, 2); | 175 Object b = new _InvalidHashCodeObject(1, 2); |
148 try { | 176 try { |
149 expect({'invalid hashcode': [a, b]}, areEqualityGroups); | 177 expect({ |
| 178 'invalid hashcode': [a, b] |
| 179 }, areEqualityGroups); |
150 fail("Should get invalid hashCode error"); | 180 fail("Should get invalid hashCode error"); |
151 } catch (e) { | 181 } catch (e) { |
152 expect(e.toString(), contains("the hashCode (${a.hashCode}) of $a" | 182 expect( |
153 " [group 'invalid hashcode', item 1] must be equal to the" | 183 e.toString(), |
154 " hashCode (${b.hashCode}) of $b")); | 184 contains("the hashCode (${a.hashCode}) of $a" |
| 185 " [group 'invalid hashcode', item 1] must be equal to the" |
| 186 " hashCode (${b.hashCode}) of $b")); |
155 } | 187 } |
156 }); | 188 }); |
157 | 189 |
158 test('Symmetry Broken', () { | 190 test('Symmetry Broken', () { |
159 try { | 191 try { |
160 expect({ | 192 expect({ |
161 'broken symmetry': [named('foo')..addPeers(['bar']), named('bar')] | 193 'broken symmetry': [ |
| 194 named('foo')..addPeers(['bar']), |
| 195 named('bar') |
| 196 ] |
162 }, areEqualityGroups); | 197 }, areEqualityGroups); |
163 fail("should fail because symmetry is broken"); | 198 fail("should fail because symmetry is broken"); |
164 } catch (e) { | 199 } catch (e) { |
165 expect(e.toString(), contains("bar [group 'broken symmetry', item 2] " | 200 expect( |
166 "must be equal to foo [group 'broken symmetry', item 1]")); | 201 e.toString(), |
| 202 contains("bar [group 'broken symmetry', item 2] " |
| 203 "must be equal to foo [group 'broken symmetry', item 1]")); |
167 } | 204 } |
168 }); | 205 }); |
169 | 206 |
170 test('Transitivity Broken In EqualityGroup', () { | 207 test('Transitivity Broken In EqualityGroup', () { |
171 try { | 208 try { |
172 expect({ | 209 expect({ |
173 'transitivity broken': [ | 210 'transitivity broken': [ |
174 named('foo')..addPeers(['bar', 'baz']), | 211 named('foo')..addPeers(['bar', 'baz']), |
175 named('bar')..addPeers(['foo']), | 212 named('bar')..addPeers(['foo']), |
176 named('baz')..addPeers(['foo']) | 213 named('baz')..addPeers(['foo']) |
177 ] | 214 ] |
178 }, areEqualityGroups); | 215 }, areEqualityGroups); |
179 fail("should fail because transitivity is broken"); | 216 fail("should fail because transitivity is broken"); |
180 } catch (e) { | 217 } catch (e) { |
181 expect(e.toString(), contains("bar [group 'transitivity broken', " | 218 expect( |
182 "item 2] must be equal to baz [group 'transitivity " | 219 e.toString(), |
183 "broken', item 3]")); | 220 contains("bar [group 'transitivity broken', " |
| 221 "item 2] must be equal to baz [group 'transitivity " |
| 222 "broken', item 3]")); |
184 } | 223 } |
185 }); | 224 }); |
186 | 225 |
187 test('Unequal Objects In EqualityGroup', () { | 226 test('Unequal Objects In EqualityGroup', () { |
188 try { | 227 try { |
189 expect({ | 228 expect({ |
190 'unequal objects': [named('foo'), named('bar')] | 229 'unequal objects': [named('foo'), named('bar')] |
191 }, areEqualityGroups); | 230 }, areEqualityGroups); |
192 fail('should fail because of unequal objects in the same equality ' | 231 fail('should fail because of unequal objects in the same equality ' |
193 'group'); | 232 'group'); |
194 } catch (e) { | 233 } catch (e) { |
195 expect(e.toString(), contains("foo [group 'unequal objects', item 1] " | 234 expect( |
196 "must be equal to bar [group 'unequal objects', item 2]")); | 235 e.toString(), |
| 236 contains("foo [group 'unequal objects', item 1] " |
| 237 "must be equal to bar [group 'unequal objects', item 2]")); |
197 } | 238 } |
198 }); | 239 }); |
199 | 240 |
200 test('Transitivity Broken Across EqualityGroups', () { | 241 test('Transitivity Broken Across EqualityGroups', () { |
201 try { | 242 try { |
202 expect({ | 243 expect({ |
203 'transitivity one': [ | 244 'transitivity one': [ |
204 named('foo')..addPeers(['bar']), | 245 named('foo')..addPeers(['bar']), |
205 named('bar')..addPeers(['foo', 'x']) | 246 named('bar')..addPeers(['foo', 'x']) |
206 ], | 247 ], |
207 'transitivity two': [ | 248 'transitivity two': [ |
208 named('baz')..addPeers(['x']), | 249 named('baz')..addPeers(['x']), |
209 named('x')..addPeers(['baz', 'bar']) | 250 named('x')..addPeers(['baz', 'bar']) |
210 ] | 251 ] |
211 }, areEqualityGroups); | 252 }, areEqualityGroups); |
212 fail('should fail because transitivity is broken'); | 253 fail('should fail because transitivity is broken'); |
213 } catch (e) { | 254 } catch (e) { |
214 expect(e.toString(), contains("bar [group 'transitivity one', item 2]" | 255 expect( |
215 " must not be equal to x [group 'transitivity two'," | 256 e.toString(), |
216 " item 2]")); | 257 contains("bar [group 'transitivity one', item 2]" |
| 258 " must not be equal to x [group 'transitivity two'," |
| 259 " item 2]")); |
217 } | 260 } |
218 }); | 261 }); |
219 | 262 |
220 test('EqualityGroups', () { | 263 test('EqualityGroups', () { |
221 expect({ | 264 expect({ |
222 'valid groups one': [ | 265 'valid groups one': [ |
223 named('foo').addPeers(['bar']), | 266 named('foo')..addPeers(['bar']), |
224 named('bar').addPeers(['foo']) | 267 named('bar')..addPeers(['foo']) |
225 ], | 268 ], |
226 'valid groups two': [named('baz'), named('baz')] | 269 'valid groups two': [named('baz'), named('baz')] |
227 }, areEqualityGroups); | 270 }, areEqualityGroups); |
228 }); | 271 }); |
229 }); | 272 }); |
230 } | 273 } |
231 | 274 |
232 /// Test class that violates reflexitivity. It is not equal to itself. | 275 /// Test class that violates reflexitivity. It is not equal to itself. |
233 class _NonReflexiveObject { | 276 class _NonReflexiveObject { |
234 @override | 277 @override |
235 bool operator ==(Object o) => false; | 278 bool operator ==(Object o) => false; |
236 | 279 |
237 @override | 280 @override |
238 int get hashCode => super.hashCode; | 281 int get hashCode => super.hashCode; |
239 } | 282 } |
240 | 283 |
241 /** | 284 /// Test class with valid equals and hashCode methods. Testers created |
242 * Test class with valid equals and hashCode methods. Testers created | 285 /// with instances of this class should always pass. |
243 * with instances of this class should always pass. | |
244 */ | |
245 class _ValidTestObject { | 286 class _ValidTestObject { |
246 int aspect1; | 287 int aspect1; |
247 int aspect2; | 288 int aspect2; |
248 | 289 |
249 _ValidTestObject(this.aspect1, this.aspect2); | 290 _ValidTestObject(this.aspect1, this.aspect2); |
250 | 291 |
251 @override | 292 @override |
252 bool operator ==(Object o) { | 293 bool operator ==(Object o) { |
253 if (!(o is _ValidTestObject)) { | 294 if (!(o is _ValidTestObject)) { |
254 return false; | 295 return false; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 if (obj is _NamedObject) { | 388 if (obj is _NamedObject) { |
348 _NamedObject that = obj; | 389 _NamedObject that = obj; |
349 return name == that.name || peerNames.contains(that.name); | 390 return name == that.name || peerNames.contains(that.name); |
350 } | 391 } |
351 return false; | 392 return false; |
352 } | 393 } |
353 | 394 |
354 @override | 395 @override |
355 int get hashCode => 0; | 396 int get hashCode => 0; |
356 | 397 |
357 @override String toString() => name; | 398 @override |
| 399 String toString() => name; |
358 } | 400 } |
OLD | NEW |