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