| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Helper class to allow for expected unit test failures. |
| 17 |
* |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.setTestOnly('goog.testing.ExpectedFailures'); |
| 22 |
goog.provide('goog.testing.ExpectedFailures'); |
| 23 |
|
| 24 |
goog.require('goog.asserts'); |
| 25 |
goog.require('goog.debug.DivConsole'); |
| 26 |
goog.require('goog.dom'); |
| 27 |
goog.require('goog.dom.TagName'); |
| 28 |
goog.require('goog.events'); |
| 29 |
goog.require('goog.events.EventType'); |
| 30 |
goog.require('goog.log'); |
| 31 |
goog.require('goog.style'); |
| 32 |
goog.require('goog.testing.JsUnitException'); |
| 33 |
goog.require('goog.testing.TestCase'); |
| 34 |
goog.require('goog.testing.asserts'); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Helper class for allowing some unit tests to fail, particularly designed to |
| 40 |
* mark tests that should be fixed on a given browser. |
| 41 |
* |
| 42 |
* <pre> |
| 43 |
* var expectedFailures = new goog.testing.ExpectedFailures(); |
| 44 |
* |
| 45 |
* function tearDown() { |
| 46 |
* expectedFailures.handleTearDown(); |
| 47 |
* } |
| 48 |
* |
| 49 |
* function testSomethingThatBreaksInWebKit() { |
| 50 |
* expectedFailures.expectFailureFor(goog.userAgent.WEBKIT); |
| 51 |
* |
| 52 |
* try { |
| 53 |
* ... |
| 54 |
* assert(somethingThatFailsInWebKit); |
| 55 |
* ... |
| 56 |
* } catch (e) { |
| 57 |
* expectedFailures.handleException(e); |
| 58 |
* } |
| 59 |
* } |
| 60 |
* </pre> |
| 61 |
* |
| 62 |
* @constructor |
| 63 |
* @final |
| 64 |
*/ |
| 65 |
goog.testing.ExpectedFailures = function() { |
| 66 |
goog.testing.ExpectedFailures.setUpConsole_(); |
| 67 |
this.reset_(); |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* The lazily created debugging console. |
| 73 |
* @type {goog.debug.DivConsole?} |
| 74 |
* @private |
| 75 |
*/ |
| 76 |
goog.testing.ExpectedFailures.console_ = null; |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Logger for the expected failures. |
| 81 |
* @type {goog.log.Logger} |
| 82 |
* @private |
| 83 |
*/ |
| 84 |
goog.testing.ExpectedFailures.prototype.logger_ = |
| 85 |
goog.log.getLogger('goog.testing.ExpectedFailures'); |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Whether or not we are expecting failure. |
| 90 |
* @type {boolean} |
| 91 |
* @private |
| 92 |
*/ |
| 93 |
goog.testing.ExpectedFailures.prototype.expectingFailure_; |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* The string to emit upon an expected failure. |
| 98 |
* @type {string} |
| 99 |
* @private |
| 100 |
*/ |
| 101 |
goog.testing.ExpectedFailures.prototype.failureMessage_; |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* An array of suppressed failures. |
| 106 |
* @type {Array<!Error>} |
| 107 |
* @private |
| 108 |
*/ |
| 109 |
goog.testing.ExpectedFailures.prototype.suppressedFailures_; |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Sets up the debug console, if it isn't already set up. |
| 114 |
* @private |
| 115 |
*/ |
| 116 |
goog.testing.ExpectedFailures.setUpConsole_ = function() { |
| 117 |
if (!goog.testing.ExpectedFailures.console_) { |
| 118 |
var xButton = goog.dom.createDom( |
| 119 |
goog.dom.TagName.DIV, { |
| 120 |
'style': 'position: absolute; border-left:1px solid #333;' + |
| 121 |
'border-bottom:1px solid #333; right: 0; top: 0; width: 1em;' + |
| 122 |
'height: 1em; cursor: pointer; background-color: #cde;' + |
| 123 |
'text-align: center; color: black' |
| 124 |
}, |
| 125 |
'X'); |
| 126 |
var div = goog.dom.createDom( |
| 127 |
goog.dom.TagName.DIV, { |
| 128 |
'style': 'position: absolute; border: 1px solid #333; right: 10px;' + |
| 129 |
'top : 10px; width: 400px; display: none' |
| 130 |
}, |
| 131 |
xButton); |
| 132 |
document.body.appendChild(div); |
| 133 |
goog.events.listen(xButton, goog.events.EventType.CLICK, function() { |
| 134 |
goog.style.setElementShown(div, false); |
| 135 |
}); |
| 136 |
|
| 137 |
goog.testing.ExpectedFailures.console_ = new goog.debug.DivConsole(div); |
| 138 |
goog.log.addHandler( |
| 139 |
goog.testing.ExpectedFailures.prototype.logger_, |
| 140 |
goog.bind(goog.style.setElementShown, null, div, true)); |
| 141 |
goog.log.addHandler( |
| 142 |
goog.testing.ExpectedFailures.prototype.logger_, |
| 143 |
goog.bind( |
| 144 |
goog.testing.ExpectedFailures.console_.addLogRecord, |
| 145 |
goog.testing.ExpectedFailures.console_)); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Register to expect failure for the given condition. Multiple calls to this |
| 152 |
* function act as a boolean OR. The first applicable message will be used. |
| 153 |
* @param {boolean} condition Whether to expect failure. |
| 154 |
* @param {string=} opt_message Descriptive message of this expected failure. |
| 155 |
*/ |
| 156 |
goog.testing.ExpectedFailures.prototype.expectFailureFor = function( |
| 157 |
condition, opt_message) { |
| 158 |
this.expectingFailure_ = this.expectingFailure_ || condition; |
| 159 |
if (condition) { |
| 160 |
this.failureMessage_ = this.failureMessage_ || opt_message || ''; |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Determines if the given exception was expected. |
| 167 |
* @param {Object} ex The exception to check. |
| 168 |
* @return {boolean} Whether the exception was expected. |
| 169 |
*/ |
| 170 |
goog.testing.ExpectedFailures.prototype.isExceptionExpected = function(ex) { |
| 171 |
return this.expectingFailure_ && ex instanceof goog.testing.JsUnitException; |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Handle an exception, suppressing it if it is a unit test failure that we |
| 177 |
* expected. |
| 178 |
* @param {Error} ex The exception to handle. |
| 179 |
*/ |
| 180 |
goog.testing.ExpectedFailures.prototype.handleException = function(ex) { |
| 181 |
if (this.isExceptionExpected(ex)) { |
| 182 |
goog.asserts.assertInstanceof(ex, goog.testing.JsUnitException); |
| 183 |
goog.log.info( |
| 184 |
this.logger_, 'Suppressing test failure in ' + |
| 185 |
goog.testing.TestCase.currentTestName + ':' + |
| 186 |
(this.failureMessage_ ? '\n(' + this.failureMessage_ + ')' : ''), |
| 187 |
ex); |
| 188 |
this.suppressedFailures_.push(ex); |
| 189 |
goog.testing.TestCase.invalidateAssertionException(ex); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
// Rethrow the exception if we weren't expecting it or if it is a normal |
| 194 |
// exception. |
| 195 |
throw ex; |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* Run the given function, catching any expected failures. |
| 201 |
* @param {Function} func The function to run. |
| 202 |
* @param {boolean=} opt_lenient Whether to ignore if the expected failures |
| 203 |
* didn't occur. In this case a warning will be logged in handleTearDown. |
| 204 |
*/ |
| 205 |
goog.testing.ExpectedFailures.prototype.run = function(func, opt_lenient) { |
| 206 |
try { |
| 207 |
func(); |
| 208 |
} catch (ex) { |
| 209 |
this.handleException(ex); |
| 210 |
} |
| 211 |
|
| 212 |
if (!opt_lenient && this.expectingFailure_ && |
| 213 |
!this.suppressedFailures_.length) { |
| 214 |
fail(this.getExpectationMessage_()); |
| 215 |
} |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* @return {string} A warning describing an expected failure that didn't occur. |
| 221 |
* @private |
| 222 |
*/ |
| 223 |
goog.testing.ExpectedFailures.prototype.getExpectationMessage_ = function() { |
| 224 |
return 'Expected a test failure in \'' + |
| 225 |
goog.testing.TestCase.currentTestName + '\' but the test passed.'; |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
/** |
| 230 |
* Handle the tearDown phase of a test, alerting the user if an expected test |
| 231 |
* was not suppressed. |
| 232 |
*/ |
| 233 |
goog.testing.ExpectedFailures.prototype.handleTearDown = function() { |
| 234 |
if (this.expectingFailure_ && !this.suppressedFailures_.length) { |
| 235 |
goog.log.warning(this.logger_, this.getExpectationMessage_()); |
| 236 |
} |
| 237 |
this.reset_(); |
| 238 |
}; |
| 239 |
|
| 240 |
|
| 241 |
/** |
| 242 |
* Reset internal state. |
| 243 |
* @private |
| 244 |
*/ |
| 245 |
goog.testing.ExpectedFailures.prototype.reset_ = function() { |
| 246 |
this.expectingFailure_ = false; |
| 247 |
this.failureMessage_ = ''; |
| 248 |
this.suppressedFailures_ = []; |
| 249 |
}; |