| 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 MirrorSystem when | 5 // Dart test program for checking implemention of MirrorSystem when |
| 6 // inspecting the current isolate. | 6 // inspecting the current isolate. |
| 7 // |
| 8 // VMOptions=--enable_type_checks |
| 7 | 9 |
| 8 #library('isolate_mirror_local_test'); | 10 #library('isolate_mirror_local_test'); |
| 9 | 11 |
| 10 #import('dart:isolate'); | 12 #import('dart:isolate'); |
| 11 #import('dart:mirrors'); | 13 #import('dart:mirrors'); |
| 12 | 14 |
| 13 ReceivePort exit_port; | 15 ReceivePort exit_port; |
| 14 Set expectedTests; | 16 Set expectedTests; |
| 15 | 17 |
| 16 void testDone(String test) { | 18 void testDone(String test) { |
| 17 if (!expectedTests.contains(test)) { | 19 if (!expectedTests.contains(test)) { |
| 18 throw "Unexpected test name '$test'"; | 20 throw "Unexpected test name '$test'"; |
| 19 } | 21 } |
| 20 expectedTests.remove(test); | 22 expectedTests.remove(test); |
| 21 if (expectedTests.isEmpty()) { | 23 if (expectedTests.isEmpty()) { |
| 22 // All tests are done. | 24 // All tests are done. |
| 23 exit_port.close(); | 25 exit_port.close(); |
| 24 } | 26 } |
| 25 } | 27 } |
| 26 | 28 |
| 27 int global_var = 0; | 29 int global_var = 0; |
| 28 final int final_global_var = 0; | 30 final int final_global_var = 0; |
| 29 | 31 |
| 30 // Top-level getter and setter. | 32 // Top-level getter and setter. |
| 31 int get myVar() { return 5; } | 33 int get myVar() { return 5; } |
| 32 int set myVar(x) {} | 34 void set myVar(x) {} |
| 33 | 35 |
| 34 // This function will be invoked reflectively. | 36 // This function will be invoked reflectively. |
| 35 int function(int x) { | 37 int function(int x) { |
| 36 global_var = x; | 38 global_var = x; |
| 37 return x + 1; | 39 return x + 1; |
| 38 } | 40 } |
| 39 | 41 |
| 42 typedef void FuncType(String a); |
| 43 |
| 44 FuncType myFunc = null; |
| 45 |
| 40 _stringCompare(String a, String b) => a.compareTo(b); | 46 _stringCompare(String a, String b) => a.compareTo(b); |
| 41 sort(list) => list.sort(_stringCompare); | 47 sort(list) => list.sort(_stringCompare); |
| 42 | 48 |
| 43 String buildMethodString(MethodMirror func) { | 49 String buildMethodString(MethodMirror func) { |
| 44 var result = '${func.simpleName}'; | 50 var result = '${func.simpleName} return(${func.returnType.simpleName})'; |
| 45 if (func.isPrivate) { | 51 if (func.isPrivate) { |
| 46 result = '$result private'; | 52 result = '$result private'; |
| 47 } | 53 } |
| 48 if (func.isTopLevel) { | 54 if (func.isTopLevel) { |
| 49 result = '$result toplevel'; | 55 result = '$result toplevel'; |
| 50 } | 56 } |
| 51 if (func.isStatic) { | 57 if (func.isStatic) { |
| 52 result = '$result static'; | 58 result = '$result static'; |
| 53 } | 59 } |
| 54 if (func.isAbstract) { | 60 if (func.isAbstract) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 75 if (func.isRedirectingConstructor) { | 81 if (func.isRedirectingConstructor) { |
| 76 result = '$result redirecting'; | 82 result = '$result redirecting'; |
| 77 } | 83 } |
| 78 if (func.isFactoryConstructor) { | 84 if (func.isFactoryConstructor) { |
| 79 result = '$result factory'; | 85 result = '$result factory'; |
| 80 } | 86 } |
| 81 return result; | 87 return result; |
| 82 } | 88 } |
| 83 | 89 |
| 84 String buildVariableString(VariableMirror variable) { | 90 String buildVariableString(VariableMirror variable) { |
| 85 var result = '${variable.simpleName}'; | 91 var result = '${variable.simpleName} type(${variable.type.simpleName})'; |
| 86 if (variable.isPrivate) { | 92 if (variable.isPrivate) { |
| 87 result = '$result private'; | 93 result = '$result private'; |
| 88 } | 94 } |
| 89 if (variable.isTopLevel) { | 95 if (variable.isTopLevel) { |
| 90 result = '$result toplevel'; | 96 result = '$result toplevel'; |
| 91 } | 97 } |
| 92 if (variable.isStatic) { | 98 if (variable.isStatic) { |
| 93 result = '$result static'; | 99 result = '$result static'; |
| 94 } | 100 } |
| 95 if (variable.isFinal) { | 101 if (variable.isFinal) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 115 Expect.equals('int', retval.type.simpleName); | 121 Expect.equals('int', retval.type.simpleName); |
| 116 Expect.isTrue(retval.hasReflectee); | 122 Expect.isTrue(retval.hasReflectee); |
| 117 Expect.equals(124, retval.reflectee); | 123 Expect.equals(124, retval.reflectee); |
| 118 testDone('testRootLibraryMirror'); | 124 testDone('testRootLibraryMirror'); |
| 119 }); | 125 }); |
| 120 | 126 |
| 121 // Check that the members map is complete. | 127 // Check that the members map is complete. |
| 122 List keys = lib_mirror.members.getKeys(); | 128 List keys = lib_mirror.members.getKeys(); |
| 123 sort(keys); | 129 sort(keys); |
| 124 Expect.equals('[' | 130 Expect.equals('[' |
| 131 'FuncType, ' |
| 132 'GenericClass, ' |
| 125 'MyClass, ' | 133 'MyClass, ' |
| 126 'MyException, ' | 134 'MyException, ' |
| 127 'MyInterface, ' | 135 'MyInterface, ' |
| 128 'MySuperClass, ' | 136 'MySuperClass, ' |
| 129 '_stringCompare, ' | 137 '_stringCompare, ' |
| 130 'buildMethodString, ' | 138 'buildMethodString, ' |
| 131 'buildVariableString, ' | 139 'buildVariableString, ' |
| 132 'exit_port, ' | 140 'exit_port, ' |
| 133 'expectedTests, ' | 141 'expectedTests, ' |
| 134 'final_global_var, ' | 142 'final_global_var, ' |
| 135 'function, ' | 143 'function, ' |
| 136 'global_var, ' | 144 'global_var, ' |
| 137 'main, ' | 145 'main, ' |
| 138 'methodWithError, ' | 146 'methodWithError, ' |
| 139 'methodWithException, ' | 147 'methodWithException, ' |
| 148 'myFunc, ' |
| 140 'myVar, ' | 149 'myVar, ' |
| 141 'myVar=, ' | 150 'myVar=, ' |
| 142 'sort, ' | 151 'sort, ' |
| 143 'testBoolInstanceMirror, ' | 152 'testBoolInstanceMirror, ' |
| 144 'testCustomInstanceMirror, ' | 153 'testCustomInstanceMirror, ' |
| 145 'testDone, ' | 154 'testDone, ' |
| 146 'testIntegerInstanceMirror, ' | 155 'testIntegerInstanceMirror, ' |
| 147 'testLibrariesMap, ' | 156 'testLibrariesMap, ' |
| 148 'testMirrorErrors, ' | 157 'testMirrorErrors, ' |
| 149 'testMirrorSystem, ' | 158 'testMirrorSystem, ' |
| 150 'testNullInstanceMirror, ' | 159 'testNullInstanceMirror, ' |
| 151 'testRootLibraryMirror, ' | 160 'testRootLibraryMirror, ' |
| 152 'testStringInstanceMirror]', | 161 'testStringInstanceMirror]', |
| 153 '$keys'); | 162 '$keys'); |
| 154 | 163 |
| 155 // Check that the classes map is complete. | 164 // Check that the classes map is complete. |
| 156 keys = lib_mirror.classes.getKeys(); | 165 keys = lib_mirror.classes.getKeys(); |
| 157 sort(keys); | 166 sort(keys); |
| 158 Expect.equals('[' | 167 Expect.equals('[' |
| 168 'GenericClass, ' |
| 159 'MyClass, ' | 169 'MyClass, ' |
| 160 'MyException, ' | 170 'MyException, ' |
| 161 'MyInterface, ' | 171 'MyInterface, ' |
| 162 'MySuperClass]', | 172 'MySuperClass]', |
| 163 '$keys'); | 173 '$keys'); |
| 164 | 174 |
| 165 // Check that the functions map is complete. | 175 // Check that the functions map is complete. |
| 166 keys = lib_mirror.functions.getKeys(); | 176 keys = lib_mirror.functions.getKeys(); |
| 167 sort(keys); | 177 sort(keys); |
| 168 Expect.equals('[' | 178 Expect.equals('[' |
| (...skipping 29 matching lines...) Expand all Loading... |
| 198 sort(keys); | 208 sort(keys); |
| 199 Expect.equals('[myVar=]', '$keys'); | 209 Expect.equals('[myVar=]', '$keys'); |
| 200 | 210 |
| 201 // Check that the variables map is complete. | 211 // Check that the variables map is complete. |
| 202 keys = lib_mirror.variables.getKeys(); | 212 keys = lib_mirror.variables.getKeys(); |
| 203 sort(keys); | 213 sort(keys); |
| 204 Expect.equals('[' | 214 Expect.equals('[' |
| 205 'exit_port, ' | 215 'exit_port, ' |
| 206 'expectedTests, ' | 216 'expectedTests, ' |
| 207 'final_global_var, ' | 217 'final_global_var, ' |
| 208 'global_var]', | 218 'global_var, ' |
| 219 'myFunc]', |
| 209 '$keys'); | 220 '$keys'); |
| 210 | 221 |
| 211 ClassMirror cls_mirror = lib_mirror.members['MyClass']; | 222 ClassMirror cls_mirror = lib_mirror.members['MyClass']; |
| 223 ClassMirror generic_cls_mirror = lib_mirror.members['GenericClass']; |
| 212 | 224 |
| 213 // Test function mirrors. | 225 // Test function mirrors. |
| 214 MethodMirror func = lib_mirror.members['function']; | 226 MethodMirror func = lib_mirror.members['function']; |
| 215 Expect.isTrue(func is MethodMirror); | 227 Expect.isTrue(func is MethodMirror); |
| 216 Expect.equals('function toplevel static method', buildMethodString(func)); | 228 Expect.equals('function return(int) toplevel static method', |
| 229 buildMethodString(func)); |
| 217 | 230 |
| 218 func = lib_mirror.members['myVar']; | 231 func = lib_mirror.members['myVar']; |
| 219 Expect.isTrue(func is MethodMirror); | 232 Expect.isTrue(func is MethodMirror); |
| 220 Expect.equals('myVar toplevel static getter', buildMethodString(func)); | 233 Expect.equals('myVar return(int) toplevel static getter', |
| 234 buildMethodString(func)); |
| 221 | 235 |
| 222 func = lib_mirror.members['myVar=']; | 236 func = lib_mirror.members['myVar=']; |
| 223 Expect.isTrue(func is MethodMirror); | 237 Expect.isTrue(func is MethodMirror); |
| 224 Expect.equals('myVar= toplevel static setter', buildMethodString(func)); | 238 Expect.equals('myVar= return(void) toplevel static setter', |
| 239 buildMethodString(func)); |
| 225 | 240 |
| 226 func = cls_mirror.members['method']; | 241 func = cls_mirror.members['method']; |
| 227 Expect.isTrue(func is MethodMirror); | 242 Expect.isTrue(func is MethodMirror); |
| 228 Expect.equals('method method', buildMethodString(func)); | 243 Expect.equals('method return(int) method', buildMethodString(func)); |
| 229 | 244 |
| 230 func = cls_mirror.members['MyClass']; | 245 func = cls_mirror.constructors['MyClass']; |
| 231 Expect.isTrue(func is MethodMirror); | 246 Expect.isTrue(func is MethodMirror); |
| 232 Expect.equals('MyClass constructor', buildMethodString(func)); | 247 Expect.equals('MyClass return(MyClass) constructor', buildMethodString(func)); |
| 233 | 248 |
| 234 func = cls_mirror.members['MyClass.named']; | 249 func = cls_mirror.constructors['MyClass.named']; |
| 235 Expect.isTrue(func is MethodMirror); | 250 Expect.isTrue(func is MethodMirror); |
| 236 Expect.equals('MyClass.named constructor', buildMethodString(func)); | 251 Expect.equals('MyClass.named return(MyClass) constructor', |
| 252 buildMethodString(func)); |
| 253 |
| 254 func = generic_cls_mirror.members['method']; |
| 255 Expect.isTrue(func is MethodMirror); |
| 256 Expect.equals('method return(T) method', buildMethodString(func)); |
| 237 | 257 |
| 238 // Test variable mirrors. | 258 // Test variable mirrors. |
| 239 VariableMirror variable = lib_mirror.members['global_var']; | 259 VariableMirror variable = lib_mirror.members['global_var']; |
| 240 Expect.isTrue(variable is VariableMirror); | 260 Expect.isTrue(variable is VariableMirror); |
| 241 Expect.equals('global_var toplevel static', buildVariableString(variable)); | 261 Expect.equals('global_var type(int) toplevel static', |
| 262 buildVariableString(variable)); |
| 242 | 263 |
| 243 variable = lib_mirror.members['final_global_var']; | 264 variable = lib_mirror.members['final_global_var']; |
| 244 Expect.isTrue(variable is VariableMirror); | 265 Expect.isTrue(variable is VariableMirror); |
| 245 Expect.equals('final_global_var toplevel static final', | 266 Expect.equals('final_global_var type(int) toplevel static final', |
| 246 buildVariableString(variable)); | 267 buildVariableString(variable)); |
| 247 | 268 |
| 248 variable = cls_mirror.members['value']; | 269 variable = cls_mirror.members['value']; |
| 249 Expect.isTrue(variable is VariableMirror); | 270 Expect.isTrue(variable is VariableMirror); |
| 250 Expect.equals('value final', buildVariableString(variable)); | 271 Expect.equals('value type(Dynamic) final', buildVariableString(variable)); |
| 272 |
| 273 // Test type variable mirrors. |
| 274 var type_var = generic_cls_mirror.members['method'].returnType; |
| 275 Expect.isTrue(type_var is TypeVariableMirror); |
| 276 Expect.equals('GenericClass', type_var.owner.simpleName); |
| 277 Expect.equals('Object', type_var.upperBound.simpleName); |
| 278 |
| 279 // Test typedef mirrors. |
| 280 var typedef_mirror = lib_mirror.members['myFunc'].type; |
| 281 Expect.isTrue(typedef_mirror is TypedefMirror); |
| 282 Expect.equals('isolate_mirror_local_test', typedef_mirror.owner.simpleName); |
| 283 |
| 284 // Test function type mirrors. |
| 285 var func_cls_mirror = typedef_mirror.referent; |
| 286 Expect.isTrue(func_cls_mirror is FunctionTypeMirror); |
| 287 Expect.equals('void (dart:core.String)', func_cls_mirror.simpleName); |
| 288 Expect.equals('void', func_cls_mirror.returnType.simpleName); |
| 251 } | 289 } |
| 252 | 290 |
| 253 void testLibrariesMap(Map libraries) { | 291 void testLibrariesMap(Map libraries) { |
| 254 // Just look for a couple of well-known libs. | 292 // Just look for a couple of well-known libs. |
| 255 LibraryMirror core_lib = libraries['dart:core']; | 293 LibraryMirror core_lib = libraries['dart:core']; |
| 256 Expect.isTrue(core_lib is LibraryMirror); | 294 Expect.isTrue(core_lib is LibraryMirror); |
| 257 | 295 |
| 258 LibraryMirror mirror_lib = libraries['dart:mirrors']; | 296 LibraryMirror mirror_lib = libraries['dart:mirrors']; |
| 259 Expect.isTrue(mirror_lib is LibraryMirror); | 297 Expect.isTrue(mirror_lib is LibraryMirror); |
| 260 | 298 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 MyClass(this.value) {} | 394 MyClass(this.value) {} |
| 357 MyClass.named() {} | 395 MyClass.named() {} |
| 358 | 396 |
| 359 final value; | 397 final value; |
| 360 | 398 |
| 361 int method(int arg) { | 399 int method(int arg) { |
| 362 return arg + value; | 400 return arg + value; |
| 363 } | 401 } |
| 364 } | 402 } |
| 365 | 403 |
| 404 class GenericClass<T> { |
| 405 T method(int arg) { |
| 406 return null; |
| 407 } |
| 408 } |
| 409 |
| 366 void testCustomInstanceMirror(InstanceMirror mirror) { | 410 void testCustomInstanceMirror(InstanceMirror mirror) { |
| 367 Expect.isTrue(mirror.hasReflectee); | 411 Expect.isTrue(mirror.hasReflectee); |
| 368 bool saw_exception = false; | 412 bool saw_exception = false; |
| 369 try { | 413 try { |
| 370 mirror.reflectee; | 414 mirror.reflectee; |
| 371 } on MirrorException catch (me) { | 415 } on MirrorException catch (me) { |
| 372 saw_exception = true; | 416 saw_exception = true; |
| 373 } | 417 } |
| 374 Expect.isFalse(saw_exception); | 418 Expect.isFalse(saw_exception); |
| 375 Expect.equals("InstanceMirror on instance of 'MyClass'", mirror.toString()); | 419 Expect.equals("InstanceMirror on instance of 'MyClass'", mirror.toString()); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 // Test that an isolate can reflect on itself. | 532 // Test that an isolate can reflect on itself. |
| 489 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); | 533 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); |
| 490 | 534 |
| 491 testIntegerInstanceMirror(reflect(1001)); | 535 testIntegerInstanceMirror(reflect(1001)); |
| 492 testStringInstanceMirror(reflect('This\nis\na\nString')); | 536 testStringInstanceMirror(reflect('This\nis\na\nString')); |
| 493 testBoolInstanceMirror(reflect(true)); | 537 testBoolInstanceMirror(reflect(true)); |
| 494 testNullInstanceMirror(reflect(null)); | 538 testNullInstanceMirror(reflect(null)); |
| 495 testCustomInstanceMirror(reflect(new MyClass(17))); | 539 testCustomInstanceMirror(reflect(new MyClass(17))); |
| 496 testMirrorErrors(currentMirrorSystem()); | 540 testMirrorErrors(currentMirrorSystem()); |
| 497 } | 541 } |
| OLD | NEW |