| 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() { |
| 11 // TODO(jmesserly): need all standard List API tests. | 11 // TODO(jmesserly): need all standard List API tests. |
| 12 | 12 |
| 13 StreamSubscription sub; | 13 StreamSubscription sub; |
| 14 | 14 |
| 15 sharedTearDown() { sub.cancel(); } | 15 sharedTearDown() { sub.cancel(); } |
| 16 | 16 |
| 17 group('observe length', () { | 17 group('observe length', () { |
| 18 | 18 |
| 19 ObservableList list; | 19 ObservableList list; |
| 20 List<ChangeRecord> changes; | 20 List<ChangeRecord> changes; |
| 21 | 21 |
| 22 setUp(() { | 22 setUp(() { |
| 23 list = toObservable([1, 2, 3]); | 23 list = toObservable([1, 2, 3]); |
| 24 changes = null; | 24 changes = null; |
| 25 sub = list.changes.listen((records) { | 25 sub = list.changes.listen((records) { |
| 26 changes = records.where((r) => r.changes(#length)).toList(); | 26 changes = getPropertyChangeRecords(records, #length); |
| 27 }); | 27 }); |
| 28 }); | 28 }); |
| 29 | 29 |
| 30 tearDown(sharedTearDown); | 30 tearDown(sharedTearDown); |
| 31 | 31 |
| 32 observeTest('add changes length', () { | 32 observeTest('add changes length', () { |
| 33 list.add(4); | 33 list.add(4); |
| 34 expect(list, [1, 2, 3, 4]); | 34 expect(list, [1, 2, 3, 4]); |
| 35 performMicrotaskCheckpoint(); | 35 performMicrotaskCheckpoint(); |
| 36 expectChanges(changes, [_lengthChange]); | 36 expectChanges(changes, [_lengthChange(list, 3, 4)]); |
| 37 }); | 37 }); |
| 38 | 38 |
| 39 observeTest('removeObject', () { | 39 observeTest('removeObject', () { |
| 40 list.remove(2); | 40 list.remove(2); |
| 41 expect(list, orderedEquals([1, 3])); | 41 expect(list, orderedEquals([1, 3])); |
| 42 | 42 |
| 43 performMicrotaskCheckpoint(); | 43 performMicrotaskCheckpoint(); |
| 44 expectChanges(changes, [_lengthChange]); | 44 expectChanges(changes, [_lengthChange(list, 3, 2)]); |
| 45 }); | 45 }); |
| 46 | 46 |
| 47 observeTest('removeRange changes length', () { | 47 observeTest('removeRange changes length', () { |
| 48 list.add(4); | 48 list.add(4); |
| 49 list.removeRange(1, 3); | 49 list.removeRange(1, 3); |
| 50 expect(list, [1, 4]); | 50 expect(list, [1, 4]); |
| 51 performMicrotaskCheckpoint(); | 51 performMicrotaskCheckpoint(); |
| 52 expectChanges(changes, [_lengthChange]); | 52 expectChanges(changes, [_lengthChange(list, 3, 2)]); |
| 53 }); | 53 }); |
| 54 | 54 |
| 55 observeTest('length= changes length', () { | 55 observeTest('length= changes length', () { |
| 56 list.length = 5; | 56 list.length = 5; |
| 57 expect(list, [1, 2, 3, null, null]); | 57 expect(list, [1, 2, 3, null, null]); |
| 58 performMicrotaskCheckpoint(); | 58 performMicrotaskCheckpoint(); |
| 59 expectChanges(changes, [_lengthChange]); | 59 expectChanges(changes, [_lengthChange(list, 3, 5)]); |
| 60 }); | 60 }); |
| 61 | 61 |
| 62 observeTest('[]= does not change length', () { | 62 observeTest('[]= does not change length', () { |
| 63 list[2] = 9000; | 63 list[2] = 9000; |
| 64 expect(list, [1, 2, 9000]); | 64 expect(list, [1, 2, 9000]); |
| 65 performMicrotaskCheckpoint(); | 65 performMicrotaskCheckpoint(); |
| 66 expectChanges(changes, []); | 66 expectChanges(changes, []); |
| 67 }); | 67 }); |
| 68 | 68 |
| 69 observeTest('clear changes length', () { | 69 observeTest('clear changes length', () { |
| 70 list.clear(); | 70 list.clear(); |
| 71 expect(list, []); | 71 expect(list, []); |
| 72 performMicrotaskCheckpoint(); | 72 performMicrotaskCheckpoint(); |
| 73 expectChanges(changes, [_lengthChange]); | 73 expectChanges(changes, [_lengthChange(list, 3, 0)]); |
| 74 }); | 74 }); |
| 75 }); | 75 }); |
| 76 | 76 |
| 77 group('observe index', () { | 77 group('observe index', () { |
| 78 ObservableList list; | 78 ObservableList list; |
| 79 List<ChangeRecord> changes; | 79 List<ChangeRecord> changes; |
| 80 | 80 |
| 81 setUp(() { | 81 setUp(() { |
| 82 list = toObservable([1, 2, 3]); | 82 list = toObservable([1, 2, 3]); |
| 83 changes = null; | 83 changes = null; |
| 84 sub = list.changes.listen((records) { | 84 sub = list.changes.listen((records) { |
| 85 changes = records.where((r) => r.changes(1)).toList(); | 85 changes = getListChangeRecords(records, 1); |
| 86 }); | 86 }); |
| 87 }); | 87 }); |
| 88 | 88 |
| 89 tearDown(sharedTearDown); | 89 tearDown(sharedTearDown); |
| 90 | 90 |
| 91 observeTest('add does not change existing items', () { | 91 observeTest('add does not change existing items', () { |
| 92 list.add(4); | 92 list.add(4); |
| 93 expect(list, [1, 2, 3, 4]); | 93 expect(list, [1, 2, 3, 4]); |
| 94 performMicrotaskCheckpoint(); | 94 performMicrotaskCheckpoint(); |
| 95 expectChanges(changes, []); | 95 expectChanges(changes, []); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 expectChanges(records, null); | 189 expectChanges(records, null); |
| 190 }); | 190 }); |
| 191 | 191 |
| 192 observeTest('add', () { | 192 observeTest('add', () { |
| 193 list.add(5); | 193 list.add(5); |
| 194 list.add(6); | 194 list.add(6); |
| 195 expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6])); | 195 expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6])); |
| 196 | 196 |
| 197 performMicrotaskCheckpoint(); | 197 performMicrotaskCheckpoint(); |
| 198 expectChanges(records, [ | 198 expectChanges(records, [ |
| 199 _lengthChange, | 199 _lengthChange(list, 6, 8), |
| 200 _change(6, addedCount: 2) | 200 _change(6, addedCount: 2) |
| 201 ]); | 201 ]); |
| 202 }); | 202 }); |
| 203 | 203 |
| 204 observeTest('[]=', () { | 204 observeTest('[]=', () { |
| 205 list[1] = list.last; | 205 list[1] = list.last; |
| 206 expect(list, orderedEquals([1, 4, 3, 1, 3, 4])); | 206 expect(list, orderedEquals([1, 4, 3, 1, 3, 4])); |
| 207 | 207 |
| 208 performMicrotaskCheckpoint(); | 208 performMicrotaskCheckpoint(); |
| 209 expectChanges(records, [ _change(1, addedCount: 1, removedCount: 1) ]); | 209 expectChanges(records, [ _change(1, addedCount: 1, removedCount: 1) ]); |
| 210 }); | 210 }); |
| 211 | 211 |
| 212 observeTest('removeLast', () { | 212 observeTest('removeLast', () { |
| 213 expect(list.removeLast(), 4); | 213 expect(list.removeLast(), 4); |
| 214 expect(list, orderedEquals([1, 2, 3, 1, 3])); | 214 expect(list, orderedEquals([1, 2, 3, 1, 3])); |
| 215 | 215 |
| 216 performMicrotaskCheckpoint(); | 216 performMicrotaskCheckpoint(); |
| 217 expectChanges(records, [ | 217 expectChanges(records, [ |
| 218 _lengthChange, | 218 _lengthChange(list, 6, 5), |
| 219 _change(5, removedCount: 1) | 219 _change(5, removedCount: 1) |
| 220 ]); | 220 ]); |
| 221 }); | 221 }); |
| 222 | 222 |
| 223 observeTest('removeRange', () { | 223 observeTest('removeRange', () { |
| 224 list.removeRange(1, 4); | 224 list.removeRange(1, 4); |
| 225 expect(list, orderedEquals([1, 3, 4])); | 225 expect(list, orderedEquals([1, 3, 4])); |
| 226 | 226 |
| 227 performMicrotaskCheckpoint(); | 227 performMicrotaskCheckpoint(); |
| 228 expectChanges(records, [ | 228 expectChanges(records, [ |
| 229 _lengthChange, | 229 _lengthChange(list, 6, 3), |
| 230 _change(1, removedCount: 3), | 230 _change(1, removedCount: 3), |
| 231 ]); | 231 ]); |
| 232 }); | 232 }); |
| 233 | 233 |
| 234 observeTest('sort', () { | 234 observeTest('sort', () { |
| 235 list.sort((x, y) => x - y); | 235 list.sort((x, y) => x - y); |
| 236 expect(list, orderedEquals([1, 1, 2, 3, 3, 4])); | 236 expect(list, orderedEquals([1, 1, 2, 3, 3, 4])); |
| 237 | 237 |
| 238 performMicrotaskCheckpoint(); | 238 performMicrotaskCheckpoint(); |
| 239 expectChanges(records, [ | 239 expectChanges(records, [ |
| 240 _change(1, addedCount: 5, removedCount: 5), | 240 _change(1, addedCount: 5, removedCount: 5), |
| 241 ]); | 241 ]); |
| 242 }); | 242 }); |
| 243 | 243 |
| 244 observeTest('clear', () { | 244 observeTest('clear', () { |
| 245 list.clear(); | 245 list.clear(); |
| 246 expect(list, []); | 246 expect(list, []); |
| 247 | 247 |
| 248 performMicrotaskCheckpoint(); | 248 performMicrotaskCheckpoint(); |
| 249 expectChanges(records, [ | 249 expectChanges(records, [ |
| 250 _lengthChange, | 250 _lengthChange(list, 6, 0), |
| 251 _change(0, removedCount: 6) | 251 _change(0, removedCount: 6) |
| 252 ]); | 252 ]); |
| 253 }); | 253 }); |
| 254 }); | 254 }); |
| 255 } | 255 } |
| 256 | 256 |
| 257 final _lengthChange = new PropertyChangeRecord(#length); | 257 _lengthChange(list, int oldValue, int newValue) => |
| 258 new PropertyChangeRecord(list, #length, oldValue, newValue); |
| 258 | 259 |
| 259 _change(index, {removedCount: 0, addedCount: 0}) => new ListChangeRecord( | 260 _change(index, {removedCount: 0, addedCount: 0}) => new ListChangeRecord( |
| 260 index, removedCount: removedCount, addedCount: addedCount); | 261 index, removedCount: removedCount, addedCount: addedCount); |
| OLD | NEW |