Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of unittest; | 5 library unittest.simple_configuration; |
|
nweiz
2015/01/26 22:44:10
I don't understand the changes to this code or why
kevmoo
2015/01/27 00:11:30
Done.
| |
| 6 | 6 |
| 7 // A custom failure handler for [expect] that routes expect failures | 7 import 'dart:isolate'; |
| 8 // to the config. | |
| 9 class _ExpectFailureHandler extends DefaultFailureHandler { | |
| 10 final SimpleConfiguration _config; | |
| 11 | 8 |
| 12 _ExpectFailureHandler(this._config); | 9 import '../unittest.dart'; |
| 13 | 10 import 'configuration.dart'; |
| 14 void fail(String reason) { | 11 import 'utils.dart'; |
| 15 _config.onExpectFailure(reason); | |
| 16 } | |
| 17 } | |
| 18 | 12 |
| 19 /// Hooks to configure the unittest library for different platforms. This class | 13 /// Hooks to configure the unittest library for different platforms. This class |
| 20 /// implements the API in a platform-independent way. Tests that want to take | 14 /// implements the API in a platform-independent way. Tests that want to take |
| 21 /// advantage of the platform can create a subclass and override methods from | 15 /// advantage of the platform can create a subclass and override methods from |
| 22 /// this class. | 16 /// this class. |
| 23 class SimpleConfiguration extends Configuration { | 17 class SimpleConfiguration extends Configuration { |
| 24 // The VM won't shut down if a receive port is open. Use this to make sure | 18 // The VM won't shut down if a receive port is open. Use this to make sure |
| 25 // we correctly wait for asynchronous tests. | 19 // we correctly wait for asynchronous tests. |
| 26 ReceivePort _receivePort; | 20 ReceivePort _receivePort; |
| 27 | 21 |
| 28 /// Subclasses can override this with something useful for diagnostics. | 22 /// Subclasses can override this with something useful for diagnostics. |
| 29 /// Particularly useful in cases where we have parent/child configurations | 23 /// Particularly useful in cases where we have parent/child configurations |
| 30 /// such as layout tests. | 24 /// such as layout tests. |
| 31 String get name => 'Configuration'; | 25 String get name => 'Configuration'; |
| 32 | 26 |
| 33 bool get autoStart => true; | 27 bool get autoStart => true; |
| 34 | 28 |
| 35 /// If true (the default), throw an exception at the end if any tests failed. | 29 /// If true (the default), throw an exception at the end if any tests failed. |
| 36 bool throwOnTestFailures = true; | 30 bool throwOnTestFailures = true; |
| 37 | 31 |
| 38 /// If true (the default), then tests will stop after the first failed | |
| 39 /// [expect]. If false, failed [expect]s will not cause the test | |
| 40 /// to stop (other exceptions will still terminate the test). | |
| 41 bool stopTestOnExpectFailure = true; | |
| 42 | |
| 43 // If stopTestOnExpectFailure is false, we need to capture failures, which | |
| 44 // we do with this List. | |
| 45 final _testLogBuffer = <Pair<String, StackTrace>>[]; | |
| 46 | |
| 47 /// The constructor sets up a failure handler for [expect] that redirects | 32 /// The constructor sets up a failure handler for [expect] that redirects |
| 48 /// [expect] failures to [onExpectFailure]. | 33 /// [expect] failures to [onExpectFailure]. |
| 49 SimpleConfiguration() : super.blank() { | 34 SimpleConfiguration() : super.blank(); |
| 50 configureExpectFailureHandler(new _ExpectFailureHandler(this)); | |
| 51 } | |
| 52 | 35 |
| 53 void onInit() { | 36 void onInit() { |
| 54 // For Dart internal tests, we don't want stack frame filtering. | 37 // For Dart internal tests, we don't want stack frame filtering. |
| 55 // We turn it off here in the default config, but by default turn | 38 // We turn it off here in the default config, but by default turn |
| 56 // it back on in the vm and html configs. | 39 // it back on in the vm and html configs. |
| 57 filterStacks = false; | 40 filterStacks = false; |
| 58 _receivePort = new ReceivePort(); | 41 _receivePort = new ReceivePort(); |
| 59 _postMessage('unittest-suite-wait-for-done'); | 42 _postMessage('unittest-suite-wait-for-done'); |
| 60 } | 43 } |
| 61 | 44 |
| 62 /// Called when each test starts. Useful to show intermediate progress on | 45 /// Called when each test starts. Useful to show intermediate progress on |
| 63 /// a test suite. Derived classes should call this first before their own | 46 /// a test suite. Derived classes should call this first before their own |
| 64 /// override code. | 47 /// override code. |
| 65 void onTestStart(TestCase testCase) { | 48 void onTestStart(TestCase testCase) { |
| 66 assert(testCase != null); | 49 assert(testCase != null); |
| 67 _testLogBuffer.clear(); | |
| 68 } | |
| 69 | |
| 70 /// Called when each test is first completed. Useful to show intermediate | |
| 71 /// progress on a test suite. Derived classes should call this first | |
| 72 /// before their own override code. | |
| 73 void onTestResult(TestCase testCase) { | |
| 74 assert(testCase != null); | |
| 75 if (!stopTestOnExpectFailure && _testLogBuffer.length > 0) { | |
| 76 // Write the message/stack pairs up to the last pairs. | |
| 77 var reason = new StringBuffer(); | |
| 78 for (var reasonAndTrace | |
| 79 in _testLogBuffer.take(_testLogBuffer.length - 1)) { | |
| 80 reason.write(reasonAndTrace.first); | |
| 81 reason.write('\n'); | |
| 82 reason.write(reasonAndTrace.last); | |
| 83 reason.write('\n'); | |
| 84 } | |
| 85 var lastReasonAndTrace = _testLogBuffer.last; | |
| 86 // Write the last message. | |
| 87 reason.write(lastReasonAndTrace.first); | |
| 88 if (testCase.result == PASS) { | |
| 89 testCase._result = FAIL; | |
| 90 testCase._message = reason.toString(); | |
| 91 // Use the last stack as the overall failure stack. | |
| 92 testCase._stackTrace = lastReasonAndTrace.last; | |
| 93 } else { | |
| 94 // Add the last stack to the message; we have a further stack | |
| 95 // caused by some other failure. | |
| 96 reason.write(lastReasonAndTrace.last); | |
| 97 reason.write('\n'); | |
| 98 // Add the existing reason to the end of the expect log to | |
| 99 // create the final message. | |
| 100 testCase._message = '${reason.toString()}\n${testCase._message}'; | |
| 101 } | |
| 102 } | |
| 103 } | 50 } |
| 104 | 51 |
| 105 void onTestResultChanged(TestCase testCase) { | 52 void onTestResultChanged(TestCase testCase) { |
| 106 assert(testCase != null); | 53 assert(testCase != null); |
| 107 } | 54 } |
| 108 | 55 |
| 109 /// Handles the logging of messages by a test case. The default in | 56 /// Handles the logging of messages by a test case. The default in |
| 110 /// this base configuration is to call print(); | 57 /// this base configuration is to call print(); |
| 111 void onLogMessage(TestCase testCase, String message) { | 58 void onLogMessage(TestCase testCase, String message) { |
| 112 print(message); | 59 print(message); |
| 113 } | 60 } |
| 114 | 61 |
| 115 /// Handles failures from expect(). The default in | |
| 116 /// this base configuration is to throw an exception; | |
| 117 void onExpectFailure(String reason) { | |
| 118 if (stopTestOnExpectFailure) { | |
| 119 throw new TestFailure(reason); | |
| 120 } else { | |
| 121 try { | |
| 122 throw ''; | |
| 123 } catch (_, stack) { | |
| 124 var trace = getTrace(stack, formatStacks, filterStacks); | |
| 125 if (trace == null) trace = stack; | |
| 126 _testLogBuffer.add(new Pair<String, StackTrace>(reason, trace)); | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 /// Format a test result. | 62 /// Format a test result. |
| 132 String formatResult(TestCase testCase) { | 63 String formatResult(TestCase testCase) { |
| 133 var result = new StringBuffer(); | 64 var result = new StringBuffer(); |
| 134 result.write(testCase.result.toUpperCase()); | 65 result.write(testCase.result.toUpperCase()); |
| 135 result.write(": "); | 66 result.write(": "); |
| 136 result.write(testCase.description); | 67 result.write(testCase.description); |
| 137 result.write("\n"); | 68 result.write("\n"); |
| 138 | 69 |
| 139 if (testCase.message != '') { | 70 if (testCase.message != '') { |
| 140 result.write(indent(testCase.message)); | 71 result.write(indent(testCase.message)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 } | 121 } |
| 191 } | 122 } |
| 192 } | 123 } |
| 193 | 124 |
| 194 void _postMessage(String message) { | 125 void _postMessage(String message) { |
| 195 // In dart2js browser tests, the JavaScript-based test controller | 126 // In dart2js browser tests, the JavaScript-based test controller |
| 196 // intercepts calls to print and listens for "secret" messages. | 127 // intercepts calls to print and listens for "secret" messages. |
| 197 print(message); | 128 print(message); |
| 198 } | 129 } |
| 199 } | 130 } |
| OLD | NEW |