Index: pkg/observe/test/observable_list_test.dart |
diff --git a/pkg/observe/test/observable_list_test.dart b/pkg/observe/test/observable_list_test.dart |
index b886f039aafb9c82f53ddb8e38e5a0ea60c54d7b..60ddadd5368a5260b8fa82bced655f9bc680db29 100644 |
--- a/pkg/observe/test/observable_list_test.dart |
+++ b/pkg/observe/test/observable_list_test.dart |
@@ -2,15 +2,20 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
+import 'dart:async'; |
import 'package:observe/observe.dart'; |
import 'package:unittest/unittest.dart'; |
-import 'utils.dart'; |
+import 'observe_test_utils.dart'; |
main() { |
// TODO(jmesserly): need all standard List API tests. |
const _LENGTH = const Symbol('length'); |
+ StreamSubscription sub; |
+ |
+ sharedTearDown() { sub.cancel(); } |
+ |
group('observe length', () { |
ObservableList list; |
@@ -19,53 +24,54 @@ main() { |
setUp(() { |
list = toObservable([1, 2, 3]); |
changes = null; |
- list.changes.listen((records) { |
+ sub = list.changes.listen((records) { |
changes = records.where((r) => r.changes(_LENGTH)).toList(); |
}); |
}); |
- test('add changes length', () { |
+ tearDown(sharedTearDown); |
+ |
+ observeTest('add changes length', () { |
list.add(4); |
expect(list, [1, 2, 3, 4]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [_lengthChange]); |
}); |
- test('removeObject', () { |
+ observeTest('removeObject', () { |
list.remove(2); |
expect(list, orderedEquals([1, 3])); |
- deliverChangeRecords(); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [_lengthChange]); |
}); |
- test('removeRange changes length', () { |
+ observeTest('removeRange changes length', () { |
list.add(4); |
list.removeRange(1, 3); |
expect(list, [1, 4]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [_lengthChange]); |
}); |
- test('length= changes length', () { |
+ observeTest('length= changes length', () { |
list.length = 5; |
expect(list, [1, 2, 3, null, null]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [_lengthChange]); |
}); |
- test('[]= does not change length', () { |
+ observeTest('[]= does not change length', () { |
list[2] = 9000; |
expect(list, [1, 2, 9000]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, []); |
}); |
- test('clear changes length', () { |
+ observeTest('clear changes length', () { |
list.clear(); |
expect(list, []); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [_lengthChange]); |
}); |
}); |
@@ -77,78 +83,80 @@ main() { |
setUp(() { |
list = toObservable([1, 2, 3]); |
changes = null; |
- list.changes.listen((records) { |
+ sub = list.changes.listen((records) { |
changes = records.where((r) => r.changes(1)).toList(); |
}); |
}); |
- test('add does not change existing items', () { |
+ tearDown(sharedTearDown); |
+ |
+ observeTest('add does not change existing items', () { |
list.add(4); |
expect(list, [1, 2, 3, 4]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, []); |
}); |
- test('[]= changes item', () { |
+ observeTest('[]= changes item', () { |
list[1] = 777; |
expect(list, [1, 777, 3]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [_change(1, addedCount: 1, removedCount: 1)]); |
}); |
- test('[]= on a different item does not fire change', () { |
+ observeTest('[]= on a different item does not fire change', () { |
list[2] = 9000; |
expect(list, [1, 2, 9000]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, []); |
}); |
- test('set multiple times results in one change', () { |
+ observeTest('set multiple times results in one change', () { |
list[1] = 777; |
list[1] = 42; |
expect(list, [1, 42, 3]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [ |
_change(1, addedCount: 1, removedCount: 1), |
]); |
}); |
- test('set length without truncating item means no change', () { |
+ observeTest('set length without truncating item means no change', () { |
list.length = 2; |
expect(list, [1, 2]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, []); |
}); |
- test('truncate removes item', () { |
+ observeTest('truncate removes item', () { |
list.length = 1; |
expect(list, [1]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [_change(1, removedCount: 2)]); |
}); |
- test('truncate and add new item', () { |
+ observeTest('truncate and add new item', () { |
list.length = 1; |
list.add(42); |
expect(list, [1, 42]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [ |
_change(1, removedCount: 2, addedCount: 1) |
]); |
}); |
- test('truncate and add same item', () { |
+ observeTest('truncate and add same item', () { |
list.length = 1; |
list.add(2); |
expect(list, [1, 2]); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(changes, [ |
_change(1, removedCount: 2, addedCount: 1) |
]); |
}); |
}); |
- test('toString', () { |
+ observeTest('toString', () { |
var list = toObservable([1, 2, 3]); |
expect(list.toString(), '[1, 2, 3]'); |
}); |
@@ -161,10 +169,12 @@ main() { |
setUp(() { |
list = toObservable([1, 2, 3, 1, 3, 4]); |
records = null; |
- list.changes.listen((r) { records = r; }); |
+ sub = list.changes.listen((r) { records = r; }); |
}); |
- test('read operations', () { |
+ tearDown(sharedTearDown); |
+ |
+ observeTest('read operations', () { |
expect(list.length, 6); |
expect(list[0], 1); |
expect(list.indexOf(4), 5); |
@@ -175,69 +185,69 @@ main() { |
var copy = new List<int>(); |
list.forEach((i) { copy.add(i); }); |
expect(copy, orderedEquals([1, 2, 3, 1, 3, 4])); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
// no change from read-only operators |
expectChanges(records, null); |
}); |
- test('add', () { |
+ observeTest('add', () { |
list.add(5); |
list.add(6); |
expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6])); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(records, [ |
_lengthChange, |
_change(6, addedCount: 2) |
]); |
}); |
- test('[]=', () { |
+ observeTest('[]=', () { |
list[1] = list.last; |
expect(list, orderedEquals([1, 4, 3, 1, 3, 4])); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(records, [ _change(1, addedCount: 1, removedCount: 1) ]); |
}); |
- test('removeLast', () { |
+ observeTest('removeLast', () { |
expect(list.removeLast(), 4); |
expect(list, orderedEquals([1, 2, 3, 1, 3])); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(records, [ |
_lengthChange, |
_change(5, removedCount: 1) |
]); |
}); |
- test('removeRange', () { |
+ observeTest('removeRange', () { |
list.removeRange(1, 4); |
expect(list, orderedEquals([1, 3, 4])); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(records, [ |
_lengthChange, |
_change(1, removedCount: 3), |
]); |
}); |
- test('sort', () { |
+ observeTest('sort', () { |
list.sort((x, y) => x - y); |
expect(list, orderedEquals([1, 1, 2, 3, 3, 4])); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(records, [ |
_change(1, addedCount: 5, removedCount: 5), |
]); |
}); |
- test('clear', () { |
+ observeTest('clear', () { |
list.clear(); |
expect(list, []); |
- deliverChangeRecords(); |
+ performMicrotaskCheckpoint(); |
expectChanges(records, [ |
_lengthChange, |
_change(0, removedCount: 6) |