| 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 'package:observe/observe.dart'; | 6 import 'package:observe/observe.dart'; |
| 6 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
| 7 import 'utils.dart'; | 8 import 'observe_test_utils.dart'; |
| 8 | 9 |
| 9 main() { | 10 main() { |
| 10 // TODO(jmesserly): need all standard List API tests. | 11 // TODO(jmesserly): need all standard List API tests. |
| 11 | 12 |
| 12 const _LENGTH = const Symbol('length'); | 13 const _LENGTH = const Symbol('length'); |
| 13 | 14 |
| 15 StreamSubscription sub; |
| 16 |
| 17 sharedTearDown() { sub.cancel(); } |
| 18 |
| 14 group('observe length', () { | 19 group('observe length', () { |
| 15 | 20 |
| 16 ObservableList list; | 21 ObservableList list; |
| 17 List<ChangeRecord> changes; | 22 List<ChangeRecord> changes; |
| 18 | 23 |
| 19 setUp(() { | 24 setUp(() { |
| 20 list = toObservable([1, 2, 3]); | 25 list = toObservable([1, 2, 3]); |
| 21 changes = null; | 26 changes = null; |
| 22 list.changes.listen((records) { | 27 sub = list.changes.listen((records) { |
| 23 changes = records.where((r) => r.changes(_LENGTH)).toList(); | 28 changes = records.where((r) => r.changes(_LENGTH)).toList(); |
| 24 }); | 29 }); |
| 25 }); | 30 }); |
| 26 | 31 |
| 27 test('add changes length', () { | 32 tearDown(sharedTearDown); |
| 33 |
| 34 observeTest('add changes length', () { |
| 28 list.add(4); | 35 list.add(4); |
| 29 expect(list, [1, 2, 3, 4]); | 36 expect(list, [1, 2, 3, 4]); |
| 30 deliverChangeRecords(); | 37 performMicrotaskCheckpoint(); |
| 31 expectChanges(changes, [_lengthChange]); | 38 expectChanges(changes, [_lengthChange]); |
| 32 }); | 39 }); |
| 33 | 40 |
| 34 test('removeObject', () { | 41 observeTest('removeObject', () { |
| 35 list.remove(2); | 42 list.remove(2); |
| 36 expect(list, orderedEquals([1, 3])); | 43 expect(list, orderedEquals([1, 3])); |
| 37 | 44 |
| 38 deliverChangeRecords(); | 45 performMicrotaskCheckpoint(); |
| 39 deliverChangeRecords(); | |
| 40 expectChanges(changes, [_lengthChange]); | 46 expectChanges(changes, [_lengthChange]); |
| 41 }); | 47 }); |
| 42 | 48 |
| 43 test('removeRange changes length', () { | 49 observeTest('removeRange changes length', () { |
| 44 list.add(4); | 50 list.add(4); |
| 45 list.removeRange(1, 3); | 51 list.removeRange(1, 3); |
| 46 expect(list, [1, 4]); | 52 expect(list, [1, 4]); |
| 47 deliverChangeRecords(); | 53 performMicrotaskCheckpoint(); |
| 48 expectChanges(changes, [_lengthChange]); | 54 expectChanges(changes, [_lengthChange]); |
| 49 }); | 55 }); |
| 50 | 56 |
| 51 test('length= changes length', () { | 57 observeTest('length= changes length', () { |
| 52 list.length = 5; | 58 list.length = 5; |
| 53 expect(list, [1, 2, 3, null, null]); | 59 expect(list, [1, 2, 3, null, null]); |
| 54 deliverChangeRecords(); | 60 performMicrotaskCheckpoint(); |
| 55 expectChanges(changes, [_lengthChange]); | 61 expectChanges(changes, [_lengthChange]); |
| 56 }); | 62 }); |
| 57 | 63 |
| 58 test('[]= does not change length', () { | 64 observeTest('[]= does not change length', () { |
| 59 list[2] = 9000; | 65 list[2] = 9000; |
| 60 expect(list, [1, 2, 9000]); | 66 expect(list, [1, 2, 9000]); |
| 61 deliverChangeRecords(); | 67 performMicrotaskCheckpoint(); |
| 62 expectChanges(changes, []); | 68 expectChanges(changes, []); |
| 63 }); | 69 }); |
| 64 | 70 |
| 65 test('clear changes length', () { | 71 observeTest('clear changes length', () { |
| 66 list.clear(); | 72 list.clear(); |
| 67 expect(list, []); | 73 expect(list, []); |
| 68 deliverChangeRecords(); | 74 performMicrotaskCheckpoint(); |
| 69 expectChanges(changes, [_lengthChange]); | 75 expectChanges(changes, [_lengthChange]); |
| 70 }); | 76 }); |
| 71 }); | 77 }); |
| 72 | 78 |
| 73 group('observe index', () { | 79 group('observe index', () { |
| 74 ObservableList list; | 80 ObservableList list; |
| 75 List<ChangeRecord> changes; | 81 List<ChangeRecord> changes; |
| 76 | 82 |
| 77 setUp(() { | 83 setUp(() { |
| 78 list = toObservable([1, 2, 3]); | 84 list = toObservable([1, 2, 3]); |
| 79 changes = null; | 85 changes = null; |
| 80 list.changes.listen((records) { | 86 sub = list.changes.listen((records) { |
| 81 changes = records.where((r) => r.changes(1)).toList(); | 87 changes = records.where((r) => r.changes(1)).toList(); |
| 82 }); | 88 }); |
| 83 }); | 89 }); |
| 84 | 90 |
| 85 test('add does not change existing items', () { | 91 tearDown(sharedTearDown); |
| 92 |
| 93 observeTest('add does not change existing items', () { |
| 86 list.add(4); | 94 list.add(4); |
| 87 expect(list, [1, 2, 3, 4]); | 95 expect(list, [1, 2, 3, 4]); |
| 88 deliverChangeRecords(); | 96 performMicrotaskCheckpoint(); |
| 89 expectChanges(changes, []); | 97 expectChanges(changes, []); |
| 90 }); | 98 }); |
| 91 | 99 |
| 92 test('[]= changes item', () { | 100 observeTest('[]= changes item', () { |
| 93 list[1] = 777; | 101 list[1] = 777; |
| 94 expect(list, [1, 777, 3]); | 102 expect(list, [1, 777, 3]); |
| 95 deliverChangeRecords(); | 103 performMicrotaskCheckpoint(); |
| 96 expectChanges(changes, [_change(1, addedCount: 1, removedCount: 1)]); | 104 expectChanges(changes, [_change(1, addedCount: 1, removedCount: 1)]); |
| 97 }); | 105 }); |
| 98 | 106 |
| 99 test('[]= on a different item does not fire change', () { | 107 observeTest('[]= on a different item does not fire change', () { |
| 100 list[2] = 9000; | 108 list[2] = 9000; |
| 101 expect(list, [1, 2, 9000]); | 109 expect(list, [1, 2, 9000]); |
| 102 deliverChangeRecords(); | 110 performMicrotaskCheckpoint(); |
| 103 expectChanges(changes, []); | 111 expectChanges(changes, []); |
| 104 }); | 112 }); |
| 105 | 113 |
| 106 test('set multiple times results in one change', () { | 114 observeTest('set multiple times results in one change', () { |
| 107 list[1] = 777; | 115 list[1] = 777; |
| 108 list[1] = 42; | 116 list[1] = 42; |
| 109 expect(list, [1, 42, 3]); | 117 expect(list, [1, 42, 3]); |
| 110 deliverChangeRecords(); | 118 performMicrotaskCheckpoint(); |
| 111 expectChanges(changes, [ | 119 expectChanges(changes, [ |
| 112 _change(1, addedCount: 1, removedCount: 1), | 120 _change(1, addedCount: 1, removedCount: 1), |
| 113 ]); | 121 ]); |
| 114 }); | 122 }); |
| 115 | 123 |
| 116 test('set length without truncating item means no change', () { | 124 observeTest('set length without truncating item means no change', () { |
| 117 list.length = 2; | 125 list.length = 2; |
| 118 expect(list, [1, 2]); | 126 expect(list, [1, 2]); |
| 119 deliverChangeRecords(); | 127 performMicrotaskCheckpoint(); |
| 120 expectChanges(changes, []); | 128 expectChanges(changes, []); |
| 121 }); | 129 }); |
| 122 | 130 |
| 123 test('truncate removes item', () { | 131 observeTest('truncate removes item', () { |
| 124 list.length = 1; | 132 list.length = 1; |
| 125 expect(list, [1]); | 133 expect(list, [1]); |
| 126 deliverChangeRecords(); | 134 performMicrotaskCheckpoint(); |
| 127 expectChanges(changes, [_change(1, removedCount: 2)]); | 135 expectChanges(changes, [_change(1, removedCount: 2)]); |
| 128 }); | 136 }); |
| 129 | 137 |
| 130 test('truncate and add new item', () { | 138 observeTest('truncate and add new item', () { |
| 131 list.length = 1; | 139 list.length = 1; |
| 132 list.add(42); | 140 list.add(42); |
| 133 expect(list, [1, 42]); | 141 expect(list, [1, 42]); |
| 134 deliverChangeRecords(); | 142 performMicrotaskCheckpoint(); |
| 135 expectChanges(changes, [ | 143 expectChanges(changes, [ |
| 136 _change(1, removedCount: 2, addedCount: 1) | 144 _change(1, removedCount: 2, addedCount: 1) |
| 137 ]); | 145 ]); |
| 138 }); | 146 }); |
| 139 | 147 |
| 140 test('truncate and add same item', () { | 148 observeTest('truncate and add same item', () { |
| 141 list.length = 1; | 149 list.length = 1; |
| 142 list.add(2); | 150 list.add(2); |
| 143 expect(list, [1, 2]); | 151 expect(list, [1, 2]); |
| 144 deliverChangeRecords(); | 152 performMicrotaskCheckpoint(); |
| 145 expectChanges(changes, [ | 153 expectChanges(changes, [ |
| 146 _change(1, removedCount: 2, addedCount: 1) | 154 _change(1, removedCount: 2, addedCount: 1) |
| 147 ]); | 155 ]); |
| 148 }); | 156 }); |
| 149 }); | 157 }); |
| 150 | 158 |
| 151 test('toString', () { | 159 observeTest('toString', () { |
| 152 var list = toObservable([1, 2, 3]); | 160 var list = toObservable([1, 2, 3]); |
| 153 expect(list.toString(), '[1, 2, 3]'); | 161 expect(list.toString(), '[1, 2, 3]'); |
| 154 }); | 162 }); |
| 155 | 163 |
| 156 group('change records', () { | 164 group('change records', () { |
| 157 | 165 |
| 158 List<ChangeRecord> records; | 166 List<ChangeRecord> records; |
| 159 ObservableList list; | 167 ObservableList list; |
| 160 | 168 |
| 161 setUp(() { | 169 setUp(() { |
| 162 list = toObservable([1, 2, 3, 1, 3, 4]); | 170 list = toObservable([1, 2, 3, 1, 3, 4]); |
| 163 records = null; | 171 records = null; |
| 164 list.changes.listen((r) { records = r; }); | 172 sub = list.changes.listen((r) { records = r; }); |
| 165 }); | 173 }); |
| 166 | 174 |
| 167 test('read operations', () { | 175 tearDown(sharedTearDown); |
| 176 |
| 177 observeTest('read operations', () { |
| 168 expect(list.length, 6); | 178 expect(list.length, 6); |
| 169 expect(list[0], 1); | 179 expect(list[0], 1); |
| 170 expect(list.indexOf(4), 5); | 180 expect(list.indexOf(4), 5); |
| 171 expect(list.indexOf(1), 0); | 181 expect(list.indexOf(1), 0); |
| 172 expect(list.indexOf(1, 1), 3); | 182 expect(list.indexOf(1, 1), 3); |
| 173 expect(list.lastIndexOf(1), 3); | 183 expect(list.lastIndexOf(1), 3); |
| 174 expect(list.last, 4); | 184 expect(list.last, 4); |
| 175 var copy = new List<int>(); | 185 var copy = new List<int>(); |
| 176 list.forEach((i) { copy.add(i); }); | 186 list.forEach((i) { copy.add(i); }); |
| 177 expect(copy, orderedEquals([1, 2, 3, 1, 3, 4])); | 187 expect(copy, orderedEquals([1, 2, 3, 1, 3, 4])); |
| 178 deliverChangeRecords(); | 188 performMicrotaskCheckpoint(); |
| 179 | 189 |
| 180 // no change from read-only operators | 190 // no change from read-only operators |
| 181 expectChanges(records, null); | 191 expectChanges(records, null); |
| 182 }); | 192 }); |
| 183 | 193 |
| 184 test('add', () { | 194 observeTest('add', () { |
| 185 list.add(5); | 195 list.add(5); |
| 186 list.add(6); | 196 list.add(6); |
| 187 expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6])); | 197 expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6])); |
| 188 | 198 |
| 189 deliverChangeRecords(); | 199 performMicrotaskCheckpoint(); |
| 190 expectChanges(records, [ | 200 expectChanges(records, [ |
| 191 _lengthChange, | 201 _lengthChange, |
| 192 _change(6, addedCount: 2) | 202 _change(6, addedCount: 2) |
| 193 ]); | 203 ]); |
| 194 }); | 204 }); |
| 195 | 205 |
| 196 test('[]=', () { | 206 observeTest('[]=', () { |
| 197 list[1] = list.last; | 207 list[1] = list.last; |
| 198 expect(list, orderedEquals([1, 4, 3, 1, 3, 4])); | 208 expect(list, orderedEquals([1, 4, 3, 1, 3, 4])); |
| 199 | 209 |
| 200 deliverChangeRecords(); | 210 performMicrotaskCheckpoint(); |
| 201 expectChanges(records, [ _change(1, addedCount: 1, removedCount: 1) ]); | 211 expectChanges(records, [ _change(1, addedCount: 1, removedCount: 1) ]); |
| 202 }); | 212 }); |
| 203 | 213 |
| 204 test('removeLast', () { | 214 observeTest('removeLast', () { |
| 205 expect(list.removeLast(), 4); | 215 expect(list.removeLast(), 4); |
| 206 expect(list, orderedEquals([1, 2, 3, 1, 3])); | 216 expect(list, orderedEquals([1, 2, 3, 1, 3])); |
| 207 | 217 |
| 208 deliverChangeRecords(); | 218 performMicrotaskCheckpoint(); |
| 209 expectChanges(records, [ | 219 expectChanges(records, [ |
| 210 _lengthChange, | 220 _lengthChange, |
| 211 _change(5, removedCount: 1) | 221 _change(5, removedCount: 1) |
| 212 ]); | 222 ]); |
| 213 }); | 223 }); |
| 214 | 224 |
| 215 test('removeRange', () { | 225 observeTest('removeRange', () { |
| 216 list.removeRange(1, 4); | 226 list.removeRange(1, 4); |
| 217 expect(list, orderedEquals([1, 3, 4])); | 227 expect(list, orderedEquals([1, 3, 4])); |
| 218 | 228 |
| 219 deliverChangeRecords(); | 229 performMicrotaskCheckpoint(); |
| 220 expectChanges(records, [ | 230 expectChanges(records, [ |
| 221 _lengthChange, | 231 _lengthChange, |
| 222 _change(1, removedCount: 3), | 232 _change(1, removedCount: 3), |
| 223 ]); | 233 ]); |
| 224 }); | 234 }); |
| 225 | 235 |
| 226 test('sort', () { | 236 observeTest('sort', () { |
| 227 list.sort((x, y) => x - y); | 237 list.sort((x, y) => x - y); |
| 228 expect(list, orderedEquals([1, 1, 2, 3, 3, 4])); | 238 expect(list, orderedEquals([1, 1, 2, 3, 3, 4])); |
| 229 | 239 |
| 230 deliverChangeRecords(); | 240 performMicrotaskCheckpoint(); |
| 231 expectChanges(records, [ | 241 expectChanges(records, [ |
| 232 _change(1, addedCount: 5, removedCount: 5), | 242 _change(1, addedCount: 5, removedCount: 5), |
| 233 ]); | 243 ]); |
| 234 }); | 244 }); |
| 235 | 245 |
| 236 test('clear', () { | 246 observeTest('clear', () { |
| 237 list.clear(); | 247 list.clear(); |
| 238 expect(list, []); | 248 expect(list, []); |
| 239 | 249 |
| 240 deliverChangeRecords(); | 250 performMicrotaskCheckpoint(); |
| 241 expectChanges(records, [ | 251 expectChanges(records, [ |
| 242 _lengthChange, | 252 _lengthChange, |
| 243 _change(0, removedCount: 6) | 253 _change(0, removedCount: 6) |
| 244 ]); | 254 ]); |
| 245 }); | 255 }); |
| 246 }); | 256 }); |
| 247 } | 257 } |
| 248 | 258 |
| 249 const _LENGTH = const Symbol('length'); | 259 const _LENGTH = const Symbol('length'); |
| 250 | 260 |
| 251 final _lengthChange = new PropertyChangeRecord(_LENGTH); | 261 final _lengthChange = new PropertyChangeRecord(_LENGTH); |
| 252 | 262 |
| 253 _change(index, {removedCount: 0, addedCount: 0}) => new ListChangeRecord( | 263 _change(index, {removedCount: 0, addedCount: 0}) => new ListChangeRecord( |
| 254 index, removedCount: removedCount, addedCount: addedCount); | 264 index, removedCount: removedCount, addedCount: addedCount); |
| OLD | NEW |