| 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 // Test that loading of a library (with top-level functions only) can | 5 // Test that loading of a library (with top-level functions only) can |
| 6 // be deferred. | 6 // be deferred. |
| 7 | 7 |
| 8 import "package:expect/expect.dart"; | 8 import '../../../async_helper.dart'; |
| 9 |
| 10 import 'package:expect/expect.dart'; |
| 11 |
| 9 import 'dart:async'; | 12 import 'dart:async'; |
| 13 |
| 10 @lazy import 'deferred_function_library.dart'; | 14 @lazy import 'deferred_function_library.dart'; |
| 11 | 15 |
| 12 const lazy = const DeferredLibrary('deferred_function_library'); | 16 const lazy = const DeferredLibrary('deferred_function_library'); |
| 13 | 17 |
| 14 isNoSuchMethodError(e) => e is NoSuchMethodError; | 18 isNoSuchMethodError(e) => e is NoSuchMethodError; |
| 15 | 19 |
| 16 readFoo() { | 20 readFoo() { |
| 17 // TODO(ahe): There is a problem with type inference of deferred | 21 // TODO(ahe): There is a problem with type inference of deferred |
| 18 // function closures. We think they are never null. | 22 // function closures. We think they are never null. |
| 19 if (new DateTime.now().millisecondsSinceEpoch == 87) return null; | 23 if (new DateTime.now().millisecondsSinceEpoch == 87) return null; |
| 20 return foo; | 24 return foo; |
| 21 } | 25 } |
| 22 | 26 |
| 23 main() { | 27 main() { |
| 24 print('unittest-suite-wait-for-done'); | |
| 25 | |
| 26 Expect.throws(() { foo('a'); }, isNoSuchMethodError); | 28 Expect.throws(() { foo('a'); }, isNoSuchMethodError); |
| 27 Expect.isNull(readFoo()); | 29 Expect.isNull(readFoo()); |
| 28 int counter = 0; | 30 int counter = 0; |
| 31 asyncStart(); |
| 29 lazy.load().then((bool didLoad) { | 32 lazy.load().then((bool didLoad) { |
| 30 Expect.isTrue(didLoad); | 33 Expect.isTrue(didLoad); |
| 31 Expect.equals(1, ++counter); | 34 Expect.equals(1, ++counter); |
| 32 print('lazy was loaded'); | 35 print('lazy was loaded'); |
| 33 Expect.equals(42, foo('b')); | 36 Expect.equals(42, foo('b')); |
| 34 Expect.isNotNull(readFoo()); | 37 Expect.isNotNull(readFoo()); |
| 38 asyncEnd(); |
| 35 }); | 39 }); |
| 36 Expect.equals(0, counter); | 40 Expect.equals(0, counter); |
| 41 asyncStart(); |
| 37 lazy.load().then((bool didLoad) { | 42 lazy.load().then((bool didLoad) { |
| 38 Expect.isFalse(didLoad); | 43 Expect.isFalse(didLoad); |
| 39 Expect.equals(2, ++counter); | 44 Expect.equals(2, ++counter); |
| 40 print('lazy was loaded'); | 45 print('lazy was loaded'); |
| 41 Expect.equals(42, foo('b')); | 46 Expect.equals(42, foo('b')); |
| 42 Expect.isNotNull(readFoo()); | 47 Expect.isNotNull(readFoo()); |
| 43 print('unittest-suite-success'); | 48 asyncEnd(); |
| 44 }); | 49 }); |
| 45 Expect.equals(0, counter); | 50 Expect.equals(0, counter); |
| 46 Expect.throws(() { foo('a'); }, isNoSuchMethodError); | 51 Expect.throws(() { foo('a'); }, isNoSuchMethodError); |
| 47 Expect.isNull(readFoo()); | 52 Expect.isNull(readFoo()); |
| 48 } | 53 } |
| OLD | NEW |