| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library reflective_tests; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 @MirrorsUsed(metaTargets: 'ReflectiveTestCase') | |
| 10 import 'dart:mirrors'; | |
| 11 | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 | |
| 15 /** | |
| 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]. | |
| 26 * | |
| 27 * 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. | |
| 29 * | |
| 30 * Each method is run with a new instance of [type]. | |
| 31 * So, [type] should have a default constructor. | |
| 32 * | |
| 33 * If [type] declares method `setUp`, it methods will be invoked before any test | |
| 34 * method invocation. | |
| 35 * | |
| 36 * If [type] declares method `tearDown`, it will be invoked after any test | |
| 37 * method invocation. If method returns [Future] to test some asyncronous | |
| 38 * behavior, then `tearDown` will be invoked in `Future.complete`. | |
| 39 */ | |
| 40 void runReflectiveTests(Type type) { | |
| 41 ClassMirror classMirror = reflectClass(type); | |
| 42 String className = MirrorSystem.getName(classMirror.simpleName); | |
| 43 group(className, () { | |
| 44 classMirror.instanceMembers.forEach((symbol, memberMirror) { | |
| 45 // we need only methods | |
| 46 if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) { | |
| 47 return; | |
| 48 } | |
| 49 String memberName = MirrorSystem.getName(symbol); | |
| 50 // test_ | |
| 51 if (memberName.startsWith('test_')) { | |
| 52 String testName = memberName.substring('test_'.length); | |
| 53 test(testName, () { | |
| 54 return _runTest(classMirror, symbol); | |
| 55 }); | |
| 56 return; | |
| 57 } | |
| 58 // solo_test_ | |
| 59 if (memberName.startsWith('solo_test_')) { | |
| 60 String testName = memberName.substring('solo_test_'.length); | |
| 61 solo_test(testName, () { | |
| 62 return _runTest(classMirror, symbol); | |
| 63 }); | |
| 64 } | |
| 65 }); | |
| 66 }); | |
| 67 } | |
| 68 | |
| 69 | |
| 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) { | |
| 79 var invocationResult = null; | |
| 80 try { | |
| 81 invocationResult = instanceMirror.invoke(symbol, []).reflectee; | |
| 82 } on NoSuchMethodError catch (e) { | |
| 83 } | |
| 84 if (invocationResult is Future) { | |
| 85 return invocationResult; | |
| 86 } else { | |
| 87 return new Future.value(invocationResult); | |
| 88 } | |
| 89 } | |
| OLD | NEW |