From d5161a4d8739517ecfc4e40b44a6bb0bbd621d76 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Mon, 26 Mar 2018 01:28:38 -0700 Subject: [PATCH 1/2] Add timeout support to test_reflective_loader. To use it, annotate a method with @TestTimeout(t) where t is a const instance of the Timeout class from the `test` package. E.g.: @TestTimeout(const Timeout.factor(2)) --- lib/test_reflective_loader.dart | 39 ++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/test_reflective_loader.dart b/lib/test_reflective_loader.dart index 9ebaf84..4aac7b2 100644 --- a/lib/test_reflective_loader.dart +++ b/lib/test_reflective_loader.dart @@ -118,7 +118,7 @@ void defineReflectiveTests(Type type) { _hasAnnotationInstance(memberMirror, soloTest); // test_ if (memberName.startsWith('test_')) { - group.addTest(isSolo, memberName, () { + group.addTest(isSolo, memberName, memberMirror, () { if (_hasFailingTestAnnotation(memberMirror) || _isCheckedMode && _hasAssertFailingTestAnnotation(memberMirror)) { return _runFailingTest(classMirror, symbol); @@ -130,19 +130,19 @@ void defineReflectiveTests(Type type) { } // solo_test_ if (memberName.startsWith('solo_test_')) { - group.addTest(true, memberName, () { + group.addTest(true, memberName, memberMirror, () { return _runTest(classMirror, symbol); }); } // fail_test_ if (memberName.startsWith('fail_')) { - group.addTest(isSolo, memberName, () { + group.addTest(isSolo, memberName, memberMirror, () { return _runFailingTest(classMirror, symbol); }); } // solo_fail_test_ if (memberName.startsWith('solo_fail_')) { - group.addTest(true, memberName, () { + group.addTest(true, memberName, memberMirror, () { return _runFailingTest(classMirror, symbol); }); } @@ -162,7 +162,8 @@ void _addTestsIfTopLevelSuite() { if (allGroups || group.isSolo) { for (_Test test in group.tests) { if (allTests || test.isSolo) { - test_package.test(test.name, test.function); + test_package.test(test.name, test.function, + timeout: test.timeout); } } } @@ -194,6 +195,15 @@ String _combineNames(String base, String addition) { } } +Object _getAnnotationInstance(DeclarationMirror declaration, Type type) { + for (InstanceMirror annotation in declaration.metadata) { + if (annotation.reflectee.runtimeType == type) { + return annotation.reflectee; + } + } + return null; +} + bool _hasAnnotationInstance(DeclarationMirror declaration, instance) => declaration.metadata.any((InstanceMirror annotation) => identical(annotation.reflectee, instance)); @@ -264,6 +274,16 @@ class ReflectiveTest { const ReflectiveTest(); } +/** + * A marker annotation used to annotate overridden test methods with additional + * timeout information. + */ +class TestTimeout { + final test_package.Timeout timeout; + + const TestTimeout(this.timeout); +} + /** * A marker annotation used to annotate overridden test methods (so we cannot * rename them to `fail_`) which are expected to fail at `assert` in the @@ -293,9 +313,11 @@ class _Group { bool get hasSoloTest => tests.any((test) => test.isSolo); - void addTest(bool isSolo, String name, _TestFunction function) { + void addTest(bool isSolo, String name, MethodMirror memberMirror, + _TestFunction function) { String fullName = _combineNames(this.name, name); - tests.add(new _Test(isSolo, fullName, function)); + TestTimeout timeout = _getAnnotationInstance(memberMirror, TestTimeout); + tests.add(new _Test(isSolo, fullName, function, timeout?.timeout)); } } @@ -313,6 +335,7 @@ class _Test { final bool isSolo; final String name; final _TestFunction function; + final test_package.Timeout timeout; - _Test(this.isSolo, this.name, this.function); + _Test(this.isSolo, this.name, this.function, this.timeout); } From 148ce7a11efe0f99be7c3f032201f004ef32f5d7 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Mon, 26 Mar 2018 10:18:54 -0700 Subject: [PATCH 2/2] Fix comment --- lib/test_reflective_loader.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/test_reflective_loader.dart b/lib/test_reflective_loader.dart index 4aac7b2..a309948 100644 --- a/lib/test_reflective_loader.dart +++ b/lib/test_reflective_loader.dart @@ -275,8 +275,8 @@ class ReflectiveTest { } /** - * A marker annotation used to annotate overridden test methods with additional - * timeout information. + * A marker annotation used to annotate test methods with additional timeout + * information. */ class TestTimeout { final test_package.Timeout timeout;