OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Tests equality utilities. | 5 // Tests equality utilities. |
6 | 6 |
7 import "dart:collection"; | 7 import "dart:collection"; |
8 import "package:collection/collection.dart"; | 8 import "package:collection/collection.dart"; |
9 import "package:test/test.dart"; | 9 import "package:test/test.dart"; |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 test("IterableEquality - LinkedSet", () { | 26 test("IterableEquality - LinkedSet", () { |
27 var l1 = new LinkedHashSet.from(list1); | 27 var l1 = new LinkedHashSet.from(list1); |
28 var l2 = new LinkedHashSet.from(list2); | 28 var l2 = new LinkedHashSet.from(list2); |
29 expect(const IterableEquality().equals(l1, l2), isTrue); | 29 expect(const IterableEquality().equals(l1, l2), isTrue); |
30 Equality iterId = const IterableEquality(const IdentityEquality()); | 30 Equality iterId = const IterableEquality(const IdentityEquality()); |
31 expect(iterId.equals(l1, l2), isFalse); | 31 expect(iterId.equals(l1, l2), isFalse); |
32 }); | 32 }); |
33 | 33 |
34 test("ListEquality", () { | 34 test("ListEquality", () { |
35 expect(const ListEquality().equals(list1, list2), | 35 expect(const ListEquality().equals(list1, list2), isTrue); |
36 isTrue); | |
37 Equality listId = const ListEquality(const IdentityEquality()); | 36 Equality listId = const ListEquality(const IdentityEquality()); |
38 expect(listId.equals(list1, list2), isFalse); | 37 expect(listId.equals(list1, list2), isFalse); |
39 }); | 38 }); |
40 | 39 |
41 test("ListInequality length", () { | 40 test("ListInequality length", () { |
42 var list4 = [o(1), o(2), o(3), o(4), o(5), o(6)]; | 41 var list4 = [o(1), o(2), o(3), o(4), o(5), o(6)]; |
43 expect(const ListEquality().equals(list1, list4), | 42 expect(const ListEquality().equals(list1, list4), isFalse); |
44 isFalse); | |
45 expect(const ListEquality(const IdentityEquality()).equals(list1, list4), | 43 expect(const ListEquality(const IdentityEquality()).equals(list1, list4), |
46 isFalse); | 44 isFalse); |
47 }); | 45 }); |
48 | 46 |
49 test("ListInequality value", () { | 47 test("ListInequality value", () { |
50 var list5 = [o(1), o(2), o(3), o(4), o(6)]; | 48 var list5 = [o(1), o(2), o(3), o(4), o(6)]; |
51 expect(const ListEquality().equals(list1, list5), | 49 expect(const ListEquality().equals(list1, list5), isFalse); |
52 isFalse); | |
53 expect(const ListEquality(const IdentityEquality()).equals(list1, list5), | 50 expect(const ListEquality(const IdentityEquality()).equals(list1, list5), |
54 isFalse); | 51 isFalse); |
55 }); | 52 }); |
56 | 53 |
57 test("UnorderedIterableEquality", () { | 54 test("UnorderedIterableEquality", () { |
58 expect(const UnorderedIterableEquality().equals(list1, list3), | 55 expect(const UnorderedIterableEquality().equals(list1, list3), isTrue); |
59 isTrue); | |
60 Equality uniterId = | 56 Equality uniterId = |
61 const UnorderedIterableEquality(const IdentityEquality()); | 57 const UnorderedIterableEquality(const IdentityEquality()); |
62 expect(uniterId.equals(list1, list3), isFalse); | 58 expect(uniterId.equals(list1, list3), isFalse); |
63 }); | 59 }); |
64 | 60 |
65 test("UnorderedIterableInequality length", () { | 61 test("UnorderedIterableInequality length", () { |
66 var list6 = [o(1), o(3), o(5), o(4), o(2), o(1)]; | 62 var list6 = [o(1), o(3), o(5), o(4), o(2), o(1)]; |
67 expect(const UnorderedIterableEquality().equals(list1, list6), | 63 expect(const UnorderedIterableEquality().equals(list1, list6), isFalse); |
68 isFalse); | 64 expect( |
69 expect(const UnorderedIterableEquality(const IdentityEquality()) | 65 const UnorderedIterableEquality(const IdentityEquality()) |
70 .equals(list1, list6), | 66 .equals(list1, list6), |
71 isFalse); | 67 isFalse); |
72 }); | 68 }); |
73 | 69 |
74 test("UnorderedIterableInequality values", () { | 70 test("UnorderedIterableInequality values", () { |
75 var list7 = [o(1), o(3), o(5), o(4), o(6)]; | 71 var list7 = [o(1), o(3), o(5), o(4), o(6)]; |
76 expect(const UnorderedIterableEquality().equals(list1, list7), | 72 expect(const UnorderedIterableEquality().equals(list1, list7), isFalse); |
77 isFalse); | 73 expect( |
78 expect(const UnorderedIterableEquality(const IdentityEquality()) | 74 const UnorderedIterableEquality(const IdentityEquality()) |
79 .equals(list1, list7), | 75 .equals(list1, list7), |
80 isFalse); | 76 isFalse); |
81 }); | 77 }); |
82 | 78 |
83 test("SetEquality", () { | 79 test("SetEquality", () { |
84 var set1 = new HashSet.from(list1); | 80 var set1 = new HashSet.from(list1); |
85 var set2 = new LinkedHashSet.from(list3); | 81 var set2 = new LinkedHashSet.from(list3); |
86 expect(const SetEquality().equals(set1, set2), isTrue); | 82 expect(const SetEquality().equals(set1, set2), isTrue); |
87 Equality setId = const SetEquality(const IdentityEquality()); | 83 Equality setId = const SetEquality(const IdentityEquality()); |
88 expect(setId.equals(set1, set2), isFalse); | 84 expect(setId.equals(set1, set2), isFalse); |
89 }); | 85 }); |
90 | 86 |
91 test("SetInequality length", () { | 87 test("SetInequality length", () { |
92 var list8 = [o(1), o(3), o(5), o(4), o(2), o(6)]; | 88 var list8 = [o(1), o(3), o(5), o(4), o(2), o(6)]; |
93 var set1 = new HashSet.from(list1); | 89 var set1 = new HashSet.from(list1); |
94 var set2 = new LinkedHashSet.from(list8); | 90 var set2 = new LinkedHashSet.from(list8); |
95 expect(const SetEquality().equals(set1, set2), | 91 expect(const SetEquality().equals(set1, set2), isFalse); |
96 isFalse); | |
97 expect(const SetEquality(const IdentityEquality()).equals(set1, set2), | 92 expect(const SetEquality(const IdentityEquality()).equals(set1, set2), |
98 isFalse); | 93 isFalse); |
99 }); | 94 }); |
100 | 95 |
101 test("SetInequality value", () { | 96 test("SetInequality value", () { |
102 var list7 = [o(1), o(3), o(5), o(4), o(6)]; | 97 var list7 = [o(1), o(3), o(5), o(4), o(6)]; |
103 var set1 = new HashSet.from(list1); | 98 var set1 = new HashSet.from(list1); |
104 var set2 = new LinkedHashSet.from(list7); | 99 var set2 = new LinkedHashSet.from(list7); |
105 expect(const SetEquality().equals(set1, set2), | 100 expect(const SetEquality().equals(set1, set2), isFalse); |
106 isFalse); | |
107 expect(const SetEquality(const IdentityEquality()).equals(set1, set2), | 101 expect(const SetEquality(const IdentityEquality()).equals(set1, set2), |
108 isFalse); | 102 isFalse); |
109 }); | 103 }); |
110 | 104 |
111 var map1a = {"x": [o(1), o(2), o(3)], "y": [true, false, null]}; | 105 var map1a = { |
112 var map1b = {"x": [o(4), o(5), o(6)], "y": [false, true, null]}; | 106 "x": [o(1), o(2), o(3)], |
113 var map2a = {"x": [o(3), o(2), o(1)], "y": [false, true, null]}; | 107 "y": [true, false, null] |
114 var map2b = {"x": [o(6), o(5), o(4)], "y": [null, false, true]}; | 108 }; |
| 109 var map1b = { |
| 110 "x": [o(4), o(5), o(6)], |
| 111 "y": [false, true, null] |
| 112 }; |
| 113 var map2a = { |
| 114 "x": [o(3), o(2), o(1)], |
| 115 "y": [false, true, null] |
| 116 }; |
| 117 var map2b = { |
| 118 "x": [o(6), o(5), o(4)], |
| 119 "y": [null, false, true] |
| 120 }; |
115 var l1 = [map1a, map1b]; | 121 var l1 = [map1a, map1b]; |
116 var l2 = [map2a, map2b]; | 122 var l2 = [map2a, map2b]; |
117 var s1 = new Set.from(l1); | 123 var s1 = new Set<Map>.from(l1); |
118 var s2 = new Set.from([map2b, map2a]); | 124 var s2 = new Set<Map>.from([map2b, map2a]); |
119 | 125 |
120 test("RecursiveEquality", () { | 126 test("RecursiveEquality", () { |
121 const unordered = const UnorderedIterableEquality(); | 127 const unordered = const UnorderedIterableEquality(); |
122 expect(unordered.equals(map1a["x"], map2a["x"]), | 128 expect(unordered.equals(map1a["x"], map2a["x"]), isTrue); |
123 isTrue); | 129 expect(unordered.equals(map1a["y"], map2a["y"]), isTrue); |
124 expect(unordered.equals(map1a["y"], map2a["y"]), | 130 expect(unordered.equals(map1b["x"], map2b["x"]), isTrue); |
125 isTrue); | 131 expect(unordered.equals(map1b["y"], map2b["y"]), isTrue); |
126 expect(unordered.equals(map1b["x"], map2b["x"]), | |
127 isTrue); | |
128 expect(unordered.equals(map1b["y"], map2b["y"]), | |
129 isTrue); | |
130 const mapval = const MapEquality(values: unordered); | 132 const mapval = const MapEquality(values: unordered); |
131 expect( | 133 expect(mapval.equals(map1a, map2a), isTrue); |
132 mapval.equals(map1a, map2a), | 134 expect(mapval.equals(map1b, map2b), isTrue); |
133 isTrue); | |
134 expect(mapval.equals(map1b, map2b), | |
135 isTrue); | |
136 const listmapval = const ListEquality(mapval); | 135 const listmapval = const ListEquality(mapval); |
137 expect(listmapval.equals(l1, l2), | 136 expect(listmapval.equals(l1, l2), isTrue); |
138 isTrue); | 137 const setmapval = const SetEquality<Map>(mapval); |
139 const setmapval = const SetEquality(mapval); | 138 expect(setmapval.equals(s1, s2), isTrue); |
140 expect(setmapval.equals(s1, s2), | |
141 isTrue); | |
142 }); | 139 }); |
143 | 140 |
144 test("DeepEquality", () { | 141 test("DeepEquality", () { |
145 var colleq = const DeepCollectionEquality.unordered(); | 142 var colleq = const DeepCollectionEquality.unordered(); |
146 expect(colleq.equals(map1a["x"], map2a["x"]), | 143 expect(colleq.equals(map1a["x"], map2a["x"]), isTrue); |
147 isTrue); | 144 expect(colleq.equals(map1a["y"], map2a["y"]), isTrue); |
148 expect(colleq.equals(map1a["y"], map2a["y"]), | 145 expect(colleq.equals(map1b["x"], map2b["x"]), isTrue); |
149 isTrue); | 146 expect(colleq.equals(map1b["y"], map2b["y"]), isTrue); |
150 expect(colleq.equals(map1b["x"], map2b["x"]), | 147 expect(colleq.equals(map1a, map2a), isTrue); |
151 isTrue); | 148 expect(colleq.equals(map1b, map2b), isTrue); |
152 expect(colleq.equals(map1b["y"], map2b["y"]), | 149 expect(colleq.equals(l1, l2), isTrue); |
153 isTrue); | 150 expect(colleq.equals(s1, s2), isTrue); |
154 expect(colleq.equals(map1a, map2a), | 151 }); |
155 isTrue); | 152 |
156 expect(colleq.equals(map1b, map2b), | 153 test("CaseInsensitiveEquality", () { |
157 isTrue); | 154 var equality = const CaseInsensitiveEquality(); |
158 expect(colleq.equals(l1, l2), | 155 expect(equality.equals("foo", "foo"), isTrue); |
159 isTrue); | 156 expect(equality.equals("fOo", "FoO"), isTrue); |
160 expect(colleq.equals(s1, s2), | 157 expect(equality.equals("FoO", "fOo"), isTrue); |
161 isTrue); | 158 expect(equality.equals("foo", "bar"), isFalse); |
| 159 expect(equality.equals("fÕÕ", "fõõ"), isFalse); |
| 160 |
| 161 expect(equality.hash("foo"), equals(equality.hash("foo"))); |
| 162 expect(equality.hash("fOo"), equals(equality.hash("FoO"))); |
| 163 expect(equality.hash("FoO"), equals(equality.hash("fOo"))); |
| 164 expect(equality.hash("foo"), isNot(equals(equality.hash("bar")))); |
| 165 expect(equality.hash("fÕÕ"), isNot(equals(equality.hash("fõõ")))); |
| 166 }); |
| 167 |
| 168 group("EqualityBy should use a derived value for ", () { |
| 169 var firstEquality = new EqualityBy<List<String>, String>((e) => e.first); |
| 170 var firstInsensitiveEquality = new EqualityBy<List<String>, String>( |
| 171 (e) => e.first, const CaseInsensitiveEquality()); |
| 172 var firstObjectEquality = new EqualityBy<List<Object>, Object>( |
| 173 (e) => e.first, const IterableEquality()); |
| 174 |
| 175 test("equality", () { |
| 176 expect(firstEquality.equals(["foo", "foo"], ["foo", "bar"]), isTrue); |
| 177 expect(firstEquality.equals(["foo", "foo"], ["bar", "bar"]), isFalse); |
| 178 }); |
| 179 |
| 180 test("equality with an inner equality", () { |
| 181 expect(firstInsensitiveEquality.equals(["fOo"], ["FoO"]), isTrue); |
| 182 expect(firstInsensitiveEquality.equals(["foo"], ["ffõõ"]), isFalse); |
| 183 }); |
| 184 |
| 185 test("hash", () { |
| 186 expect(firstEquality.hash(["foo", "bar"]), "foo".hashCode); |
| 187 }); |
| 188 |
| 189 test("hash with an inner equality", () { |
| 190 expect(firstInsensitiveEquality.hash(["fOo"]), |
| 191 const CaseInsensitiveEquality().hash("foo")); |
| 192 }); |
| 193 |
| 194 test("isValidKey", () { |
| 195 expect(firstEquality.isValidKey(["foo"]), isTrue); |
| 196 expect(firstEquality.isValidKey("foo"), isFalse); |
| 197 expect(firstEquality.isValidKey([1]), isFalse); |
| 198 }); |
| 199 |
| 200 test('isValidKey with an inner equality', () { |
| 201 expect(firstObjectEquality.isValidKey([[]]), isTrue); |
| 202 expect(firstObjectEquality.isValidKey([{}]), isFalse); |
| 203 }); |
| 204 }); |
| 205 |
| 206 test("Equality accepts null", () { |
| 207 var ie = new IterableEquality(); |
| 208 var le = new ListEquality(); |
| 209 var se = new SetEquality(); |
| 210 var me = new MapEquality(); |
| 211 expect(ie.equals(null, null), true); |
| 212 expect(ie.equals([], null), false); |
| 213 expect(ie.equals(null, []), false); |
| 214 expect(ie.hash(null), null.hashCode); |
| 215 |
| 216 expect(le.equals(null, null), true); |
| 217 expect(le.equals([], null), false); |
| 218 expect(le.equals(null, []), false); |
| 219 expect(le.hash(null), null.hashCode); |
| 220 |
| 221 expect(se.equals(null, null), true); |
| 222 expect(se.equals(new Set(), null), false); |
| 223 expect(se.equals(null, new Set()), false); |
| 224 expect(se.hash(null), null.hashCode); |
| 225 |
| 226 expect(me.equals(null, null), true); |
| 227 expect(me.equals({}, null), false); |
| 228 expect(me.equals(null, {}), false); |
| 229 expect(me.hash(null), null.hashCode); |
162 }); | 230 }); |
163 } | 231 } |
164 | 232 |
165 /// Wrapper objects for an `id` value. | 233 /// Wrapper objects for an `id` value. |
166 /// | 234 /// |
167 /// Compares the `id` value by equality and for comparison. | 235 /// Compares the `id` value by equality and for comparison. |
168 /// Allows creating simple objects that are equal without being identical. | 236 /// Allows creating simple objects that are equal without being identical. |
169 class Element implements Comparable<Element> { | 237 class Element implements Comparable<Element> { |
170 final Comparable id; | 238 final Comparable id; |
171 const Element(this.id); | 239 const Element(this.id); |
172 int get hashCode => id.hashCode; | 240 int get hashCode => id.hashCode; |
173 bool operator==(Object other) => other is Element && id == other.id; | 241 bool operator ==(Object other) => other is Element && id == other.id; |
174 int compareTo(other) => id.compareTo(other.id); | 242 int compareTo(other) => id.compareTo(other.id); |
175 } | 243 } |
OLD | NEW |