aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/mockcontrol.js
blob: 937e79ab5af766e8c730e8dd059deaeac067074c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview A MockControl holds a set of mocks for a particular test.
 * It consolidates calls to $replay, $verify, and $tearDown, which simplifies
 * the test and helps avoid omissions.
 *
 * You can create and control a mock:
 *   var mockFoo = mockControl.addMock(new MyMock(Foo));
 *
 * MockControl also exposes some convenience functions that create
 * controlled mocks for common mocks: StrictMock, LooseMock,
 * FunctionMock, MethodMock, and GlobalFunctionMock.
 *
 */


goog.provide('goog.testing.MockControl');

goog.require('goog.array');
goog.require('goog.testing');
goog.require('goog.testing.LooseMock');
goog.require('goog.testing.MockInterface');
goog.require('goog.testing.StrictMock');



/**
 * Controls a set of mocks.  Controlled mocks are replayed, verified, and
 * cleaned-up at the same time.
 * @constructor
 */
goog.testing.MockControl = function() {
  /**
   * The list of mocks being controlled.
   * @type {Array.<goog.testing.MockInterface>}
   * @private
   */
  this.mocks_ = [];
};


/**
 * Takes control of this mock.
 * @param {goog.testing.MockInterface} mock Mock to be controlled.
 * @return {goog.testing.MockInterface} The same mock passed in,
 *     for convenience.
 */
goog.testing.MockControl.prototype.addMock = function(mock) {
  this.mocks_.push(mock);
  return mock;
};


/**
 * Calls replay on each controlled mock.
 */
goog.testing.MockControl.prototype.$replayAll = function() {
  goog.array.forEach(this.mocks_, function(m) {
    m.$replay();
  });
};


/**
 * Calls reset on each controlled mock.
 */
goog.testing.MockControl.prototype.$resetAll = function() {
  goog.array.forEach(this.mocks_, function(m) {
    m.$reset();
  });
};


/**
 * Calls verify on each controlled mock.
 */
goog.testing.MockControl.prototype.$verifyAll = function() {
  goog.array.forEach(this.mocks_, function(m) {
    m.$verify();
  });
};


/**
 * Calls tearDown on each controlled mock, if necesssary.
 */
goog.testing.MockControl.prototype.$tearDown = function() {
  goog.array.forEach(this.mocks_, function(m) {
    // $tearDown if defined.
    if (m.$tearDown) {
      m.$tearDown();
    }
    // TODO(user): Somehow determine if verifyAll should have been called
    // but was not.
  });
};


/**
 * Creates a controlled StrictMock.  Passes its arguments through to the
 * StrictMock constructor.
 * @param {Object} objectToMock The object to mock.
 * @param {boolean=} opt_mockStaticMethods An optional argument denoting that
 *     a mock should be constructed from the static functions of a class.
 * @param {boolean=} opt_createProxy An optional argument denoting that
 *     a proxy for the target mock should be created.
 * @return {goog.testing.StrictMock} The mock object.
 */
goog.testing.MockControl.prototype.createStrictMock = function(
    objectToMock, opt_mockStaticMethods, opt_createProxy) {
  var m = new goog.testing.StrictMock(objectToMock, opt_mockStaticMethods,
                                      opt_createProxy);
  this.addMock(m);
  return m;
};


/**
 * Creates a controlled LooseMock.  Passes its arguments through to the
 * LooseMock constructor.
 * @param {Object} objectToMock The object to mock.
 * @param {boolean=} opt_ignoreUnexpectedCalls Whether to ignore unexpected
 *     calls.
 * @param {boolean=} opt_mockStaticMethods An optional argument denoting that
 *     a mock should be constructed from the static functions of a class.
 * @param {boolean=} opt_createProxy An optional argument denoting that
 *     a proxy for the target mock should be created.
 * @return {goog.testing.LooseMock} The mock object.
 */
goog.testing.MockControl.prototype.createLooseMock = function(
    objectToMock, opt_ignoreUnexpectedCalls,
    opt_mockStaticMethods, opt_createProxy) {
  var m = new goog.testing.LooseMock(objectToMock, opt_ignoreUnexpectedCalls,
                                     opt_mockStaticMethods, opt_createProxy);
  this.addMock(m);
  return m;
};


/**
 * Creates a controlled FunctionMock.  Passes its arguments through to the
 * FunctionMock constructor.
 * @param {string=} opt_functionName The optional name of the function to mock
 *     set to '[anonymous mocked function]' if not passed in.
 * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
 *     goog.testing.Mock.STRICT. The default is STRICT.
 * @return {goog.testing.MockInterface} The mocked function.
 */
goog.testing.MockControl.prototype.createFunctionMock = function(
    opt_functionName, opt_strictness) {
  var m = goog.testing.createFunctionMock(opt_functionName, opt_strictness);
  this.addMock(m);
  return m;
};


/**
 * Creates a controlled MethodMock.  Passes its arguments through to the
 * MethodMock constructor.
 * @param {Object} scope The scope of the method to be mocked out.
 * @param {string} functionName The name of the function we're going to mock.
 * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
 *     goog.testing.Mock.STRICT. The default is STRICT.
 * @return {goog.testing.MockInterface} The mocked method.
 */
goog.testing.MockControl.prototype.createMethodMock = function(
    scope, functionName, opt_strictness) {
  var m = goog.testing.createMethodMock(scope, functionName, opt_strictness);
  this.addMock(m);
  return m;
};


/**
 * Creates a controlled MethodMock for a constructor.  Passes its arguments
 * through to the MethodMock constructor. See
 * {@link goog.testing.createConstructorMock} for details.
 * @param {Object} scope The scope of the constructor to be mocked out.
 * @param {string} constructorName The name of the function we're going to mock.
 * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
 *     goog.testing.Mock.STRICT. The default is STRICT.
 * @return {goog.testing.MockInterface} The mocked method.
 */
goog.testing.MockControl.prototype.createConstructorMock = function(
    scope, constructorName, opt_strictness) {
  var m = goog.testing.createConstructorMock(scope, constructorName,
                                             opt_strictness);
  this.addMock(m);
  return m;
};


/**
 * Creates a controlled GlobalFunctionMock.  Passes its arguments through to the
 * GlobalFunctionMock constructor.
 * @param {string} functionName The name of the function we're going to mock.
 * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
 *     goog.testing.Mock.STRICT. The default is STRICT.
 * @return {goog.testing.MockInterface} The mocked function.
 */
goog.testing.MockControl.prototype.createGlobalFunctionMock = function(
    functionName, opt_strictness) {
  var m = goog.testing.createGlobalFunctionMock(functionName, opt_strictness);
  this.addMock(m);
  return m;
};