| OLD | NEW |
| (Empty) |
| 1 library declarative_tests; | |
| 2 | |
| 3 import 'dart:mirrors'; | |
| 4 | |
| 5 import 'package:unittest/unittest.dart' show group, test; | |
| 6 | |
| 7 /** | |
| 8 * Use [runTest] annotation to indicate that method is a test method. | |
| 9 * Alternatively method name can have the `test` prefix. | |
| 10 */ | |
| 11 const runTest = const _RunTest(); | |
| 12 | |
| 13 class _RunTest { | |
| 14 const _RunTest(); | |
| 15 } | |
| 16 | |
| 17 /** | |
| 18 * Creates a new named group of tests with the name of the given [Type], then | |
| 19 * adds new tests using [addTestMethods]. | |
| 20 */ | |
| 21 addTestSuite(Type type) { | |
| 22 group(type.toString(), () { | |
| 23 addTestMethods(type); | |
| 24 }); | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Creates a new test case for the each static method with the name starting | |
| 29 * with `test` or having the [runTest] annotation. | |
| 30 */ | |
| 31 addTestMethods(Type type) { | |
| 32 var typeMirror = reflectClass(type); | |
| 33 typeMirror.staticMembers.forEach((methodSymbol, method) { | |
| 34 if (_isTestMethod(method)) { | |
| 35 var methodName = MirrorSystem.getName(methodSymbol); | |
| 36 test(methodName, () { | |
| 37 typeMirror.invoke(methodSymbol, []); | |
| 38 }); | |
| 39 } | |
| 40 }); | |
| 41 } | |
| 42 | |
| 43 bool _isTestMethod(MethodMirror method) { | |
| 44 if (method.parameters.isNotEmpty) { | |
| 45 return false; | |
| 46 } | |
| 47 var methodSymbol = method.simpleName; | |
| 48 // name starts with "test" | |
| 49 var methodName = MirrorSystem.getName(methodSymbol); | |
| 50 if (methodName.startsWith('test')) { | |
| 51 return true; | |
| 52 } | |
| 53 // has @testMethod | |
| 54 return method.metadata.any((annotation) { | |
| 55 return identical(annotation.reflectee, runTest); | |
| 56 }); | |
| 57 } | |
| OLD | NEW |