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