Chromium Code Reviews| 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 | 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 print('Test $test finished'); | |
|
cshapiro
2012/08/17 19:06:09
Is this stale debugging code?
turnidge
2012/08/17 19:44:10
Removed the prints.
| |
| 17 if (!expectedTests.contains(test)) { | 18 if (!expectedTests.contains(test)) { |
| 18 throw "Unexpected test name '$test'"; | 19 throw "Unexpected test name '$test'"; |
| 19 } | 20 } |
| 20 expectedTests.remove(test); | 21 expectedTests.remove(test); |
| 21 if (expectedTests.isEmpty()) { | 22 if (expectedTests.isEmpty()) { |
| 23 print('All dart:mirrors tests complete'); | |
|
cshapiro
2012/08/17 19:06:09
Same here.
| |
| 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; } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName); | 95 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName); |
| 94 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart')); | 96 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart')); |
| 95 Expect.equals("LibraryMirror on 'isolate_mirror_local_test'", | 97 Expect.equals("LibraryMirror on 'isolate_mirror_local_test'", |
| 96 lib_mirror.toString()); | 98 lib_mirror.toString()); |
| 97 | 99 |
| 98 // Test library invocation by calling function(123). | 100 // Test library invocation by calling function(123). |
| 99 Expect.equals(0, global_var); | 101 Expect.equals(0, global_var); |
| 100 lib_mirror.invoke('function', [ 123 ]).then( | 102 lib_mirror.invoke('function', [ 123 ]).then( |
| 101 (InstanceMirror retval) { | 103 (InstanceMirror retval) { |
| 102 Expect.equals(123, global_var); | 104 Expect.equals(123, global_var); |
| 103 Expect.equals('int', retval.getClass().simpleName); | 105 Expect.equals('int', retval.type.simpleName); |
| 104 Expect.isTrue(retval.hasReflectee); | 106 Expect.isTrue(retval.hasReflectee); |
| 105 Expect.equals(124, retval.reflectee); | 107 Expect.equals(124, retval.reflectee); |
| 106 testDone('testRootLibraryMirror'); | 108 testDone('testRootLibraryMirror'); |
| 107 }); | 109 }); |
| 108 | 110 |
| 109 // Check that the members map is complete. | 111 // Check that the members map is complete. |
| 110 List keys = lib_mirror.members().getKeys(); | 112 List keys = lib_mirror.members.getKeys(); |
| 111 sort(keys); | 113 sort(keys); |
| 112 Expect.equals('[' | 114 Expect.equals('[' |
| 113 'MyClass, ' | 115 'MyClass, ' |
| 114 'MyException, ' | 116 'MyException, ' |
| 115 'MyInterface, ' | 117 'MyInterface, ' |
| 116 'MySuperClass, ' | 118 'MySuperClass, ' |
| 117 '_stringCompare, ' | 119 '_stringCompare, ' |
| 118 'buildMethodString, ' | 120 'buildMethodString, ' |
| 119 'buildVariableString, ' | 121 'buildVariableString, ' |
| 120 'exit_port, ' | 122 'exit_port, ' |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 134 'testIntegerInstanceMirror, ' | 136 'testIntegerInstanceMirror, ' |
| 135 'testLibrariesMap, ' | 137 'testLibrariesMap, ' |
| 136 'testMirrorErrors, ' | 138 'testMirrorErrors, ' |
| 137 'testMirrorSystem, ' | 139 'testMirrorSystem, ' |
| 138 'testNullInstanceMirror, ' | 140 'testNullInstanceMirror, ' |
| 139 'testRootLibraryMirror, ' | 141 'testRootLibraryMirror, ' |
| 140 'testStringInstanceMirror]', | 142 'testStringInstanceMirror]', |
| 141 '$keys'); | 143 '$keys'); |
| 142 | 144 |
| 143 // Check that the classes map is complete. | 145 // Check that the classes map is complete. |
| 144 keys = lib_mirror.classes().getKeys(); | 146 keys = lib_mirror.classes.getKeys(); |
| 145 sort(keys); | 147 sort(keys); |
| 146 Expect.equals('[' | 148 Expect.equals('[' |
| 147 'MyClass, ' | 149 'MyClass, ' |
| 148 'MyException, ' | 150 'MyException, ' |
| 149 'MyInterface, ' | 151 'MyInterface, ' |
| 150 'MySuperClass]', | 152 'MySuperClass]', |
| 151 '$keys'); | 153 '$keys'); |
| 152 | 154 |
| 153 // Check that the functions map is complete. | 155 // Check that the functions map is complete. |
| 154 keys = lib_mirror.functions().getKeys(); | 156 keys = lib_mirror.functions.getKeys(); |
| 155 sort(keys); | 157 sort(keys); |
| 156 Expect.equals('[' | 158 Expect.equals('[' |
| 157 '_stringCompare, ' | 159 '_stringCompare, ' |
| 158 'buildMethodString, ' | 160 'buildMethodString, ' |
| 159 'buildVariableString, ' | 161 'buildVariableString, ' |
| 160 'function, ' | 162 'function, ' |
| 161 'main, ' | 163 'main, ' |
| 162 'methodWithError, ' | 164 'methodWithError, ' |
| 163 'methodWithException, ' | 165 'methodWithException, ' |
| 164 'myVar, ' | 166 'myVar, ' |
| 165 'myVar=, ' | 167 'myVar=, ' |
| 166 'sort, ' | 168 'sort, ' |
| 167 'testBoolInstanceMirror, ' | 169 'testBoolInstanceMirror, ' |
| 168 'testCustomInstanceMirror, ' | 170 'testCustomInstanceMirror, ' |
| 169 'testDone, ' | 171 'testDone, ' |
| 170 'testIntegerInstanceMirror, ' | 172 'testIntegerInstanceMirror, ' |
| 171 'testLibrariesMap, ' | 173 'testLibrariesMap, ' |
| 172 'testMirrorErrors, ' | 174 'testMirrorErrors, ' |
| 173 'testMirrorSystem, ' | 175 'testMirrorSystem, ' |
| 174 'testNullInstanceMirror, ' | 176 'testNullInstanceMirror, ' |
| 175 'testRootLibraryMirror, ' | 177 'testRootLibraryMirror, ' |
| 176 'testStringInstanceMirror]', | 178 'testStringInstanceMirror]', |
| 177 '$keys'); | 179 '$keys'); |
| 178 | 180 |
| 179 // Check that the variables map is complete. | 181 // Check that the variables map is complete. |
| 180 keys = lib_mirror.variables().getKeys(); | 182 keys = lib_mirror.variables.getKeys(); |
| 181 sort(keys); | 183 sort(keys); |
| 182 Expect.equals('[' | 184 Expect.equals('[' |
| 183 'exit_port, ' | 185 'exit_port, ' |
| 184 'expectedTests, ' | 186 'expectedTests, ' |
| 185 'final_global_var, ' | 187 'final_global_var, ' |
| 186 'global_var]', | 188 'global_var]', |
| 187 '$keys'); | 189 '$keys'); |
| 188 | 190 |
| 189 InterfaceMirror cls_mirror = lib_mirror.members()['MyClass']; | 191 ClassMirror cls_mirror = lib_mirror.members['MyClass']; |
| 190 | 192 |
| 191 // Test function mirrors. | 193 // Test function mirrors. |
| 192 MethodMirror func = lib_mirror.members()['function']; | 194 MethodMirror func = lib_mirror.members['function']; |
| 193 Expect.isTrue(func is MethodMirror); | 195 Expect.isTrue(func is MethodMirror); |
| 194 Expect.equals('function toplevel static method', buildMethodString(func)); | 196 Expect.equals('function toplevel static method', buildMethodString(func)); |
| 195 | 197 |
| 196 func = lib_mirror.members()['myVar']; | 198 func = lib_mirror.members['myVar']; |
| 197 Expect.isTrue(func is MethodMirror); | 199 Expect.isTrue(func is MethodMirror); |
| 198 Expect.equals('myVar toplevel static getter', buildMethodString(func)); | 200 Expect.equals('myVar toplevel static getter', buildMethodString(func)); |
| 199 | 201 |
| 200 func = lib_mirror.members()['myVar=']; | 202 func = lib_mirror.members['myVar=']; |
| 201 Expect.isTrue(func is MethodMirror); | 203 Expect.isTrue(func is MethodMirror); |
| 202 Expect.equals('myVar= toplevel static setter', buildMethodString(func)); | 204 Expect.equals('myVar= toplevel static setter', buildMethodString(func)); |
| 203 | 205 |
| 204 func = cls_mirror.members()['method']; | 206 func = cls_mirror.members['method']; |
| 205 Expect.isTrue(func is MethodMirror); | 207 Expect.isTrue(func is MethodMirror); |
| 206 Expect.equals('method method', buildMethodString(func)); | 208 Expect.equals('method method', buildMethodString(func)); |
| 207 | 209 |
| 208 func = cls_mirror.members()['MyClass']; | 210 func = cls_mirror.members['MyClass']; |
| 209 Expect.isTrue(func is MethodMirror); | 211 Expect.isTrue(func is MethodMirror); |
| 210 Expect.equals('MyClass constructor', buildMethodString(func)); | 212 Expect.equals('MyClass constructor', buildMethodString(func)); |
| 211 | 213 |
| 212 // Test variable mirrors. | 214 // Test variable mirrors. |
| 213 VariableMirror variable = lib_mirror.members()['global_var']; | 215 VariableMirror variable = lib_mirror.members['global_var']; |
| 214 Expect.isTrue(variable is VariableMirror); | 216 Expect.isTrue(variable is VariableMirror); |
| 215 Expect.equals('global_var toplevel static', buildVariableString(variable)); | 217 Expect.equals('global_var toplevel static', buildVariableString(variable)); |
| 216 | 218 |
| 217 variable = lib_mirror.members()['final_global_var']; | 219 variable = lib_mirror.members['final_global_var']; |
| 218 Expect.isTrue(variable is VariableMirror); | 220 Expect.isTrue(variable is VariableMirror); |
| 219 Expect.equals('final_global_var toplevel static final', | 221 Expect.equals('final_global_var toplevel static final', |
| 220 buildVariableString(variable)); | 222 buildVariableString(variable)); |
| 221 | 223 |
| 222 variable = cls_mirror.members()['value']; | 224 variable = cls_mirror.members['value']; |
| 223 Expect.isTrue(variable is VariableMirror); | 225 Expect.isTrue(variable is VariableMirror); |
| 224 Expect.equals('value final', buildVariableString(variable)); | 226 Expect.equals('value final', buildVariableString(variable)); |
| 225 } | 227 } |
| 226 | 228 |
| 227 void testLibrariesMap(Map libraries) { | 229 void testLibrariesMap(Map libraries) { |
| 228 // Just look for a couple of well-known libs. | 230 // Just look for a couple of well-known libs. |
| 229 LibraryMirror core_lib = libraries['dart:core']; | 231 LibraryMirror core_lib = libraries['dart:core']; |
| 230 Expect.isTrue(core_lib is LibraryMirror); | 232 Expect.isTrue(core_lib is LibraryMirror); |
| 231 | 233 |
| 232 LibraryMirror mirror_lib = libraries['dart:mirrors']; | 234 LibraryMirror mirror_lib = libraries['dart:mirrors']; |
| 233 Expect.isTrue(mirror_lib is LibraryMirror); | 235 Expect.isTrue(mirror_lib is LibraryMirror); |
| 234 | 236 |
| 235 // Lookup an interface from a library and make sure it is sane. | 237 // Lookup an interface from a library and make sure it is sane. |
| 236 InterfaceMirror list_intf = core_lib.members()['List']; | 238 ClassMirror list_intf = core_lib.members['List']; |
| 237 Expect.isTrue(list_intf is InterfaceMirror); | 239 Expect.isTrue(list_intf is ClassMirror); |
| 238 Expect.equals('List', list_intf.simpleName); | 240 Expect.equals('List', list_intf.simpleName); |
| 239 Expect.equals('Object', list_intf.superclass().simpleName); | 241 Expect.equals('Object', list_intf.superclass.simpleName); |
| 240 Expect.equals('ListFactory', list_intf.defaultFactory().simpleName); | 242 Expect.equals('ListFactory', list_intf.defaultFactory.simpleName); |
| 241 Expect.equals('dart:core', list_intf.library.simpleName); | 243 Expect.equals('dart:core', list_intf.library.simpleName); |
| 242 Expect.isFalse(list_intf.isClass); | 244 Expect.isFalse(list_intf.isClass); |
| 243 Expect.equals('Collection', list_intf.superinterfaces()[0].simpleName); | 245 Expect.equals('Collection', list_intf.superinterfaces[0].simpleName); |
| 244 Expect.equals("InterfaceMirror on 'List'", list_intf.toString()); | 246 Expect.equals("ClassMirror on 'List'", list_intf.toString()); |
| 245 | 247 |
| 246 // Lookup a class from a library and make sure it is sane. | 248 // Lookup a class from a library and make sure it is sane. |
| 247 InterfaceMirror oom_cls = core_lib.members()['OutOfMemoryException']; | 249 ClassMirror oom_cls = core_lib.members['OutOfMemoryException']; |
| 248 Expect.isTrue(oom_cls is InterfaceMirror); | 250 Expect.isTrue(oom_cls is ClassMirror); |
| 249 Expect.equals('OutOfMemoryException', oom_cls.simpleName); | 251 Expect.equals('OutOfMemoryException', oom_cls.simpleName); |
| 250 Expect.equals('Object', oom_cls.superclass().simpleName); | 252 Expect.equals('Object', oom_cls.superclass.simpleName); |
| 251 Expect.isTrue(oom_cls.defaultFactory() === null); | 253 Expect.isTrue(oom_cls.defaultFactory === null); |
| 252 Expect.equals('dart:core', oom_cls.library.simpleName); | 254 Expect.equals('dart:core', oom_cls.library.simpleName); |
| 253 Expect.isTrue(oom_cls.isClass); | 255 Expect.isTrue(oom_cls.isClass); |
| 254 Expect.equals('Exception', oom_cls.superinterfaces()[0].simpleName); | 256 Expect.equals('Exception', oom_cls.superinterfaces[0].simpleName); |
| 255 Expect.equals("InterfaceMirror on 'OutOfMemoryException'", | 257 Expect.equals("ClassMirror on 'OutOfMemoryException'", |
| 256 oom_cls.toString()); | 258 oom_cls.toString()); |
| 257 testDone('testLibrariesMap'); | 259 testDone('testLibrariesMap'); |
| 258 } | 260 } |
| 259 | 261 |
| 260 void testMirrorSystem(MirrorSystem mirrors) { | 262 void testMirrorSystem(MirrorSystem mirrors) { |
| 261 Expect.isTrue(mirrors.isolate.debugName.contains('main')); | 263 Expect.isTrue(mirrors.isolate.debugName.contains('main')); |
| 262 testRootLibraryMirror(mirrors.rootLibrary); | 264 testRootLibraryMirror(mirrors.isolate.rootLibrary); |
| 263 testLibrariesMap(mirrors.libraries()); | 265 testLibrariesMap(mirrors.libraries); |
| 266 Expect.equals('void', mirrors.voidType.simpleName); | |
| 267 Expect.equals('Dynamic', mirrors.dynamicType.simpleName); | |
| 264 testDone('testMirrorSystem'); | 268 testDone('testMirrorSystem'); |
| 265 } | 269 } |
| 266 | 270 |
| 267 void testIntegerInstanceMirror(InstanceMirror mirror) { | 271 void testIntegerInstanceMirror(InstanceMirror mirror) { |
| 268 Expect.equals('int', mirror.getClass().simpleName); | 272 Expect.equals('int', mirror.type.simpleName); |
| 269 Expect.isTrue(mirror.hasReflectee); | 273 Expect.isTrue(mirror.hasReflectee); |
| 270 Expect.equals(1001, mirror.reflectee); | 274 Expect.equals(1001, mirror.reflectee); |
| 271 Expect.equals("InstanceMirror on <1001>", mirror.toString()); | 275 Expect.equals("InstanceMirror on <1001>", mirror.toString()); |
| 272 | 276 |
| 273 // Invoke (mirror + mirror). | 277 // Invoke (mirror + mirror). |
| 274 mirror.invoke('+', [ mirror ]).then( | 278 mirror.invoke('+', [ mirror ]).then( |
| 275 (InstanceMirror retval) { | 279 (InstanceMirror retval) { |
| 276 Expect.equals('int', retval.getClass().simpleName); | 280 Expect.equals('int', retval.type.simpleName); |
| 277 Expect.isTrue(retval.hasReflectee); | 281 Expect.isTrue(retval.hasReflectee); |
| 278 Expect.equals(2002, retval.reflectee); | 282 Expect.equals(2002, retval.reflectee); |
| 279 testDone('testIntegerInstanceMirror'); | 283 testDone('testIntegerInstanceMirror'); |
| 280 }); | 284 }); |
| 281 } | 285 } |
| 282 | 286 |
| 283 void testStringInstanceMirror(InstanceMirror mirror) { | 287 void testStringInstanceMirror(InstanceMirror mirror) { |
| 284 Expect.equals('String', mirror.getClass().simpleName); | 288 Expect.equals('String', mirror.type.simpleName); |
| 285 Expect.isTrue(mirror.hasReflectee); | 289 Expect.isTrue(mirror.hasReflectee); |
| 286 Expect.equals('This\nis\na\nString', mirror.reflectee); | 290 Expect.equals('This\nis\na\nString', mirror.reflectee); |
| 287 Expect.equals("InstanceMirror on <'This\\nis\\na\\nString'>", | 291 Expect.equals("InstanceMirror on <'This\\nis\\na\\nString'>", |
| 288 mirror.toString()); | 292 mirror.toString()); |
| 289 | 293 |
| 290 // Invoke mirror[0]. | 294 // Invoke mirror[0]. |
| 291 mirror.invoke('[]', [ 0 ]).then( | 295 mirror.invoke('[]', [ 0 ]).then( |
| 292 (InstanceMirror retval) { | 296 (InstanceMirror retval) { |
| 293 Expect.equals('String', retval.getClass().simpleName); | 297 Expect.equals('String', retval.type.simpleName); |
| 294 Expect.isTrue(retval.hasReflectee); | 298 Expect.isTrue(retval.hasReflectee); |
| 295 Expect.equals('T', retval.reflectee); | 299 Expect.equals('T', retval.reflectee); |
| 296 testDone('testStringInstanceMirror'); | 300 testDone('testStringInstanceMirror'); |
| 297 }); | 301 }); |
| 298 } | 302 } |
| 299 | 303 |
| 300 void testBoolInstanceMirror(InstanceMirror mirror) { | 304 void testBoolInstanceMirror(InstanceMirror mirror) { |
| 301 Expect.equals('bool', mirror.getClass().simpleName); | 305 Expect.equals('bool', mirror.type.simpleName); |
| 302 Expect.isTrue(mirror.hasReflectee); | 306 Expect.isTrue(mirror.hasReflectee); |
| 303 Expect.equals(true, mirror.reflectee); | 307 Expect.equals(true, mirror.reflectee); |
| 304 Expect.equals("InstanceMirror on <true>", mirror.toString()); | 308 Expect.equals("InstanceMirror on <true>", mirror.toString()); |
| 305 testDone('testBoolInstanceMirror'); | 309 testDone('testBoolInstanceMirror'); |
| 306 } | 310 } |
| 307 | 311 |
| 308 void testNullInstanceMirror(InstanceMirror mirror) { | 312 void testNullInstanceMirror(InstanceMirror mirror) { |
| 309 // TODO(turnidge): This is returning the wrong class. Fix it. | 313 // TODO(turnidge): This is returning the wrong class. Fix it. |
| 310 Expect.equals('Object', mirror.getClass().simpleName); | 314 Expect.equals('Object', mirror.type.simpleName); |
| 311 Expect.isTrue(mirror.hasReflectee); | 315 Expect.isTrue(mirror.hasReflectee); |
| 312 Expect.equals(null, mirror.reflectee); | 316 Expect.equals(null, mirror.reflectee); |
| 313 Expect.equals("InstanceMirror on <null>", mirror.toString()); | 317 Expect.equals("InstanceMirror on <null>", mirror.toString()); |
| 314 testDone('testNullInstanceMirror'); | 318 testDone('testNullInstanceMirror'); |
| 315 } | 319 } |
| 316 | 320 |
| 317 class MySuperClass { | 321 class MySuperClass { |
| 318 } | 322 } |
| 319 | 323 |
| 320 class MyInterface { | 324 class MyInterface { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 334 Expect.isTrue(mirror.hasReflectee); | 338 Expect.isTrue(mirror.hasReflectee); |
| 335 bool saw_exception = false; | 339 bool saw_exception = false; |
| 336 try { | 340 try { |
| 337 mirror.reflectee; | 341 mirror.reflectee; |
| 338 } catch (MirrorException me) { | 342 } catch (MirrorException me) { |
| 339 saw_exception = true; | 343 saw_exception = true; |
| 340 } | 344 } |
| 341 Expect.isFalse(saw_exception); | 345 Expect.isFalse(saw_exception); |
| 342 Expect.equals("InstanceMirror on instance of 'MyClass'", mirror.toString()); | 346 Expect.equals("InstanceMirror on instance of 'MyClass'", mirror.toString()); |
| 343 | 347 |
| 344 InterfaceMirror cls = mirror.getClass(); | 348 ClassMirror cls = mirror.type; |
| 345 Expect.isTrue(cls is InterfaceMirror); | 349 Expect.isTrue(cls is ClassMirror); |
| 346 Expect.equals('MyClass', cls.simpleName); | 350 Expect.equals('MyClass', cls.simpleName); |
| 347 Expect.equals('MySuperClass', cls.superclass().simpleName); | 351 Expect.equals('MySuperClass', cls.superclass.simpleName); |
| 348 Expect.isTrue(cls.defaultFactory() === null); | 352 Expect.isTrue(cls.defaultFactory === null); |
| 349 Expect.equals('isolate_mirror_local_test', cls.library.simpleName); | 353 Expect.equals('isolate_mirror_local_test', cls.library.simpleName); |
| 350 Expect.isTrue(cls.isClass); | 354 Expect.isTrue(cls.isClass); |
| 351 Expect.equals('MyInterface', cls.superinterfaces()[0].simpleName); | 355 Expect.equals('MyInterface', cls.superinterfaces[0].simpleName); |
| 352 Expect.equals("InterfaceMirror on 'MyClass'", | 356 Expect.equals("ClassMirror on 'MyClass'", |
| 353 cls.toString()); | 357 cls.toString()); |
| 354 | 358 |
| 355 // Invoke mirror.method(1000). | 359 // Invoke mirror.method(1000). |
| 356 mirror.invoke('method', [ 1000 ]).then( | 360 mirror.invoke('method', [ 1000 ]).then( |
| 357 (InstanceMirror retval) { | 361 (InstanceMirror retval) { |
| 358 Expect.equals('int', retval.getClass().simpleName); | 362 Expect.equals('int', retval.type.simpleName); |
| 359 Expect.isTrue(retval.hasReflectee); | 363 Expect.isTrue(retval.hasReflectee); |
| 360 Expect.equals(1017, retval.reflectee); | 364 Expect.equals(1017, retval.reflectee); |
| 361 testDone('testCustomInstanceMirror'); | 365 testDone('testCustomInstanceMirror'); |
| 362 }); | 366 }); |
| 363 | 367 |
| 364 } | 368 } |
| 365 | 369 |
| 366 class MyException implements Exception { | 370 class MyException implements Exception { |
| 367 MyException(this._message); | 371 MyException(this._message); |
| 368 final String _message; | 372 final String _message; |
| 369 String toString() { return 'MyException: $_message'; } | 373 String toString() { return 'MyException: $_message'; } |
| 370 } | 374 } |
| 371 | 375 |
| 372 void methodWithException() { | 376 void methodWithException() { |
| 373 throw new MyException("from methodWithException"); | 377 throw new MyException("from methodWithException"); |
| 374 } | 378 } |
| 375 | 379 |
| 376 void methodWithError() { | 380 void methodWithError() { |
| 377 // We get a parse error when we try to run this function. | 381 // We get a parse error when we try to run this function. |
| 378 +++; | 382 +++; |
| 379 } | 383 } |
| 380 | 384 |
| 381 void testMirrorErrors(MirrorSystem mirrors) { | 385 void testMirrorErrors(MirrorSystem mirrors) { |
| 382 LibraryMirror lib_mirror = mirrors.rootLibrary; | 386 LibraryMirror lib_mirror = mirrors.isolate.rootLibrary; |
| 383 | 387 |
| 384 Future<InstanceMirror> future = | 388 Future<InstanceMirror> future = |
| 385 lib_mirror.invoke('methodWithException', []); | 389 lib_mirror.invoke('methodWithException', []); |
| 386 future.handleException( | 390 future.handleException( |
| 387 (MirroredError exc) { | 391 (MirroredError exc) { |
| 388 Expect.isTrue(exc is MirroredUncaughtExceptionError); | 392 Expect.isTrue(exc is MirroredUncaughtExceptionError); |
| 389 Expect.equals('MyException', | 393 Expect.equals('MyException', |
| 390 exc.exception_mirror.getClass().simpleName); | 394 exc.exception_mirror.type.simpleName); |
| 391 Expect.equals('MyException: from methodWithException', | 395 Expect.equals('MyException: from methodWithException', |
| 392 exc.exception_string); | 396 exc.exception_string); |
| 393 Expect.isTrue(exc.stacktrace.toString().contains( | 397 Expect.isTrue(exc.stacktrace.toString().contains( |
| 394 'isolate_mirror_local_test.dart')); | 398 'isolate_mirror_local_test.dart')); |
| 395 testDone('testMirrorErrors1'); | 399 testDone('testMirrorErrors1'); |
| 396 return true; | 400 return true; |
| 397 }); | 401 }); |
| 398 future.then( | 402 future.then( |
| 399 (InstanceMirror retval) { | 403 (InstanceMirror retval) { |
| 400 // Should not reach here. | 404 // Should not reach here. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 430 return true; | 434 return true; |
| 431 }); | 435 }); |
| 432 future3.then( | 436 future3.then( |
| 433 (InstanceMirror retval) { | 437 (InstanceMirror retval) { |
| 434 // Should not reach here. | 438 // Should not reach here. |
| 435 Expect.isTrue(false); | 439 Expect.isTrue(false); |
| 436 }); | 440 }); |
| 437 } | 441 } |
| 438 | 442 |
| 439 void main() { | 443 void main() { |
| 444 print('Starting dart:mirrors tests'); | |
|
cshapiro
2012/08/17 19:06:09
More stale code?
| |
| 440 // When all of the expected tests complete, the exit_port is closed, | 445 // When all of the expected tests complete, the exit_port is closed, |
| 441 // allowing the program to terminate. | 446 // allowing the program to terminate. |
| 442 exit_port = new ReceivePort(); | 447 exit_port = new ReceivePort(); |
| 443 expectedTests = new Set<String>.from(['testRootLibraryMirror', | 448 expectedTests = new Set<String>.from(['testRootLibraryMirror', |
| 444 'testLibrariesMap', | 449 'testLibrariesMap', |
| 445 'testMirrorSystem', | 450 'testMirrorSystem', |
| 446 'testIntegerInstanceMirror', | 451 'testIntegerInstanceMirror', |
| 447 'testStringInstanceMirror', | 452 'testStringInstanceMirror', |
| 448 'testBoolInstanceMirror', | 453 'testBoolInstanceMirror', |
| 449 'testNullInstanceMirror', | 454 'testNullInstanceMirror', |
| 450 'testCustomInstanceMirror', | 455 'testCustomInstanceMirror', |
| 451 'testMirrorErrors1', | 456 'testMirrorErrors1', |
| 452 'testMirrorErrors2', | 457 'testMirrorErrors2', |
| 453 'testMirrorErrors3']); | 458 'testMirrorErrors3']); |
| 454 | 459 |
| 455 // Test that an isolate can reflect on itself. | 460 // Test that an isolate can reflect on itself. |
| 456 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); | 461 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); |
| 457 | 462 |
| 458 MirrorSystem mirrors = currentMirrorSystem(); | 463 testIntegerInstanceMirror(reflect(1001)); |
| 459 testIntegerInstanceMirror(mirrors.mirrorOf(1001)); | 464 testStringInstanceMirror(reflect('This\nis\na\nString')); |
| 460 testStringInstanceMirror(mirrors.mirrorOf('This\nis\na\nString')); | 465 testBoolInstanceMirror(reflect(true)); |
| 461 testBoolInstanceMirror(mirrors.mirrorOf(true)); | 466 testNullInstanceMirror(reflect(null)); |
| 462 testNullInstanceMirror(mirrors.mirrorOf(null)); | 467 testCustomInstanceMirror(reflect(new MyClass(17))); |
| 463 testCustomInstanceMirror(mirrors.mirrorOf(new MyClass(17))); | |
| 464 testMirrorErrors(currentMirrorSystem()); | 468 testMirrorErrors(currentMirrorSystem()); |
| 465 } | 469 } |
| OLD | NEW |