OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Dart test program for checking implemention of IsolateMirror when | 5 // Dart test program for checking implemention of IsolateMirror when |
6 // inspecting the current isolate. | 6 // inspecting the current isolate. |
7 | 7 |
8 #library('isolate_mirror_local_test'); | 8 #library('isolate_mirror_local_test'); |
9 | 9 |
10 #import('dart:isolate'); | 10 #import('dart:isolate'); |
11 #import('dart:mirrors'); | 11 #import('dart:mirrors'); |
12 | 12 |
13 ReceivePort exit_port; | 13 ReceivePort exit_port; |
14 Set expectedTests; | 14 Set expectedTests; |
15 | 15 |
16 void testDone(String test) { | 16 void testDone(String test) { |
17 if (!expectedTests.contains(test)) { | 17 if (!expectedTests.contains(test)) { |
18 throw "Unexpected test name '$test'"; | 18 throw "Unexpected test name '$test'"; |
19 } | 19 } |
20 expectedTests.remove(test); | 20 expectedTests.remove(test); |
21 if (expectedTests.isEmpty()) { | 21 if (expectedTests.isEmpty()) { |
22 // All tests are done. | 22 // All tests are done. |
23 exit_port.close(); | 23 exit_port.close(); |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 int global_var = 0; | 27 int global_var = 0; |
| 28 final int final_global_var = 0; |
| 29 |
| 30 // Top-level getter and setter. |
| 31 int get myVar() { return 5; } |
| 32 int set myVar(x) {} |
28 | 33 |
29 // This function will be invoked reflectively. | 34 // This function will be invoked reflectively. |
30 int function(int x) { | 35 int function(int x) { |
31 global_var = x; | 36 global_var = x; |
32 return x + 1; | 37 return x + 1; |
33 } | 38 } |
34 | 39 |
| 40 _stringCompare(String a, String b) => a.compareTo(b); |
| 41 sort(list) => list.sort(_stringCompare); |
| 42 |
| 43 String buildMethodString(MethodMirror func) { |
| 44 var result = '${func.simpleName}'; |
| 45 if (func.isTopLevel) { |
| 46 result = '$result toplevel'; |
| 47 } |
| 48 if (func.isStatic) { |
| 49 result = '$result static'; |
| 50 } |
| 51 if (func.isMethod) { |
| 52 result = '$result method'; |
| 53 } |
| 54 if (func.isGetter) { |
| 55 result = '$result getter'; |
| 56 } |
| 57 if (func.isSetter) { |
| 58 result = '$result setter'; |
| 59 } |
| 60 if (func.isConstructor) { |
| 61 result = '$result constructor'; |
| 62 } |
| 63 if (func.isConstConstructor) { |
| 64 result = '$result const'; |
| 65 } |
| 66 if (func.isGenerativeConstructor) { |
| 67 result = '$result generative'; |
| 68 } |
| 69 if (func.isRedirectingConstructor) { |
| 70 result = '$result redirecting'; |
| 71 } |
| 72 if (func.isFactoryConstructor) { |
| 73 result = '$result factory'; |
| 74 } |
| 75 return result; |
| 76 } |
| 77 |
| 78 String buildVariableString(VariableMirror variable) { |
| 79 var result = '${variable.simpleName}'; |
| 80 if (variable.isTopLevel) { |
| 81 result = '$result toplevel'; |
| 82 } |
| 83 if (variable.isStatic) { |
| 84 result = '$result static'; |
| 85 } |
| 86 if (variable.isFinal) { |
| 87 result = '$result final'; |
| 88 } |
| 89 return result; |
| 90 } |
| 91 |
35 void testRootLibraryMirror(LibraryMirror lib_mirror) { | 92 void testRootLibraryMirror(LibraryMirror lib_mirror) { |
36 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName); | 93 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName); |
37 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart')); | 94 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart')); |
38 Expect.equals("LibraryMirror on 'isolate_mirror_local_test'", | 95 Expect.equals("LibraryMirror on 'isolate_mirror_local_test'", |
39 lib_mirror.toString()); | 96 lib_mirror.toString()); |
40 | 97 |
41 // Test library invocation by calling function(123). | 98 // Test library invocation by calling function(123). |
42 Expect.equals(0, global_var); | 99 Expect.equals(0, global_var); |
43 lib_mirror.invoke('function', [ 123 ]).then( | 100 lib_mirror.invoke('function', [ 123 ]).then( |
44 (InstanceMirror retval) { | 101 (InstanceMirror retval) { |
45 Expect.equals(123, global_var); | 102 Expect.equals(123, global_var); |
46 Expect.equals('Smi', retval.getClass().simpleName); | 103 Expect.equals('Smi', retval.getClass().simpleName); |
47 Expect.isTrue(retval.hasSimpleValue); | 104 Expect.isTrue(retval.hasSimpleValue); |
48 Expect.equals(124, retval.simpleValue); | 105 Expect.equals(124, retval.simpleValue); |
49 testDone('testRootLibraryMirror'); | 106 testDone('testRootLibraryMirror'); |
50 }); | 107 }); |
| 108 |
| 109 // Check that the members map is complete. |
| 110 List keys = lib_mirror.members().getKeys(); |
| 111 sort(keys); |
| 112 Expect.equals('[' |
| 113 'MyClass, ' |
| 114 'MyException, ' |
| 115 'MyInterface, ' |
| 116 'MySuperClass, ' |
| 117 '_stringCompare, ' |
| 118 'buildMethodString, ' |
| 119 'buildVariableString, ' |
| 120 'exit_port, ' |
| 121 'expectedTests, ' |
| 122 'final_global_var, ' |
| 123 'function, ' |
| 124 'global_var, ' |
| 125 'main, ' |
| 126 'methodWithError, ' |
| 127 'methodWithException, ' |
| 128 'myVar, ' |
| 129 'myVar=, ' |
| 130 'sort, ' |
| 131 'testBoolInstanceMirror, ' |
| 132 'testCustomInstanceMirror, ' |
| 133 'testDone, ' |
| 134 'testIntegerInstanceMirror, ' |
| 135 'testIsolateMirror, ' |
| 136 'testLibrariesMap, ' |
| 137 'testMirrorErrors, ' |
| 138 'testNullInstanceMirror, ' |
| 139 'testRootLibraryMirror, ' |
| 140 'testStringInstanceMirror]', |
| 141 '$keys'); |
| 142 |
| 143 // Check that the classes map is complete. |
| 144 keys = lib_mirror.classes().getKeys(); |
| 145 sort(keys); |
| 146 Expect.equals('[' |
| 147 'MyClass, ' |
| 148 'MyException, ' |
| 149 'MyInterface, ' |
| 150 'MySuperClass]', |
| 151 '$keys'); |
| 152 |
| 153 // Check that the functions map is complete. |
| 154 keys = lib_mirror.functions().getKeys(); |
| 155 sort(keys); |
| 156 Expect.equals('[' |
| 157 '_stringCompare, ' |
| 158 'buildMethodString, ' |
| 159 'buildVariableString, ' |
| 160 'function, ' |
| 161 'main, ' |
| 162 'methodWithError, ' |
| 163 'methodWithException, ' |
| 164 'myVar, ' |
| 165 'myVar=, ' |
| 166 'sort, ' |
| 167 'testBoolInstanceMirror, ' |
| 168 'testCustomInstanceMirror, ' |
| 169 'testDone, ' |
| 170 'testIntegerInstanceMirror, ' |
| 171 'testIsolateMirror, ' |
| 172 'testLibrariesMap, ' |
| 173 'testMirrorErrors, ' |
| 174 'testNullInstanceMirror, ' |
| 175 'testRootLibraryMirror, ' |
| 176 'testStringInstanceMirror]', |
| 177 '$keys'); |
| 178 |
| 179 // Check that the variables map is complete. |
| 180 keys = lib_mirror.variables().getKeys(); |
| 181 sort(keys); |
| 182 Expect.equals('[' |
| 183 'exit_port, ' |
| 184 'expectedTests, ' |
| 185 'final_global_var, ' |
| 186 'global_var]', |
| 187 '$keys'); |
| 188 |
| 189 InterfaceMirror cls_mirror = lib_mirror.members()['MyClass']; |
| 190 |
| 191 // Test function mirrors. |
| 192 MethodMirror func = lib_mirror.members()['function']; |
| 193 Expect.isTrue(func is MethodMirror); |
| 194 Expect.equals('function toplevel static method', buildMethodString(func)); |
| 195 |
| 196 func = lib_mirror.members()['myVar']; |
| 197 Expect.isTrue(func is MethodMirror); |
| 198 Expect.equals('myVar toplevel static getter', buildMethodString(func)); |
| 199 |
| 200 func = lib_mirror.members()['myVar=']; |
| 201 Expect.isTrue(func is MethodMirror); |
| 202 Expect.equals('myVar= toplevel static setter', buildMethodString(func)); |
| 203 |
| 204 func = cls_mirror.members()['method']; |
| 205 Expect.isTrue(func is MethodMirror); |
| 206 Expect.equals('method method', buildMethodString(func)); |
| 207 |
| 208 func = cls_mirror.members()['MyClass']; |
| 209 Expect.isTrue(func is MethodMirror); |
| 210 Expect.equals('MyClass constructor', buildMethodString(func)); |
| 211 |
| 212 // Test variable mirrors. |
| 213 VariableMirror variable = lib_mirror.members()['global_var']; |
| 214 Expect.isTrue(variable is VariableMirror); |
| 215 Expect.equals('global_var toplevel static', buildVariableString(variable)); |
| 216 |
| 217 variable = lib_mirror.members()['final_global_var']; |
| 218 Expect.isTrue(variable is VariableMirror); |
| 219 Expect.equals('final_global_var toplevel static final', |
| 220 buildVariableString(variable)); |
| 221 |
| 222 variable = cls_mirror.members()['value']; |
| 223 Expect.isTrue(variable is VariableMirror); |
| 224 Expect.equals('value final', buildVariableString(variable)); |
51 } | 225 } |
52 | 226 |
53 void testLibrariesMap(Map libraries) { | 227 void testLibrariesMap(Map libraries) { |
54 // Just look for a couple of well-known libs. | 228 // Just look for a couple of well-known libs. |
55 LibraryMirror core_lib = libraries['dart:core']; | 229 LibraryMirror core_lib = libraries['dart:core']; |
56 Expect.isTrue(core_lib is LibraryMirror); | 230 Expect.isTrue(core_lib is LibraryMirror); |
57 | 231 |
58 LibraryMirror mirror_lib = libraries['dart:mirrors']; | 232 LibraryMirror mirror_lib = libraries['dart:mirrors']; |
59 Expect.isTrue(mirror_lib is LibraryMirror); | 233 Expect.isTrue(mirror_lib is LibraryMirror); |
60 | 234 |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 // Test that an isolate can reflect on itself. | 459 // Test that an isolate can reflect on itself. |
286 isolateMirrorOf(exit_port.toSendPort()).then(testIsolateMirror); | 460 isolateMirrorOf(exit_port.toSendPort()).then(testIsolateMirror); |
287 | 461 |
288 testIntegerInstanceMirror(mirrorOf(1001)); | 462 testIntegerInstanceMirror(mirrorOf(1001)); |
289 testStringInstanceMirror(mirrorOf('This\nis\na\nString')); | 463 testStringInstanceMirror(mirrorOf('This\nis\na\nString')); |
290 testBoolInstanceMirror(mirrorOf(true)); | 464 testBoolInstanceMirror(mirrorOf(true)); |
291 testNullInstanceMirror(mirrorOf(null)); | 465 testNullInstanceMirror(mirrorOf(null)); |
292 testCustomInstanceMirror(mirrorOf(new MyClass(17))); | 466 testCustomInstanceMirror(mirrorOf(new MyClass(17))); |
293 testMirrorErrors(currentIsolateMirror()); | 467 testMirrorErrors(currentIsolateMirror()); |
294 } | 468 } |
OLD | NEW |