Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/testing/mockmatchers.js
4113 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Matchers to be used with the mock utilities. They allow for
9
* flexible matching by type. Custom matchers can be created by passing a
10
* matcher function into an ArgumentMatcher instance.
11
*
12
* For examples, please see the unit test.
13
*/
14
15
16
goog.setTestOnly('goog.testing.mockmatchers');
17
goog.provide('goog.testing.mockmatchers');
18
goog.provide('goog.testing.mockmatchers.ArgumentMatcher');
19
goog.provide('goog.testing.mockmatchers.IgnoreArgument');
20
goog.provide('goog.testing.mockmatchers.InstanceOf');
21
goog.provide('goog.testing.mockmatchers.ObjectEquals');
22
goog.provide('goog.testing.mockmatchers.RegexpMatch');
23
goog.provide('goog.testing.mockmatchers.SaveArgument');
24
goog.provide('goog.testing.mockmatchers.TypeOf');
25
26
goog.require('goog.array');
27
goog.require('goog.dom');
28
goog.require('goog.testing.asserts');
29
goog.requireType('goog.testing.MockExpectation');
30
31
32
33
/**
34
* A simple interface for executing argument matching. A match in this case is
35
* testing to see if a supplied object fits a given criteria. True is returned
36
* if the given criteria is met.
37
* @param {Function=} opt_matchFn A function that evaluates a given argument
38
* and returns true if it meets a given criteria.
39
* @param {?string=} opt_matchName The name expressing intent as part of
40
* an error message for when a match fails.
41
* @constructor
42
*/
43
goog.testing.mockmatchers.ArgumentMatcher = function(
44
opt_matchFn, opt_matchName) {
45
'use strict';
46
/**
47
* A function that evaluates a given argument and returns true if it meets a
48
* given criteria.
49
* @type {Function}
50
* @private
51
*/
52
this.matchFn_ = opt_matchFn || null;
53
54
/**
55
* A string indicating the match intent (e.g. isBoolean or isString).
56
* @type {?string}
57
* @private
58
*/
59
this.matchName_ = opt_matchName || null;
60
};
61
62
63
/**
64
* A function that takes a match argument and an optional MockExpectation
65
* which (if provided) will get error information and returns whether or
66
* not it matches.
67
* @param {*} toVerify The argument that should be verified.
68
* @param {?goog.testing.MockExpectation=} opt_expectation The expectation
69
* for this match.
70
* @return {boolean} Whether or not a given argument passes verification.
71
*/
72
goog.testing.mockmatchers.ArgumentMatcher.prototype.matches = function(
73
toVerify, opt_expectation) {
74
'use strict';
75
if (this.matchFn_) {
76
var isamatch = this.matchFn_(toVerify);
77
if (!isamatch && opt_expectation) {
78
if (this.matchName_) {
79
opt_expectation.addErrorMessage(
80
'Expected: ' + this.matchName_ + ' but was: ' +
81
_displayStringForValue(toVerify));
82
} else {
83
opt_expectation.addErrorMessage(
84
'Expected: missing mockmatcher' +
85
' description but was: ' + _displayStringForValue(toVerify));
86
}
87
}
88
return isamatch;
89
} else {
90
throw new Error('No match function defined for this mock matcher');
91
}
92
};
93
94
95
96
/**
97
* A matcher that verifies that an argument is an instance of a given class.
98
* @param {Function} ctor The class that will be used for verification.
99
* @constructor
100
* @extends {goog.testing.mockmatchers.ArgumentMatcher}
101
* @final
102
*/
103
goog.testing.mockmatchers.InstanceOf = function(ctor) {
104
'use strict';
105
goog.testing.mockmatchers.ArgumentMatcher.call(this, function(obj) {
106
'use strict';
107
return obj instanceof ctor;
108
// NOTE: Browser differences on ctor.toString() output
109
// make using that here problematic. So for now, just let
110
// people know the instanceOf() failed without providing
111
// browser specific details...
112
}, 'instanceOf()');
113
};
114
goog.inherits(
115
goog.testing.mockmatchers.InstanceOf,
116
goog.testing.mockmatchers.ArgumentMatcher);
117
118
119
120
/**
121
* A matcher that verifies that an argument is of a given type (e.g. "object").
122
* @param {string} type The type that a given argument must have.
123
* @constructor
124
* @extends {goog.testing.mockmatchers.ArgumentMatcher}
125
* @final
126
*/
127
goog.testing.mockmatchers.TypeOf = function(type) {
128
'use strict';
129
goog.testing.mockmatchers.ArgumentMatcher.call(this, function(obj) {
130
'use strict';
131
return goog.typeOf(obj) == type;
132
}, 'typeOf(' + type + ')');
133
};
134
goog.inherits(
135
goog.testing.mockmatchers.TypeOf,
136
goog.testing.mockmatchers.ArgumentMatcher);
137
138
139
140
/**
141
* A matcher that verifies that an argument matches a given RegExp.
142
* @param {RegExp} regexp The regular expression that the argument must match.
143
* @constructor
144
* @extends {goog.testing.mockmatchers.ArgumentMatcher}
145
* @final
146
*/
147
goog.testing.mockmatchers.RegexpMatch = function(regexp) {
148
'use strict';
149
goog.testing.mockmatchers.ArgumentMatcher.call(this, function(str) {
150
'use strict';
151
return regexp.test(str);
152
}, 'match(' + regexp + ')');
153
};
154
goog.inherits(
155
goog.testing.mockmatchers.RegexpMatch,
156
goog.testing.mockmatchers.ArgumentMatcher);
157
158
159
160
/**
161
* A matcher that always returns true. It is useful when the user does not care
162
* for some arguments.
163
* For example: mockFunction('username', 'password', new IgnoreArgument());
164
* @constructor
165
* @extends {goog.testing.mockmatchers.ArgumentMatcher}
166
* @final
167
*/
168
goog.testing.mockmatchers.IgnoreArgument = function() {
169
'use strict';
170
goog.testing.mockmatchers.ArgumentMatcher.call(this, function() {
171
'use strict';
172
return true;
173
}, 'true');
174
};
175
goog.inherits(
176
goog.testing.mockmatchers.IgnoreArgument,
177
goog.testing.mockmatchers.ArgumentMatcher);
178
179
180
181
/**
182
* A matcher that verifies that the argument is an object that equals the given
183
* expected object, using a deep comparison.
184
* @param {Object} expectedObject An object to match against when
185
* verifying the argument.
186
* @constructor
187
* @extends {goog.testing.mockmatchers.ArgumentMatcher}
188
*/
189
goog.testing.mockmatchers.ObjectEquals = function(expectedObject) {
190
'use strict';
191
/** @private */
192
this.expectedObject_ = expectedObject;
193
};
194
goog.inherits(
195
goog.testing.mockmatchers.ObjectEquals,
196
goog.testing.mockmatchers.ArgumentMatcher);
197
198
199
/** @override */
200
goog.testing.mockmatchers.ObjectEquals.prototype.matches = function(
201
toVerify, opt_expectation) {
202
'use strict';
203
// Override the default matches implementation to provide a custom error
204
// message to opt_expectation if it exists.
205
var differences =
206
goog.testing.asserts.findDifferences(this.expectedObject_, toVerify);
207
if (differences) {
208
if (opt_expectation) {
209
opt_expectation.addErrorMessage('Expected equal objects\n' + differences);
210
}
211
return false;
212
}
213
return true;
214
};
215
216
217
218
/**
219
* A matcher that saves the argument that it is verifying so that your unit test
220
* can perform extra tests with this argument later. For example, if the
221
* argument is a callback method, the unit test can then later call this
222
* callback to test the asynchronous portion of the call.
223
* @param {goog.testing.mockmatchers.ArgumentMatcher|Function=} opt_matcher
224
* Argument matcher or matching function that will be used to validate the
225
* argument. By default, argument will always be valid.
226
* @param {?string=} opt_matchName The name expressing intent as part of
227
* an error message for when a match fails.
228
* @constructor
229
* @extends {goog.testing.mockmatchers.ArgumentMatcher}
230
* @final
231
*/
232
goog.testing.mockmatchers.SaveArgument = function(opt_matcher, opt_matchName) {
233
'use strict';
234
goog.testing.mockmatchers.ArgumentMatcher.call(
235
this, /** @type {Function} */ (opt_matcher), opt_matchName);
236
237
/**
238
* All saved arguments that were verified.
239
* @const {!Array<*>}
240
*/
241
this.allArgs = [];
242
243
if (opt_matcher instanceof goog.testing.mockmatchers.ArgumentMatcher) {
244
/**
245
* Delegate match requests to this matcher.
246
* @type {goog.testing.mockmatchers.ArgumentMatcher}
247
* @private
248
*/
249
this.delegateMatcher_ = opt_matcher;
250
} else if (!opt_matcher) {
251
this.delegateMatcher_ = goog.testing.mockmatchers.ignoreArgument;
252
}
253
};
254
goog.inherits(
255
goog.testing.mockmatchers.SaveArgument,
256
goog.testing.mockmatchers.ArgumentMatcher);
257
258
259
/** @override */
260
goog.testing.mockmatchers.SaveArgument.prototype.matches = function(
261
toVerify, opt_expectation) {
262
'use strict';
263
this.arg = toVerify;
264
this.allArgs.push(toVerify);
265
if (this.delegateMatcher_) {
266
return this.delegateMatcher_.matches(toVerify, opt_expectation);
267
}
268
return goog.testing.mockmatchers.SaveArgument.superClass_.matches.call(
269
this, toVerify, opt_expectation);
270
};
271
272
273
/**
274
* The last (or only) saved argument that was verified.
275
* @type {*}
276
*/
277
goog.testing.mockmatchers.SaveArgument.prototype.arg;
278
279
280
/**
281
* An instance of the IgnoreArgument matcher. Returns true for all matches.
282
* @type {!goog.testing.mockmatchers.IgnoreArgument}
283
*/
284
goog.testing.mockmatchers.ignoreArgument =
285
new goog.testing.mockmatchers.IgnoreArgument();
286
287
288
/**
289
* A matcher that verifies that an argument is an array.
290
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
291
*/
292
goog.testing.mockmatchers.isArray =
293
new goog.testing.mockmatchers.ArgumentMatcher(Array.isArray, 'isArray');
294
295
296
/**
297
* A matcher that verifies that an argument is a array-like. A NodeList is an
298
* example of a collection that is very close to an array.
299
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
300
*/
301
goog.testing.mockmatchers.isArrayLike =
302
new goog.testing.mockmatchers.ArgumentMatcher(
303
goog.isArrayLike, 'isArrayLike');
304
305
306
/**
307
* A matcher that verifies that an argument is a date-like.
308
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
309
*/
310
goog.testing.mockmatchers.isDateLike =
311
new goog.testing.mockmatchers.ArgumentMatcher(
312
goog.isDateLike, 'isDateLike');
313
314
315
/**
316
* A matcher that verifies that an argument is a string.
317
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
318
*/
319
goog.testing.mockmatchers.isString =
320
new goog.testing.mockmatchers.ArgumentMatcher(
321
x => typeof x === 'string', 'isString');
322
323
324
/**
325
* A matcher that verifies that an argument is a boolean.
326
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
327
*/
328
goog.testing.mockmatchers.isBoolean =
329
new goog.testing.mockmatchers.ArgumentMatcher(
330
x => typeof x === 'boolean', 'isBoolean');
331
332
333
/**
334
* A matcher that verifies that an argument is a number.
335
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
336
*/
337
goog.testing.mockmatchers.isNumber =
338
new goog.testing.mockmatchers.ArgumentMatcher(
339
x => typeof x === 'number', 'isNumber');
340
341
342
/**
343
* A matcher that verifies that an argument is a function.
344
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
345
*/
346
goog.testing.mockmatchers.isFunction =
347
new goog.testing.mockmatchers.ArgumentMatcher(
348
x => typeof x === 'function', 'isFunction');
349
350
351
/**
352
* A matcher that verifies that an argument is an object.
353
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
354
*/
355
goog.testing.mockmatchers.isObject =
356
new goog.testing.mockmatchers.ArgumentMatcher(goog.isObject, 'isObject');
357
358
359
/**
360
* A matcher that verifies that an argument is like a DOM node.
361
* @type {!goog.testing.mockmatchers.ArgumentMatcher}
362
*/
363
goog.testing.mockmatchers.isNodeLike =
364
new goog.testing.mockmatchers.ArgumentMatcher(
365
goog.dom.isNodeLike, 'isNodeLike');
366
367
368
/**
369
* A function that checks to see if an array matches a given set of
370
* expectations. The expectations array can be a mix of ArgumentMatcher
371
* implementations and values. True will be returned if values are identical or
372
* if a matcher returns a positive result.
373
* @param {Array<?>} expectedArr An array of expectations which can be either
374
* values to check for equality or ArgumentMatchers.
375
* @param {Array<?>} arr The array to match.
376
* @param {goog.testing.MockExpectation?=} opt_expectation The expectation
377
* for this match.
378
* @return {boolean} Whether or not the given array matches the expectations.
379
*/
380
goog.testing.mockmatchers.flexibleArrayMatcher = function(
381
expectedArr, arr, opt_expectation) {
382
'use strict';
383
return goog.array.equals(expectedArr, arr, function(a, b) {
384
'use strict';
385
var errCount = 0;
386
if (opt_expectation) {
387
errCount = opt_expectation.getErrorMessageCount();
388
}
389
var isamatch = a === b ||
390
a instanceof goog.testing.mockmatchers.ArgumentMatcher &&
391
a.matches(b, opt_expectation);
392
var failureMessage = null;
393
if (!isamatch) {
394
failureMessage = goog.testing.asserts.findDifferences(a, b);
395
isamatch = !failureMessage;
396
}
397
if (!isamatch && opt_expectation) {
398
// If the error count changed, the match sent out an error
399
// message. If the error count has not changed, then
400
// we need to send out an error message...
401
if (errCount == opt_expectation.getErrorMessageCount()) {
402
// Use the _displayStringForValue() from assert.js
403
// for consistency...
404
if (!failureMessage) {
405
failureMessage = 'Expected: ' + _displayStringForValue(a) +
406
' but was: ' + _displayStringForValue(b);
407
}
408
opt_expectation.addErrorMessage(failureMessage);
409
}
410
}
411
return isamatch;
412
});
413
};
414
415