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