Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout support to test_reflective_loader. #14

Merged
merged 2 commits into from
Mar 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions lib/test_reflective_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -264,6 +274,16 @@ class ReflectiveTest {
const ReflectiveTest();
}

/**
* A marker annotation used to annotate 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
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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);
}