| 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 21 matching lines...) Expand all Loading... |
| 32 * | 32 * |
| 33 * If [type] declares method `setUp`, it methods will be invoked before any test | 33 * If [type] declares method `setUp`, it methods will be invoked before any test |
| 34 * method invocation. | 34 * method invocation. |
| 35 * | 35 * |
| 36 * If [type] declares method `tearDown`, it will be invoked after any test | 36 * If [type] declares method `tearDown`, it will be invoked after any test |
| 37 * method invocation. If method returns [Future] to test some asyncronous | 37 * method invocation. If method returns [Future] to test some asyncronous |
| 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 String className = MirrorSystem.getName(classMirror.simpleName); |
| 43 // we need only methods | 43 group(className, () { |
| 44 if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) { | 44 classMirror.instanceMembers.forEach((symbol, memberMirror) { |
| 45 return; | 45 // we need only methods |
| 46 } | 46 if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) { |
| 47 String memberName = MirrorSystem.getName(symbol); | 47 return; |
| 48 // test_ | 48 } |
| 49 if (memberName.startsWith('test_')) { | 49 String memberName = MirrorSystem.getName(symbol); |
| 50 String testName = memberName.substring('test_'.length); | 50 // test_ |
| 51 test(testName, () { | 51 if (memberName.startsWith('test_')) { |
| 52 return _runTest(classMirror, symbol); | 52 String testName = memberName.substring('test_'.length); |
| 53 }); | 53 test(testName, () { |
| 54 return; | 54 return _runTest(classMirror, symbol); |
| 55 } | 55 }); |
| 56 // solo_test_ | 56 return; |
| 57 if (memberName.startsWith('solo_test_')) { | 57 } |
| 58 String testName = memberName.substring('solo_test_'.length); | 58 // solo_test_ |
| 59 solo_test(testName, () { | 59 if (memberName.startsWith('solo_test_')) { |
| 60 return _runTest(classMirror, symbol); | 60 String testName = memberName.substring('solo_test_'.length); |
| 61 }); | 61 solo_test(testName, () { |
| 62 } | 62 return _runTest(classMirror, symbol); |
| 63 }); |
| 64 } |
| 65 }); |
| 63 }); | 66 }); |
| 64 } | 67 } |
| 65 | 68 |
| 66 | 69 |
| 67 _runTest(ClassMirror classMirror, Symbol symbol) { | 70 _runTest(ClassMirror classMirror, Symbol symbol) { |
| 68 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []); | 71 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []); |
| 69 return _invokeSymbolIfExists(instanceMirror, #setUp).then( | 72 return _invokeSymbolIfExists(instanceMirror, #setUp).then( |
| 70 (_) => instanceMirror.invoke(symbol, []).reflectee).whenComplete( | 73 (_) => instanceMirror.invoke(symbol, []).reflectee).whenComplete( |
| 71 () => _invokeSymbolIfExists(instanceMirror, #tearDown)); | 74 () => _invokeSymbolIfExists(instanceMirror, #tearDown)); |
| 72 } | 75 } |
| 73 | 76 |
| 74 | 77 |
| 75 Future _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) { | 78 Future _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) { |
| 76 var invocationResult = null; | 79 var invocationResult = null; |
| 77 try { | 80 try { |
| 78 invocationResult = instanceMirror.invoke(symbol, []).reflectee; | 81 invocationResult = instanceMirror.invoke(symbol, []).reflectee; |
| 79 } on NoSuchMethodError catch (e) { | 82 } on NoSuchMethodError catch (e) { |
| 80 } | 83 } |
| 81 if (invocationResult is Future) { | 84 if (invocationResult is Future) { |
| 82 return invocationResult; | 85 return invocationResult; |
| 83 } else { | 86 } else { |
| 84 return new Future.value(invocationResult); | 87 return new Future.value(invocationResult); |
| 85 } | 88 } |
| 86 } | 89 } |
| OLD | NEW |