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

Side by Side Diff: packages/collection/test/wrapper_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
« no previous file with comments | « packages/collection/test/unmodifiable_collection_test.dart ('k') | packages/csslib/.gitignore » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /// Tests wrapper utilities.
6
7 import "dart:collection";
8 import "package:collection/collection.dart";
9 import "package:test/test.dart";
10
11 // Test that any member access/call on the wrapper object is equal to
12 // an expected access on the wrapped object.
13 // This is implemented by capturing accesses using noSuchMethod and comparing
14 // them to expected accesses captured previously.
15
16 // Compare two Invocations for having equal type and arguments.
17 void testInvocations(Invocation i1, Invocation i2) {
18 String name = "${i1.memberName}";
19 expect(i1.isGetter, equals(i2.isGetter), reason: name);
20 expect(i1.isSetter, equals(i2.isSetter), reason: name);
21 expect(i1.memberName, equals(i2.memberName), reason: name);
22 expect(i1.positionalArguments, equals(i2.positionalArguments), reason: name);
23 expect(i1.namedArguments, equals(i2.namedArguments), reason: name);
24 }
25
26 /**
27 * Utility class to record a member access and a member access on a wrapped
28 * object, and compare them for equality.
29 */
30 abstract class Expector {
31 getWrappedObject(action(Invocation i));
32 // Hack to test assignment ([]=) because it doesn't return the result
33 // of the member call. Instead use (expect..[4]=5).equal[4]=5 where
34 // you would normally use expect[4].equals[4] for non-assignments.
35 var equals;
36
37 noSuchMethod(Invocation m) => new _Equals(equals = getWrappedObject((m2) {
38 testInvocations(m, m2);
39 }));
40
41 toString() => new _Equals(equals = getWrappedObject((m2) {
42 testInvocations(TO_STRING_INVOCATION, m2);
43 }));
44 }
45
46 // An object with a field called "equals", only introduced into the
47 // flow to allow writing expect.xxx.equals.xxx.
48 class _Equals {
49 final equals;
50 _Equals(this.equals);
51 }
52
53 class SyntheticInvocation implements Invocation {
54 static const int METHOD = 0x00;
55 static const int GETTER = 0x01;
56 static const int SETTER = 0x02;
57 final Symbol memberName;
58 final List positionalArguments;
59 final Map namedArguments;
60 final int _type;
61 const SyntheticInvocation(this.memberName,
62 this.positionalArguments,
63 this.namedArguments,
64 this._type);
65 bool get isMethod => _type == METHOD;
66
67 bool get isGetter => _type == GETTER;
68
69 bool get isSetter => _type == SETTER;
70
71 bool get isAccessor => isGetter || isSetter;
72 }
73
74 // Parameterization of noSuchMethod.
75 class NSM {
76 Function _action;
77 NSM(this._action);
78 noSuchMethod(Invocation i) => _action(i);
79 }
80
81 const TO_STRING_INVOCATION = const SyntheticInvocation(
82 #toString, const[], const{}, SyntheticInvocation.METHOD);
83
84 // LikeNSM, but has types Iterable, Set and List to allow it as
85 // argument to DelegatingIterable/Set/List.
86 class IterableNSM extends NSM implements Iterable, Set, List, Queue {
87 IterableNSM(action(Invocation i)) : super(action);
88 noSuchMethod(Invocation i) => super.noSuchMethod(i); // Silence warnings
89 toString() => super.noSuchMethod(TO_STRING_INVOCATION);
90 }
91
92 // Expector that wraps in DelegatingIterable.
93 class IterableExpector extends Expector {
94 getWrappedObject(void action(Invocation i)) {
95 return new DelegatingIterable(new IterableNSM(action));
96 }
97 }
98
99 // Expector that wraps in DelegatingList.
100 class ListExpector extends Expector {
101 getWrappedObject(void action(Invocation i)) {
102 return new DelegatingList(new IterableNSM(action));
103 }
104 }
105
106 // Expector that wraps in DelegatingSet.
107 class SetExpector extends Expector {
108 getWrappedObject(void action(Invocation i)) {
109 return new DelegatingSet(new IterableNSM(action));
110 }
111 }
112
113 // Expector that wraps in DelegatingSet.
114 class QueueExpector extends Expector {
115 getWrappedObject(void action(Invocation i)) {
116 return new DelegatingQueue(new IterableNSM(action));
117 }
118 }
119
120 // Like NSM but implements Map to allow as argument for DelegatingMap.
121 class MapNSM extends NSM implements Map {
122 MapNSM(action(Invocation i)) : super(action);
123 noSuchMethod(Invocation i) => super.noSuchMethod(i);
124 toString() => super.noSuchMethod(TO_STRING_INVOCATION);
125 }
126
127 // Expector that wraps in DelegatingMap.
128 class MapExpector extends Expector {
129 getWrappedObject(void action(Invocation i)) {
130 return new DelegatingMap(new MapNSM(action));
131 }
132 }
133
134 // Utility values to use as arguments in calls.
135 func0() {}
136 func1(x) {}
137 func2(x, y) {}
138 var val = new Object();
139
140 void main() {
141 testIterable(var expect) {
142 expect.any(func1).equals.any(func1);
143 expect.contains(val).equals.contains(val);
144 expect.elementAt(0).equals.elementAt(0);
145 expect.every(func1).equals.every(func1);
146 expect.expand(func1).equals.expand(func1);
147 expect.first.equals.first;
148 // Default values of the Iterable interface will be added in the
149 // second call to firstWhere, so we must record them in our
150 // expectation (which doesn't have the interface implementat or
151 // its default values).
152 expect.firstWhere(func1, orElse: null).equals.firstWhere(func1);
153 expect.firstWhere(func1, orElse: func0).equals.
154 firstWhere(func1, orElse: func0);
155 expect.fold(null, func2).equals.fold(null, func2);
156 expect.forEach(func1).equals.forEach(func1);
157 expect.isEmpty.equals.isEmpty;
158 expect.isNotEmpty.equals.isNotEmpty;
159 expect.iterator.equals.iterator;
160 expect.join('').equals.join();
161 expect.join("X").equals.join("X");
162 expect.last.equals.last;
163 expect.lastWhere(func1, orElse: null).equals.lastWhere(func1);
164 expect.lastWhere(func1, orElse: func0).equals.
165 lastWhere(func1, orElse: func0);
166 expect.length.equals.length;
167 expect.map(func1).equals.map(func1);
168 expect.reduce(func2).equals.reduce(func2);
169 expect.single.equals.single;
170 expect.singleWhere(func1).equals.singleWhere(func1);
171 expect.skip(5).equals.skip(5);
172 expect.skipWhile(func1).equals.skipWhile(func1);
173 expect.take(5).equals.take(5);
174 expect.takeWhile(func1).equals.takeWhile(func1);
175 expect.toList(growable: true).equals.toList();
176 expect.toList(growable: true).equals.toList(growable: true);
177 expect.toList(growable: false).equals.toList(growable: false);
178 expect.toSet().equals.toSet();
179 expect.toString().equals.toString();
180 expect.where(func1).equals.where(func1);
181 }
182
183 void testList(var expect) {
184 testIterable(expect);
185
186 expect[4].equals[4];
187 (expect..[4] = 5).equals[4] = 5;
188
189 expect.add(val).equals.add(val);
190 expect.addAll([val]).equals.addAll([val]);
191 expect.asMap().equals.asMap();
192 expect.clear().equals.clear();
193 expect.fillRange(4, 5, null).equals.fillRange(4, 5);
194 expect.fillRange(4, 5, val).equals.fillRange(4, 5, val);
195 expect.getRange(4, 5).equals.getRange(4, 5);
196 expect.indexOf(val, 0).equals.indexOf(val);
197 expect.indexOf(val, 4).equals.indexOf(val, 4);
198 expect.insert(4, val).equals.insert(4, val);
199 expect.insertAll(4, [val]).equals.insertAll(4, [val]);
200 expect.lastIndexOf(val, null).equals.lastIndexOf(val);
201 expect.lastIndexOf(val, 4).equals.lastIndexOf(val, 4);
202 (expect..length = 4).equals.length = 4;
203 expect.remove(val).equals.remove(val);
204 expect.removeAt(4).equals.removeAt(4);
205 expect.removeLast().equals.removeLast();
206 expect.removeRange(4, 5).equals.removeRange(4, 5);
207 expect.removeWhere(func1).equals.removeWhere(func1);
208 expect.replaceRange(4, 5, [val]).equals.replaceRange(4, 5, [val]);
209 expect.retainWhere(func1).equals.retainWhere(func1);
210 expect.reversed.equals.reversed;
211 expect.setAll(4, [val]).equals.setAll(4, [val]);
212 expect.setRange(4, 5, [val], 0).equals.setRange(4, 5, [val]);
213 expect.setRange(4, 5, [val], 3).equals.setRange(4, 5, [val], 3);
214 expect.sort(null).equals.sort();
215 expect.sort(func2).equals.sort(func2);
216 expect.sublist(4, null).equals.sublist(4);
217 expect.sublist(4, 5).equals.sublist(4, 5);
218 }
219
220 void testSet(var expect) {
221 testIterable(expect);
222 Set set = new Set();
223 expect.add(val).equals.add(val);
224 expect.addAll([val]).equals.addAll([val]);
225 expect.clear().equals.clear();
226 expect.containsAll([val]).equals.containsAll([val]);
227 expect.difference(set).equals.difference(set);
228 expect.intersection(set).equals.intersection(set);
229 expect.remove(val).equals.remove(val);
230 expect.removeAll([val]).equals.removeAll([val]);
231 expect.removeWhere(func1).equals.removeWhere(func1);
232 expect.retainAll([val]).equals.retainAll([val]);
233 expect.retainWhere(func1).equals.retainWhere(func1);
234 expect.union(set).equals.union(set);
235 }
236
237 void testQueue(var expect) {
238 testIterable(expect);
239 expect.add(val).equals.add(val);
240 expect.addAll([val]).equals.addAll([val]);
241 expect.addFirst(val).equals.addFirst(val);
242 expect.addLast(val).equals.addLast(val);
243 expect.clear().equals.clear();
244 expect.remove(val).equals.remove(val);
245 expect.removeFirst().equals.removeFirst();
246 expect.removeLast().equals.removeLast();
247 }
248
249 void testMap(var expect) {
250 Map map = new Map();
251 expect[val].equals[val];
252 (expect..[val] = val).equals[val] = val;
253 expect.addAll(map).equals.addAll(map);
254 expect.clear().equals.clear();
255 expect.containsKey(val).equals.containsKey(val);
256 expect.containsValue(val).equals.containsValue(val);
257 expect.forEach(func2).equals.forEach(func2);
258 expect.isEmpty.equals.isEmpty;
259 expect.isNotEmpty.equals.isNotEmpty;
260 expect.keys.equals.keys;
261 expect.length.equals.length;
262 expect.putIfAbsent(val, func0).equals.putIfAbsent(val, func0);
263 expect.remove(val).equals.remove(val);
264 expect.values.equals.values;
265 expect.toString().equals.toString();
266 }
267
268 // Runs tests of Set behavior.
269 //
270 // [setUpSet] should return a set with two elements: "foo" and "bar".
271 void testTwoElementSet(Set<String> setUpSet()) {
272 group("with two elements", () {
273 var set;
274 setUp(() => set = setUpSet());
275
276 test(".any", () {
277 expect(set.any((element) => element == "foo"), isTrue);
278 expect(set.any((element) => element == "baz"), isFalse);
279 });
280
281 test(".elementAt", () {
282 expect(set.elementAt(0), equals("foo"));
283 expect(set.elementAt(1), equals("bar"));
284 expect(() => set.elementAt(2), throwsRangeError);
285 });
286
287 test(".every", () {
288 expect(set.every((element) => element == "foo"), isFalse);
289 expect(set.every((element) => element is String), isTrue);
290 });
291
292 test(".expand", () {
293 expect(set.expand((element) {
294 return [element.substring(0, 1), element.substring(1)];
295 }), equals(["f", "oo", "b", "ar"]));
296 });
297
298 test(".first", () {
299 expect(set.first, equals("foo"));
300 });
301
302 test(".firstWhere", () {
303 expect(set.firstWhere((element) => element is String), equals("foo"));
304 expect(set.firstWhere((element) => element.startsWith("b")),
305 equals("bar"));
306 expect(() => set.firstWhere((element) => element is int),
307 throwsStateError);
308 expect(set.firstWhere((element) => element is int, orElse: () => "baz"),
309 equals("baz"));
310 });
311
312 test(".fold", () {
313 expect(set.fold("start", (previous, element) => previous + element),
314 equals("startfoobar"));
315 });
316
317 test(".forEach", () {
318 var values = [];
319 set.forEach(values.add);
320 expect(values, equals(["foo", "bar"]));
321 });
322
323 test(".iterator", () {
324 var values = [];
325 for (var element in set) {
326 values.add(element);
327 }
328 expect(values, equals(["foo", "bar"]));
329 });
330
331 test(".join", () {
332 expect(set.join(", "), equals("foo, bar"));
333 });
334
335 test(".last", () {
336 expect(set.last, equals("bar"));
337 });
338
339 test(".lastWhere", () {
340 expect(set.lastWhere((element) => element is String), equals("bar"));
341 expect(set.lastWhere((element) => element.startsWith("f")),
342 equals("foo"));
343 expect(() => set.lastWhere((element) => element is int),
344 throwsStateError);
345 expect(set.lastWhere((element) => element is int, orElse: () => "baz"),
346 equals("baz"));
347 });
348
349 test(".map", () {
350 expect(set.map((element) => element.substring(1)),
351 equals(["oo", "ar"]));
352 });
353
354 test(".reduce", () {
355 expect(set.reduce((previous, element) => previous + element),
356 equals("foobar"));
357 });
358
359 test(".singleWhere", () {
360 expect(() => set.singleWhere((element) => element == "baz"),
361 throwsStateError);
362 expect(set.singleWhere((element) => element == "foo"),
363 "foo");
364 expect(() => set.singleWhere((element) => element is String),
365 throwsStateError);
366 });
367
368 test(".skip", () {
369 expect(set.skip(0), equals(["foo", "bar"]));
370 expect(set.skip(1), equals(["bar"]));
371 expect(set.skip(2), equals([]));
372 });
373
374 test(".skipWhile", () {
375 expect(set.skipWhile((element) => element.startsWith("f")),
376 equals(["bar"]));
377 expect(set.skipWhile((element) => element.startsWith("z")),
378 equals(["foo", "bar"]));
379 expect(set.skipWhile((element) => element is String),
380 equals([]));
381 });
382
383 test(".take", () {
384 expect(set.take(0), equals([]));
385 expect(set.take(1), equals(["foo"]));
386 expect(set.take(2), equals(["foo", "bar"]));
387 });
388
389 test(".takeWhile", () {
390 expect(set.takeWhile((element) => element.startsWith("f")),
391 equals(["foo"]));
392 expect(set.takeWhile((element) => element.startsWith("z")),
393 equals([]));
394 expect(set.takeWhile((element) => element is String),
395 equals(["foo", "bar"]));
396 });
397
398 test(".toList", () {
399 expect(set.toList(), equals(["foo", "bar"]));
400 expect(() => set.toList(growable: false).add("baz"),
401 throwsUnsupportedError);
402 expect(set.toList()..add("baz"), equals(["foo", "bar", "baz"]));
403 });
404
405 test(".toSet", () {
406 expect(set.toSet(), equals(new Set.from(["foo", "bar"])));
407 });
408
409 test(".where", () {
410 expect(set.where((element) => element.startsWith("f")),
411 equals(["foo"]));
412 expect(set.where((element) => element.startsWith("z")), equals([]));
413 expect(set.where((element) => element is String),
414 equals(["foo", "bar"]));
415 });
416
417 test(".containsAll", () {
418 expect(set.containsAll(["foo", "bar"]), isTrue);
419 expect(set.containsAll(["foo"]), isTrue);
420 expect(set.containsAll(["foo", "bar", "qux"]), isFalse);
421 });
422
423 test(".difference", () {
424 expect(set.difference(new Set.from(["foo", "baz"])),
425 equals(new Set.from(["bar"])));
426 });
427
428 test(".intersection", () {
429 expect(set.intersection(new Set.from(["foo", "baz"])),
430 equals(new Set.from(["foo"])));
431 });
432
433 test(".union", () {
434 expect(set.union(new Set.from(["foo", "baz"])),
435 equals(new Set.from(["foo", "bar", "baz"])));
436 });
437 });
438 }
439
440 test("Iterable", () {
441 testIterable(new IterableExpector());
442 });
443
444 test("List", () {
445 testList(new ListExpector());
446 });
447
448 test("Set", () {
449 testSet(new SetExpector());
450 });
451
452 test("Queue", () {
453 testQueue(new QueueExpector());
454 });
455
456 test("Map", () {
457 testMap(new MapExpector());
458 });
459
460 group("MapKeySet", () {
461 var map;
462 var set;
463
464 setUp(() {
465 map = new Map<String, int>();
466 set = new MapKeySet<String>(map);
467 });
468
469 testTwoElementSet(() {
470 map["foo"] = 1;
471 map["bar"] = 2;
472 return set;
473 });
474
475 test(".single", () {
476 expect(() => set.single, throwsStateError);
477 map["foo"] = 1;
478 expect(set.single, equals("foo"));
479 map["bar"] = 1;
480 expect(() => set.single, throwsStateError);
481 });
482
483 test(".toString", () {
484 expect(set.toString(), equals("{}"));
485 map["foo"] = 1;
486 map["bar"] = 2;
487 expect(set.toString(), equals("{foo, bar}"));
488 });
489
490 test(".contains", () {
491 expect(set.contains("foo"), isFalse);
492 map["foo"] = 1;
493 expect(set.contains("foo"), isTrue);
494 });
495
496 test(".isEmpty", () {
497 expect(set.isEmpty, isTrue);
498 map["foo"] = 1;
499 expect(set.isEmpty, isFalse);
500 });
501
502 test(".isNotEmpty", () {
503 expect(set.isNotEmpty, isFalse);
504 map["foo"] = 1;
505 expect(set.isNotEmpty, isTrue);
506 });
507
508 test(".length", () {
509 expect(set, hasLength(0));
510 map["foo"] = 1;
511 expect(set, hasLength(1));
512 map["bar"] = 2;
513 expect(set, hasLength(2));
514 });
515
516 test("is unmodifiable", () {
517 expect(() => set.add("baz"), throwsUnsupportedError);
518 expect(() => set.addAll(["baz", "bang"]), throwsUnsupportedError);
519 expect(() => set.remove("foo"), throwsUnsupportedError);
520 expect(() => set.removeAll(["baz", "bang"]), throwsUnsupportedError);
521 expect(() => set.retainAll(["foo"]), throwsUnsupportedError);
522 expect(() => set.removeWhere((_) => true), throwsUnsupportedError);
523 expect(() => set.retainWhere((_) => true), throwsUnsupportedError);
524 expect(() => set.clear(), throwsUnsupportedError);
525 });
526 });
527
528 group("MapValueSet", () {
529 var map;
530 var set;
531
532 setUp(() {
533 map = new Map<String, String>();
534 set = new MapValueSet<String, String>(map,
535 (string) => string.substring(0, 1));
536 });
537
538 testTwoElementSet(() {
539 map["f"] = "foo";
540 map["b"] = "bar";
541 return set;
542 });
543
544 test(".single", () {
545 expect(() => set.single, throwsStateError);
546 map["f"] = "foo";
547 expect(set.single, equals("foo"));
548 map["b"] = "bar";
549 expect(() => set.single, throwsStateError);
550 });
551
552 test(".toString", () {
553 expect(set.toString(), equals("{}"));
554 map["f"] = "foo";
555 map["b"] = "bar";
556 expect(set.toString(), equals("{foo, bar}"));
557 });
558
559 test(".contains", () {
560 expect(set.contains("foo"), isFalse);
561 map["f"] = "foo";
562 expect(set.contains("foo"), isTrue);
563 expect(set.contains("fblthp"), isTrue);
564 });
565
566 test(".isEmpty", () {
567 expect(set.isEmpty, isTrue);
568 map["f"] = "foo";
569 expect(set.isEmpty, isFalse);
570 });
571
572 test(".isNotEmpty", () {
573 expect(set.isNotEmpty, isFalse);
574 map["f"] = "foo";
575 expect(set.isNotEmpty, isTrue);
576 });
577
578 test(".length", () {
579 expect(set, hasLength(0));
580 map["f"] = "foo";
581 expect(set, hasLength(1));
582 map["b"] = "bar";
583 expect(set, hasLength(2));
584 });
585
586 test(".lookup", () {
587 map["f"] = "foo";
588 expect(set.lookup("fblthp"), equals("foo"));
589 expect(set.lookup("bar"), isNull);
590 });
591
592 test(".add", () {
593 set.add("foo");
594 set.add("bar");
595 expect(map, equals({"f": "foo", "b": "bar"}));
596 });
597
598 test(".addAll", () {
599 set.addAll(["foo", "bar"]);
600 expect(map, equals({"f": "foo", "b": "bar"}));
601 });
602
603 test(".clear", () {
604 map["f"] = "foo";
605 map["b"] = "bar";
606 set.clear();
607 expect(map, isEmpty);
608 });
609
610 test(".remove", () {
611 map["f"] = "foo";
612 map["b"] = "bar";
613 set.remove("fblthp");
614 expect(map, equals({"b": "bar"}));
615 });
616
617 test(".removeAll", () {
618 map["f"] = "foo";
619 map["b"] = "bar";
620 map["q"] = "qux";
621 set.removeAll(["fblthp", "qux"]);
622 expect(map, equals({"b": "bar"}));
623 });
624
625 test(".removeWhere", () {
626 map["f"] = "foo";
627 map["b"] = "bar";
628 map["q"] = "qoo";
629 set.removeWhere((element) => element.endsWith("o"));
630 expect(map, equals({"b": "bar"}));
631 });
632
633 test(".retainAll", () {
634 map["f"] = "foo";
635 map["b"] = "bar";
636 map["q"] = "qux";
637 set.retainAll(["fblthp", "qux"]);
638 expect(map, equals({"f": "foo", "q": "qux"}));
639 });
640
641 test(".retainAll respects an unusual notion of equality", () {
642 map = new HashMap<String, String>(
643 equals: (value1, value2) =>
644 value1.toLowerCase() == value2.toLowerCase(),
645 hashCode: (value) => value.toLowerCase().hashCode);
646 set = new MapValueSet<String, String>(map,
647 (string) => string.substring(0, 1));
648
649 map["f"] = "foo";
650 map["B"] = "bar";
651 map["Q"] = "qux";
652 set.retainAll(["fblthp", "qux"]);
653 expect(map, equals({"f": "foo", "Q": "qux"}));
654 });
655
656 test(".retainWhere", () {
657 map["f"] = "foo";
658 map["b"] = "bar";
659 map["q"] = "qoo";
660 set.retainWhere((element) => element.endsWith("o"));
661 expect(map, equals({"f": "foo", "q": "qoo"}));
662 });
663 });
664 }
OLDNEW
« no previous file with comments | « packages/collection/test/unmodifiable_collection_test.dart ('k') | packages/csslib/.gitignore » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698