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() => dirtyCheckZone().run(_runTests); | 10 main() => dirtyCheckZone().run(_runTests); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 [_removeKey('b', 2), _insertKey('b', 2)]); | 181 [_removeKey('b', 2), _insertKey('b', 2)]); |
182 }); | 182 }); |
183 }); | 183 }); |
184 }); | 184 }); |
185 | 185 |
186 test('toString', () { | 186 test('toString', () { |
187 var map = toObservable({'a': 1, 'b': 2}); | 187 var map = toObservable({'a': 1, 'b': 2}); |
188 expect(map.toString(), '{a: 1, b: 2}'); | 188 expect(map.toString(), '{a: 1, b: 2}'); |
189 }); | 189 }); |
190 | 190 |
| 191 group('observe keys/values', () { |
| 192 ObservableMap map; |
| 193 int keysChanged; |
| 194 int valuesChanged; |
| 195 |
| 196 setUp(() { |
| 197 map = toObservable({'a': 1, 'b': 2, 'c': 3}); |
| 198 keysChanged = 0; |
| 199 valuesChanged = 0; |
| 200 sub = map.changes.listen((records) { |
| 201 keysChanged += getPropertyChangeRecords(records, #keys).length; |
| 202 valuesChanged += getPropertyChangeRecords(records, #values).length; |
| 203 }); |
| 204 }); |
| 205 |
| 206 tearDown(sharedTearDown); |
| 207 |
| 208 test('add item changes keys/values', () { |
| 209 map['d'] = 4; |
| 210 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); |
| 211 return new Future(() { |
| 212 expect(keysChanged, 1); |
| 213 expect(valuesChanged, 1); |
| 214 }); |
| 215 }); |
| 216 |
| 217 test('putIfAbsent changes keys/values', () { |
| 218 map.putIfAbsent('d', () => 4); |
| 219 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); |
| 220 return new Future(() { |
| 221 expect(keysChanged, 1); |
| 222 expect(valuesChanged, 1); |
| 223 }); |
| 224 }); |
| 225 |
| 226 test('remove changes keys/values', () { |
| 227 map.remove('c'); |
| 228 map.remove('a'); |
| 229 expect(map, {'b': 2}); |
| 230 return new Future(() { |
| 231 expect(keysChanged, 2); |
| 232 expect(valuesChanged, 2); |
| 233 }); |
| 234 }); |
| 235 |
| 236 test('remove non-existent item does not change keys/values', () { |
| 237 map.remove('d'); |
| 238 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
| 239 return new Future(() { |
| 240 expect(keysChanged, 0); |
| 241 expect(valuesChanged, 0); |
| 242 }); |
| 243 }); |
| 244 |
| 245 test('set existing item does not change keys', () { |
| 246 map['c'] = 9000; |
| 247 expect(map, {'a': 1, 'b': 2, 'c': 9000}); |
| 248 return new Future(() { |
| 249 expect(keysChanged, 0); |
| 250 expect(valuesChanged, 1); |
| 251 }); |
| 252 }); |
| 253 |
| 254 test('clear changes keys/values', () { |
| 255 map.clear(); |
| 256 expect(map, {}); |
| 257 return new Future(() { |
| 258 expect(keysChanged, 1); |
| 259 expect(valuesChanged, 1); |
| 260 }); |
| 261 }); |
| 262 }); |
| 263 |
| 264 |
191 group('change records', () { | 265 group('change records', () { |
192 List<ChangeRecord> records; | 266 List<ChangeRecord> records; |
193 ObservableMap map; | 267 ObservableMap map; |
194 | 268 |
195 setUp(() { | 269 setUp(() { |
196 map = toObservable({'a': 1, 'b': 2}); | 270 map = toObservable({'a': 1, 'b': 2}); |
197 records = null; | 271 records = null; |
198 map.changes.first.then((r) { records = r; }); | 272 map.changes.first.then((r) { records = r; }); |
199 }); | 273 }); |
200 | 274 |
(...skipping 24 matching lines...) Expand all Loading... |
225 map.putIfAbsent('a', () => 42); | 299 map.putIfAbsent('a', () => 42); |
226 expect(map, {'a': 1, 'b': 2}); | 300 expect(map, {'a': 1, 'b': 2}); |
227 | 301 |
228 map.putIfAbsent('c', () => 3); | 302 map.putIfAbsent('c', () => 3); |
229 expect(map, {'a': 1, 'b': 2, 'c': 3}); | 303 expect(map, {'a': 1, 'b': 2, 'c': 3}); |
230 | 304 |
231 return new Future(() { | 305 return new Future(() { |
232 expectChanges(records, [ | 306 expectChanges(records, [ |
233 _lengthChange(map, 2, 3), | 307 _lengthChange(map, 2, 3), |
234 _insertKey('c', 3), | 308 _insertKey('c', 3), |
| 309 _propChange(map, #keys), |
| 310 _propChange(map, #values), |
235 ]); | 311 ]); |
236 }); | 312 }); |
237 }); | 313 }); |
238 | 314 |
239 test('[]=', () { | 315 test('[]=', () { |
240 map['a'] = 42; | 316 map['a'] = 42; |
241 expect(map, {'a': 42, 'b': 2}); | 317 expect(map, {'a': 42, 'b': 2}); |
242 | 318 |
243 map['c'] = 3; | 319 map['c'] = 3; |
244 expect(map, {'a': 42, 'b': 2, 'c': 3}); | 320 expect(map, {'a': 42, 'b': 2, 'c': 3}); |
245 | 321 |
246 return new Future(() { | 322 return new Future(() { |
247 expectChanges(records, [ | 323 expectChanges(records, [ |
248 _changeKey('a', 1, 42), | 324 _changeKey('a', 1, 42), |
| 325 _propChange(map, #values), |
249 _lengthChange(map, 2, 3), | 326 _lengthChange(map, 2, 3), |
250 _insertKey('c', 3) | 327 _insertKey('c', 3), |
| 328 _propChange(map, #keys), |
| 329 _propChange(map, #values), |
251 ]); | 330 ]); |
252 }); | 331 }); |
253 }); | 332 }); |
254 | 333 |
255 test('remove', () { | 334 test('remove', () { |
256 map.remove('b'); | 335 map.remove('b'); |
257 expect(map, {'a': 1}); | 336 expect(map, {'a': 1}); |
258 | 337 |
259 return new Future(() { | 338 return new Future(() { |
260 expectChanges(records, [ | 339 expectChanges(records, [ |
261 _removeKey('b', 2), | 340 _removeKey('b', 2), |
262 _lengthChange(map, 2, 1), | 341 _lengthChange(map, 2, 1), |
| 342 _propChange(map, #keys), |
| 343 _propChange(map, #values), |
263 ]); | 344 ]); |
264 }); | 345 }); |
265 }); | 346 }); |
266 | 347 |
267 test('clear', () { | 348 test('clear', () { |
268 map.clear(); | 349 map.clear(); |
269 expect(map, {}); | 350 expect(map, {}); |
270 | 351 |
271 return new Future(() { | 352 return new Future(() { |
272 expectChanges(records, [ | 353 expectChanges(records, [ |
273 _removeKey('a', 1), | 354 _removeKey('a', 1), |
274 _removeKey('b', 2), | 355 _removeKey('b', 2), |
275 _lengthChange(map, 2, 0), | 356 _lengthChange(map, 2, 0), |
| 357 _propChange(map, #keys), |
| 358 _propChange(map, #values), |
276 ]); | 359 ]); |
277 }); | 360 }); |
278 }); | 361 }); |
279 }); | 362 }); |
280 } | 363 } |
281 | 364 |
282 _lengthChange(map, int oldValue, int newValue) => | 365 _lengthChange(map, int oldValue, int newValue) => |
283 new PropertyChangeRecord(map, #length, oldValue, newValue); | 366 new PropertyChangeRecord(map, #length, oldValue, newValue); |
284 | 367 |
285 _changeKey(key, old, newValue) => new MapChangeRecord(key, old, newValue); | 368 _changeKey(key, old, newValue) => new MapChangeRecord(key, old, newValue); |
286 | 369 |
287 _insertKey(key, newValue) => new MapChangeRecord.insert(key, newValue); | 370 _insertKey(key, newValue) => new MapChangeRecord.insert(key, newValue); |
288 | 371 |
289 _removeKey(key, oldValue) => new MapChangeRecord.remove(key, oldValue); | 372 _removeKey(key, oldValue) => new MapChangeRecord.remove(key, oldValue); |
| 373 |
| 374 _propChange(map, prop) => new PropertyChangeRecord(map, prop, null, null); |
OLD | NEW |