Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/testing/functionmock.js
4503 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Enable mocking of functions not attached to objects
9
* whether they be global / top-level or anonymous methods / closures.
10
*
11
* See the unit tests for usage.
12
*/
13
14
goog.setTestOnly('goog.testing');
15
goog.provide('goog.testing');
16
goog.provide('goog.testing.FunctionMock');
17
goog.provide('goog.testing.GlobalFunctionMock');
18
goog.provide('goog.testing.MethodMock');
19
20
goog.require('goog.object');
21
goog.require('goog.testing.LooseMock');
22
goog.require('goog.testing.Mock');
23
goog.require('goog.testing.MockInterface');
24
goog.require('goog.testing.PropertyReplacer');
25
goog.require('goog.testing.StrictMock');
26
27
28
/**
29
* Class used to mock a function. Useful for mocking closures and anonymous
30
* callbacks etc. Creates a function object that extends goog.testing.Mock.
31
* @param {string=} opt_functionName The optional name of the function to mock.
32
* Set to '[anonymous mocked function]' if not passed in.
33
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
34
* goog.testing.Mock.STRICT. The default is STRICT.
35
* @return {!goog.testing.MockInterface} The mocked function.
36
* @suppress {missingProperties} Mocks do not fit in the type system well.
37
*/
38
goog.testing.FunctionMock = function(opt_functionName, opt_strictness) {
39
'use strict';
40
var fn = function() {
41
'use strict';
42
var args = Array.prototype.slice.call(arguments);
43
args.splice(0, 0, opt_functionName || '[anonymous mocked function]');
44
return fn.$mockMethod.apply(fn, args);
45
};
46
var base = opt_strictness === goog.testing.Mock.LOOSE ?
47
goog.testing.LooseMock :
48
goog.testing.StrictMock;
49
goog.object.extend(fn, new base({}));
50
51
return /** @type {!goog.testing.MockInterface} */ (fn);
52
};
53
54
55
/**
56
* Mocks an existing function. Creates a goog.testing.FunctionMock
57
* and registers it in the given scope with the name specified by functionName.
58
* @param {Object} scope The scope of the method to be mocked out.
59
* @param {string} functionName The name of the function we're going to mock.
60
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
61
* goog.testing.Mock.STRICT. The default is STRICT.
62
* @return {!goog.testing.MockInterface} The mocked method.
63
* @suppress {strictMissingProperties} $propertyReplacer_ and $tearDown are
64
* not defined on goog.testing.MockInterface
65
*/
66
goog.testing.MethodMock = function(scope, functionName, opt_strictness) {
67
'use strict';
68
if (!(functionName in scope)) {
69
throw new Error(functionName + ' is not a property of the given scope.');
70
}
71
72
var fn = goog.testing.FunctionMock(functionName, opt_strictness);
73
74
fn.$propertyReplacer_ = new goog.testing.PropertyReplacer();
75
fn.$propertyReplacer_.set(scope, functionName, fn);
76
fn.$tearDown = goog.testing.MethodMock.$tearDown;
77
78
return fn;
79
};
80
81
82
/**
83
* @private
84
* @record @extends {goog.testing.MockInterface}
85
*/
86
goog.testing.MethodMock.MockInternalInterface_ = function() {};
87
88
/** @const {!goog.testing.PropertyReplacer} */
89
goog.testing.MethodMock.MockInternalInterface_.prototype.$propertyReplacer_;
90
91
92
/**
93
* Resets the global function that we mocked back to its original state.
94
* @this {goog.testing.MockInterface}
95
*/
96
goog.testing.MethodMock.$tearDown = function() {
97
'use strict';
98
/** @type {!goog.testing.MethodMock.MockInternalInterface_} */ (this)
99
.$propertyReplacer_.reset();
100
};
101
102
103
/**
104
* Mocks a global / top-level function. Creates a goog.testing.MethodMock
105
* in the global scope with the name specified by functionName.
106
* @param {string} functionName The name of the function we're going to mock.
107
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
108
* goog.testing.Mock.STRICT. The default is STRICT.
109
* @return {!goog.testing.MockInterface} The mocked global function.
110
*/
111
goog.testing.GlobalFunctionMock = function(functionName, opt_strictness) {
112
'use strict';
113
return goog.testing.MethodMock(goog.global, functionName, opt_strictness);
114
};
115
116
117
/**
118
* Convenience method for creating a mock for a function.
119
* @param {string=} opt_functionName The optional name of the function to mock
120
* set to '[anonymous mocked function]' if not passed in.
121
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
122
* goog.testing.Mock.STRICT. The default is STRICT.
123
* @return {!goog.testing.MockInterface} The mocked function.
124
*/
125
goog.testing.createFunctionMock = function(opt_functionName, opt_strictness) {
126
'use strict';
127
return goog.testing.FunctionMock(opt_functionName, opt_strictness);
128
};
129
130
131
/**
132
* Convenience method for creating a mock for a method.
133
* @param {Object} scope The scope of the method to be mocked out.
134
* @param {string} functionName The name of the function we're going to mock.
135
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
136
* goog.testing.Mock.STRICT. The default is STRICT.
137
* @return {!goog.testing.MockInterface} The mocked global function.
138
*/
139
goog.testing.createMethodMock = function(scope, functionName, opt_strictness) {
140
'use strict';
141
return goog.testing.MethodMock(scope, functionName, opt_strictness);
142
};
143
144
145
/**
146
* Convenience method for creating a mock for a constructor. Copies class
147
* members to the mock.
148
*
149
* <p>When mocking a constructor to return a mocked instance, remember to create
150
* the instance mock before mocking the constructor. If you mock the constructor
151
* first, then the mock framework will be unable to examine the prototype chain
152
* when creating the mock instance.
153
* @param {Object} scope The scope of the constructor to be mocked out.
154
* @param {string} constructorName The name of the constructor we're going to
155
* mock.
156
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
157
* goog.testing.Mock.STRICT. The default is STRICT.
158
* @return {!goog.testing.MockInterface} The mocked constructor.
159
*/
160
goog.testing.createConstructorMock = function(
161
scope, constructorName, opt_strictness) {
162
'use strict';
163
var realConstructor = scope[constructorName];
164
var constructorMock =
165
goog.testing.MethodMock(scope, constructorName, opt_strictness);
166
167
// Copy class members from the real constructor to the mock. Do not copy
168
// the closure superClass_ property (see goog.inherits), the built-in
169
// prototype property, or properties added to Function.prototype
170
// TODO(nickreid): Should this work for non-enumerable properties, like are
171
// created by ES6 classes.
172
for (var property in realConstructor) {
173
if (property != 'superClass_' && property != 'prototype' &&
174
realConstructor.hasOwnProperty(property)) {
175
constructorMock[property] = realConstructor[property];
176
}
177
}
178
return constructorMock;
179
};
180
181
182
/**
183
* Convenience method for creating a mocks for a global / top-level function.
184
* @param {string} functionName The name of the function we're going to mock.
185
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
186
* goog.testing.Mock.STRICT. The default is STRICT.
187
* @return {!goog.testing.MockInterface} The mocked global function.
188
*/
189
goog.testing.createGlobalFunctionMock = function(functionName, opt_strictness) {
190
'use strict';
191
return goog.testing.GlobalFunctionMock(functionName, opt_strictness);
192
};
193
194