OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'package:observe/observe.dart'; | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'observe_test_utils.dart'; | |
9 | 6 |
10 main() => dirtyCheckZone().run(_runTests); | 7 import 'package:observable/observable.dart'; |
| 8 import 'package:test/test.dart'; |
| 9 |
| 10 import 'observable_test_utils.dart'; |
| 11 |
| 12 main() => _runTests(); |
11 | 13 |
12 _runTests() { | 14 _runTests() { |
13 // TODO(jmesserly): need all standard Map API tests. | 15 // TODO(jmesserly): need all standard Map API tests. |
14 | 16 |
15 StreamSubscription sub; | 17 StreamSubscription sub; |
16 | 18 |
17 sharedTearDown() { | 19 sharedTearDown() { |
18 if (sub != null) { | 20 if (sub != null) { |
19 sub.cancel(); | 21 sub.cancel(); |
20 sub = null; | 22 sub = null; |
(...skipping 30 matching lines...) Expand all Loading... |
51 }); | 53 }); |
52 }); | 54 }); |
53 | 55 |
54 test('remove changes length', () { | 56 test('remove changes length', () { |
55 map.remove('c'); | 57 map.remove('c'); |
56 map.remove('a'); | 58 map.remove('a'); |
57 expect(map, {'b': 2}); | 59 expect(map, {'b': 2}); |
58 return new Future(() { | 60 return new Future(() { |
59 expectChanges(changes, [ | 61 expectChanges(changes, [ |
60 _lengthChange(map, 3, 2), | 62 _lengthChange(map, 3, 2), |
61 _lengthChange(map, 2, 1) | 63 _lengthChange(map, 2, 1), |
62 ]); | 64 ]); |
63 }); | 65 }); |
64 }); | 66 }); |
65 | 67 |
66 test('remove non-existent item does not change length', () { | 68 test('remove non-existent item does not change length', () { |
67 map.remove('d'); | 69 map.remove('d'); |
68 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 70 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
69 return new Future(() { | 71 return new Future(() { |
70 expectChanges(changes, null); | 72 expectChanges(changes, null); |
71 }); | 73 }); |
(...skipping 10 matching lines...) Expand all Loading... |
82 test('clear changes length', () { | 84 test('clear changes length', () { |
83 map.clear(); | 85 map.clear(); |
84 expect(map, {}); | 86 expect(map, {}); |
85 return new Future(() { | 87 return new Future(() { |
86 expectChanges(changes, [_lengthChange(map, 3, 0)]); | 88 expectChanges(changes, [_lengthChange(map, 3, 0)]); |
87 }); | 89 }); |
88 }); | 90 }); |
89 }); | 91 }); |
90 | 92 |
91 group('observe item', () { | 93 group('observe item', () { |
92 | |
93 ObservableMap map; | 94 ObservableMap map; |
94 List<ChangeRecord> changes; | 95 List<ChangeRecord> changes; |
95 | 96 |
96 setUp(() { | 97 setUp(() { |
97 map = toObservable({'a': 1, 'b': 2, 'c': 3}); | 98 map = toObservable({'a': 1, 'b': 2, 'c': 3}); |
98 changes = null; | 99 changes = null; |
99 sub = map.changes.listen((records) { | 100 sub = map.changes.listen((records) { |
100 changes = records.where((r) => r is MapChangeRecord && r.key == 'b') | 101 changes = |
101 .toList(); | 102 records.where((r) => r is MapChangeRecord && r.key == 'b').toList(); |
102 }); | 103 }); |
103 }); | 104 }); |
104 | 105 |
105 tearDown(sharedTearDown); | 106 tearDown(sharedTearDown); |
106 | 107 |
107 test('putIfAbsent new item does not change existing item', () { | 108 test('putIfAbsent new item does not change existing item', () { |
108 map.putIfAbsent('d', () => 4); | 109 map.putIfAbsent('d', () => 4); |
109 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); | 110 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); |
110 return new Future(() { | 111 return new Future(() { |
111 expectChanges(changes, []); | 112 expectChanges(changes, []); |
(...skipping 21 matching lines...) Expand all Loading... |
133 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 134 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
134 return new Future(() { | 135 return new Future(() { |
135 expectChanges(changes, null); | 136 expectChanges(changes, null); |
136 }); | 137 }); |
137 }); | 138 }); |
138 | 139 |
139 test('change a different item', () { | 140 test('change a different item', () { |
140 map['c'] = 9000; | 141 map['c'] = 9000; |
141 expect(map, {'a': 1, 'b': 2, 'c': 9000}); | 142 expect(map, {'a': 1, 'b': 2, 'c': 9000}); |
142 return new Future(() { | 143 return new Future(() { |
143 expectChanges(changes, []); | 144 expectChanges(changes, []); |
144 }); | 145 }); |
145 }); | 146 }); |
146 | 147 |
147 test('change the item', () { | 148 test('change the item', () { |
148 map['b'] = 9001; | 149 map['b'] = 9001; |
149 map['b'] = 42; | 150 map['b'] = 42; |
150 expect(map, {'a': 1, 'b': 42, 'c': 3}); | 151 expect(map, {'a': 1, 'b': 42, 'c': 3}); |
151 return new Future(() { | 152 return new Future(() { |
152 expectChanges(changes, [ | 153 expectChanges(changes, [ |
153 _changeKey('b', 2, 9001), | 154 _changeKey('b', 2, 9001), |
154 _changeKey('b', 9001, 42) | 155 _changeKey('b', 9001, 42), |
155 ]); | 156 ]); |
156 }); | 157 }); |
157 }); | 158 }); |
158 | 159 |
159 test('remove other items', () { | 160 test('remove other items', () { |
160 map.remove('a'); | 161 map.remove('a'); |
161 expect(map, {'b': 2, 'c': 3}); | 162 expect(map, {'b': 2, 'c': 3}); |
162 return new Future(() { | 163 return new Future(() { |
163 expectChanges(changes, []); | 164 expectChanges(changes, []); |
164 }); | 165 }); |
165 }); | 166 }); |
166 | 167 |
167 test('remove the item', () { | 168 test('remove the item', () { |
168 map.remove('b'); | 169 map.remove('b'); |
169 expect(map, {'a': 1, 'c': 3}); | 170 expect(map, {'a': 1, 'c': 3}); |
170 return new Future(() { | 171 return new Future(() { |
171 expectChanges(changes, [_removeKey('b', 2)]); | 172 expectChanges(changes, [_removeKey('b', 2)]); |
172 }); | 173 }); |
173 }); | 174 }); |
174 | 175 |
175 test('remove and add back', () { | 176 test('remove and add back', () { |
176 map.remove('b'); | 177 map.remove('b'); |
177 map['b'] = 2; | 178 map['b'] = 2; |
178 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 179 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
179 return new Future(() { | 180 return new Future(() { |
180 expectChanges(changes, | 181 expectChanges(changes, [ |
181 [_removeKey('b', 2), _insertKey('b', 2)]); | 182 _removeKey('b', 2), |
| 183 _insertKey('b', 2), |
| 184 ]); |
182 }); | 185 }); |
183 }); | 186 }); |
184 }); | 187 }); |
185 | 188 |
186 test('toString', () { | 189 test('toString', () { |
187 var map = toObservable({'a': 1, 'b': 2}); | 190 var map = toObservable({'a': 1, 'b': 2}); |
188 expect(map.toString(), '{a: 1, b: 2}'); | 191 expect(map.toString(), '{a: 1, b: 2}'); |
189 }); | 192 }); |
190 | 193 |
191 group('observe keys/values', () { | 194 group('observe keys/values', () { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 test('clear changes keys/values', () { | 257 test('clear changes keys/values', () { |
255 map.clear(); | 258 map.clear(); |
256 expect(map, {}); | 259 expect(map, {}); |
257 return new Future(() { | 260 return new Future(() { |
258 expect(keysChanged, 1); | 261 expect(keysChanged, 1); |
259 expect(valuesChanged, 1); | 262 expect(valuesChanged, 1); |
260 }); | 263 }); |
261 }); | 264 }); |
262 }); | 265 }); |
263 | 266 |
264 | |
265 group('change records', () { | 267 group('change records', () { |
266 List<ChangeRecord> records; | 268 List<ChangeRecord> records; |
267 ObservableMap map; | 269 ObservableMap map; |
268 | 270 |
269 setUp(() { | 271 setUp(() { |
270 map = toObservable({'a': 1, 'b': 2}); | 272 map = toObservable({'a': 1, 'b': 2}); |
271 records = null; | 273 records = null; |
272 map.changes.first.then((r) { records = r; }); | 274 map.changes.first.then((r) => records = r); |
273 }); | 275 }); |
274 | 276 |
275 tearDown(sharedTearDown); | 277 tearDown(sharedTearDown); |
276 | 278 |
277 test('read operations', () { | 279 test('read operations', () { |
278 expect(map.length, 2); | 280 expect(map.length, 2); |
279 expect(map.isEmpty, false); | 281 expect(map.isEmpty, false); |
280 expect(map['a'], 1); | 282 expect(map['a'], 1); |
281 expect(map.containsKey(2), false); | 283 expect(map.containsKey(2), false); |
282 expect(map.containsValue(2), true); | 284 expect(map.containsValue(2), true); |
283 expect(map.containsKey('b'), true); | 285 expect(map.containsKey('b'), true); |
284 expect(map.keys.toList(), ['a', 'b']); | 286 expect(map.keys.toList(), ['a', 'b']); |
285 expect(map.values.toList(), [1, 2]); | 287 expect(map.values.toList(), [1, 2]); |
286 var copy = {}; | 288 var copy = {}; |
287 map.forEach((k, v) { copy[k] = v; }); | 289 map.forEach((k, v) => copy[k] = v); |
288 expect(copy, {'a': 1, 'b': 2}); | 290 expect(copy, {'a': 1, 'b': 2}); |
289 return new Future(() { | 291 return new Future(() { |
290 // no change from read-only operators | 292 // no change from read-only operators |
291 expect(records, null); | 293 expect(records, null); |
292 | 294 |
293 // Make a change so the subscription gets unregistered. | 295 // Make a change so the subscription gets unregistered. |
294 map.clear(); | 296 map.clear(); |
295 }); | 297 }); |
296 }); | 298 }); |
297 | 299 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 _lengthChange(map, int oldValue, int newValue) => | 367 _lengthChange(map, int oldValue, int newValue) => |
366 new PropertyChangeRecord(map, #length, oldValue, newValue); | 368 new PropertyChangeRecord(map, #length, oldValue, newValue); |
367 | 369 |
368 _changeKey(key, old, newValue) => new MapChangeRecord(key, old, newValue); | 370 _changeKey(key, old, newValue) => new MapChangeRecord(key, old, newValue); |
369 | 371 |
370 _insertKey(key, newValue) => new MapChangeRecord.insert(key, newValue); | 372 _insertKey(key, newValue) => new MapChangeRecord.insert(key, newValue); |
371 | 373 |
372 _removeKey(key, oldValue) => new MapChangeRecord.remove(key, oldValue); | 374 _removeKey(key, oldValue) => new MapChangeRecord.remove(key, oldValue); |
373 | 375 |
374 _propChange(map, prop) => new PropertyChangeRecord(map, prop, null, null); | 376 _propChange(map, prop) => new PropertyChangeRecord(map, prop, null, null); |
OLD | NEW |