| 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 'package:mdv_observe/mdv_observe.dart'; | 5 import 'package:observe/observe.dart'; |
| 6 import 'package:unittest/unittest.dart'; | 6 import 'package:unittest/unittest.dart'; |
| 7 | 7 |
| 8 // This file contains code ported from: | 8 // This file contains code ported from: |
| 9 // https://github.com/rafaelw/ChangeSummary/blob/master/tests/test.js | 9 // https://github.com/rafaelw/ChangeSummary/blob/master/tests/test.js |
| 10 | 10 |
| 11 main() { | 11 main() { |
| 12 group('PathObserver', observePathTests); | 12 group('PathObserver', observePathTests); |
| 13 } | 13 } |
| 14 | 14 |
| 15 observePath(obj, path) => new PathObserver(obj, path); | 15 observePath(obj, path) => new PathObserver(obj, path); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 156 |
| 157 test('Path Value With Indices', () { | 157 test('Path Value With Indices', () { |
| 158 var model = toObservable([]); | 158 var model = toObservable([]); |
| 159 observePath(model, '0').values.listen(expectAsync1((v) { | 159 observePath(model, '0').values.listen(expectAsync1((v) { |
| 160 expect(v, 123); | 160 expect(v, 123); |
| 161 })); | 161 })); |
| 162 model.add(123); | 162 model.add(123); |
| 163 }); | 163 }); |
| 164 | 164 |
| 165 test('Path Observation', () { | 165 test('Path Observation', () { |
| 166 var model = new TestModel(const Symbol('a'), | 166 var model = new TestModel()..a = |
| 167 new TestModel(const Symbol('b'), | 167 (new TestModel()..b = (new TestModel()..c = 'hello, world')); |
| 168 new TestModel(const Symbol('c'), 'hello, world'))); | |
| 169 | 168 |
| 170 var path = observePath(model, 'a.b.c'); | 169 var path = observePath(model, 'a.b.c'); |
| 171 var lastValue = null; | 170 var lastValue = null; |
| 172 var sub = path.values.listen((v) { lastValue = v; }); | 171 var sub = path.values.listen((v) { lastValue = v; }); |
| 173 | 172 |
| 174 model.value.value.value = 'hello, mom'; | 173 model.a.b.c = 'hello, mom'; |
| 175 | 174 |
| 176 expect(lastValue, null); | 175 expect(lastValue, null); |
| 177 deliverChangeRecords(); | 176 deliverChangeRecords(); |
| 178 expect(lastValue, 'hello, mom'); | 177 expect(lastValue, 'hello, mom'); |
| 179 | 178 |
| 180 model.value.value = new TestModel(const Symbol('c'), 'hello, dad'); | 179 model.a.b = new TestModel()..c = 'hello, dad'; |
| 181 deliverChangeRecords(); | 180 deliverChangeRecords(); |
| 182 expect(lastValue, 'hello, dad'); | 181 expect(lastValue, 'hello, dad'); |
| 183 | 182 |
| 184 model.value = new TestModel(const Symbol('b'), | 183 model.a = new TestModel()..b = |
| 185 new TestModel(const Symbol('c'), 'hello, you')); | 184 (new TestModel()..c = 'hello, you'); |
| 186 deliverChangeRecords(); | 185 deliverChangeRecords(); |
| 187 expect(lastValue, 'hello, you'); | 186 expect(lastValue, 'hello, you'); |
| 188 | 187 |
| 189 model.value.value = 1; | 188 model.a.b = 1; |
| 190 deliverChangeRecords(); | 189 deliverChangeRecords(); |
| 191 expect(lastValue, null); | 190 expect(lastValue, null); |
| 192 | 191 |
| 193 // Stop observing | 192 // Stop observing |
| 194 sub.cancel(); | 193 sub.cancel(); |
| 195 | 194 |
| 196 model.value.value = new TestModel(const Symbol('c'), | 195 model.a.b = new TestModel()..c = 'hello, back again -- but not observing'; |
| 197 'hello, back again -- but not observing'); | |
| 198 deliverChangeRecords(); | 196 deliverChangeRecords(); |
| 199 expect(lastValue, null); | 197 expect(lastValue, null); |
| 200 | 198 |
| 201 // Resume observing | 199 // Resume observing |
| 202 sub = path.values.listen((v) { lastValue = v; }); | 200 sub = path.values.listen((v) { lastValue = v; }); |
| 203 | 201 |
| 204 model.value.value.value = 'hello. Back for reals'; | 202 model.a.b.c = 'hello. Back for reals'; |
| 205 deliverChangeRecords(); | 203 deliverChangeRecords(); |
| 206 expect(lastValue, 'hello. Back for reals'); | 204 expect(lastValue, 'hello. Back for reals'); |
| 207 }); | 205 }); |
| 208 | 206 |
| 209 test('observe map', () { | 207 test('observe map', () { |
| 210 var model = toSymbolMap({'a': 1}); | 208 var model = toSymbolMap({'a': 1}); |
| 211 var path = observePath(model, 'a'); | 209 var path = observePath(model, 'a'); |
| 212 | 210 |
| 213 var values = [path.value]; | 211 var values = [path.value]; |
| 214 var sub = path.values.listen((v) { values.add(v); }); | 212 var sub = path.values.listen((v) { values.add(v); }); |
| 215 expect(values, [1]); | 213 expect(values, [1]); |
| 216 | 214 |
| 217 model[sym('a')] = 2; | 215 model[sym('a')] = 2; |
| 218 deliverChangeRecords(); | 216 deliverChangeRecords(); |
| 219 expect(values, [1, 2]); | 217 expect(values, [1, 2]); |
| 220 | 218 |
| 221 sub.cancel(); | 219 sub.cancel(); |
| 222 model[sym('a')] = 3; | 220 model[sym('a')] = 3; |
| 223 deliverChangeRecords(); | 221 deliverChangeRecords(); |
| 224 expect(values, [1, 2]); | 222 expect(values, [1, 2]); |
| 225 }); | 223 }); |
| 226 } | 224 } |
| 227 | 225 |
| 228 class TestModel extends ObservableBase { | 226 class TestModel extends ObservableBase { |
| 229 final Symbol fieldName; | 227 var _a, _b, _c; |
| 230 var _value; | |
| 231 | 228 |
| 232 TestModel(this.fieldName, [initialValue]) : _value = initialValue; | 229 TestModel(); |
| 233 | 230 |
| 234 get value => _value; | 231 get a => _a; |
| 235 | 232 |
| 236 void set value(newValue) { | 233 void set a(newValue) { |
| 237 _value = notifyPropertyChange(fieldName, _value, newValue); | 234 _a = notifyPropertyChange(const Symbol('a'), _a, newValue); |
| 238 } | 235 } |
| 239 | 236 |
| 240 getValueWorkaround(key) { | 237 get b => _b; |
| 241 if (key == fieldName) return value; | 238 |
| 242 return null; | 239 void set b(newValue) { |
| 243 } | 240 _b = notifyPropertyChange(const Symbol('b'), _b, newValue); |
| 244 void setValueWorkaround(key, newValue) { | |
| 245 if (key == fieldName) value = newValue; | |
| 246 } | 241 } |
| 247 | 242 |
| 248 toString() => '#<$runtimeType $fieldName: $_value>'; | 243 get c => _c; |
| 244 |
| 245 void set c(newValue) { |
| 246 _c = notifyPropertyChange(const Symbol('c'), _c, newValue); |
| 247 } |
| 249 } | 248 } |
| OLD | NEW |