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 library eval_test; | 5 library eval_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 // Import mirrors to cause all mirrors to be retained by dart2js. | 9 // Import mirrors to cause all mirrors to be retained by dart2js. |
10 // The tests reflect on LinkedHashMap.length and String.length. | 10 // The tests reflect on LinkedHashMap.length and String.length. |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 var child = new Foo(name: 'child'); | 237 var child = new Foo(name: 'child'); |
238 var parent = new Foo(child: child); | 238 var parent = new Foo(child: child); |
239 assign(parse('child.name'), 'Joe', new Scope(model: parent)); | 239 assign(parse('child.name'), 'Joe', new Scope(model: parent)); |
240 expect(parent.child.name, 'Joe'); | 240 expect(parent.child.name, 'Joe'); |
241 }); | 241 }); |
242 | 242 |
243 test('should assign an index', () { | 243 test('should assign an index', () { |
244 var foo = new Foo(items: [1, 2, 3]); | 244 var foo = new Foo(items: [1, 2, 3]); |
245 assign(parse('items[0]'), 4, new Scope(model: foo)); | 245 assign(parse('items[0]'), 4, new Scope(model: foo)); |
246 expect(foo.items[0], 4); | 246 expect(foo.items[0], 4); |
| 247 assign(parse('items[a]'), 5, new Scope(model: foo, variables: {'a': 0})); |
| 248 expect(foo.items[0], 5); |
| 249 }); |
| 250 |
| 251 test('should assign with a function call subexpression', () { |
| 252 var child = new Foo(); |
| 253 var foo = new Foo(items: [1, 2, 3], child: child); |
| 254 assign(parse('getChild().name'), 'child', new Scope(model: foo)); |
| 255 expect(child.name, 'child'); |
247 }); | 256 }); |
248 | 257 |
249 test('should assign through transformers', () { | 258 test('should assign through transformers', () { |
250 var foo = new Foo(name: '42', age: 32); | 259 var foo = new Foo(name: '42', age: 32); |
251 var globals = { | 260 var globals = { |
252 'a': '42', | 261 'a': '42', |
253 'parseInt': parseInt, | 262 'parseInt': parseInt, |
254 'add': add, | 263 'add': add, |
255 }; | 264 }; |
256 var scope = new Scope(model: foo, variables: globals); | 265 var scope = new Scope(model: foo, variables: globals); |
257 assign(parse('age | add(7)'), 29, scope); | 266 assign(parse('age | add(7)'), 29, scope); |
258 expect(foo.age, 22); | 267 expect(foo.age, 22); |
259 assign(parse('name | parseInt() | add(10)'), 29, scope); | 268 assign(parse('name | parseInt() | add(10)'), 29, scope); |
260 expect(foo.name, '19'); | 269 expect(foo.name, '19'); |
261 }); | 270 }); |
262 | 271 |
263 test('should throw on assignments to properties on null', () { | 272 test('should not throw on assignments to properties on null', () { |
264 assign(parse('name'), 'b', new Scope(model: null)); | 273 assign(parse('name'), 'b', new Scope(model: null)); |
265 }); | 274 }); |
266 | 275 |
| 276 test('should throw on assignments to non-assignable expressions', () { |
| 277 var foo = new Foo(name: 'a'); |
| 278 var scope = new Scope(model: foo); |
| 279 expect(() => assign(parse('name + 1'), 1, scope), |
| 280 throwsA(new isInstanceOf<EvalException>())); |
| 281 expect(() => assign(parse('toString()'), 1, scope), |
| 282 throwsA(new isInstanceOf<EvalException>())); |
| 283 expect(() => assign(parse('name | filter'), 1, scope), |
| 284 throwsA(new isInstanceOf<EvalException>())); |
| 285 }); |
| 286 |
| 287 test('should not throw on assignments to non-assignable expressions if ' |
| 288 'checkAssignability is false', () { |
| 289 var foo = new Foo(name: 'a'); |
| 290 var scope = new Scope(model: foo); |
| 291 expect( |
| 292 assign(parse('name + 1'), 1, scope, checkAssignability: false), |
| 293 null); |
| 294 expect( |
| 295 assign(parse('toString()'), 1, scope, checkAssignability: false), |
| 296 null); |
| 297 expect( |
| 298 assign(parse('name | filter'), 1, scope, checkAssignability: false), |
| 299 null); |
| 300 }); |
| 301 |
267 }); | 302 }); |
268 | 303 |
269 group('scope', () { | 304 group('scope', () { |
270 test('should return fields on the model', () { | 305 test('should return fields on the model', () { |
271 var foo = new Foo(name: 'a', age: 1); | 306 var foo = new Foo(name: 'a', age: 1); |
272 var scope = new Scope(model: foo); | 307 var scope = new Scope(model: foo); |
273 expect(scope['name'], 'a'); | 308 expect(scope['name'], 'a'); |
274 expect(scope['age'], 1); | 309 expect(scope['age'], 1); |
275 }); | 310 }); |
276 | 311 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 _name = notifyPropertyChange(#name, _name, n); | 378 _name = notifyPropertyChange(#name, _name, n); |
344 } | 379 } |
345 | 380 |
346 int age; | 381 int age; |
347 Foo child; | 382 Foo child; |
348 List<int> items; | 383 List<int> items; |
349 | 384 |
350 Foo({name, this.age, this.child, this.items}) : _name = name; | 385 Foo({name, this.age, this.child, this.items}) : _name = name; |
351 | 386 |
352 int x() => age * age; | 387 int x() => age * age; |
| 388 |
| 389 getChild() => child; |
| 390 |
| 391 filter(i) => i; |
353 } | 392 } |
354 | 393 |
355 @reflectable | 394 @reflectable |
356 class ListHolder { | 395 class ListHolder { |
357 List items; | 396 List items; |
358 ListHolder(this.items); | 397 ListHolder(this.items); |
359 } | 398 } |
360 | 399 |
361 parseInt([int radix = 10]) => new IntToString(radix: radix); | 400 parseInt([int radix = 10]) => new IntToString(radix: radix); |
362 | 401 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 return Future.wait([future, new Future(() { | 446 return Future.wait([future, new Future(() { |
408 expect(passed, true, reason: "Didn't receive a change notification on $s"); | 447 expect(passed, true, reason: "Didn't receive a change notification on $s"); |
409 })]); | 448 })]); |
410 } | 449 } |
411 | 450 |
412 // Regression test from https://code.google.com/p/dart/issues/detail?id=13459 | 451 // Regression test from https://code.google.com/p/dart/issues/detail?id=13459 |
413 class WordElement extends Observable { | 452 class WordElement extends Observable { |
414 @observable List chars1 = 'abcdefg'.split(''); | 453 @observable List chars1 = 'abcdefg'.split(''); |
415 @reflectable List filteredList(List original) => [original[0], original[1]]; | 454 @reflectable List filteredList(List original) => [original[0], original[1]]; |
416 } | 455 } |
OLD | NEW |