Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: packages/collection/test/unmodifiable_collection_test.dart

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

Powered by Google App Engine
This is Rietveld 408576698