Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 1630a63

Browse files
committed
Update with implementation from 'analyzer'.
R=brianwilkerson@google.com BUG= Review URL: https://codereview.chromium.org/2300503003 .
1 parent d7e0f5c commit 1630a63

File tree

4 files changed

+103
-18
lines changed

4 files changed

+103
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.0.4
4+
5+
- Added @failingTest, @assertFailingTest and @soloTest annotations.
6+
37
## 0.0.1
48

59
- Initial version

codereview.settings

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file is used by gcl to get repository specific information.
2+
CODE_REVIEW_SERVER: http://codereview.chromium.org
3+
VIEW_VC: https://github.com/dart-lang/test_reflective_loader/commit/
4+
CC_LIST: reviews@dartlang.org

lib/test_reflective_loader.dart

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,50 @@
44

55
library test_reflective_loader;
66

7+
import 'dart:async';
78
@MirrorsUsed(metaTargets: 'ReflectiveTest')
89
import 'dart:mirrors';
9-
import 'dart:async';
1010

1111
import 'package:unittest/unittest.dart';
1212

1313
/**
14-
* Define tests using methods existing in the given [type].
14+
* A marker annotation used to annotate overridden test methods (so we cannot
15+
* rename them to `fail_`) which are expected to fail at `assert` in the
16+
* checked mode.
17+
*/
18+
const _AssertFailingTest assertFailingTest = const _AssertFailingTest();
19+
20+
/**
21+
* A marker annotation used to annotate overridden test methods (so we cannot
22+
* rename them to `fail_`) which are expected to fail.
23+
*/
24+
const _FailingTest failingTest = const _FailingTest();
25+
26+
/**
27+
* A marker annotation used to instruct dart2js to keep reflection information
28+
* for the annotated classes.
29+
*/
30+
const ReflectiveTest reflectiveTest = const ReflectiveTest();
31+
32+
/**
33+
* Test classes annotated with this annotation are run using [solo_group].
34+
*/
35+
const _SoloTest soloTest = const _SoloTest();
36+
37+
/**
38+
* Is `true` the application is running in the checked mode.
39+
*/
40+
final bool _isCheckedMode = () {
41+
try {
42+
assert(false);
43+
return false;
44+
} catch (_) {
45+
return true;
46+
}
47+
}();
48+
49+
/**
50+
* Runs test methods existing in the given [type].
1551
*
1652
* Methods with names starting with `test` are run using [test] function.
1753
* Methods with names starting with `solo_test` are run using [solo_test] function.
@@ -23,20 +59,20 @@ import 'package:unittest/unittest.dart';
2359
* method invocation.
2460
*
2561
* If [type] declares method `tearDown`, it will be invoked after any test
26-
* method invocation. If method returns [Future] to test some asyncronous
62+
* method invocation. If method returns [Future] to test some asynchronous
2763
* behavior, then `tearDown` will be invoked in `Future.complete`.
2864
*/
2965
void defineReflectiveTests(Type type) {
3066
ClassMirror classMirror = reflectClass(type);
3167
if (!classMirror.metadata.any((InstanceMirror annotation) =>
32-
annotation.type.reflectedType == ReflectiveTest)) {
68+
annotation.type.reflectedType == ReflectiveTest)) {
3369
String name = MirrorSystem.getName(classMirror.qualifiedName);
3470
throw new Exception('Class $name must have annotation "@reflectiveTest" '
3571
'in order to be run by runReflectiveTests.');
3672
}
37-
String className = MirrorSystem.getName(classMirror.simpleName);
38-
group(className, () {
39-
classMirror.instanceMembers.forEach((symbol, memberMirror) {
73+
void runMembers() {
74+
classMirror.instanceMembers
75+
.forEach((Symbol symbol, MethodMirror memberMirror) {
4076
// we need only methods
4177
if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) {
4278
return;
@@ -45,7 +81,12 @@ void defineReflectiveTests(Type type) {
4581
// test_
4682
if (memberName.startsWith('test_')) {
4783
test(memberName, () {
48-
return _runTest(classMirror, symbol);
84+
if (_hasFailingTestAnnotation(memberMirror) ||
85+
_isCheckedMode && _hasAssertFailingTestAnnotation(memberMirror)) {
86+
return _runFailingTest(classMirror, symbol);
87+
} else {
88+
return _runTest(classMirror, symbol);
89+
}
4990
});
5091
return;
5192
}
@@ -68,19 +109,36 @@ void defineReflectiveTests(Type type) {
68109
});
69110
}
70111
});
71-
});
112+
}
113+
String className = MirrorSystem.getName(classMirror.simpleName);
114+
if (_hasAnnotationInstance(classMirror, soloTest)) {
115+
solo_group(className, runMembers);
116+
} else {
117+
group(className, runMembers);
118+
}
72119
}
73120

121+
bool _hasAnnotationInstance(DeclarationMirror declaration, instance) =>
122+
declaration.metadata.any((InstanceMirror annotation) =>
123+
identical(annotation.reflectee, instance));
124+
125+
bool _hasAssertFailingTestAnnotation(MethodMirror method) =>
126+
_hasAnnotationInstance(method, assertFailingTest);
127+
128+
bool _hasFailingTestAnnotation(MethodMirror method) =>
129+
_hasAnnotationInstance(method, failingTest);
130+
74131
Future _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) {
75132
var invocationResult = null;
133+
InstanceMirror closure;
76134
try {
77-
invocationResult = instanceMirror.invoke(symbol, []).reflectee;
135+
closure = instanceMirror.getField(symbol);
78136
} on NoSuchMethodError {}
79-
if (invocationResult is Future) {
80-
return invocationResult;
81-
} else {
82-
return new Future.value(invocationResult);
137+
138+
if (closure is ClosureMirror) {
139+
invocationResult = closure.apply([]).reflectee;
83140
}
141+
return new Future.value(invocationResult);
84142
}
85143

86144
/**
@@ -115,7 +173,26 @@ class ReflectiveTest {
115173
}
116174

117175
/**
118-
* A marker annotation used to instruct dart2js to keep reflection information
119-
* for the annotated classes.
176+
* A marker annotation used to annotate overridden test methods (so we cannot
177+
* rename them to `fail_`) which are expected to fail at `assert` in the
178+
* checked mode.
120179
*/
121-
const ReflectiveTest reflectiveTest = const ReflectiveTest();
180+
class _AssertFailingTest {
181+
const _AssertFailingTest();
182+
}
183+
184+
/**
185+
* A marker annotation used to annotate overridden test methods (so we cannot
186+
* rename them to `fail_`) which are expected to fail.
187+
*/
188+
class _FailingTest {
189+
const _FailingTest();
190+
}
191+
192+
/**
193+
* A marker annotation used to annotate a test class to run it using
194+
* [solo_group].
195+
*/
196+
class _SoloTest {
197+
const _SoloTest();
198+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: test_reflective_loader
2-
version: 0.0.3
2+
version: 0.0.4
33
description: Support for discovering tests and test suites using reflection.
44
author: Dart Team <misc@dartlang.org>
55
homepage: https://github.com/dart-lang/test_reflective_loader

0 commit comments

Comments
 (0)