Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Unified Diff: pkg/analysis_server/test/declarative_tests.dart

Issue 228013003: Use mirrors and annotations to find test methods. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/protocol_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/test/declarative_tests.dart
diff --git a/pkg/analysis_server/test/declarative_tests.dart b/pkg/analysis_server/test/declarative_tests.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f6a6e68dc879246db28bc49db53a2013bbb8e466
--- /dev/null
+++ b/pkg/analysis_server/test/declarative_tests.dart
@@ -0,0 +1,51 @@
+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);
+ });
+}
« no previous file with comments | « no previous file | pkg/analysis_server/test/protocol_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698