Skip to content

Commit 8841c35

Browse files
committed
Make hashCode cache tests less brittle.
1 parent 36fc86e commit 8841c35

File tree

6 files changed

+31
-65
lines changed

6 files changed

+31
-65
lines changed

test/list/built_list_test.dart

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ void main() {
113113
});
114114

115115
test('caches hash', () {
116-
var list = BuiltList<Object>([_HashcodeOnlyOnce()]);
116+
var hashCodeSpy = HashCodeSpy();
117+
var list = BuiltList<Object>([hashCodeSpy]);
117118

119+
hashCodeSpy.hashCodeSeen = 0;
118120
list.hashCode;
119121
list.hashCode;
122+
expect(hashCodeSpy.hashCodeSeen, 1);
120123
});
121124

122125
test('compares equal to same instance', () {
@@ -452,15 +455,3 @@ void main() {
452455
class _A {}
453456

454457
class _ExtendsA extends _A {}
455-
456-
class _HashcodeOnlyOnce {
457-
bool hashCodeAllowed = true;
458-
459-
@override
460-
// ignore: hash_and_equals
461-
int get hashCode {
462-
expect(hashCodeAllowed, isTrue);
463-
hashCodeAllowed = false;
464-
return 0;
465-
}
466-
}

test/list_multimap/built_list_multimap_test.dart

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,15 @@ void main() {
212212
});
213213

214214
test('caches hash', () {
215+
var hashCodeSpy = HashCodeSpy();
215216
var multimap = BuiltListMultimap<Object, Object>({
216-
1: [_HashcodeOnlyOnce()]
217+
1: [hashCodeSpy]
217218
});
218219

220+
hashCodeSpy.hashCodeSeen = 0;
219221
multimap.hashCode;
220222
multimap.hashCode;
223+
expect(hashCodeSpy.hashCodeSeen, 1);
221224
});
222225

223226
test('compares equal to same instance', () {
@@ -600,18 +603,6 @@ class _A {}
600603

601604
class _ExtendsA extends _A {}
602605

603-
class _HashcodeOnlyOnce {
604-
bool hashCodeAllowed = true;
605-
606-
@override
607-
// ignore: hash_and_equals
608-
int get hashCode {
609-
expect(hashCodeAllowed, isTrue);
610-
hashCodeAllowed = false;
611-
return 0;
612-
}
613-
}
614-
615606
// All the methods from `ListMultimap` that we care about, to avoid taking a
616607
// dependency on `quiver`.
617608
class _ListMultimap<K, V> {

test/map/built_map_test.dart

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,13 @@ void main() {
186186
});
187187

188188
test('caches hash', () {
189-
var map = BuiltMap<Object, Object>({1: _HashcodeOnlyOnce()});
189+
var hashCodeSpy = HashCodeSpy();
190+
var map = BuiltMap<Object, Object>({1: hashCodeSpy});
190191

192+
hashCodeSpy.hashCodeSeen = 0;
191193
map.hashCode;
192194
map.hashCode;
195+
expect(hashCodeSpy.hashCodeSeen, 1);
193196
});
194197

195198
test('compares equal to same instance', () {
@@ -419,15 +422,3 @@ void main() {
419422
class _A {}
420423

421424
class _ExtendsA extends _A {}
422-
423-
class _HashcodeOnlyOnce {
424-
bool hashCodeAllowed = true;
425-
426-
@override
427-
// ignore: hash_and_equals
428-
int get hashCode {
429-
expect(hashCodeAllowed, isTrue);
430-
hashCodeAllowed = false;
431-
return 0;
432-
}
433-
}

test/performance.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,14 @@ void expectNotMuchFaster(Function notFastFunction, Function slowFunction) {
5353
' Measured: first=${fastStopWatch.elapsedMicroseconds}'
5454
' second=${slowStopWatch.elapsedMicroseconds}';
5555
}
56+
57+
class HashCodeSpy {
58+
int hashCodeSeen = 0;
59+
60+
@override
61+
// ignore: hash_and_equals
62+
int get hashCode {
63+
++hashCodeSeen;
64+
return 0;
65+
}
66+
}

test/set/built_set_test.dart

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ void main() {
132132
});
133133

134134
test('caches hash', () {
135-
var set = BuiltSet<Object>([_HashcodeOnlyTwice()]);
135+
var hashCodeSpy = HashCodeSpy();
136+
var set = BuiltSet<Object>([hashCodeSpy]);
136137

138+
hashCodeSpy.hashCodeSeen = 0;
137139
set.hashCode;
138140
set.hashCode;
141+
expect(hashCodeSpy.hashCodeSeen, 1);
139142
});
140143

141144
test('compares equal to same instance', () {
@@ -452,15 +455,3 @@ void main() {
452455
class _A {}
453456

454457
class _ExtendsA extends _A {}
455-
456-
class _HashcodeOnlyTwice {
457-
int hashCodeAllowed = 2;
458-
459-
@override
460-
// ignore: hash_and_equals
461-
int get hashCode {
462-
expect(hashCodeAllowed, isNot(0));
463-
hashCodeAllowed--;
464-
return 0;
465-
}
466-
}

test/set_multimap/built_set_multimap_test.dart

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,15 @@ void main() {
209209
});
210210

211211
test('caches hash', () {
212+
var hashCodeSpy = HashCodeSpy();
212213
var multimap = BuiltSetMultimap<Object, Object>({
213-
1: [_HashcodeOnlyTwice()]
214+
1: [hashCodeSpy]
214215
});
215216

217+
hashCodeSpy.hashCodeSeen = 0;
216218
multimap.hashCode;
217219
multimap.hashCode;
220+
expect(hashCodeSpy.hashCodeSeen, 1);
218221
});
219222

220223
test('compares equal to same instance', () {
@@ -596,18 +599,6 @@ class _A {}
596599

597600
class _ExtendsA extends _A {}
598601

599-
class _HashcodeOnlyTwice {
600-
int hashCodeAllowed = 2;
601-
602-
@override
603-
// ignore: hash_and_equals
604-
int get hashCode {
605-
expect(hashCodeAllowed, isNot(0));
606-
hashCodeAllowed--;
607-
return 0;
608-
}
609-
}
610-
611602
// All the methods from `SetMultimap` that we care about, to avoid taking a
612603
// dependency on `quiver`.
613604
class _SetMultimap<K, V> {

0 commit comments

Comments
 (0)