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