OLD | NEW |
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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'package:observe/observe.dart'; | 6 import 'package:observe/observe.dart'; |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'observe_test_utils.dart'; | 8 import 'observe_test_utils.dart'; |
9 | 9 |
10 main() { | 10 main() => dirtyCheckZone().run(_runTests); |
| 11 |
| 12 _runTests() { |
11 // TODO(jmesserly): need all standard Map API tests. | 13 // TODO(jmesserly): need all standard Map API tests. |
12 | 14 |
13 StreamSubscription sub; | 15 StreamSubscription sub; |
14 | 16 |
15 sharedTearDown() { | 17 sharedTearDown() { |
16 if (sub != null) { | 18 if (sub != null) { |
17 sub.cancel(); | 19 sub.cancel(); |
18 sub = null; | 20 sub = null; |
19 } | 21 } |
20 } | 22 } |
21 | 23 |
22 group('observe length', () { | 24 group('observe length', () { |
23 ObservableMap map; | 25 ObservableMap map; |
24 List<ChangeRecord> changes; | 26 List<ChangeRecord> changes; |
25 | 27 |
26 setUp(() { | 28 setUp(() { |
27 map = toObservable({'a': 1, 'b': 2, 'c': 3}); | 29 map = toObservable({'a': 1, 'b': 2, 'c': 3}); |
28 changes = null; | 30 changes = null; |
29 sub = map.changes.listen((records) { | 31 sub = map.changes.listen((records) { |
30 changes = getPropertyChangeRecords(records, #length); | 32 changes = getPropertyChangeRecords(records, #length); |
31 }); | 33 }); |
32 }); | 34 }); |
33 | 35 |
34 tearDown(sharedTearDown); | 36 tearDown(sharedTearDown); |
35 | 37 |
36 observeTest('add item changes length', () { | 38 test('add item changes length', () { |
37 map['d'] = 4; | 39 map['d'] = 4; |
38 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); | 40 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); |
39 performMicrotaskCheckpoint(); | 41 return new Future(() { |
40 expectChanges(changes, [_lengthChange(map, 3, 4)]); | 42 expectChanges(changes, [_lengthChange(map, 3, 4)]); |
| 43 }); |
41 }); | 44 }); |
42 | 45 |
43 observeTest('putIfAbsent changes length', () { | 46 test('putIfAbsent changes length', () { |
44 map.putIfAbsent('d', () => 4); | 47 map.putIfAbsent('d', () => 4); |
45 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); | 48 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); |
46 performMicrotaskCheckpoint(); | 49 return new Future(() { |
47 expectChanges(changes, [_lengthChange(map, 3, 4)]); | 50 expectChanges(changes, [_lengthChange(map, 3, 4)]); |
| 51 }); |
48 }); | 52 }); |
49 | 53 |
50 observeTest('remove changes length', () { | 54 test('remove changes length', () { |
51 map.remove('c'); | 55 map.remove('c'); |
52 map.remove('a'); | 56 map.remove('a'); |
53 expect(map, {'b': 2}); | 57 expect(map, {'b': 2}); |
54 performMicrotaskCheckpoint(); | 58 return new Future(() { |
55 expectChanges(changes, [ | 59 expectChanges(changes, [ |
56 _lengthChange(map, 3, 2), | 60 _lengthChange(map, 3, 2), |
57 _lengthChange(map, 2, 1) | 61 _lengthChange(map, 2, 1) |
58 ]); | 62 ]); |
| 63 }); |
59 }); | 64 }); |
60 | 65 |
61 observeTest('remove non-existent item does not change length', () { | 66 test('remove non-existent item does not change length', () { |
62 map.remove('d'); | 67 map.remove('d'); |
63 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 68 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
64 performMicrotaskCheckpoint(); | 69 return new Future(() { |
65 expectChanges(changes, null); | 70 expectChanges(changes, null); |
| 71 }); |
66 }); | 72 }); |
67 | 73 |
68 observeTest('set existing item does not change length', () { | 74 test('set existing item does not change length', () { |
69 map['c'] = 9000; | 75 map['c'] = 9000; |
70 expect(map, {'a': 1, 'b': 2, 'c': 9000}); | 76 expect(map, {'a': 1, 'b': 2, 'c': 9000}); |
71 performMicrotaskCheckpoint(); | 77 return new Future(() { |
72 expectChanges(changes, []); | 78 expectChanges(changes, []); |
| 79 }); |
73 }); | 80 }); |
74 | 81 |
75 observeTest('clear changes length', () { | 82 test('clear changes length', () { |
76 map.clear(); | 83 map.clear(); |
77 expect(map, {}); | 84 expect(map, {}); |
78 performMicrotaskCheckpoint(); | 85 return new Future(() { |
79 expectChanges(changes, [_lengthChange(map, 3, 0)]); | 86 expectChanges(changes, [_lengthChange(map, 3, 0)]); |
| 87 }); |
80 }); | 88 }); |
81 }); | 89 }); |
82 | 90 |
83 group('observe item', () { | 91 group('observe item', () { |
84 | 92 |
85 ObservableMap map; | 93 ObservableMap map; |
86 List<ChangeRecord> changes; | 94 List<ChangeRecord> changes; |
87 | 95 |
88 setUp(() { | 96 setUp(() { |
89 map = toObservable({'a': 1, 'b': 2, 'c': 3}); | 97 map = toObservable({'a': 1, 'b': 2, 'c': 3}); |
90 changes = null; | 98 changes = null; |
91 sub = map.changes.listen((records) { | 99 sub = map.changes.listen((records) { |
92 changes = records.where((r) => r is MapChangeRecord && r.key == 'b') | 100 changes = records.where((r) => r is MapChangeRecord && r.key == 'b') |
93 .toList(); | 101 .toList(); |
94 }); | 102 }); |
95 }); | 103 }); |
96 | 104 |
97 tearDown(sharedTearDown); | 105 tearDown(sharedTearDown); |
98 | 106 |
99 observeTest('putIfAbsent new item does not change existing item', () { | 107 test('putIfAbsent new item does not change existing item', () { |
100 map.putIfAbsent('d', () => 4); | 108 map.putIfAbsent('d', () => 4); |
101 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); | 109 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); |
102 performMicrotaskCheckpoint(); | 110 return new Future(() { |
103 expectChanges(changes, []); | 111 expectChanges(changes, []); |
| 112 }); |
104 }); | 113 }); |
105 | 114 |
106 observeTest('set item to null', () { | 115 test('set item to null', () { |
107 map['b'] = null; | 116 map['b'] = null; |
108 expect(map, {'a': 1, 'b': null, 'c': 3}); | 117 expect(map, {'a': 1, 'b': null, 'c': 3}); |
109 performMicrotaskCheckpoint(); | 118 return new Future(() { |
110 expectChanges(changes, [_changeKey('b', 2, null)]); | 119 expectChanges(changes, [_changeKey('b', 2, null)]); |
| 120 }); |
111 }); | 121 }); |
112 | 122 |
113 observeTest('set item to value', () { | 123 test('set item to value', () { |
114 map['b'] = 777; | 124 map['b'] = 777; |
115 expect(map, {'a': 1, 'b': 777, 'c': 3}); | 125 expect(map, {'a': 1, 'b': 777, 'c': 3}); |
116 performMicrotaskCheckpoint(); | 126 return new Future(() { |
117 expectChanges(changes, [_changeKey('b', 2, 777)]); | 127 expectChanges(changes, [_changeKey('b', 2, 777)]); |
| 128 }); |
118 }); | 129 }); |
119 | 130 |
120 observeTest('putIfAbsent does not change if already there', () { | 131 test('putIfAbsent does not change if already there', () { |
121 map.putIfAbsent('b', () => 1234); | 132 map.putIfAbsent('b', () => 1234); |
122 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 133 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
123 performMicrotaskCheckpoint(); | 134 return new Future(() { |
124 expectChanges(changes, null); | 135 expectChanges(changes, null); |
| 136 }); |
125 }); | 137 }); |
126 | 138 |
127 observeTest('change a different item', () { | 139 test('change a different item', () { |
128 map['c'] = 9000; | 140 map['c'] = 9000; |
129 expect(map, {'a': 1, 'b': 2, 'c': 9000}); | 141 expect(map, {'a': 1, 'b': 2, 'c': 9000}); |
130 performMicrotaskCheckpoint(); | 142 return new Future(() { |
131 expectChanges(changes, []); | 143 expectChanges(changes, []); |
| 144 }); |
132 }); | 145 }); |
133 | 146 |
134 observeTest('change the item', () { | 147 test('change the item', () { |
135 map['b'] = 9001; | 148 map['b'] = 9001; |
136 map['b'] = 42; | 149 map['b'] = 42; |
137 expect(map, {'a': 1, 'b': 42, 'c': 3}); | 150 expect(map, {'a': 1, 'b': 42, 'c': 3}); |
138 performMicrotaskCheckpoint(); | 151 return new Future(() { |
139 expectChanges(changes, [ | 152 expectChanges(changes, [ |
140 _changeKey('b', 2, 9001), | 153 _changeKey('b', 2, 9001), |
141 _changeKey('b', 9001, 42) | 154 _changeKey('b', 9001, 42) |
142 ]); | 155 ]); |
| 156 }); |
143 }); | 157 }); |
144 | 158 |
145 observeTest('remove other items', () { | 159 test('remove other items', () { |
146 map.remove('a'); | 160 map.remove('a'); |
147 expect(map, {'b': 2, 'c': 3}); | 161 expect(map, {'b': 2, 'c': 3}); |
148 performMicrotaskCheckpoint(); | 162 return new Future(() { |
149 expectChanges(changes, []); | 163 expectChanges(changes, []); |
| 164 }); |
150 }); | 165 }); |
151 | 166 |
152 observeTest('remove the item', () { | 167 test('remove the item', () { |
153 map.remove('b'); | 168 map.remove('b'); |
154 expect(map, {'a': 1, 'c': 3}); | 169 expect(map, {'a': 1, 'c': 3}); |
155 performMicrotaskCheckpoint(); | 170 return new Future(() { |
156 expectChanges(changes, [_removeKey('b', 2)]); | 171 expectChanges(changes, [_removeKey('b', 2)]); |
| 172 }); |
157 }); | 173 }); |
158 | 174 |
159 observeTest('remove and add back', () { | 175 test('remove and add back', () { |
160 map.remove('b'); | 176 map.remove('b'); |
161 map['b'] = 2; | 177 map['b'] = 2; |
162 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 178 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
163 performMicrotaskCheckpoint(); | 179 return new Future(() { |
164 expectChanges(changes, | 180 expectChanges(changes, |
165 [_removeKey('b', 2), _insertKey('b', 2)]); | 181 [_removeKey('b', 2), _insertKey('b', 2)]); |
| 182 }); |
166 }); | 183 }); |
167 }); | 184 }); |
168 | 185 |
169 observeTest('toString', () { | 186 test('toString', () { |
170 var map = toObservable({'a': 1, 'b': 2}); | 187 var map = toObservable({'a': 1, 'b': 2}); |
171 expect(map.toString(), '{a: 1, b: 2}'); | 188 expect(map.toString(), '{a: 1, b: 2}'); |
172 }); | 189 }); |
173 | 190 |
174 group('change records', () { | 191 group('change records', () { |
175 List<ChangeRecord> records; | 192 List<ChangeRecord> records; |
176 ObservableMap map; | 193 ObservableMap map; |
177 | 194 |
178 setUp(() { | 195 setUp(() { |
179 map = toObservable({'a': 1, 'b': 2}); | 196 map = toObservable({'a': 1, 'b': 2}); |
180 records = null; | 197 records = null; |
181 map.changes.first.then((r) { records = r; }); | 198 map.changes.first.then((r) { records = r; }); |
182 }); | 199 }); |
183 | 200 |
184 tearDown(sharedTearDown); | 201 tearDown(sharedTearDown); |
185 | 202 |
186 observeTest('read operations', () { | 203 test('read operations', () { |
187 expect(map.length, 2); | 204 expect(map.length, 2); |
188 expect(map.isEmpty, false); | 205 expect(map.isEmpty, false); |
189 expect(map['a'], 1); | 206 expect(map['a'], 1); |
190 expect(map.containsKey(2), false); | 207 expect(map.containsKey(2), false); |
191 expect(map.containsValue(2), true); | 208 expect(map.containsValue(2), true); |
192 expect(map.containsKey('b'), true); | 209 expect(map.containsKey('b'), true); |
193 expect(map.keys.toList(), ['a', 'b']); | 210 expect(map.keys.toList(), ['a', 'b']); |
194 expect(map.values.toList(), [1, 2]); | 211 expect(map.values.toList(), [1, 2]); |
195 var copy = {}; | 212 var copy = {}; |
196 map.forEach((k, v) { copy[k] = v; }); | 213 map.forEach((k, v) { copy[k] = v; }); |
197 expect(copy, {'a': 1, 'b': 2}); | 214 expect(copy, {'a': 1, 'b': 2}); |
198 performMicrotaskCheckpoint(); | 215 return new Future(() { |
| 216 // no change from read-only operators |
| 217 expect(records, null); |
199 | 218 |
200 // no change from read-only operators | 219 // Make a change so the subscription gets unregistered. |
201 expect(records, null); | 220 map.clear(); |
202 | 221 }); |
203 // Make a change so the subscription gets unregistered. | |
204 map.clear(); | |
205 }); | 222 }); |
206 | 223 |
207 observeTest('putIfAbsent', () { | 224 test('putIfAbsent', () { |
208 map.putIfAbsent('a', () => 42); | 225 map.putIfAbsent('a', () => 42); |
209 expect(map, {'a': 1, 'b': 2}); | 226 expect(map, {'a': 1, 'b': 2}); |
210 | 227 |
211 map.putIfAbsent('c', () => 3); | 228 map.putIfAbsent('c', () => 3); |
212 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 229 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
213 | 230 |
214 performMicrotaskCheckpoint(); | 231 return new Future(() { |
215 expectChanges(records, [ | 232 expectChanges(records, [ |
216 _lengthChange(map, 2, 3), | 233 _lengthChange(map, 2, 3), |
217 _insertKey('c', 3), | 234 _insertKey('c', 3), |
218 ]); | 235 ]); |
| 236 }); |
219 }); | 237 }); |
220 | 238 |
221 observeTest('[]=', () { | 239 test('[]=', () { |
222 map['a'] = 42; | 240 map['a'] = 42; |
223 expect(map, {'a': 42, 'b': 2}); | 241 expect(map, {'a': 42, 'b': 2}); |
224 | 242 |
225 map['c'] = 3; | 243 map['c'] = 3; |
226 expect(map, {'a': 42, 'b': 2, 'c': 3}); | 244 expect(map, {'a': 42, 'b': 2, 'c': 3}); |
227 | 245 |
228 performMicrotaskCheckpoint(); | 246 return new Future(() { |
229 expectChanges(records, [ | 247 expectChanges(records, [ |
230 _changeKey('a', 1, 42), | 248 _changeKey('a', 1, 42), |
231 _lengthChange(map, 2, 3), | 249 _lengthChange(map, 2, 3), |
232 _insertKey('c', 3) | 250 _insertKey('c', 3) |
233 ]); | 251 ]); |
| 252 }); |
234 }); | 253 }); |
235 | 254 |
236 observeTest('remove', () { | 255 test('remove', () { |
237 map.remove('b'); | 256 map.remove('b'); |
238 expect(map, {'a': 1}); | 257 expect(map, {'a': 1}); |
239 | 258 |
240 performMicrotaskCheckpoint(); | 259 return new Future(() { |
241 expectChanges(records, [ | 260 expectChanges(records, [ |
242 _removeKey('b', 2), | 261 _removeKey('b', 2), |
243 _lengthChange(map, 2, 1), | 262 _lengthChange(map, 2, 1), |
244 ]); | 263 ]); |
| 264 }); |
245 }); | 265 }); |
246 | 266 |
247 observeTest('clear', () { | 267 test('clear', () { |
248 map.clear(); | 268 map.clear(); |
249 expect(map, {}); | 269 expect(map, {}); |
250 | 270 |
251 performMicrotaskCheckpoint(); | 271 return new Future(() { |
252 expectChanges(records, [ | 272 expectChanges(records, [ |
253 _removeKey('a', 1), | 273 _removeKey('a', 1), |
254 _removeKey('b', 2), | 274 _removeKey('b', 2), |
255 _lengthChange(map, 2, 0), | 275 _lengthChange(map, 2, 0), |
256 ]); | 276 ]); |
| 277 }); |
257 }); | 278 }); |
258 }); | 279 }); |
259 } | 280 } |
260 | 281 |
261 _lengthChange(map, int oldValue, int newValue) => | 282 _lengthChange(map, int oldValue, int newValue) => |
262 new PropertyChangeRecord(map, #length, oldValue, newValue); | 283 new PropertyChangeRecord(map, #length, oldValue, newValue); |
263 | 284 |
264 _changeKey(key, old, newValue) => new MapChangeRecord(key, old, newValue); | 285 _changeKey(key, old, newValue) => new MapChangeRecord(key, old, newValue); |
265 | 286 |
266 _insertKey(key, newValue) => new MapChangeRecord.insert(key, newValue); | 287 _insertKey(key, newValue) => new MapChangeRecord.insert(key, newValue); |
267 | 288 |
268 _removeKey(key, oldValue) => new MapChangeRecord.remove(key, oldValue); | 289 _removeKey(key, oldValue) => new MapChangeRecord.remove(key, oldValue); |
OLD | NEW |