Files
sdk/pkg/analysis_server/test/declarative_tests.dart
T
scheglov@google.com 25f3afdb4d Use mirrors and annotations to find test methods.
It was too tiresome to type test method names three times, jumping back and forth
between test class and main earlier this morning. :-)

Also, we had a discussion about tests in the office.

R=brianwilkerson@google.com, danrubel@google.com, pquitslund@google.com
BUG=

Review URL: https://codereview.chromium.org//228013003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@35067 260f80e4-7a28-3924-810f-c04153c831b5
2014-04-15 16:34:17 +00:00

52 lines
1.4 KiB
Dart

library declarative_tests;
import 'dart:mirrors';
import 'package:unittest/unittest.dart' show group, test;
/// Use [runTest] annotation to indicate that method is a test method.
/// Alternatively method name can have the `test` prefix.
const runTest = const _RunTest();
class _RunTest {
const _RunTest();
}
/// Creates a new named group of tests with the name of the given [Type], then
/// adds new tests using [addTestMethods].
addTestSuite(Type type) {
group(type.toString(), () {
addTestMethods(type);
});
}
/// Creates a new test case for the each static method with the name starting
/// with `test` or having the [runTest] annotation.
addTestMethods(Type type) {
var typeMirror = reflectClass(type);
typeMirror.staticMembers.forEach((methodSymbol, method) {
if (_isTestMethod(method)) {
var methodName = MirrorSystem.getName(methodSymbol);
test(methodName, () {
typeMirror.invoke(methodSymbol, []);
});
}
});
}
bool _isTestMethod(MethodMirror method) {
if (method.parameters.isNotEmpty) {
return false;
}
var methodSymbol = method.simpleName;
// name starts with "test"
var methodName = MirrorSystem.getName(methodSymbol);
if (methodName.startsWith('test')) {
return true;
}
// has @testMethod
return method.metadata.any((annotation) {
return identical(annotation.reflectee, runTest);
});
}