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 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 @MirrorsUsed(metaTargets: 'ReflectiveTestCase') | 9 @MirrorsUsed(metaTargets: 'ReflectiveTestCase') |
10 import 'dart:mirrors'; | 10 import 'dart:mirrors'; |
(...skipping 27 matching lines...) Expand all Loading... |
38 * behavior, then `tearDown` will be invoked in `Future.complete`. | 38 * behavior, then `tearDown` will be invoked in `Future.complete`. |
39 */ | 39 */ |
40 void runReflectiveTests(Type type) { | 40 void runReflectiveTests(Type type) { |
41 ClassMirror classMirror = reflectClass(type); | 41 ClassMirror classMirror = reflectClass(type); |
42 classMirror.instanceMembers.forEach((symbol, memberMirror) { | 42 classMirror.instanceMembers.forEach((symbol, memberMirror) { |
43 // we need only methods | 43 // we need only methods |
44 if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) { | 44 if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) { |
45 return; | 45 return; |
46 } | 46 } |
47 String memberName = MirrorSystem.getName(symbol); | 47 String memberName = MirrorSystem.getName(symbol); |
| 48 wrapTest() { |
| 49 var result = _runTest(classMirror, symbol); |
| 50 if (result is Future) { |
| 51 // We can't just return the future because of dartbug.com/20153. So |
| 52 // use expectAsync() to ensure that the test doesn't end until the |
| 53 // future completes. TODO(paulberry): get rid of this workaround once |
| 54 // 20153 is fixed. |
| 55 result.then(expectAsync((_) {})); |
| 56 } |
| 57 } |
48 // test_ | 58 // test_ |
49 if (memberName.startsWith('test_')) { | 59 if (memberName.startsWith('test_')) { |
50 String testName = memberName.substring('test_'.length); | 60 String testName = memberName.substring('test_'.length); |
51 test(testName, () { | 61 test(testName, wrapTest); |
52 return _runTest(classMirror, symbol); | |
53 }); | |
54 return; | 62 return; |
55 } | 63 } |
56 // solo_test_ | 64 // solo_test_ |
57 if (memberName.startsWith('solo_test_')) { | 65 if (memberName.startsWith('solo_test_')) { |
58 String testName = memberName.substring('solo_test_'.length); | 66 String testName = memberName.substring('solo_test_'.length); |
59 solo_test(testName, () { | 67 solo_test(testName, wrapTest); |
60 return _runTest(classMirror, symbol); | |
61 }); | |
62 } | 68 } |
63 }); | 69 }); |
64 } | 70 } |
65 | 71 |
66 | 72 |
67 _runTest(ClassMirror classMirror, Symbol symbol) { | 73 _runTest(ClassMirror classMirror, Symbol symbol) { |
68 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []); | 74 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []); |
69 return _invokeSymbolIfExists(instanceMirror, #setUp).then( | 75 return _invokeSymbolIfExists(instanceMirror, #setUp).then( |
70 (_) => instanceMirror.invoke(symbol, []).reflectee).whenComplete( | 76 (_) => instanceMirror.invoke(symbol, []).reflectee).whenComplete( |
71 () => _invokeSymbolIfExists(instanceMirror, #tearDown)); | 77 () => _invokeSymbolIfExists(instanceMirror, #tearDown)); |
72 } | 78 } |
73 | 79 |
74 | 80 |
75 Future _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) { | 81 Future _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) { |
76 var invocationResult = null; | 82 var invocationResult = null; |
77 try { | 83 try { |
78 invocationResult = instanceMirror.invoke(symbol, []).reflectee; | 84 invocationResult = instanceMirror.invoke(symbol, []).reflectee; |
79 } on NoSuchMethodError catch (e) { | 85 } on NoSuchMethodError catch (e) { |
80 } | 86 } |
81 if (invocationResult is Future) { | 87 if (invocationResult is Future) { |
82 return invocationResult; | 88 return invocationResult; |
83 } else { | 89 } else { |
84 return new Future.value(invocationResult); | 90 return new Future.value(invocationResult); |
85 } | 91 } |
86 } | 92 } |
OLD | NEW |