Chromium Code Reviews| 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; | |
|
Brian Wilkerson
2014/06/02 20:12:58
Do we really need another test framework? I'm not
| |
| 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) { | |
|
Paul Berry
2014/06/02 20:07:34
I thought we decided that we were going to move aw
scheglov
2014/06/02 20:38:09
I have been trying different approaches to write t
| |
| 41 ClassMirror classMirror = reflectClass(type); | |
| 42 classMirror.instanceMembers.forEach((symbol, memberMirror) { | |
| 43 // we need only methods | |
| 44 if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) { | |
| 45 return; | |
| 46 } | |
| 47 // check name | |
| 48 String memberName = MirrorSystem.getName(symbol); | |
| 49 if (memberName.startsWith('test_')) { | |
| 50 String testName = memberName.substring('test_'.length); | |
| 51 test(testName, () { | |
| 52 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []); | |
| 53 _invokeSymbolIfExists(instanceMirror, #setUp); | |
| 54 var testReturn = instanceMirror.invoke(symbol, []).reflectee; | |
| 55 if (testReturn is Future) { | |
| 56 return testReturn.whenComplete(() { | |
| 57 _invokeSymbolIfExists(instanceMirror, #tearDown); | |
| 58 }); | |
| 59 } else { | |
| 60 _invokeSymbolIfExists(instanceMirror, #tearDown); | |
| 61 return testReturn; | |
| 62 } | |
| 63 }); | |
| 64 } | |
| 65 }); | |
| 66 } | |
| 67 | |
| 68 | |
| 69 void _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) { | |
| 70 try { | |
| 71 instanceMirror.invoke(symbol, []); | |
| 72 } on NoSuchMethodError catch (e) { | |
| 73 } | |
| 74 } | |
| OLD | NEW |