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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName); | 110 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName); |
111 Expect.equals('isolate_mirror_local_test', lib_mirror.qualifiedName); | 111 Expect.equals('isolate_mirror_local_test', lib_mirror.qualifiedName); |
112 Expect.equals(null, lib_mirror.owner); | 112 Expect.equals(null, lib_mirror.owner); |
113 Expect.isFalse(lib_mirror.isPrivate); | 113 Expect.isFalse(lib_mirror.isPrivate); |
114 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart')); | 114 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart')); |
115 Expect.equals("LibraryMirror on 'isolate_mirror_local_test'", | 115 Expect.equals("LibraryMirror on 'isolate_mirror_local_test'", |
116 lib_mirror.toString()); | 116 lib_mirror.toString()); |
117 | 117 |
118 // Test library invocation by calling function(123). | 118 // Test library invocation by calling function(123). |
119 Expect.equals(0, global_var); | 119 Expect.equals(0, global_var); |
120 lib_mirror.invoke('function', [ 123 ]).then( | 120 lib_mirror.invokeAsync('function', [123]).then( |
121 (InstanceMirror retval) { | 121 (InstanceMirror retval) { |
122 Expect.equals(123, global_var); | 122 Expect.equals(123, global_var); |
123 Expect.equals('int', retval.type.simpleName); | 123 Expect.equals('int', retval.type.simpleName); |
124 Expect.isTrue(retval.hasReflectee); | 124 Expect.isTrue(retval.hasReflectee); |
125 Expect.equals(124, retval.reflectee); | 125 Expect.equals(124, retval.reflectee); |
126 testDone('testRootLibraryMirror'); | 126 testDone('testRootLibraryMirror'); |
127 }); | 127 }); |
128 | 128 |
129 // Check that the members map is complete. | 129 // Check that the members map is complete. |
130 List keys = lib_mirror.members.keys.toList(); | 130 List keys = lib_mirror.members.keys.toList(); |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 testDone('testMirrorSystem'); | 336 testDone('testMirrorSystem'); |
337 } | 337 } |
338 | 338 |
339 void testIntegerInstanceMirror(InstanceMirror mirror) { | 339 void testIntegerInstanceMirror(InstanceMirror mirror) { |
340 Expect.equals('int', mirror.type.simpleName); | 340 Expect.equals('int', mirror.type.simpleName); |
341 Expect.isTrue(mirror.hasReflectee); | 341 Expect.isTrue(mirror.hasReflectee); |
342 Expect.equals(1001, mirror.reflectee); | 342 Expect.equals(1001, mirror.reflectee); |
343 Expect.equals("InstanceMirror on <1001>", mirror.toString()); | 343 Expect.equals("InstanceMirror on <1001>", mirror.toString()); |
344 | 344 |
345 // Invoke (mirror + mirror). | 345 // Invoke (mirror + mirror). |
346 mirror.invoke('+', [ mirror ]).then( | 346 mirror.invokeAsync('+', [ mirror ]).then( |
347 (InstanceMirror retval) { | 347 (InstanceMirror retval) { |
348 Expect.equals('int', retval.type.simpleName); | 348 Expect.equals('int', retval.type.simpleName); |
349 Expect.isTrue(retval.hasReflectee); | 349 Expect.isTrue(retval.hasReflectee); |
350 Expect.equals(2002, retval.reflectee); | 350 Expect.equals(2002, retval.reflectee); |
351 testDone('testIntegerInstanceMirror'); | 351 testDone('testIntegerInstanceMirror'); |
352 }); | 352 }); |
353 } | 353 } |
354 | 354 |
355 void testStringInstanceMirror(InstanceMirror mirror) { | 355 void testStringInstanceMirror(InstanceMirror mirror) { |
356 Expect.equals('String', mirror.type.simpleName); | 356 Expect.equals('String', mirror.type.simpleName); |
357 Expect.isTrue(mirror.hasReflectee); | 357 Expect.isTrue(mirror.hasReflectee); |
358 Expect.equals('This\nis\na\nString', mirror.reflectee); | 358 Expect.equals('This\nis\na\nString', mirror.reflectee); |
359 Expect.equals("InstanceMirror on <'This\\nis\\na\\nString'>", | 359 Expect.equals("InstanceMirror on <'This\\nis\\na\\nString'>", |
360 mirror.toString()); | 360 mirror.toString()); |
361 | 361 |
362 // Invoke mirror[0]. | 362 // Invoke mirror[0]. |
363 mirror.invoke('[]', [ 0 ]).then( | 363 mirror.invokeAsync('[]', [ 0 ]).then( |
364 (InstanceMirror retval) { | 364 (InstanceMirror retval) { |
365 Expect.equals('String', retval.type.simpleName); | 365 Expect.equals('String', retval.type.simpleName); |
366 Expect.isTrue(retval.hasReflectee); | 366 Expect.isTrue(retval.hasReflectee); |
367 Expect.equals('T', retval.reflectee); | 367 Expect.equals('T', retval.reflectee); |
368 testDone('testStringInstanceMirror'); | 368 testDone('testStringInstanceMirror'); |
369 }); | 369 }); |
370 } | 370 } |
371 | 371 |
372 void testBoolInstanceMirror(InstanceMirror mirror) { | 372 void testBoolInstanceMirror(InstanceMirror mirror) { |
373 Expect.equals('bool', mirror.type.simpleName); | 373 Expect.equals('bool', mirror.type.simpleName); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 Expect.equals('MyClass', cls.simpleName); | 425 Expect.equals('MyClass', cls.simpleName); |
426 Expect.equals('MySuperClass', cls.superclass.simpleName); | 426 Expect.equals('MySuperClass', cls.superclass.simpleName); |
427 Expect.isTrue(cls.defaultFactory == null); | 427 Expect.isTrue(cls.defaultFactory == null); |
428 Expect.equals('isolate_mirror_local_test', cls.owner.simpleName); | 428 Expect.equals('isolate_mirror_local_test', cls.owner.simpleName); |
429 Expect.isTrue(cls.isClass); | 429 Expect.isTrue(cls.isClass); |
430 Expect.equals('MyInterface', cls.superinterfaces[0].simpleName); | 430 Expect.equals('MyInterface', cls.superinterfaces[0].simpleName); |
431 Expect.equals("ClassMirror on 'MyClass'", | 431 Expect.equals("ClassMirror on 'MyClass'", |
432 cls.toString()); | 432 cls.toString()); |
433 | 433 |
434 // Invoke mirror.method(1000). | 434 // Invoke mirror.method(1000). |
435 mirror.invoke('method', [ 1000 ]).then( | 435 mirror.invokeAsync('method', [ 1000 ]).then( |
436 (InstanceMirror retval) { | 436 (InstanceMirror retval) { |
437 Expect.equals('int', retval.type.simpleName); | 437 Expect.equals('int', retval.type.simpleName); |
438 Expect.isTrue(retval.hasReflectee); | 438 Expect.isTrue(retval.hasReflectee); |
439 Expect.equals(1017, retval.reflectee); | 439 Expect.equals(1017, retval.reflectee); |
440 testDone('testCustomInstanceMirror'); | 440 testDone('testCustomInstanceMirror'); |
441 }); | 441 }); |
442 | 442 |
443 } | 443 } |
444 | 444 |
445 class MyException implements Exception { | 445 class MyException implements Exception { |
446 MyException(this._message); | 446 MyException(this._message); |
447 final String _message; | 447 final String _message; |
448 String toString() { return 'MyException: $_message'; } | 448 String toString() { return 'MyException: $_message'; } |
449 } | 449 } |
450 | 450 |
451 void methodWithException() { | 451 void methodWithException() { |
452 throw new MyException("from methodWithException"); | 452 throw new MyException("from methodWithException"); |
453 } | 453 } |
454 | 454 |
455 void methodWithError() { | 455 void methodWithError() { |
456 // We get a parse error when we try to run this function. | 456 // We get a parse error when we try to run this function. |
457 +++; | 457 +++; |
458 } | 458 } |
459 | 459 |
460 void testMirrorErrors(MirrorSystem mirrors) { | 460 void testMirrorErrors(MirrorSystem mirrors) { |
461 LibraryMirror lib_mirror = mirrors.isolate.rootLibrary; | 461 LibraryMirror lib_mirror = mirrors.isolate.rootLibrary; |
462 | 462 |
463 lib_mirror.invoke('methodWithException', []) | 463 lib_mirror.invokeAsync('methodWithException', []) |
464 .then((InstanceMirror retval) { | 464 .then((InstanceMirror retval) { |
465 // Should not reach here. | 465 // Should not reach here. |
466 Expect.isTrue(false); | 466 Expect.isTrue(false); |
467 }) | 467 }) |
468 .catchError((exc) { | 468 .catchError((exc) { |
469 Expect.isTrue(exc.error is MirroredUncaughtExceptionError); | 469 Expect.isTrue(exc.error is MirroredUncaughtExceptionError); |
470 Expect.equals('MyException', | 470 Expect.equals('MyException', |
471 exc.error.exception_mirror.type.simpleName); | 471 exc.error.exception_mirror.type.simpleName); |
472 Expect.equals('MyException: from methodWithException', | 472 Expect.equals('MyException: from methodWithException', |
473 exc.error.exception_string); | 473 exc.error.exception_string); |
474 Expect.isTrue(exc.error.stacktrace.toString().contains( | 474 Expect.isTrue(exc.error.stacktrace.toString().contains( |
475 'isolate_mirror_local_test.dart')); | 475 'isolate_mirror_local_test.dart')); |
476 testDone('testMirrorErrors1'); | 476 testDone('testMirrorErrors1'); |
477 }); | 477 }); |
478 | 478 |
479 lib_mirror.invoke('methodWithError', []) | 479 lib_mirror.invokeAsync('methodWithError', []) |
480 .then((InstanceMirror retval) { | 480 .then((InstanceMirror retval) { |
481 // Should not reach here. | 481 // Should not reach here. |
482 Expect.isTrue(false); | 482 Expect.isTrue(false); |
483 }) | 483 }) |
484 .catchError((exc) { | 484 .catchError((exc) { |
485 Expect.isTrue(exc.error is MirroredCompilationError); | 485 Expect.isTrue(exc.error is MirroredCompilationError); |
486 Expect.isTrue(exc.error.message.contains('unexpected token')); | 486 Expect.isTrue(exc.error.message.contains('unexpected token')); |
487 testDone('testMirrorErrors2'); | 487 testDone('testMirrorErrors2'); |
488 }); | 488 }); |
489 | 489 |
490 // TODO(turnidge): When we call a method that doesn't exist, we | 490 // TODO(turnidge): When we call a method that doesn't exist, we |
491 // should probably call noSuchMethod(). I'm adding this test to | 491 // should probably call noSuchMethod(). I'm adding this test to |
492 // document the current behavior in the meantime. | 492 // document the current behavior in the meantime. |
493 lib_mirror.invoke('methodNotFound', []) | 493 lib_mirror.invokeAsync('methodNotFound', []) |
494 .then((InstanceMirror retval) { | 494 .then((InstanceMirror retval) { |
495 // Should not reach here. | 495 // Should not reach here. |
496 Expect.isTrue(false); | 496 Expect.isTrue(false); |
497 }) | 497 }) |
498 .catchError((exc) { | 498 .catchError((exc) { |
499 Expect.isTrue(exc.error is MirroredCompilationError); | 499 Expect.isTrue(exc.error is MirroredCompilationError); |
500 Expect.isTrue(exc.error.message.contains( | 500 Expect.isTrue(exc.error.message.contains( |
501 "did not find top-level function 'methodNotFound'")); | 501 "did not find top-level function 'methodNotFound'")); |
502 testDone('testMirrorErrors3'); | 502 testDone('testMirrorErrors3'); |
503 }); | 503 }); |
(...skipping 18 matching lines...) Expand all Loading... |
522 // Test that an isolate can reflect on itself. | 522 // Test that an isolate can reflect on itself. |
523 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); | 523 mirrorSystemOf(exit_port.toSendPort()).then(testMirrorSystem); |
524 | 524 |
525 testIntegerInstanceMirror(reflect(1001)); | 525 testIntegerInstanceMirror(reflect(1001)); |
526 testStringInstanceMirror(reflect('This\nis\na\nString')); | 526 testStringInstanceMirror(reflect('This\nis\na\nString')); |
527 testBoolInstanceMirror(reflect(true)); | 527 testBoolInstanceMirror(reflect(true)); |
528 testNullInstanceMirror(reflect(null)); | 528 testNullInstanceMirror(reflect(null)); |
529 testCustomInstanceMirror(reflect(new MyClass(17))); | 529 testCustomInstanceMirror(reflect(new MyClass(17))); |
530 testMirrorErrors(currentMirrorSystem()); | 530 testMirrorErrors(currentMirrorSystem()); |
531 } | 531 } |
OLD | NEW |