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

Side by Side Diff: pkg/collection/test/wrapper_test.dart

Issue 277563007: Add MapKeySet and MapValueSet to the collection package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « pkg/collection/pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 wrapper utilities. 5 /// Tests wrapper 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:unittest/unittest.dart"; 9 import "package:unittest/unittest.dart";
10 10
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 expect.isEmpty.equals.isEmpty; 258 expect.isEmpty.equals.isEmpty;
259 expect.isNotEmpty.equals.isNotEmpty; 259 expect.isNotEmpty.equals.isNotEmpty;
260 expect.keys.equals.keys; 260 expect.keys.equals.keys;
261 expect.length.equals.length; 261 expect.length.equals.length;
262 expect.putIfAbsent(val, func0).equals.putIfAbsent(val, func0); 262 expect.putIfAbsent(val, func0).equals.putIfAbsent(val, func0);
263 expect.remove(val).equals.remove(val); 263 expect.remove(val).equals.remove(val);
264 expect.values.equals.values; 264 expect.values.equals.values;
265 expect.toString().equals.toString(); 265 expect.toString().equals.toString();
266 } 266 }
267 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
268 test("Iterable", () { 440 test("Iterable", () {
269 testIterable(new IterableExpector()); 441 testIterable(new IterableExpector());
270 }); 442 });
271 443
272 test("List", () { 444 test("List", () {
273 testList(new ListExpector()); 445 testList(new ListExpector());
274 }); 446 });
275 447
276 test("Set", () { 448 test("Set", () {
277 testSet(new SetExpector()); 449 testSet(new SetExpector());
278 }); 450 });
279 451
280 test("Queue", () { 452 test("Queue", () {
281 testQueue(new QueueExpector()); 453 testQueue(new QueueExpector());
282 }); 454 });
283 455
284 test("Map", () { 456 test("Map", () {
285 testMap(new MapExpector()); 457 testMap(new MapExpector());
286 }); 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 });
287 } 664 }
OLDNEW
« no previous file with comments | « pkg/collection/pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698