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