OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Test starting isolate with static functions (and toplevel ones, for sanity). | |
6 | |
7 library static_function_test; | |
8 import 'dart:isolate'; | |
9 import 'dart:async'; | |
10 import 'static_function_lib.dart' as lib; | |
11 import '../../pkg/unittest/lib/unittest.dart'; | |
12 | |
13 void function(SendPort port) { port.send("TOP"); } | |
14 void _function(SendPort port) { port.send("_TOP"); } | |
15 | |
16 // A closure created at top-level (not inside a method), but not by a top-level | |
17 // function declaration. | |
18 var staticClosure = (SendPort port) { port.send("WHAT?"); }; | |
19 | |
20 // An unnamed closure created inside a function. | |
21 get dynamicClosure => (SendPort port) { port.send("WHAT??"); }; | |
22 | |
23 // A named closure created inside a function. | |
24 get namedDynamicClosure { | |
25 void foo(SendPort port) { port.send("WHAT FOO??"); }; | |
26 return foo; | |
27 } | |
28 | |
29 class C { | |
30 // Unnamed closure created during object initialization, but not inside | |
31 // a method or constructor. | |
32 final Function instanceClosure = (SendPort port) { port.send("C WHAT?"); }; | |
33 // Unnamed closure created during object initializer list evaluation. | |
34 final Function constructorInitializerClosure; | |
35 // Unnamed closure created inside constructor body. | |
36 Function constructorBodyClosure; | |
37 // Named closure created inside constructor body. | |
38 Function namedConstructorBodyClosure; | |
39 | |
40 C() : constructorInitializerClosure = | |
41 ((SendPort port) { port.send("Init?"); }) { | |
42 constructorBodyClosure = (SendPort port) { | |
43 port.send("bodyClosure?"); | |
44 }; | |
45 void foo(SendPort port) { | |
46 port.send("namedBodyClosure?"); | |
47 } | |
48 namedConstructorBodyClosure = foo; | |
49 } | |
50 | |
51 static void function(SendPort port) { port.send("YES"); } | |
52 static void _function(SendPort port) { port.send("PRIVATE"); } | |
53 void instanceMethod(SendPort port) { port.send("INSTANCE WHAT?"); } | |
54 } | |
55 | |
56 class _C { | |
57 static void function(SendPort port) { port.send("_YES"); } | |
58 static void _function(SendPort port) { port.send("_PRIVATE"); } | |
59 } | |
60 | |
61 void spawnTest(name, function, response) { | |
62 test(name, () { | |
63 ReceivePort r = new ReceivePort(); | |
64 Isolate.spawn(function, r.sendPort); | |
65 r.listen(expectAsync1((v) { | |
66 expect(v, response); | |
67 r.close(); | |
68 })); | |
69 }); | |
70 } | |
71 | |
72 void throwsTest(name, function) { | |
73 test("throws on $name", () { | |
74 expect(() { Isolate.spawn(function, null); }, throws); | |
75 }); | |
76 } | |
77 | |
78 void main() { | |
79 // Sanity check. | |
80 spawnTest("function", function, "TOP"); | |
81 spawnTest("_function", _function, "_TOP"); | |
82 spawnTest("lib.function", lib.function, "LIBTOP"); | |
83 spawnTest("lib._function", lib.privateFunction, "_LIBTOP"); | |
84 | |
85 // Local static functions. | |
86 spawnTest("class.function", C.function, "YES"); | |
87 spawnTest("class._function", C._function, "PRIVATE"); | |
88 spawnTest("_class._function", _C.function, "_YES"); | |
89 spawnTest("_class._function", _C._function, "_PRIVATE"); | |
90 | |
91 // Imported static functions. | |
92 spawnTest("lib.class.function", lib.C.function, "LIB"); | |
93 spawnTest("lib.class._function", lib.C.privateFunction, "LIBPRIVATE"); | |
94 spawnTest("lib._class._function", lib.privateClassFunction, "_LIB"); | |
95 spawnTest("lib._class._function", lib.privateClassAndFunction, "_LIBPRIVATE"); | |
96 | |
97 // Negative tests | |
98 throwsTest("static closure", staticClosure); | |
99 throwsTest("dynamic closure", dynamicClosure); | |
100 throwsTest("named dynamic closure", namedDynamicClosure); | |
101 throwsTest("instance closure", new C().instanceClosure); | |
102 throwsTest("initializer closure", new C().constructorInitializerClosure); | |
103 throwsTest("constructor closure", new C().constructorBodyClosure); | |
104 throwsTest("named constructor closure", new C().namedConstructorBodyClosure); | |
105 throwsTest("instance method", new C().instanceMethod); | |
106 } | |
OLD | NEW |