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:logging/logging.dart'; | 6 import 'package:logging/logging.dart'; |
7 import 'package:observe/observe.dart'; | 7 import 'package:observe/observe.dart'; |
8 import 'package:observe/src/dirty_check.dart' as dirty_check; | 8 import 'package:observe/src/dirty_check.dart' as dirty_check; |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 import 'observe_test_utils.dart'; | 10 import 'observe_test_utils.dart'; |
11 | 11 |
12 // Note: this ensures we run the dartanalyzer on the @observe package. | 12 // Note: this ensures we run the dartanalyzer on the @observe package. |
13 // @static-clean | 13 // @static-clean |
14 | 14 |
15 const _VALUE = const Symbol('value'); | |
16 | |
17 void main() { | 15 void main() { |
18 // Note: to test the basic Observable system, we use ObservableBox due to its | 16 // Note: to test the basic Observable system, we use ObservableBox due to its |
19 // simplicity. We also test a variant that is based on dirty-checking. | 17 // simplicity. We also test a variant that is based on dirty-checking. |
20 | 18 |
21 observeTest('no observers at the start', () { | 19 observeTest('no observers at the start', () { |
22 expect(dirty_check.allObservablesCount, 0); | 20 expect(dirty_check.allObservablesCount, 0); |
23 }); | 21 }); |
24 | 22 |
25 group('WatcherModel', () => _observeTests((x) => new WatcherModel(x))); | 23 group('WatcherModel', () => _observeTests((x) => new WatcherModel(x))); |
26 | 24 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 var records = null; | 205 var records = null; |
208 subs.add(t.changes.listen((r) { records = r; })); | 206 subs.add(t.changes.listen((r) { records = r; })); |
209 t.value = 42; | 207 t.value = 42; |
210 | 208 |
211 performMicrotaskCheckpoint(); | 209 performMicrotaskCheckpoint(); |
212 expectChanges(records, _changedValue(1)); | 210 expectChanges(records, _changedValue(1)); |
213 | 211 |
214 // Verify that mutation operations on the list fail: | 212 // Verify that mutation operations on the list fail: |
215 | 213 |
216 expect(() { | 214 expect(() { |
217 records[0] = new PropertyChangeRecord(_VALUE); | 215 records[0] = new PropertyChangeRecord(#value); |
218 }, throwsUnsupportedError); | 216 }, throwsUnsupportedError); |
219 | 217 |
220 expect(() { records.clear(); }, throwsUnsupportedError); | 218 expect(() { records.clear(); }, throwsUnsupportedError); |
221 | 219 |
222 expect(() { records.length = 0; }, throwsUnsupportedError); | 220 expect(() { records.length = 0; }, throwsUnsupportedError); |
223 }); | 221 }); |
224 | 222 |
225 observeTest('notifyChange', () { | 223 observeTest('notifyChange', () { |
226 var t = createModel(123); | 224 var t = createModel(123); |
227 var records = []; | 225 var records = []; |
228 subs.add(t.changes.listen((r) { records.addAll(r); })); | 226 subs.add(t.changes.listen((r) { records.addAll(r); })); |
229 t.notifyChange(new PropertyChangeRecord(_VALUE)); | 227 t.notifyChange(new PropertyChangeRecord(#value)); |
230 | 228 |
231 performMicrotaskCheckpoint(); | 229 performMicrotaskCheckpoint(); |
232 expectChanges(records, _changedValue(1)); | 230 expectChanges(records, _changedValue(1)); |
233 expect(t.value, 123, reason: 'value did not actually change.'); | 231 expect(t.value, 123, reason: 'value did not actually change.'); |
234 }); | 232 }); |
235 | 233 |
236 observeTest('notifyPropertyChange', () { | 234 observeTest('notifyPropertyChange', () { |
237 var t = createModel(123); | 235 var t = createModel(123); |
238 var records = null; | 236 var records = null; |
239 subs.add(t.changes.listen((r) { records = r; })); | 237 subs.add(t.changes.listen((r) { records = r; })); |
240 expect(t.notifyPropertyChange(_VALUE, t.value, 42), 42, | 238 expect(t.notifyPropertyChange(#value, t.value, 42), 42, |
241 reason: 'notifyPropertyChange returns newValue'); | 239 reason: 'notifyPropertyChange returns newValue'); |
242 | 240 |
243 performMicrotaskCheckpoint(); | 241 performMicrotaskCheckpoint(); |
244 expectChanges(records, _changedValue(1)); | 242 expectChanges(records, _changedValue(1)); |
245 expect(t.value, 123, reason: 'value did not actually change.'); | 243 expect(t.value, 123, reason: 'value did not actually change.'); |
246 }); | 244 }); |
247 } | 245 } |
248 | 246 |
249 _changedValue(len) => new List.filled(len, new PropertyChangeRecord(_VALUE)); | 247 _changedValue(len) => new List.filled(len, new PropertyChangeRecord(#value)); |
250 | 248 |
251 // A test model based on dirty checking. | 249 // A test model based on dirty checking. |
252 class WatcherModel<T> extends ObservableBase { | 250 class WatcherModel<T> extends ObservableBase { |
253 @observable T value; | 251 @observable T value; |
254 | 252 |
255 WatcherModel([T initialValue]) : value = initialValue; | 253 WatcherModel([T initialValue]) : value = initialValue; |
256 | 254 |
257 String toString() => '#<$runtimeType value: $value>'; | 255 String toString() => '#<$runtimeType value: $value>'; |
258 } | 256 } |
259 | 257 |
260 class ModelSubclass<T> extends WatcherModel<T> { | 258 class ModelSubclass<T> extends WatcherModel<T> { |
261 ModelSubclass([T initialValue]) : super(initialValue); | 259 ModelSubclass([T initialValue]) : super(initialValue); |
262 } | 260 } |
OLD | NEW |