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

Side by Side Diff: pkg/analysis_server/test/reflective_tests.dart

Issue 376693002: Extract testing utilities into a separate package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library reflective.tests;
6
7 import 'dart:async';
8
9 @MirrorsUsed(metaTargets: 'ReflectiveTestCase')
10 import 'dart:mirrors';
11
12 import 'package:unittest/unittest.dart';
13
14
15 /**
16 * A marker annotation used to instruct dart2js to keep reflection information
17 * for the annotated classes.
18 */
19 class ReflectiveTestCase {
20 const ReflectiveTestCase();
21 }
22
23
24 /**
25 * Runs test methods existing in the given [type].
26 *
27 * Methods with names starting with `test` are run using [test] function.
28 * Methods with names starting with `solo_test` are run using [solo_test] functi on.
29 *
30 * Each method is run with a new instance of [type].
31 * So, [type] should have a default constructor.
32 *
33 * If [type] declares method `setUp`, it methods will be invoked before any test
34 * method invocation.
35 *
36 * If [type] declares method `tearDown`, it will be invoked after any test
37 * method invocation. If method returns [Future] to test some asyncronous
38 * behavior, then `tearDown` will be invoked in `Future.complete`.
39 */
40 void runReflectiveTests(Type type) {
41 ClassMirror classMirror = reflectClass(type);
42 classMirror.instanceMembers.forEach((symbol, memberMirror) {
43 // we need only methods
44 if (memberMirror is! MethodMirror || !memberMirror.isRegularMethod) {
45 return;
46 }
47 String memberName = MirrorSystem.getName(symbol);
48 // test_
49 if (memberName.startsWith('test_')) {
50 String testName = memberName.substring('test_'.length);
51 test(testName, () {
52 return _runTest(classMirror, symbol);
53 });
54 return;
55 }
56 // solo_test_
57 if (memberName.startsWith('solo_test_')) {
58 String testName = memberName.substring('solo_test_'.length);
59 solo_test(testName, () {
60 return _runTest(classMirror, symbol);
61 });
62 }
63 });
64 }
65
66
67 _runTest(ClassMirror classMirror, Symbol symbol) {
68 InstanceMirror instanceMirror = classMirror.newInstance(new Symbol(''), []);
69 bool shouldRunTearDown = true;
70 _invokeSymbolIfExists(instanceMirror, #setUp);
71 try {
72 var testReturn = instanceMirror.invoke(symbol, []).reflectee;
73 if (testReturn is Future) {
74 shouldRunTearDown = false;
75 return testReturn.whenComplete(() {
76 _invokeTearDown(instanceMirror);
77 });
78 } else {
79 return testReturn;
80 }
81 } finally {
82 if (shouldRunTearDown) {
83 _invokeTearDown(instanceMirror);
84 }
85 }
86 }
87
88
89 void _invokeTearDown(InstanceMirror instanceMirror) {
90 _invokeSymbolIfExists(instanceMirror, #tearDown);
91 }
92
93
94 void _invokeSymbolIfExists(InstanceMirror instanceMirror, Symbol symbol) {
95 try {
96 instanceMirror.invoke(symbol, []);
97 } on NoSuchMethodError catch (e) {
98 }
99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698