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 'dart:async'; | 9 import 'dart:async'; |
9 @lazy import 'deferred_function_library.dart'; | 10 @lazy import 'deferred_function_library.dart'; |
10 | 11 |
11 const lazy = const DeferredLibrary('deferred_function_library'); | 12 const lazy = const DeferredLibrary('deferred_function_library'); |
12 | 13 |
13 isNoSuchMethodError(e) => e is NoSuchMethodError; | 14 isNoSuchMethodError(e) => e is NoSuchMethodError; |
14 | 15 |
15 class Expect { | |
16 static void isTrue(x) => Expect.equals(true, x); | |
17 | |
18 static void isFalse(x) => Expect.equals(false, x); | |
19 | |
20 static void equals(expected, actual) { | |
21 if (expected != actual) { | |
22 throw "Not equal. Expected: $expected. Got: $actual"; | |
23 } | |
24 } | |
25 static void throws(fun, [test]) { | |
26 try { | |
27 fun(); | |
28 } catch (e) { | |
29 if (!test(e)) throw "doesn't satisfy exception test"; | |
30 return; | |
31 } | |
32 throw "didn't throw"; | |
33 } | |
34 } | |
35 | |
36 main() { | 16 main() { |
37 print('unittest-suite-wait-for-done'); | 17 print('unittest-suite-wait-for-done'); |
38 | 18 |
39 Expect.throws(() { foo('a'); }, isNoSuchMethodError); | 19 Expect.throws(() { foo('a'); }, isNoSuchMethodError); |
40 int counter = 0; | 20 int counter = 0; |
41 lazy.load().then((bool didLoad) { | 21 lazy.load().then((bool didLoad) { |
42 Expect.isTrue(didLoad); | 22 Expect.isTrue(didLoad); |
43 Expect.equals(1, ++counter); | 23 Expect.equals(1, ++counter); |
44 print('lazy was loaded'); | 24 print('lazy was loaded'); |
45 Expect.equals(42, foo('b')); | 25 Expect.equals(42, foo('b')); |
46 }); | 26 }); |
47 Expect.equals(0, counter); | 27 Expect.equals(0, counter); |
48 lazy.load().then((bool didLoad) { | 28 lazy.load().then((bool didLoad) { |
49 Expect.isFalse(didLoad); | 29 Expect.isFalse(didLoad); |
50 Expect.equals(2, ++counter); | 30 Expect.equals(2, ++counter); |
51 print('lazy was loaded'); | 31 print('lazy was loaded'); |
52 Expect.equals(42, foo('b')); | 32 Expect.equals(42, foo('b')); |
53 print('unittest-suite-success'); | 33 print('unittest-suite-success'); |
54 }); | 34 }); |
55 Expect.equals(0, counter); | 35 Expect.equals(0, counter); |
56 Expect.throws(() { foo('a'); }, isNoSuchMethodError); | 36 Expect.throws(() { foo('a'); }, isNoSuchMethodError); |
57 } | 37 } |
OLD | NEW |