| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 for some of the utility helper functions used by the compiler. */ | 5 /** Tests for some of the utility helper functions used by the compiler. */ |
| 6 library observe_test; | 6 library observe_test; |
| 7 | 7 |
| 8 import 'dart:collection' show LinkedHashMap; | 8 import 'dart:collection' show LinkedHashMap; |
| 9 import 'package:unittest/compact_vm_config.dart'; | 9 import 'package:unittest/compact_vm_config.dart'; |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| (...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 | 583 |
| 584 group('ObservableSet', () { | 584 group('ObservableSet', () { |
| 585 // TODO(jmesserly): need all standard Set tests. | 585 // TODO(jmesserly): need all standard Set tests. |
| 586 | 586 |
| 587 test('observe length', () { | 587 test('observe length', () { |
| 588 var set = new ObservableSet<int>(); | 588 var set = new ObservableSet<int>(); |
| 589 var notification = null; | 589 var notification = null; |
| 590 observe(() => set.length, (n) { notification = n; }); | 590 observe(() => set.length, (n) { notification = n; }); |
| 591 | 591 |
| 592 set.addAll([1, 2, 3]); | 592 set.addAll([1, 2, 3]); |
| 593 expect(set, [1, 2, 3]); | 593 expect(set, unorderedEquals([1, 2, 3])); |
| 594 deliverChangesSync(); | 594 deliverChangesSync(); |
| 595 expect(notification, _change(0, 3), reason: 'addAll changes length'); | 595 expect(notification, _change(0, 3), reason: 'addAll changes length'); |
| 596 | 596 |
| 597 set.add(4); | 597 set.add(4); |
| 598 expect(set, [1, 2, 3, 4]); | 598 expect(set, unorderedEquals([1, 2, 3, 4])); |
| 599 deliverChangesSync(); | 599 deliverChangesSync(); |
| 600 expect(notification, _change(3, 4), reason: 'add changes length'); | 600 expect(notification, _change(3, 4), reason: 'add changes length'); |
| 601 | 601 |
| 602 set.removeAll([2, 3]); | 602 set.removeAll([2, 3]); |
| 603 expect(set, [1, 4]); | 603 expect(set, unorderedEquals([1, 4])); |
| 604 deliverChangesSync(); | 604 deliverChangesSync(); |
| 605 expect(notification, _change(4, 2), reason: 'removeAll changes length'); | 605 expect(notification, _change(4, 2), reason: 'removeAll changes length'); |
| 606 | 606 |
| 607 set.remove(1); | 607 set.remove(1); |
| 608 expect(set, [4]); | 608 expect(set, [4]); |
| 609 deliverChangesSync(); | 609 deliverChangesSync(); |
| 610 expect(notification, _change(2, 1), reason: 'remove changes length'); | 610 expect(notification, _change(2, 1), reason: 'remove changes length'); |
| 611 | 611 |
| 612 notification = null; | 612 notification = null; |
| 613 set.add(4); | 613 set.add(4); |
| 614 expect(set, [4]); | 614 expect(set, [4]); |
| 615 deliverChangesSync(); | 615 deliverChangesSync(); |
| 616 expect(notification, null, reason: 'item already exists'); | 616 expect(notification, null, reason: 'item already exists'); |
| 617 | 617 |
| 618 set.clear(); | 618 set.clear(); |
| 619 expect(set, []); | 619 expect(set, []); |
| 620 deliverChangesSync(); | 620 deliverChangesSync(); |
| 621 expect(notification, _change(1, 0), reason: 'clear changes length'); | 621 expect(notification, _change(1, 0), reason: 'clear changes length'); |
| 622 }); | 622 }); |
| 623 | 623 |
| 624 test('observe item', () { | 624 test('observe item', () { |
| 625 var set = new ObservableSet.from([1, 2, 3]); | 625 var set = new ObservableSet.from([1, 2, 3]); |
| 626 var notification = null; | 626 var notification = null; |
| 627 observe(() => set.contains(2), (n) { notification = n; }); | 627 observe(() => set.contains(2), (n) { notification = n; }); |
| 628 | 628 |
| 629 set.add(4); | 629 set.add(4); |
| 630 expect(set, [1, 2, 3, 4]); | 630 expect(set, unorderedEquals([1, 2, 3, 4])); |
| 631 deliverChangesSync(); | 631 deliverChangesSync(); |
| 632 expect(notification, null, reason: 'add does not change existing items'); | 632 expect(notification, null, reason: 'add does not change existing items'); |
| 633 | 633 |
| 634 set.remove(3); | 634 set.remove(3); |
| 635 expect(set, [1, 2, 4]); | 635 expect(set, unorderedEquals([1, 2, 4])); |
| 636 expect(notification, null, | 636 expect(notification, null, |
| 637 reason: 'removing an item does not change other items'); | 637 reason: 'removing an item does not change other items'); |
| 638 | 638 |
| 639 set.remove(2); | 639 set.remove(2); |
| 640 expect(set, [1, 4]); | 640 expect(set, unorderedEquals([1, 4])); |
| 641 deliverChangesSync(); | 641 deliverChangesSync(); |
| 642 expect(notification, _change(true, false)); | 642 expect(notification, _change(true, false)); |
| 643 | 643 |
| 644 notification = null; | 644 notification = null; |
| 645 set.removeAll([2, 3]); | 645 set.removeAll([2, 3]); |
| 646 expect(set, [1, 4]); | 646 expect(set, unorderedEquals([1, 4])); |
| 647 deliverChangesSync(); | 647 deliverChangesSync(); |
| 648 expect(notification, null, reason: 'item already removed'); | 648 expect(notification, null, reason: 'item already removed'); |
| 649 | 649 |
| 650 set.add(2); | 650 set.add(2); |
| 651 expect(set, [1, 2, 4]); | 651 expect(set, unorderedEquals([1, 2, 4])); |
| 652 deliverChangesSync(); | 652 deliverChangesSync(); |
| 653 expect(notification, _change(false, true), reason: 'item added again'); | 653 expect(notification, _change(false, true), reason: 'item added again'); |
| 654 }); | 654 }); |
| 655 | 655 |
| 656 test('toString', () { | 656 test('toString', () { |
| 657 var original = new Set.from([1, 2, 3]); | 657 var original = new Set.from([1, 2, 3]); |
| 658 var set = new ObservableSet.from(original); | 658 var set = new ObservableSet.from(original); |
| 659 var notification = null; | 659 var notification = null; |
| 660 observe(() => set.toString(), (n) { notification = n; }); | 660 observe(() => set.toString(), (n) { notification = n; }); |
| 661 set.add(4); | 661 set.add(4); |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 var c = _change(123, 43); | 870 var c = _change(123, 43); |
| 871 expect(a.hashCode, b.hashCode, reason: 'a == b'); | 871 expect(a.hashCode, b.hashCode, reason: 'a == b'); |
| 872 expect(b.hashCode, isNot(equals(c.hashCode)), reason: 'b != c'); | 872 expect(b.hashCode, isNot(equals(c.hashCode)), reason: 'b != c'); |
| 873 }); | 873 }); |
| 874 } | 874 } |
| 875 | 875 |
| 876 _change(oldValue, newValue) => new ChangeNotification(oldValue, newValue); | 876 _change(oldValue, newValue) => new ChangeNotification(oldValue, newValue); |
| 877 | 877 |
| 878 _record(type, key, oldValue, newValue) => | 878 _record(type, key, oldValue, newValue) => |
| 879 new ChangeRecord(type, key, oldValue, newValue); | 879 new ChangeRecord(type, key, oldValue, newValue); |
| OLD | NEW |