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

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: more documentation 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
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(".lookup", () {
517 map = new Map<Uri, int>();
518 set = new MapKeySet<Uri>(map);
519
520 var dartlang = Uri.parse("http://dartlang.org");
521 var google = Uri.parse("http://google.com");
522 map[dartlang] = 1;
523 expect(set.lookup(Uri.parse("http://dartlang.org")), same(dartlang));
524 expect(set.lookup(google), isNull);
525 });
526
527 test("is unmodifiable", () {
528 expect(() => set.add("baz"), throwsUnsupportedError);
529 expect(() => set.addAll(["baz", "bang"]), throwsUnsupportedError);
530 expect(() => set.remove("foo"), throwsUnsupportedError);
531 expect(() => set.removeAll(["baz", "bang"]), throwsUnsupportedError);
532 expect(() => set.retainAll(["foo"]), throwsUnsupportedError);
533 expect(() => set.removeWhere((_) => true), throwsUnsupportedError);
534 expect(() => set.retainWhere((_) => true), throwsUnsupportedError);
535 expect(() => set.clear(), throwsUnsupportedError);
536 });
537 });
538
539 group("MapValueSet", () {
540 var map;
541 var set;
542
543 setUp(() {
544 map = new Map<String, String>();
545 set = new MapValueSet<String, String>(map,
546 (string) => string.substring(0, 1));
547 });
548
549 testTwoElementSet(() {
550 map["f"] = "foo";
551 map["b"] = "bar";
552 return set;
553 });
554
555 test(".single", () {
556 expect(() => set.single, throwsStateError);
557 map["f"] = "foo";
558 expect(set.single, equals("foo"));
559 map["b"] = "bar";
560 expect(() => set.single, throwsStateError);
561 });
562
563 test(".toString", () {
564 expect(set.toString(), equals("{}"));
565 map["f"] = "foo";
566 map["b"] = "bar";
567 expect(set.toString(), equals("{foo, bar}"));
568 });
569
570 test(".contains", () {
571 expect(set.contains("foo"), isFalse);
572 map["f"] = "foo";
573 expect(set.contains("foo"), isTrue);
574 expect(set.contains("fblthp"), isTrue);
575 });
576
577 test(".isEmpty", () {
578 expect(set.isEmpty, isTrue);
579 map["f"] = "foo";
580 expect(set.isEmpty, isFalse);
581 });
582
583 test(".isNotEmpty", () {
584 expect(set.isNotEmpty, isFalse);
585 map["f"] = "foo";
586 expect(set.isNotEmpty, isTrue);
587 });
588
589 test(".length", () {
590 expect(set, hasLength(0));
591 map["f"] = "foo";
592 expect(set, hasLength(1));
593 map["b"] = "bar";
594 expect(set, hasLength(2));
595 });
596
597 test(".lookup", () {
598 map["f"] = "foo";
599 expect(set.lookup("fblthp"), equals("foo"));
600 expect(set.lookup("bar"), isNull);
601 });
602
603 test(".add", () {
604 set.add("foo");
605 set.add("bar");
606 expect(map, equals({"f": "foo", "b": "bar"}));
607 });
608
609 test(".addAll", () {
610 set.addAll(["foo", "bar"]);
611 expect(map, equals({"f": "foo", "b": "bar"}));
612 });
613
614 test(".clear", () {
615 map["f"] = "foo";
616 map["b"] = "bar";
617 set.clear();
618 expect(map, isEmpty);
619 });
620
621 test(".remove", () {
622 map["f"] = "foo";
623 map["b"] = "bar";
624 set.remove("fblthp");
625 expect(map, equals({"b": "bar"}));
626 });
627
628 test(".removeAll", () {
629 map["f"] = "foo";
630 map["b"] = "bar";
631 map["q"] = "qux";
632 set.removeAll(["fblthp", "qux"]);
633 expect(map, equals({"b": "bar"}));
634 });
635
636 test(".removeWhere", () {
637 map["f"] = "foo";
638 map["b"] = "bar";
639 map["q"] = "qoo";
640 set.removeWhere((element) => element.endsWith("o"));
641 expect(map, equals({"b": "bar"}));
642 });
643
644 test(".retainAll", () {
645 map["f"] = "foo";
646 map["b"] = "bar";
647 map["q"] = "qux";
648 set.retainAll(["fblthp", "qux"]);
649 expect(map, equals({"f": "foo", "q": "qux"}));
650 });
651
652 test(".retainWhere", () {
653 map["f"] = "foo";
654 map["b"] = "bar";
655 map["q"] = "qoo";
656 set.retainWhere((element) => element.endsWith("o"));
657 expect(map, equals({"f": "foo", "q": "qoo"}));
658 });
659 });
287 } 660 }
OLDNEW
« pkg/collection/lib/wrappers.dart ('K') | « pkg/collection/pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698