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