Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: pkg/observe/test/path_observer_test.dart

Issue 51483002: fix PathObserver to avoid try+catch (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/observe/pubspec.yaml ('k') | pkg/observe/test/transform_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:observe/observe.dart'; 5 import 'package:observe/observe.dart';
6 import 'package:unittest/unittest.dart'; 6 import 'package:unittest/unittest.dart';
7 import 'observe_test_utils.dart'; 7 import 'observe_test_utils.dart';
8 8
9 // This file contains code ported from: 9 // This file contains code ported from:
10 // https://github.com/rafaelw/ChangeSummary/blob/master/tests/test.js 10 // https://github.com/rafaelw/ChangeSummary/blob/master/tests/test.js
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 206
207 model['a'] = 2; 207 model['a'] = 2;
208 performMicrotaskCheckpoint(); 208 performMicrotaskCheckpoint();
209 expect(values, [1, 2]); 209 expect(values, [1, 2]);
210 210
211 sub.cancel(); 211 sub.cancel();
212 model['a'] = 3; 212 model['a'] = 3;
213 performMicrotaskCheckpoint(); 213 performMicrotaskCheckpoint();
214 expect(values, [1, 2]); 214 expect(values, [1, 2]);
215 }); 215 });
216
217 observeTest('errors thrown from getter/setter', () {
218 var model = new ObjectWithErrors();
219 var observer = new PathObserver(model, 'foo');
220
221 expect(() => observer.value, throws);
222 expect(model.getFooCalled, 1);
223
224 expect(() { observer.value = 123; }, throws);
225 expect(model.setFooCalled, [123]);
226 });
227
228 observeTest('object with noSuchMethod', () {
229 var model = new NoSuchMethodModel();
230 var observer = new PathObserver(model, 'foo');
231
232 expect(observer.value, 42);
233 observer.value = 'hi';
234 expect(model._foo, 'hi');
235 expect(observer.value, 'hi');
236
237 expect(model.log, [#foo, const Symbol('foo='), #foo]);
238
239 // These shouldn't throw
240 observer = new PathObserver(model, 'bar');
241 expect(observer.value, null, reason: 'path not found');
242 observer.value = 42;
243 expect(observer.value, null, reason: 'path not found');
244 });
245
246 observeTest('object with indexer', () {
247 var model = new IndexerModel();
248 var observer = new PathObserver(model, 'foo');
249
250 expect(observer.value, 42);
251 expect(model.log, ['[] foo']);
252 model.log.clear();
253
254 observer.value = 'hi';
255 expect(model.log, ['[]= foo hi']);
256 expect(model._foo, 'hi');
257
258 expect(observer.value, 'hi');
259
260 // These shouldn't throw
261 model.log.clear();
262 observer = new PathObserver(model, 'bar');
263 expect(observer.value, null, reason: 'path not found');
264 expect(model.log, ['[] bar']);
265 model.log.clear();
266
267 observer.value = 42;
268 expect(model.log, ['[]= bar 42']);
269 model.log.clear();
270 });
271 }
272
273 class ObjectWithErrors {
274 int getFooCalled = 0;
275 List setFooCalled = [];
276 @reflectable get foo {
277 getFooCalled++;
278 (this as dynamic).bar;
279 }
280 @reflectable set foo(value) {
281 setFooCalled.add(value);
282 (this as dynamic).bar = value;
283 }
284 }
285
286 class NoSuchMethodModel {
287 var _foo = 42;
288 List log = [];
289
290 noSuchMethod(Invocation invocation) {
291 final name = invocation.memberName;
292 log.add(name);
293 if (name == #foo && invocation.isGetter) return _foo;
294 if (name == const Symbol('foo=')) {
295 _foo = invocation.positionalArguments[0];
296 return null;
297 }
298 return super.noSuchMethod(invocation);
299 }
300 }
301
302 class IndexerModel {
303 var _foo = 42;
304 List log = [];
305
306 operator [](index) {
307 log.add('[] $index');
308 if (index == 'foo') return _foo;
309 }
310
311 operator []=(index, value) {
312 log.add('[]= $index $value');
313 if (index == 'foo') _foo = value;
314 }
216 } 315 }
217 316
218 @reflectable 317 @reflectable
219 class TestModel extends ChangeNotifier { 318 class TestModel extends ChangeNotifier {
220 var _a, _b, _c; 319 var _a, _b, _c;
221 320
222 TestModel(); 321 TestModel();
223 322
224 get a => _a; 323 get a => _a;
225 324
(...skipping 16 matching lines...) Expand all
242 341
243 class WatcherModel extends Observable { 342 class WatcherModel extends Observable {
244 // TODO(jmesserly): dart2js does not let these be on the same line: 343 // TODO(jmesserly): dart2js does not let these be on the same line:
245 // @observable var a, b, c; 344 // @observable var a, b, c;
246 @observable var a; 345 @observable var a;
247 @observable var b; 346 @observable var b;
248 @observable var c; 347 @observable var c;
249 348
250 WatcherModel(); 349 WatcherModel();
251 } 350 }
OLDNEW
« no previous file with comments | « pkg/observe/pubspec.yaml ('k') | pkg/observe/test/transform_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698