OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library reflective_tests; | 5 library reflective_tests; |
6 | 6 |
| 7 @MirrorsUsed(metaTargets: 'ReflectiveTestCase') |
| 8 import 'dart:mirrors'; |
7 import 'dart:async'; | 9 import 'dart:async'; |
8 | 10 |
9 @MirrorsUsed(metaTargets: 'ReflectiveTestCase') | |
10 import 'dart:mirrors'; | |
11 | |
12 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
13 | 12 |
14 | 13 |
15 /** | 14 /** |
16 * A marker annotation used to instruct dart2js to keep reflection information | |
17 * for the annotated classes. | |
18 */ | |
19 class ReflectiveTestCase { | |
20 const ReflectiveTestCase(); | |
21 } | |
22 | |
23 | |
24 /** | |
25 * Runs test methods existing in the given [type]. | 15 * Runs test methods existing in the given [type]. |
26 * | 16 * |
27 * Methods with names starting with `test` are run using [test] function. | 17 * Methods with names starting with `test` are run using [test] function. |
28 * Methods with names starting with `solo_test` are run using [solo_test] functi
on. | 18 * Methods with names starting with `solo_test` are run using [solo_test] functi
on. |
29 * | 19 * |
30 * Each method is run with a new instance of [type]. | 20 * Each method is run with a new instance of [type]. |
31 * So, [type] should have a default constructor. | 21 * So, [type] should have a default constructor. |
32 * | 22 * |
33 * If [type] declares method `setUp`, it methods will be invoked before any test | 23 * If [type] declares method `setUp`, it methods will be invoked before any test |
34 * method invocation. | 24 * method invocation. |
(...skipping 25 matching lines...) Expand all Loading... |
60 String testName = memberName.substring('solo_test_'.length); | 50 String testName = memberName.substring('solo_test_'.length); |
61 solo_test(testName, () { | 51 solo_test(testName, () { |
62 return _runTest(classMirror, symbol); | 52 return _runTest(classMirror, symbol); |
63 }); | 53 }); |
64 } | 54 } |
65 }); | 55 }); |
66 }); | 56 }); |
67 } | 57 } |
68 | 58 |
69 | 59 |
70 _runTest(ClassMirror classMirror, Symbol symbol) { | |
71 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []); | |
72 return _invokeSymbolIfExists(instanceMirror, #setUp).then( | |
73 (_) => instanceMirror.invoke(symbol, []).reflectee).whenComplete( | |
74 () => _invokeSymbolIfExists(instanceMirror, #tearDown)); | |
75 } | |
76 | |
77 | |
78 Future _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) { | 60 Future _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) { |
79 var invocationResult = null; | 61 var invocationResult = null; |
80 try { | 62 try { |
81 invocationResult = instanceMirror.invoke(symbol, []).reflectee; | 63 invocationResult = instanceMirror.invoke(symbol, []).reflectee; |
82 } on NoSuchMethodError catch (e) { | 64 } on NoSuchMethodError catch (e) { |
83 } | 65 } |
84 if (invocationResult is Future) { | 66 if (invocationResult is Future) { |
85 return invocationResult; | 67 return invocationResult; |
86 } else { | 68 } else { |
87 return new Future.value(invocationResult); | 69 return new Future.value(invocationResult); |
88 } | 70 } |
89 } | 71 } |
| 72 |
| 73 |
| 74 _runTest(ClassMirror classMirror, Symbol symbol) { |
| 75 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []); |
| 76 return _invokeSymbolIfExists( |
| 77 instanceMirror, |
| 78 #setUp).then( |
| 79 (_) => |
| 80 instanceMirror.invoke( |
| 81 symbol, |
| 82 [ |
| 83 ]).reflectee).whenComplete( |
| 84 () => _invokeSymbolIfExists(instanceMirror, #tearDown)
); |
| 85 } |
| 86 |
| 87 |
| 88 /** |
| 89 * A marker annotation used to instruct dart2js to keep reflection information |
| 90 * for the annotated classes. |
| 91 */ |
| 92 class ReflectiveTestCase { |
| 93 const ReflectiveTestCase(); |
| 94 } |
OLD | NEW |