OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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'; | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'observe_test_utils.dart'; | |
9 | 6 |
10 main() => dirtyCheckZone().run(_runTests); | 7 import 'package:observable/observable.dart'; |
| 8 import 'package:test/test.dart'; |
| 9 |
| 10 import 'observable_test_utils.dart'; |
| 11 |
| 12 main() => _runTests(); |
11 | 13 |
12 _runTests() { | 14 _runTests() { |
13 // TODO(jmesserly): need all standard List API tests. | 15 // TODO(jmesserly): need all standard List API tests. |
14 | 16 |
15 StreamSubscription sub, sub2; | 17 StreamSubscription sub, sub2; |
16 | 18 |
17 sharedTearDown() { | 19 sharedTearDown() { |
18 list = null; | 20 list = null; |
19 sub.cancel(); | 21 sub.cancel(); |
20 if (sub2 != null) { | 22 if (sub2 != null) { |
21 sub2.cancel(); | 23 sub2.cancel(); |
22 sub2 = null; | 24 sub2 = null; |
23 } | 25 } |
24 } | 26 } |
25 | 27 |
26 group('observe length', () { | 28 group('observe length', () { |
27 | |
28 ObservableList list; | 29 ObservableList list; |
29 List<ChangeRecord> changes; | 30 List<ChangeRecord> changes; |
30 | 31 |
31 setUp(() { | 32 setUp(() { |
32 list = toObservable([1, 2, 3]); | 33 list = toObservable([1, 2, 3]) as ObservableList; |
33 changes = null; | 34 changes = null; |
34 sub = list.changes.listen((records) { | 35 sub = list.changes.listen((records) { |
35 changes = getPropertyChangeRecords(records, #length); | 36 changes = getPropertyChangeRecords(records, #length); |
36 }); | 37 }); |
37 }); | 38 }); |
38 | 39 |
39 tearDown(sharedTearDown); | 40 tearDown(sharedTearDown); |
40 | 41 |
41 test('add changes length', () { | 42 test('add changes length', () { |
42 list.add(4); | 43 list.add(4); |
43 expect(list, [1, 2, 3, 4]); | 44 expect(list, [1, 2, 3, 4]); |
44 return new Future(() { | 45 return new Future(() { |
45 expectChanges(changes, [_lengthChange(3, 4)]); | 46 expectChanges(changes, [_lengthChange(3, 4)]); |
46 }); | 47 }); |
47 }); | 48 }); |
48 | 49 |
49 test('removeObject', () { | 50 test('removeObject changes length', () { |
50 list.remove(2); | 51 list.remove(2); |
51 expect(list, orderedEquals([1, 3])); | 52 expect(list, orderedEquals([1, 3])); |
52 | 53 |
53 return new Future(() { | 54 return new Future(() { |
54 expectChanges(changes, [_lengthChange(3, 2)]); | 55 expectChanges(changes, [_lengthChange(3, 2)]); |
55 }); | 56 }); |
56 }); | 57 }); |
57 | 58 |
58 test('removeRange changes length', () { | 59 test('removeRange changes length', () { |
59 list.add(4); | 60 list.add(4); |
(...skipping 26 matching lines...) Expand all Loading... |
86 return new Future(() { | 87 return new Future(() { |
87 expectChanges(changes, [_lengthChange(3, 0)]); | 88 expectChanges(changes, [_lengthChange(3, 0)]); |
88 }); | 89 }); |
89 }); | 90 }); |
90 }); | 91 }); |
91 | 92 |
92 group('observe index', () { | 93 group('observe index', () { |
93 List<ListChangeRecord> changes; | 94 List<ListChangeRecord> changes; |
94 | 95 |
95 setUp(() { | 96 setUp(() { |
96 list = toObservable([1, 2, 3]); | 97 list = toObservable([1, 2, 3]) as ObservableList; |
97 changes = null; | 98 changes = null; |
98 sub = list.listChanges.listen((records) { | 99 sub = list.listChanges.listen((List<ListChangeRecord> records) { |
99 changes = getListChangeRecords(records, 1); | 100 changes = getListChangeRecords(records, 1); |
100 }); | 101 }); |
101 }); | 102 }); |
102 | 103 |
103 tearDown(sharedTearDown); | 104 tearDown(sharedTearDown); |
104 | 105 |
105 test('add does not change existing items', () { | 106 test('add does not change existing items', () { |
106 list.add(4); | 107 list.add(4); |
107 expect(list, [1, 2, 3, 4]); | 108 expect(list, [1, 2, 3, 4]); |
108 return new Future(() { | 109 return new Future(() { |
109 expectChanges(changes, []); | 110 expectChanges(changes, []); |
110 }); | 111 }); |
111 }); | 112 }); |
112 | 113 |
113 test('[]= changes item', () { | 114 test('[]= changes item', () { |
114 list[1] = 777; | 115 list[1] = 777; |
115 expect(list, [1, 777, 3]); | 116 expect(list, [1, 777, 3]); |
116 return new Future(() { | 117 return new Future(() { |
117 expectChanges(changes, [_change(1, addedCount: 1, removed: [2])]); | 118 expectChanges(changes, [ |
| 119 _change(1, addedCount: 1, removed: [2]) |
| 120 ]); |
118 }); | 121 }); |
119 }); | 122 }); |
120 | 123 |
121 test('[]= on a different item does not fire change', () { | 124 test('[]= on a different item does not fire change', () { |
122 list[2] = 9000; | 125 list[2] = 9000; |
123 expect(list, [1, 2, 9000]); | 126 expect(list, [1, 2, 9000]); |
124 return new Future(() { | 127 return new Future(() { |
125 expectChanges(changes, []); | 128 expectChanges(changes, []); |
126 }); | 129 }); |
127 }); | 130 }); |
(...skipping 14 matching lines...) Expand all Loading... |
142 expect(list, [1, 2]); | 145 expect(list, [1, 2]); |
143 return new Future(() { | 146 return new Future(() { |
144 expectChanges(changes, []); | 147 expectChanges(changes, []); |
145 }); | 148 }); |
146 }); | 149 }); |
147 | 150 |
148 test('truncate removes item', () { | 151 test('truncate removes item', () { |
149 list.length = 1; | 152 list.length = 1; |
150 expect(list, [1]); | 153 expect(list, [1]); |
151 return new Future(() { | 154 return new Future(() { |
152 expectChanges(changes, [_change(1, removed: [2, 3])]); | 155 expectChanges(changes, [ |
| 156 _change(1, removed: [2, 3]) |
| 157 ]); |
153 }); | 158 }); |
154 }); | 159 }); |
155 | 160 |
156 test('truncate and add new item', () { | 161 test('truncate and add new item', () { |
157 list.length = 1; | 162 list.length = 1; |
158 list.add(42); | 163 list.add(42); |
159 expect(list, [1, 42]); | 164 expect(list, [1, 42]); |
160 return new Future(() { | 165 return new Future(() { |
161 expectChanges(changes, [ | 166 expectChanges(changes, [ |
162 _change(1, removed: [2, 3], addedCount: 1) | 167 _change(1, removed: [2, 3], addedCount: 1) |
(...skipping 10 matching lines...) Expand all Loading... |
173 }); | 178 }); |
174 }); | 179 }); |
175 }); | 180 }); |
176 | 181 |
177 test('toString', () { | 182 test('toString', () { |
178 var list = toObservable([1, 2, 3]); | 183 var list = toObservable([1, 2, 3]); |
179 expect(list.toString(), '[1, 2, 3]'); | 184 expect(list.toString(), '[1, 2, 3]'); |
180 }); | 185 }); |
181 | 186 |
182 group('change records', () { | 187 group('change records', () { |
183 | |
184 List<ChangeRecord> propRecords; | 188 List<ChangeRecord> propRecords; |
185 List<ListChangeRecord> listRecords; | 189 List<ListChangeRecord> listRecords; |
186 | 190 |
187 setUp(() { | 191 setUp(() { |
188 list = toObservable([1, 2, 3, 1, 3, 4]); | 192 list = toObservable([1, 2, 3, 1, 3, 4]) as ObservableList; |
189 propRecords = null; | 193 propRecords = null; |
190 listRecords = null; | 194 listRecords = null; |
191 sub = list.changes.listen((r) { propRecords = r; }); | 195 sub = list.changes.listen((r) => propRecords = r); |
192 sub2 = list.listChanges.listen((r) { listRecords = r; }); | 196 sub2 = list.listChanges.listen((r) => listRecords = r); |
193 }); | 197 }); |
194 | 198 |
195 tearDown(sharedTearDown); | 199 tearDown(sharedTearDown); |
196 | 200 |
197 test('read operations', () { | 201 test('read operations', () { |
198 expect(list.length, 6); | 202 expect(list.length, 6); |
199 expect(list[0], 1); | 203 expect(list[0], 1); |
200 expect(list.indexOf(4), 5); | 204 expect(list.indexOf(4), 5); |
201 expect(list.indexOf(1), 0); | 205 expect(list.indexOf(1), 0); |
202 expect(list.indexOf(1, 1), 3); | 206 expect(list.indexOf(1, 1), 3); |
203 expect(list.lastIndexOf(1), 3); | 207 expect(list.lastIndexOf(1), 3); |
204 expect(list.last, 4); | 208 expect(list.last, 4); |
205 var copy = new List<int>(); | 209 var copy = new List<int>(); |
206 list.forEach((i) { copy.add(i); }); | 210 list.forEach((int i) => copy.add(i)); |
207 expect(copy, orderedEquals([1, 2, 3, 1, 3, 4])); | 211 expect(copy, orderedEquals([1, 2, 3, 1, 3, 4])); |
208 return new Future(() { | 212 return new Future(() { |
209 // no change from read-only operators | 213 // no change from read-only operators |
210 expectChanges(propRecords, null); | 214 expectChanges(propRecords, null); |
211 expectChanges(listRecords, null); | 215 expectChanges(listRecords, null); |
212 }); | 216 }); |
213 }); | 217 }); |
214 | 218 |
215 test('add', () { | 219 test('add', () { |
216 list.add(5); | 220 list.add(5); |
217 list.add(6); | 221 list.add(6); |
218 expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6])); | 222 expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6])); |
219 | 223 |
220 return new Future(() { | 224 return new Future(() { |
221 expectChanges(propRecords, [ | 225 expectChanges(propRecords, [ |
222 _lengthChange(6, 7), | 226 _lengthChange(6, 7), |
223 _lengthChange(7, 8), | 227 _lengthChange(7, 8), |
224 ]); | 228 ]); |
225 expectChanges(listRecords, [ _change(6, addedCount: 2) ]); | 229 expectChanges(listRecords, [_change(6, addedCount: 2)]); |
226 }); | 230 }); |
227 }); | 231 }); |
228 | 232 |
229 test('[]=', () { | 233 test('[]=', () { |
230 list[1] = list.last; | 234 list[1] = list.last; |
231 expect(list, orderedEquals([1, 4, 3, 1, 3, 4])); | 235 expect(list, orderedEquals([1, 4, 3, 1, 3, 4])); |
232 | 236 |
233 return new Future(() { | 237 return new Future(() { |
234 expectChanges(propRecords, null); | 238 expectChanges(propRecords, null); |
235 expectChanges(listRecords, [ _change(1, addedCount: 1, removed: [2]) ]); | 239 expectChanges(listRecords, [ |
| 240 _change(1, addedCount: 1, removed: [2]) |
| 241 ]); |
236 }); | 242 }); |
237 }); | 243 }); |
238 | 244 |
239 test('removeLast', () { | 245 test('removeLast', () { |
240 expect(list.removeLast(), 4); | 246 expect(list.removeLast(), 4); |
241 expect(list, orderedEquals([1, 2, 3, 1, 3])); | 247 expect(list, orderedEquals([1, 2, 3, 1, 3])); |
242 | 248 |
243 return new Future(() { | 249 return new Future(() { |
244 expectChanges(propRecords, [_lengthChange(6, 5)]); | 250 expectChanges(propRecords, [_lengthChange(6, 5)]); |
245 expectChanges(listRecords, [_change(5, removed: [4])]); | 251 expectChanges(listRecords, [ |
| 252 _change(5, removed: [4]) |
| 253 ]); |
246 }); | 254 }); |
247 }); | 255 }); |
248 | 256 |
249 test('removeRange', () { | 257 test('removeRange', () { |
250 list.removeRange(1, 4); | 258 list.removeRange(1, 4); |
251 expect(list, orderedEquals([1, 3, 4])); | 259 expect(list, orderedEquals([1, 3, 4])); |
252 | 260 |
253 return new Future(() { | 261 return new Future(() { |
254 expectChanges(propRecords, [_lengthChange(6, 3)]); | 262 expectChanges(propRecords, [_lengthChange(6, 3)]); |
255 expectChanges(listRecords, [_change(1, removed: [2, 3, 1])]); | 263 expectChanges(listRecords, [ |
| 264 _change(1, removed: [2, 3, 1]) |
| 265 ]); |
256 }); | 266 }); |
257 }); | 267 }); |
258 | 268 |
259 test('sort', () { | 269 test('sort', () { |
260 list.sort((x, y) => x - y); | 270 list.sort((x, y) => x - y); |
261 expect(list, orderedEquals([1, 1, 2, 3, 3, 4])); | 271 expect(list, orderedEquals([1, 1, 2, 3, 3, 4])); |
262 | 272 |
263 return new Future(() { | 273 return new Future(() { |
264 expectChanges(propRecords, null); | 274 expectChanges(propRecords, null); |
265 expectChanges(listRecords, [ | 275 expectChanges(listRecords, [ |
266 _change(1, addedCount: 1), | 276 _change(1, addedCount: 1), |
267 _change(4, removed: [1]) | 277 _change(4, removed: [1]) |
268 ]); | 278 ]); |
269 }); | 279 }); |
270 }); | 280 }); |
271 | 281 |
272 test('sort of 2 elements', () { | 282 test('sort of 2 elements', () { |
273 var list = toObservable([3, 1]); | 283 var list = toObservable([3, 1]); |
274 // Dummy listener to record changes. | 284 // Dummy listener to record changes. |
275 // TODO(jmesserly): should we just record changes always, to support the s
ync api? | 285 // TODO(jmesserly): should we just record changes always, to support the s
ync api? |
276 sub = list.listChanges.listen((records) => null); | 286 sub = list.listChanges.listen((List<ListChangeRecord> records) => null) |
| 287 as StreamSubscription; |
277 list.sort(); | 288 list.sort(); |
278 expect(list.deliverListChanges(), true); | 289 expect(list.deliverListChanges(), true); |
279 list.sort(); | 290 list.sort(); |
280 expect(list.deliverListChanges(), false); | 291 expect(list.deliverListChanges(), false); |
281 list.sort(); | 292 list.sort(); |
282 expect(list.deliverListChanges(), false); | 293 expect(list.deliverListChanges(), false); |
283 }); | 294 }); |
284 | 295 |
285 test('clear', () { | 296 test('clear', () { |
286 list.clear(); | 297 list.clear(); |
287 expect(list, []); | 298 expect(list, []); |
288 | 299 |
289 return new Future(() { | 300 return new Future(() { |
290 expectChanges(propRecords, [ | 301 expectChanges(propRecords, [ |
291 _lengthChange(6, 0), | 302 _lengthChange(6, 0), |
292 new PropertyChangeRecord(list, #isEmpty, false, true), | 303 new PropertyChangeRecord(list, #isEmpty, false, true), |
293 new PropertyChangeRecord(list, #isNotEmpty, true, false), | 304 new PropertyChangeRecord(list, #isNotEmpty, true, false), |
294 ]); | 305 ]); |
295 expectChanges(listRecords, [_change(0, removed: [1, 2, 3, 1, 3, 4])]); | 306 expectChanges(listRecords, [ |
| 307 _change(0, removed: [1, 2, 3, 1, 3, 4]) |
| 308 ]); |
296 }); | 309 }); |
297 }); | 310 }); |
298 }); | 311 }); |
299 } | 312 } |
300 | 313 |
301 ObservableList list; | 314 ObservableList list; |
302 | 315 |
303 _lengthChange(int oldValue, int newValue) => | 316 PropertyChangeRecord _lengthChange(int oldValue, int newValue) => |
304 new PropertyChangeRecord(list, #length, oldValue, newValue); | 317 new PropertyChangeRecord(list, #length, oldValue, newValue); |
305 | 318 |
306 _change(index, {removed: const [], addedCount: 0}) => new ListChangeRecord( | 319 _change(int index, {List removed: const [], int addedCount: 0}) => |
307 list, index, removed: removed, addedCount: addedCount); | 320 new ListChangeRecord(list, index, removed: removed, addedCount: addedCount); |
308 | |
OLD | NEW |