OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 /** This file is sourced by unitest.dart. */ | |
6 | |
7 /** | |
8 * Hooks to configure the unittest library for different platforms. This class | |
9 * implements the API in a platform-independent way. Tests that want to take | |
10 * advantage of the platform can create a subclass and override methods from | |
11 * this class. | |
12 */ | |
13 class Configuration { | |
14 TestCase currentTestCase = null; | |
15 | |
16 /** | |
17 * If true, then tests are started automatically (otherwise [runTests] | |
18 * must be called explicitly after the tests are set up. | |
19 */ | |
20 get autoStart() => true; | |
21 | |
22 /** | |
23 * Called as soon as the unittest framework becomes initialized. This is done | |
24 * even before tests are added to the test framework. It might be used to | |
25 * determine/debug errors that occur before the test harness starts executing. | |
26 */ | |
27 void onInit() {} | |
28 | |
29 /** | |
30 * Called as soon as the unittest framework starts running. Used commonly to | |
31 * tell the vm or browser that tests are still running and the process should | |
32 * wait until they are done. | |
33 */ | |
34 void onStart() {} | |
35 | |
36 /** | |
37 * Called when each test starts. Useful to show intermediate progress on | |
38 * a test suite. | |
39 */ | |
40 void onTestStart(TestCase testCase) { | |
41 currentTestCase = testCase; | |
42 } | |
43 | |
44 /** | |
45 * Called when each test is completed. Useful to show intermediate progress on | |
46 * a test suite. | |
47 */ | |
48 void onTestResult(TestCase testCase) { | |
49 currentTestCase = null; | |
50 } | |
51 | |
52 /** | |
53 * Can be called by tests to log status. Tests should use this | |
54 * instead of print. Subclasses should not override this; they | |
55 * should instead override logMessage which is passed the test case. | |
56 */ | |
57 void log(String message) { | |
58 if (currentTestCase == null || _currentTest >= _tests.length || | |
59 currentTestCase.id != _tests[_currentTest].id) { | |
60 // Before or after tests run, or with a mismatch between what the | |
61 // config and the test harness think is the current test. In this | |
62 // case we pass null for the test case reference and let the config | |
63 // decide what to do with this. | |
64 logMessage(null, message); | |
65 } else { | |
66 logMessage(currentTestCase, message); | |
67 } | |
68 } | |
69 | |
70 /** | |
71 * Handles the logging of messages by a test case. The default in | |
72 * this base configuration is to call print(); | |
73 */ | |
74 void logMessage(TestCase testCase, String message) { | |
75 print(message); | |
76 } | |
77 | |
78 /** | |
79 * Called with the result of all test cases. The default implementation prints | |
80 * the result summary using the built-in [print] command. Browser tests | |
81 * commonly override this to reformat the output. | |
82 * | |
83 * When [uncaughtError] is not null, it contains an error that occured outside | |
84 * of tests (e.g. setting up the test). | |
85 */ | |
86 void onDone(int passed, int failed, int errors, List<TestCase> results, | |
87 String uncaughtError) { | |
88 // Print each test's result. | |
89 for (final t in _tests) { | |
90 print('${t.result.toUpperCase()}: ${t.description}'); | |
91 | |
92 if (t.message != '') { | |
93 print(_indent(t.message)); | |
94 } | |
95 | |
96 if (t.stackTrace != null && t.stackTrace != '') { | |
97 print(_indent(t.stackTrace)); | |
98 } | |
99 } | |
100 | |
101 // Show the summary. | |
102 print(''); | |
103 | |
104 var success = false; | |
105 if (passed == 0 && failed == 0 && errors == 0) { | |
106 print('No tests found.'); | |
107 // This is considered a failure too: if this happens you probably have a | |
108 // bug in your unit tests, unless you are filtering. | |
109 if (filter != null) { | |
110 success = true; | |
111 } | |
112 } else if (failed == 0 && errors == 0 && uncaughtError == null) { | |
113 print('All $passed tests passed.'); | |
114 success = true; | |
115 } else { | |
116 if (uncaughtError != null) { | |
117 print('Top-level uncaught error: $uncaughtError'); | |
118 } | |
119 print('$passed PASSED, $failed FAILED, $errors ERRORS'); | |
120 } | |
121 | |
122 // An exception is used by the test infrastructure to detect failure. | |
123 if (!success) throw new Exception("Some tests failed."); | |
124 } | |
125 | |
126 String _indent(String str) { | |
127 // TODO(nweiz): Use this simpler code once issue 2980 is fixed. | |
128 // return str.replaceAll(const RegExp("^", multiLine: true), " "); | |
129 | |
130 return Strings.join(str.split("\n").map((line) => " $line"), "\n"); | |
131 } | |
132 | |
133 /** Handle errors that happen outside the tests. */ | |
134 // TODO(vsm): figure out how to expose the stack trace here | |
135 // Currently e.message works in dartium, but not in dartc. | |
136 handleExternalError(e, String message) => | |
137 _reportTestError('$message\nCaught $e', ''); | |
138 } | |
OLD | NEW |