OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:collection_helpers/wrappers.dart"; | |
6 import "package:unittest/unittest.dart"; | |
7 | |
8 // Test unmodifiable collection views. | |
9 // The collections should pass through the operations that are allowed, | |
10 // an throw on the ones that aren't without affecting the original. | |
11 | |
12 main() { | |
13 List list = []; | |
14 testUnmodifiableList(list, new UnmodifiableListView(list), "empty"); | |
15 list = [42]; | |
16 testUnmodifiableList(list, new UnmodifiableListView(list), "single-42"); | |
17 list = [7]; | |
18 testUnmodifiableList(list, new UnmodifiableListView(list), "single!42"); | |
19 list = [1, 42, 10]; | |
20 testUnmodifiableList(list, new UnmodifiableListView(list), "three-42"); | |
21 list = [1, 7, 10]; | |
22 testUnmodifiableList(list, new UnmodifiableListView(list), "three!42"); | |
23 | |
24 list = []; | |
25 testNonGrowableList(list, new NonGrowableListView(list), "empty"); | |
26 list = [42]; | |
27 testNonGrowableList(list, new NonGrowableListView(list), "single-42"); | |
28 list = [7]; | |
29 testNonGrowableList(list, new NonGrowableListView(list), "single!42"); | |
30 list = [1, 42, 10]; | |
31 testNonGrowableList(list, new NonGrowableListView(list), "three-42"); | |
32 list = [1, 7, 10]; | |
33 testNonGrowableList(list, new NonGrowableListView(list), "three!42"); | |
34 | |
35 Set aSet = new Set(); | |
36 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "empty"); | |
37 aSet = new Set.from([42]); | |
38 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "single-42"); | |
39 aSet = new Set.from([7]); | |
40 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "single!42"); | |
41 aSet = new Set.from([1, 42, 10]); | |
42 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "three-42"); | |
43 aSet = new Set.from([1, 7, 10]); | |
44 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "three!42"); | |
45 | |
46 Map map = new Map(); | |
47 testUnmodifiableMap(map, new UnmodifiableMapView(map), "empty"); | |
48 map = new Map()..[0] = 2; | |
49 testUnmodifiableMap(map, new UnmodifiableMapView(map), "single-0"); | |
50 map = new Map()..[3] = 2; | |
51 testUnmodifiableMap(map, new UnmodifiableMapView(map), "single!0"); | |
52 map = new Map()..[0] = 2 | |
53 ..[1] = 1 | |
54 ..[2] = 0; | |
55 testUnmodifiableMap(map, new UnmodifiableMapView(map), "three-0"); | |
56 map = new Map()..[3] = 2 | |
57 ..[1] = 1 | |
58 ..[2] = 3; | |
59 testUnmodifiableMap(map, new UnmodifiableMapView(map), "three!0"); | |
60 } | |
61 | |
62 void testUnmodifiableList(List original, List wrapped, String name) { | |
63 name = "unmodifiable-list-$name"; | |
64 testIterable(original, wrapped, name); | |
65 testReadList(original, wrapped, name); | |
66 testNoWriteList(original, wrapped, name); | |
67 testNoChangeLengthList(original, wrapped, name); | |
68 } | |
69 | |
70 void testNonGrowableList(List original, List wrapped, String name) { | |
71 name = "nongrowable-list-$name"; | |
72 testIterable(original, wrapped, name); | |
73 testReadList(original, wrapped, name); | |
74 testWriteList(original, wrapped, name); | |
75 testNoChangeLengthList(original, wrapped, name); | |
76 } | |
77 | |
78 void testUnmodifiableSet(Set original, Set wrapped, String name) { | |
79 name = "unmodifiable-set-$name"; | |
80 testIterable(original, wrapped, name); | |
81 testReadSet(original, wrapped, name); | |
82 testNoChangeSet(original, wrapped, name); | |
83 } | |
84 | |
85 void testUnmodifiableMap(Map original, Map wrapped, name) { | |
86 name = "unmodifiable-map-$name"; | |
87 testReadMap(original, wrapped, name); | |
88 testNoChangeMap(original, wrapped, name); | |
89 } | |
90 | |
91 void testIterable(Iterable original, Iterable wrapped, String name) { | |
92 test("$name - any", () { | |
93 expect(wrapped.any((x) => true), equals(original.any((x) => true))); | |
94 expect(wrapped.any((x) => false), equals(original.any((x) => false))); | |
95 }); | |
96 | |
97 test("$name - contains", () { | |
98 expect(wrapped.contains(0), equals(original.contains(0))); | |
99 }); | |
100 | |
101 test("$name - elementAt", () { | |
102 if (original.isEmpty) { | |
103 expect(() => wrapped.elementAt(0), throws); | |
104 } else { | |
105 expect(wrapped.elementAt(0), equals(original.elementAt(0))); | |
106 } | |
107 }); | |
108 | |
109 test("$name - every", () { | |
110 expect(wrapped.every((x) => true), equals(original.every((x) => true))); | |
111 expect(wrapped.every((x) => false), equals(original.every((x) => false))); | |
112 }); | |
113 | |
114 test("$name - expand", () { | |
115 expect(wrapped.expand((x) => [x, x]), | |
116 equals(original.expand((x) => [x, x]))); | |
117 }); | |
118 | |
119 test("$name - first", () { | |
120 if (original.isEmpty) { | |
121 expect(() => wrapped.first, throws); | |
122 } else { | |
123 expect(wrapped.first, equals(original.first)); | |
124 } | |
125 }); | |
126 | |
127 test("$name - firstWhere", () { | |
128 if (original.isEmpty) { | |
129 expect(() => wrapped.firstWhere((_) => true), throws); | |
130 } else { | |
131 expect(wrapped.firstWhere((_) => true), | |
132 equals(original.firstWhere((_) => true))); | |
133 } | |
134 expect(() => wrapped.firstWhere((_) => false), throws); | |
135 }); | |
136 | |
137 test("$name - fold", () { | |
138 expect(wrapped.fold(0, (x, y) => x + y), | |
139 equals(original.fold(0, (x, y) => x + y))); | |
140 }); | |
141 | |
142 test("$name - forEach", () { | |
143 int wrapCtr = 0; | |
144 int origCtr = 0; | |
145 wrapped.forEach((x) { wrapCtr += x; }); | |
146 original.forEach((x) { origCtr += x; }); | |
147 expect(wrapCtr, equals(origCtr)); | |
148 }); | |
149 | |
150 test("$name - isEmpty", () { | |
151 expect(wrapped.isEmpty, equals(original.isEmpty)); | |
152 }); | |
153 | |
154 test("$name - isNotEmpty", () { | |
155 expect(wrapped.isNotEmpty, equals(original.isNotEmpty)); | |
156 }); | |
157 | |
158 test("$name - iterator", () { | |
159 Iterator wrapIter = wrapped.iterator; | |
160 Iterator origIter = original.iterator; | |
161 while (origIter.moveNext()) { | |
162 expect(wrapIter.moveNext(), equals(true)); | |
163 expect(wrapIter.current, equals(origIter.current)); | |
164 } | |
165 expect(wrapIter.moveNext(), equals(false)); | |
166 }); | |
167 | |
168 test("$name - join", () { | |
169 expect(wrapped.join(""), equals(original.join(""))); | |
170 expect(wrapped.join("-"), equals(original.join("-"))); | |
171 }); | |
172 | |
173 test("$name - last", () { | |
174 if (original.isEmpty) { | |
175 expect(() => wrapped.last, throws); | |
176 } else { | |
177 expect(wrapped.last, equals(original.last)); | |
178 } | |
179 }); | |
180 | |
181 test("$name - lastWhere", () { | |
182 if (original.isEmpty) { | |
183 expect(() => wrapped.lastWhere((_) => true), throws); | |
184 } else { | |
185 expect(wrapped.lastWhere((_) => true), | |
186 equals(original.lastWhere((_) => true))); | |
187 } | |
188 expect(() => wrapped.lastWhere((_) => false), throws); | |
189 }); | |
190 | |
191 test("$name - length", () { | |
192 expect(wrapped.length, equals(original.length)); | |
193 }); | |
194 | |
195 test("$name - map", () { | |
196 expect(wrapped.map((x) => "[$x]"), | |
197 equals(original.map((x) => "[$x]"))); | |
198 }); | |
199 | |
200 test("$name - reduce", () { | |
201 if (original.isEmpty) { | |
202 expect(() => wrapped.reduce((x, y) => x + y), throws); | |
203 } else { | |
204 expect(wrapped.reduce((x, y) => x + y), | |
205 equals(original.reduce((x, y) => x + y))); | |
206 } | |
207 }); | |
208 | |
209 test("$name - single", () { | |
210 if (original.length != 1) { | |
211 expect(() => wrapped.single, throws); | |
212 } else { | |
213 expect(wrapped.single, equals(original.single)); | |
214 } | |
215 }); | |
216 | |
217 test("$name - singleWhere", () { | |
218 if (original.length != 1) { | |
219 expect(() => wrapped.singleWhere((_) => true), throws); | |
220 } else { | |
221 expect(wrapped.singleWhere((_) => true), | |
222 equals(original.singleWhere((_) => true))); | |
223 } | |
224 expect(() => wrapped.singleWhere((_) => false), throws); | |
225 }); | |
226 | |
227 test("$name - skip", () { | |
228 expect(wrapped.skip(0), orderedEquals(original.skip(0))); | |
229 expect(wrapped.skip(1), orderedEquals(original.skip(1))); | |
230 expect(wrapped.skip(5), orderedEquals(original.skip(5))); | |
231 }); | |
232 | |
233 test("$name - skipWhile", () { | |
234 expect(wrapped.skipWhile((x) => true), | |
235 orderedEquals(original.skipWhile((x) => true))); | |
236 expect(wrapped.skipWhile((x) => false), | |
237 orderedEquals(original.skipWhile((x) => false))); | |
238 expect(wrapped.skipWhile((x) => x != 42), | |
239 orderedEquals(original.skipWhile((x) => x != 42))); | |
240 }); | |
241 | |
242 test("$name - take", () { | |
243 expect(wrapped.take(0), orderedEquals(original.take(0))); | |
244 expect(wrapped.take(1), orderedEquals(original.take(1))); | |
245 expect(wrapped.take(5), orderedEquals(original.take(5))); | |
246 }); | |
247 | |
248 test("$name - takeWhile", () { | |
249 expect(wrapped.takeWhile((x) => true), | |
250 orderedEquals(original.takeWhile((x) => true))); | |
251 expect(wrapped.takeWhile((x) => false), | |
252 orderedEquals(original.takeWhile((x) => false))); | |
253 expect(wrapped.takeWhile((x) => x != 42), | |
254 orderedEquals(original.takeWhile((x) => x != 42))); | |
255 }); | |
256 | |
257 test("$name - toList", () { | |
258 expect(wrapped.toList(), orderedEquals(original.toList())); | |
259 expect(wrapped.toList(growable: false), | |
260 orderedEquals(original.toList(growable: false))); | |
261 }); | |
262 | |
263 test("$name - toSet", () { | |
264 expect(wrapped.toSet(), unorderedEquals(original.toSet())); | |
265 }); | |
266 | |
267 test("$name - where", () { | |
268 expect(wrapped.where((x) => true), | |
269 orderedEquals(original.where((x) => true))); | |
270 expect(wrapped.where((x) => false), | |
271 orderedEquals(original.where((x) => false))); | |
272 expect(wrapped.where((x) => x != 42), | |
273 orderedEquals(original.where((x) => x != 42))); | |
274 }); | |
275 } | |
276 | |
277 void testReadList(List original, List wrapped, String name) { | |
278 test("$name - length", () { | |
279 expect(wrapped.length, equals(original.length)); | |
280 }); | |
281 | |
282 test("$name - isEmpty", () { | |
283 expect(wrapped.isEmpty, equals(original.isEmpty)); | |
284 }); | |
285 | |
286 test("$name - isNotEmpty", () { | |
287 expect(wrapped.isNotEmpty, equals(original.isNotEmpty)); | |
288 }); | |
289 | |
290 test("$name - []", () { | |
291 if (original.isEmpty) { | |
292 expect(() { wrapped[0]; }, throwsRangeError); | |
293 } else { | |
294 expect(wrapped[0], equals(original[0])); | |
295 } | |
296 }); | |
297 | |
298 test("$name - indexOf", () { | |
299 expect(wrapped.indexOf(42), equals(original.indexOf(42))); | |
300 }); | |
301 | |
302 test("$name - lastIndexOf", () { | |
303 expect(wrapped.lastIndexOf(42), equals(original.lastIndexOf(42))); | |
304 }); | |
305 | |
306 test("$name - getRange", () { | |
307 int len = original.length; | |
308 expect(wrapped.getRange(0, len), equals(original.getRange(0, len))); | |
309 expect(wrapped.getRange(len ~/ 2, len), | |
310 equals(original.getRange(len ~/ 2, len))); | |
311 expect(wrapped.getRange(0, len ~/ 2), | |
312 equals(original.getRange(0, len ~/ 2))); | |
313 }); | |
314 | |
315 test("$name - sublist", () { | |
316 int len = original.length; | |
317 expect(wrapped.sublist(0), equals(original.sublist(0))); | |
318 expect(wrapped.sublist(len ~/ 2), equals(original.sublist(len ~/ 2))); | |
319 expect(wrapped.sublist(0, len ~/ 2), | |
320 equals(original.sublist(0, len ~/ 2))); | |
321 }); | |
322 | |
323 test("$name - asMap", () { | |
324 expect(wrapped.asMap(), equals(original.asMap())); | |
325 }); | |
326 } | |
327 | |
328 void testNoWriteList(List original, List wrapped, String name) { | |
329 List copy = new List.from(original); | |
330 | |
331 testThrows(name, thunk) { | |
332 test(name, () { | |
333 expect(thunk, throwsUnsupportedError); | |
334 // No modifications happened. | |
335 expect(original, equals(copy)); | |
336 }); | |
337 } | |
338 | |
339 testThrows("$name - []= throws", () { wrapped[0] = 42; }); | |
340 | |
341 testThrows("$name - sort throws", () { wrapped.sort(); }); | |
342 | |
343 testThrows("$name - fillRange throws", () { | |
344 wrapped.fillRange(0, wrapped.length, 42); | |
345 }); | |
346 | |
347 testThrows("$name - setRange throws", () { | |
348 wrapped.setRange(0, wrapped.length, | |
349 new Iterable.generate(wrapped.length, (i) => i)); | |
350 }); | |
351 | |
352 testThrows("$name - setAll throws", () { | |
353 wrapped.setAll(0, new Iterable.generate(wrapped.length, (i) => i)); | |
354 }); | |
355 } | |
356 | |
357 void testWriteList(List original, List wrapped, String name) { | |
358 List copy = new List.from(original); | |
359 | |
360 test("$name - []=", () { | |
361 if (original.isNotEmpty) { | |
362 int originalFirst = original[0]; | |
363 wrapped[0] = originalFirst + 1; | |
364 expect(original[0], equals(originalFirst + 1)); | |
365 original[0] = originalFirst; | |
366 } else { | |
367 expect(() { wrapped[0] = 42; }, throws); | |
368 } | |
369 }); | |
370 | |
371 test("$name - sort", () { | |
372 List sortCopy = new List.from(original); | |
373 sortCopy.sort(); | |
374 wrapped.sort(); | |
375 expect(original, orderedEquals(sortCopy)); | |
376 original.setAll(0, copy); | |
377 }); | |
378 | |
379 test("$name - fillRange", () { | |
380 wrapped.fillRange(0, wrapped.length, 37); | |
381 for (int i = 0; i < original.length; i++) { | |
382 expect(original[i], equals(37)); | |
383 } | |
384 original.setAll(0, copy); | |
385 }); | |
386 | |
387 test("$name - setRange", () { | |
388 List reverseList = original.reversed.toList(); | |
389 wrapped.setRange(0, wrapped.length, reverseList); | |
390 expect(original, equals(reverseList)); | |
391 original.setAll(0, copy); | |
392 }); | |
393 | |
394 test("$name - setAll", () { | |
395 List reverseList = original.reversed.toList(); | |
396 wrapped.setAll(0, reverseList); | |
397 expect(original, equals(reverseList)); | |
398 original.setAll(0, copy); | |
399 }); | |
400 } | |
401 | |
402 void testNoChangeLengthList(List original, List wrapped, String name) { | |
403 List copy = new List.from(original); | |
404 | |
405 testThrows(name, thunk) { | |
406 test(name, () { | |
407 expect(thunk, throwsUnsupportedError); | |
408 // No modifications happened. | |
409 expect(original, equals(copy)); | |
410 }); | |
411 } | |
412 | |
413 testThrows("$name - length= throws", () { | |
414 wrapped.length = 100; | |
415 }); | |
416 | |
417 testThrows("$name - add throws", () { | |
418 wrapped.add(42); | |
419 }); | |
420 | |
421 testThrows("$name - addAll throws", () { | |
422 wrapped.addAll([42]); | |
423 }); | |
424 | |
425 testThrows("$name - insert throws", () { | |
426 wrapped.insert(0, 42); | |
427 }); | |
428 | |
429 testThrows("$name - insertAll throws", () { | |
430 wrapped.insertAll(0, [42]); | |
431 }); | |
432 | |
433 testThrows("$name - remove throws", () { | |
434 wrapped.remove(42); | |
435 }); | |
436 | |
437 testThrows("$name - removeAt throws", () { | |
438 wrapped.removeAt(0); | |
439 }); | |
440 | |
441 testThrows("$name - removeLast throws", () { | |
442 wrapped.removeLast(); | |
443 }); | |
444 | |
445 testThrows("$name - removeWhere throws", () { | |
446 wrapped.removeWhere((element) => false); | |
447 }); | |
448 | |
449 testThrows("$name - retainWhere throws", () { | |
450 wrapped.retainWhere((element) => true); | |
451 }); | |
452 | |
453 testThrows("$name - removeRange throws", () { | |
454 wrapped.removeRange(0, wrapped.length); | |
455 }); | |
456 | |
457 testThrows("$name - replaceRange throws", () { | |
458 wrapped.replaceRange(0, wrapped.length, [42]); | |
459 }); | |
460 | |
461 testThrows("$name - clear throws", () { | |
462 wrapped.clear(); | |
463 }); | |
464 } | |
465 | |
466 void testReadSet(Set original, Set wrapped, String name) { | |
467 Set copy = new Set.from(original); | |
468 | |
469 test("$name - containsAll", () { | |
470 expect(wrapped.containsAll(copy), isTrue); | |
471 expect(wrapped.containsAll(copy.toList()), isTrue); | |
472 expect(wrapped.containsAll([]), isTrue); | |
473 expect(wrapped.containsAll([42]), equals(original.containsAll([42]))); | |
474 }); | |
475 | |
476 test("$name - intersection", () { | |
477 expect(wrapped.intersection(new Set()), isEmpty); | |
478 expect(wrapped.intersection(copy), unorderedEquals(original)); | |
479 expect(wrapped.intersection(new Set.from([42])), | |
480 new Set.from(original.contains(42) ? [42] : [])); | |
481 }); | |
482 | |
483 test("$name - union", () { | |
484 expect(wrapped.union(new Set()), unorderedEquals(original)); | |
485 expect(wrapped.union(copy), unorderedEquals(original)); | |
486 expect(wrapped.union(new Set.from([42])), | |
487 equals(original.union(new Set.from([42])))); | |
488 }); | |
489 | |
490 test("$name - difference", () { | |
491 expect(wrapped.difference(new Set()), unorderedEquals(original)); | |
492 expect(wrapped.difference(copy), isEmpty); | |
493 expect(wrapped.difference(new Set.from([42])), | |
494 equals(original.difference(new Set.from([42])))); | |
495 }); | |
496 } | |
497 | |
498 void testNoChangeSet(Set original, Set wrapped, String name) { | |
499 List originalElements = original.toList(); | |
500 | |
501 testThrows(name, thunk) { | |
502 test(name, () { | |
503 expect(thunk, throwsUnsupportedError); | |
504 // No modifications happened. | |
505 expect(original.toList(), equals(originalElements)); | |
506 }); | |
507 } | |
508 | |
509 testThrows("$name - add throws", () { | |
510 wrapped.add(42); | |
511 }); | |
512 | |
513 testThrows("$name - addAll throws", () { | |
514 wrapped.addAll([42]); | |
515 }); | |
516 | |
517 testThrows("$name - addAll empty throws", () { | |
518 wrapped.addAll([]); | |
519 }); | |
520 | |
521 testThrows("$name - remove throws", () { | |
522 wrapped.remove(42); | |
523 }); | |
524 | |
525 testThrows("$name - removeAll throws", () { | |
526 wrapped.removeAll([42]); | |
527 }); | |
528 | |
529 testThrows("$name - removeAll empty throws", () { | |
530 wrapped.removeAll([]); | |
531 }); | |
532 | |
533 testThrows("$name - retainAll throws", () { | |
534 wrapped.retainAll([42]); | |
535 }); | |
536 | |
537 testThrows("$name - removeWhere throws", () { | |
538 wrapped.removeWhere((_) => false); | |
539 }); | |
540 | |
541 testThrows("$name - retainWhere throws", () { | |
542 wrapped.retainWhere((_) => true); | |
543 }); | |
544 | |
545 testThrows("$name - clear throws", () { | |
546 wrapped.clear(); | |
547 }); | |
548 } | |
549 | |
550 void testReadMap(Map original, Map wrapped, String name) { | |
551 test("$name length", () { | |
552 expect(wrapped.length, equals(original.length)); | |
553 }); | |
554 | |
555 test("$name isEmpty", () { | |
556 expect(wrapped.isEmpty, equals(original.isEmpty)); | |
557 }); | |
558 | |
559 test("$name isNotEmpty", () { | |
560 expect(wrapped.isNotEmpty, equals(original.isNotEmpty)); | |
561 }); | |
562 | |
563 test("$name operator[]", () { | |
564 expect(wrapped[0], equals(original[0])); | |
565 expect(wrapped[999], equals(original[999])); | |
566 }); | |
567 | |
568 test("$name containsKey", () { | |
569 expect(wrapped.containsKey(0), equals(original.containsKey(0))); | |
570 expect(wrapped.containsKey(999), equals(original.containsKey(999))); | |
571 }); | |
572 | |
573 test("$name containsValue", () { | |
574 expect(wrapped.containsValue(0), equals(original.containsValue(0))); | |
575 expect(wrapped.containsValue(999), equals(original.containsValue(999))); | |
576 }); | |
577 | |
578 test("$name forEach", () { | |
579 int origCnt = 0; | |
580 int wrapCnt = 0; | |
581 wrapped.forEach((k, v) { wrapCnt += 1 << k + 3 * v; }); | |
582 original.forEach((k, v) { origCnt += 1 << k + 3 * v; }); | |
583 expect(wrapCnt, equals(origCnt)); | |
584 }); | |
585 | |
586 test("$name keys", () { | |
587 expect(wrapped.keys, orderedEquals(original.keys)); | |
588 }); | |
589 | |
590 test("$name values", () { | |
591 expect(wrapped.values, orderedEquals(original.values)); | |
592 }); | |
593 } | |
594 | |
595 testNoChangeMap(Map original, Map wrapped, String name) { | |
596 Map copy = new Map.from(original); | |
597 | |
598 testThrows(name, thunk) { | |
599 test(name, () { | |
600 expect(thunk, throwsUnsupportedError); | |
601 // No modifications happened. | |
602 expect(original, equals(copy)); | |
603 }); | |
604 } | |
605 | |
606 testThrows("$name operator[]= throws", () { | |
607 wrapped[0] = 42; | |
608 }); | |
609 | |
610 testThrows("$name putIfAbsent throws", () { | |
611 wrapped.putIfAbsent(0, () => 42); | |
612 }); | |
613 | |
614 testThrows("$name addAll throws", () { | |
615 wrapped.addAll(new Map()..[42] = 42); | |
616 }); | |
617 | |
618 testThrows("$name addAll empty throws", () { | |
619 wrapped.addAll(new Map()); | |
620 }); | |
621 | |
622 testThrows("$name remove throws", () { | |
623 wrapped.remove(0); | |
624 }); | |
625 | |
626 testThrows("$name clear throws", () { | |
627 wrapped.clear(); | |
628 }); | |
629 } | |
OLD | NEW |