# lib/goog/testing/functionmock.js

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