| 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 // VMOptions=--enable_type_checks | 8 // VMOptions=--enable_type_checks |
| 9 | 9 |
| 10 library isolate_mirror_local_test; | 10 library isolate_mirror_local_test; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 'MySuperClass, ' | 142 'MySuperClass, ' |
| 143 '_stringCompare, ' | 143 '_stringCompare, ' |
| 144 'buildMethodString, ' | 144 'buildMethodString, ' |
| 145 'buildVariableString, ' | 145 'buildVariableString, ' |
| 146 'exit_port, ' | 146 'exit_port, ' |
| 147 'expectedTests, ' | 147 'expectedTests, ' |
| 148 'final_global_var, ' | 148 'final_global_var, ' |
| 149 'function, ' | 149 'function, ' |
| 150 'global_var, ' | 150 'global_var, ' |
| 151 'main, ' | 151 'main, ' |
| 152 'methodWithError, ' | |
| 153 'methodWithException, ' | |
| 154 'myFunc, ' | 152 'myFunc, ' |
| 155 'myVar, ' | 153 'myVar, ' |
| 156 'myVar=, ' | 154 'myVar=, ' |
| 157 'sort, ' | 155 'sort, ' |
| 158 'testBoolInstanceMirror, ' | 156 'testBoolInstanceMirror, ' |
| 159 'testCustomInstanceMirror, ' | 157 'testCustomInstanceMirror, ' |
| 160 'testDone, ' | 158 'testDone, ' |
| 161 'testImplements, ' | 159 'testImplements, ' |
| 162 'testIntegerInstanceMirror, ' | 160 'testIntegerInstanceMirror, ' |
| 163 'testLibrariesMap, ' | 161 'testLibrariesMap, ' |
| 164 'testMirrorErrors, ' | |
| 165 'testMirrorSystem, ' | 162 'testMirrorSystem, ' |
| 166 'testNullInstanceMirror, ' | 163 'testNullInstanceMirror, ' |
| 167 'testRootLibraryMirror, ' | 164 'testRootLibraryMirror, ' |
| 168 'testStringInstanceMirror]', | 165 'testStringInstanceMirror]', |
| 169 '$keys'); | 166 '$keys'); |
| 170 | 167 |
| 171 // Check that the classes map is complete. | 168 // Check that the classes map is complete. |
| 172 keys = lib_mirror.classes.keys.map(MirrorSystem.getName).toList(); | 169 keys = lib_mirror.classes.keys.map(MirrorSystem.getName).toList(); |
| 173 sort(keys); | 170 sort(keys); |
| 174 print(keys); | 171 print(keys); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 195 | 192 |
| 196 // Check that the functions map is complete. | 193 // Check that the functions map is complete. |
| 197 keys = lib_mirror.functions.keys.map(MirrorSystem.getName).toList(); | 194 keys = lib_mirror.functions.keys.map(MirrorSystem.getName).toList(); |
| 198 sort(keys); | 195 sort(keys); |
| 199 Expect.equals('[' | 196 Expect.equals('[' |
| 200 '_stringCompare, ' | 197 '_stringCompare, ' |
| 201 'buildMethodString, ' | 198 'buildMethodString, ' |
| 202 'buildVariableString, ' | 199 'buildVariableString, ' |
| 203 'function, ' | 200 'function, ' |
| 204 'main, ' | 201 'main, ' |
| 205 'methodWithError, ' | |
| 206 'methodWithException, ' | |
| 207 'myVar, ' | 202 'myVar, ' |
| 208 'myVar=, ' | 203 'myVar=, ' |
| 209 'sort, ' | 204 'sort, ' |
| 210 'testBoolInstanceMirror, ' | 205 'testBoolInstanceMirror, ' |
| 211 'testCustomInstanceMirror, ' | 206 'testCustomInstanceMirror, ' |
| 212 'testDone, ' | 207 'testDone, ' |
| 213 'testImplements, ' | 208 'testImplements, ' |
| 214 'testIntegerInstanceMirror, ' | 209 'testIntegerInstanceMirror, ' |
| 215 'testLibrariesMap, ' | 210 'testLibrariesMap, ' |
| 216 'testMirrorErrors, ' | |
| 217 'testMirrorSystem, ' | 211 'testMirrorSystem, ' |
| 218 'testNullInstanceMirror, ' | 212 'testNullInstanceMirror, ' |
| 219 'testRootLibraryMirror, ' | 213 'testRootLibraryMirror, ' |
| 220 'testStringInstanceMirror]', | 214 'testStringInstanceMirror]', |
| 221 '$keys'); | 215 '$keys'); |
| 222 | 216 |
| 223 // Check that the getters map is complete. | 217 // Check that the getters map is complete. |
| 224 keys = lib_mirror.getters.keys.map(MirrorSystem.getName).toList(); | 218 keys = lib_mirror.getters.keys.map(MirrorSystem.getName).toList(); |
| 225 sort(keys); | 219 sort(keys); |
| 226 Expect.equals('[myVar]', '$keys'); | 220 Expect.equals('[myVar]', '$keys'); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 Expect.equals("InstanceMirror on null", mirror.toString()); | 417 Expect.equals("InstanceMirror on null", mirror.toString()); |
| 424 testDone('testNullInstanceMirror'); | 418 testDone('testNullInstanceMirror'); |
| 425 } | 419 } |
| 426 | 420 |
| 427 class MySuperClass { | 421 class MySuperClass { |
| 428 } | 422 } |
| 429 | 423 |
| 430 class MyInterface { | 424 class MyInterface { |
| 431 } | 425 } |
| 432 | 426 |
| 433 @notDefined | |
| 434 class MyClass extends MySuperClass implements MyInterface { | 427 class MyClass extends MySuperClass implements MyInterface { |
| 435 MyClass(this.value) {} | 428 MyClass(this.value) {} |
| 436 MyClass.named() {} | 429 MyClass.named() {} |
| 437 | 430 |
| 438 final value; | 431 final value; |
| 439 | 432 |
| 440 int method(int arg) { | 433 int method(int arg) { |
| 441 return arg + value; | 434 return arg + value; |
| 442 } | 435 } |
| 443 } | 436 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 462 | 455 |
| 463 ClassMirror cls = mirror.type; | 456 ClassMirror cls = mirror.type; |
| 464 Expect.isTrue(cls is ClassMirror); | 457 Expect.isTrue(cls is ClassMirror); |
| 465 Expect.equals(const Symbol('MyClass'), cls.simpleName); | 458 Expect.equals(const Symbol('MyClass'), cls.simpleName); |
| 466 Expect.equals(const Symbol('MySuperClass'), cls.superclass.simpleName); | 459 Expect.equals(const Symbol('MySuperClass'), cls.superclass.simpleName); |
| 467 Expect.isTrue(cls.defaultFactory == null); | 460 Expect.isTrue(cls.defaultFactory == null); |
| 468 Expect.equals(const Symbol('isolate_mirror_local_test'), | 461 Expect.equals(const Symbol('isolate_mirror_local_test'), |
| 469 cls.owner.simpleName); | 462 cls.owner.simpleName); |
| 470 Expect.isTrue(cls.isClass); | 463 Expect.isTrue(cls.isClass); |
| 471 Expect.equals(const Symbol('MyInterface'), cls.superinterfaces[0].simpleName); | 464 Expect.equals(const Symbol('MyInterface'), cls.superinterfaces[0].simpleName); |
| 472 Expect.throws(() => cls.metadata, | |
| 473 (e) => e is MirroredCompilationError, | |
| 474 'Bad metadata'); | |
| 475 Expect.equals("ClassMirror on 'MyClass'", cls.toString()); | 465 Expect.equals("ClassMirror on 'MyClass'", cls.toString()); |
| 476 | 466 |
| 477 // Invoke mirror.method(1000). | 467 // Invoke mirror.method(1000). |
| 478 mirror.invokeAsync(const Symbol('method'), [ 1000 ]).then( | 468 mirror.invokeAsync(const Symbol('method'), [ 1000 ]).then( |
| 479 (InstanceMirror retval) { | 469 (InstanceMirror retval) { |
| 480 testImplements(retval.type, #int); | 470 testImplements(retval.type, #int); |
| 481 Expect.isTrue(retval.hasReflectee); | 471 Expect.isTrue(retval.hasReflectee); |
| 482 Expect.equals(1017, retval.reflectee); | 472 Expect.equals(1017, retval.reflectee); |
| 483 testDone('testCustomInstanceMirror'); | 473 testDone('testCustomInstanceMirror'); |
| 484 }); | 474 }); |
| 485 | 475 |
| 486 } | 476 } |
| 487 | 477 |
| 488 class MyException implements Exception { | 478 class MyException implements Exception { |
| 489 MyException(this._message); | 479 MyException(this._message); |
| 490 final String _message; | 480 final String _message; |
| 491 String toString() { return 'MyException: $_message'; } | 481 String toString() { return 'MyException: $_message'; } |
| 492 } | 482 } |
| 493 | 483 |
| 494 void methodWithException() { | |
| 495 throw new MyException("from methodWithException"); | |
| 496 } | |
| 497 | |
| 498 void methodWithError() { | |
| 499 // We get a parse error when we try to run this function. | |
| 500 +++; | |
| 501 } | |
| 502 | |
| 503 void testMirrorErrors(MirrorSystem mirrors) { | |
| 504 LibraryMirror lib_mirror = mirrors.isolate.rootLibrary; | |
| 505 | |
| 506 lib_mirror.invokeAsync(const Symbol('methodWithException'), []) | |
| 507 .then((InstanceMirror retval) { | |
| 508 // Should not reach here. | |
| 509 Expect.isTrue(false); | |
| 510 }) | |
| 511 .catchError((error) { | |
| 512 Expect.isTrue(error is MyException); | |
| 513 Expect.equals('MyException: from methodWithException', | |
| 514 error.toString()); | |
| 515 testDone('testMirrorErrors1'); | |
| 516 }); | |
| 517 | |
| 518 lib_mirror.invokeAsync(const Symbol('methodWithError'), []) | |
| 519 .then((InstanceMirror retval) { | |
| 520 // Should not reach here. | |
| 521 Expect.isTrue(false); | |
| 522 }) | |
| 523 .catchError((error) { | |
| 524 Expect.isTrue(error is MirroredCompilationError); | |
| 525 Expect.isTrue(error.message.contains('unexpected token')); | |
| 526 testDone('testMirrorErrors2'); | |
| 527 }); | |
| 528 | |
| 529 // TODO(turnidge): When we call a method that doesn't exist, we | |
| 530 // should probably call noSuchMethod(). I'm adding this test to | |
| 531 // document the current behavior in the meantime. | |
| 532 lib_mirror.invokeAsync(const Symbol('methodNotFound'), []) | |
| 533 .then((InstanceMirror retval) { | |
| 534 // Should not reach here. | |
| 535 Expect.isTrue(false); | |
| 536 }) | |
| 537 .catchError((error) { | |
| 538 Expect.isTrue(error is NoSuchMethodError); | |
| 539 Expect.isTrue(error.toString().contains( | |
| 540 "No top-level method 'methodNotFound'")); | |
| 541 testDone('testMirrorErrors3'); | |
| 542 }); | |
| 543 } | |
| 544 | |
| 545 void main() { | 484 void main() { |
| 546 // When all of the expected tests complete, the exit_port is closed, | 485 // When all of the expected tests complete, the exit_port is closed, |
| 547 // allowing the program to terminate. | 486 // allowing the program to terminate. |
| 548 exit_port = new ReceivePort(); | 487 exit_port = new ReceivePort(); |
| 549 expectedTests = new Set<String>.from(['testRootLibraryMirror', | 488 expectedTests = new Set<String>.from(['testRootLibraryMirror', |
| 550 'testLibrariesMap', | 489 'testLibrariesMap', |
| 551 'testMirrorSystem', | 490 'testMirrorSystem', |
| 552 'testIntegerInstanceMirror', | 491 'testIntegerInstanceMirror', |
| 553 'testStringInstanceMirror', | 492 'testStringInstanceMirror', |
| 554 'testBoolInstanceMirror', | 493 'testBoolInstanceMirror', |
| 555 'testNullInstanceMirror', | 494 'testNullInstanceMirror', |
| 556 'testCustomInstanceMirror', | 495 'testCustomInstanceMirror']); |
| 557 'testMirrorErrors1', | |
| 558 'testMirrorErrors2', | |
| 559 'testMirrorErrors3']); | |
| 560 | 496 |
| 561 // Test that an isolate can reflect on itself. | 497 // Test that an isolate can reflect on itself. |
| 562 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); | 498 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); |
| 563 | 499 |
| 564 testIntegerInstanceMirror(reflect(1001)); | 500 testIntegerInstanceMirror(reflect(1001)); |
| 565 testStringInstanceMirror(reflect('This\nis\na\nString')); | 501 testStringInstanceMirror(reflect('This\nis\na\nString')); |
| 566 testBoolInstanceMirror(reflect(true)); | 502 testBoolInstanceMirror(reflect(true)); |
| 567 testNullInstanceMirror(reflect(null)); | 503 testNullInstanceMirror(reflect(null)); |
| 568 testCustomInstanceMirror(reflect(new MyClass(17))); | 504 testCustomInstanceMirror(reflect(new MyClass(17))); |
| 569 testMirrorErrors(currentMirrorSystem()); | |
| 570 } | 505 } |
| OLD | NEW |