| 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 This file defines a loose mock implementation. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.setTestOnly('goog.testing.LooseExpectationCollection'); |
| 20 |
goog.provide('goog.testing.LooseExpectationCollection'); |
| 21 |
goog.provide('goog.testing.LooseMock'); |
| 22 |
|
| 23 |
goog.require('goog.array'); |
| 24 |
goog.require('goog.structs.Map'); |
| 25 |
goog.require('goog.testing.Mock'); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* This class is an ordered collection of expectations for one method. Since |
| 31 |
* the loose mock does most of its verification at the time of $verify, this |
| 32 |
* class is necessary to manage the return/throw behavior when the mock is |
| 33 |
* being called. |
| 34 |
* @constructor |
| 35 |
* @final |
| 36 |
*/ |
| 37 |
goog.testing.LooseExpectationCollection = function() { |
| 38 |
/** |
| 39 |
* The list of expectations. All of these should have the same name. |
| 40 |
* @type {Array<goog.testing.MockExpectation>} |
| 41 |
* @private |
| 42 |
*/ |
| 43 |
this.expectations_ = []; |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Adds an expectation to this collection. |
| 49 |
* @param {goog.testing.MockExpectation} expectation The expectation to add. |
| 50 |
*/ |
| 51 |
goog.testing.LooseExpectationCollection.prototype.addExpectation = function( |
| 52 |
expectation) { |
| 53 |
this.expectations_.push(expectation); |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Gets the list of expectations in this collection. |
| 59 |
* @return {Array<goog.testing.MockExpectation>} The array of expectations. |
| 60 |
*/ |
| 61 |
goog.testing.LooseExpectationCollection.prototype.getExpectations = function() { |
| 62 |
return this.expectations_; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* This is a mock that does not care about the order of method calls. As a |
| 69 |
* result, it won't throw exceptions until verify() is called. The only |
| 70 |
* exception is that if a method is called that has no expectations, then an |
| 71 |
* exception will be thrown. |
| 72 |
* @param {Object|Function} objectToMock The object that should be mocked, or |
| 73 |
* the constructor of an object to mock. |
| 74 |
* @param {boolean=} opt_ignoreUnexpectedCalls Whether to ignore unexpected |
| 75 |
* calls. |
| 76 |
* @param {boolean=} opt_mockStaticMethods An optional argument denoting that |
| 77 |
* a mock should be constructed from the static functions of a class. |
| 78 |
* @param {boolean=} opt_createProxy An optional argument denoting that |
| 79 |
* a proxy for the target mock should be created. |
| 80 |
* @constructor |
| 81 |
* @extends {goog.testing.Mock} |
| 82 |
*/ |
| 83 |
goog.testing.LooseMock = function( |
| 84 |
objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods, |
| 85 |
opt_createProxy) { |
| 86 |
goog.testing.Mock.call( |
| 87 |
this, objectToMock, opt_mockStaticMethods, opt_createProxy); |
| 88 |
|
| 89 |
/** |
| 90 |
* A map of method names to a LooseExpectationCollection for that method. |
| 91 |
* @type {goog.structs.Map} |
| 92 |
* @private |
| 93 |
*/ |
| 94 |
this.$expectations_ = new goog.structs.Map(); |
| 95 |
|
| 96 |
/** |
| 97 |
* The calls that have been made; we cache them to verify at the end. Each |
| 98 |
* element is an array where the first element is the name, and the second |
| 99 |
* element is the arguments. |
| 100 |
* @type {Array<Array<*>>} |
| 101 |
* @private |
| 102 |
*/ |
| 103 |
this.$calls_ = []; |
| 104 |
|
| 105 |
/** |
| 106 |
* Whether to ignore unexpected calls. |
| 107 |
* @type {boolean} |
| 108 |
* @private |
| 109 |
*/ |
| 110 |
this.$ignoreUnexpectedCalls_ = !!opt_ignoreUnexpectedCalls; |
| 111 |
}; |
| 112 |
goog.inherits(goog.testing.LooseMock, goog.testing.Mock); |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* A setter for the ignoreUnexpectedCalls field. |
| 117 |
* @param {boolean} ignoreUnexpectedCalls Whether to ignore unexpected calls. |
| 118 |
* @return {!goog.testing.LooseMock} This mock object. |
| 119 |
*/ |
| 120 |
goog.testing.LooseMock.prototype.$setIgnoreUnexpectedCalls = function( |
| 121 |
ignoreUnexpectedCalls) { |
| 122 |
this.$ignoreUnexpectedCalls_ = ignoreUnexpectedCalls; |
| 123 |
return this; |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
/** @override */ |
| 128 |
goog.testing.LooseMock.prototype.$recordExpectation = function() { |
| 129 |
if (!this.$expectations_.containsKey(this.$pendingExpectation.name)) { |
| 130 |
this.$expectations_.set( |
| 131 |
this.$pendingExpectation.name, |
| 132 |
new goog.testing.LooseExpectationCollection()); |
| 133 |
} |
| 134 |
|
| 135 |
var collection = this.$expectations_.get(this.$pendingExpectation.name); |
| 136 |
collection.addExpectation(this.$pendingExpectation); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/** @override */ |
| 141 |
goog.testing.LooseMock.prototype.$recordCall = function(name, args) { |
| 142 |
if (!this.$expectations_.containsKey(name)) { |
| 143 |
if (this.$ignoreUnexpectedCalls_) { |
| 144 |
return; |
| 145 |
} |
| 146 |
this.$throwCallException(name, args); |
| 147 |
} |
| 148 |
|
| 149 |
// Start from the beginning of the expectations for this name, |
| 150 |
// and iterate over them until we find an expectation that matches |
| 151 |
// and also has calls remaining. |
| 152 |
var collection = this.$expectations_.get(name); |
| 153 |
var matchingExpectation = null; |
| 154 |
var expectations = collection.getExpectations(); |
| 155 |
for (var i = 0; i < expectations.length; i++) { |
| 156 |
var expectation = expectations[i]; |
| 157 |
if (this.$verifyCall(expectation, name, args)) { |
| 158 |
matchingExpectation = expectation; |
| 159 |
if (expectation.actualCalls < expectation.maxCalls) { |
| 160 |
break; |
| 161 |
} // else continue and see if we can find something that does match |
| 162 |
} |
| 163 |
} |
| 164 |
if (matchingExpectation == null) { |
| 165 |
this.$throwCallException(name, args, expectation); |
| 166 |
} |
| 167 |
|
| 168 |
matchingExpectation.actualCalls++; |
| 169 |
if (matchingExpectation.actualCalls > matchingExpectation.maxCalls) { |
| 170 |
this.$throwException( |
| 171 |
'Too many calls to ' + matchingExpectation.name + '\nExpected: ' + |
| 172 |
matchingExpectation.maxCalls + ' but was: ' + |
| 173 |
matchingExpectation.actualCalls); |
| 174 |
} |
| 175 |
|
| 176 |
this.$calls_.push([name, args]); |
| 177 |
return this.$do(matchingExpectation, args); |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
/** @override */ |
| 182 |
goog.testing.LooseMock.prototype.$reset = function() { |
| 183 |
goog.testing.LooseMock.superClass_.$reset.call(this); |
| 184 |
|
| 185 |
this.$expectations_ = new goog.structs.Map(); |
| 186 |
this.$calls_ = []; |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
/** @override */ |
| 191 |
goog.testing.LooseMock.prototype.$replay = function() { |
| 192 |
goog.testing.LooseMock.superClass_.$replay.call(this); |
| 193 |
|
| 194 |
// Verify that there are no expectations that can never be reached. |
| 195 |
// This can't catch every situation, but it is a decent sanity check |
| 196 |
// and it's similar to the behavior of EasyMock in java. |
| 197 |
var collections = this.$expectations_.getValues(); |
| 198 |
for (var i = 0; i < collections.length; i++) { |
| 199 |
var expectations = collections[i].getExpectations(); |
| 200 |
for (var j = 0; j < expectations.length; j++) { |
| 201 |
var expectation = expectations[j]; |
| 202 |
// If this expectation can be called infinite times, then |
| 203 |
// check if any subsequent expectation has the exact same |
| 204 |
// argument list. |
| 205 |
if (!isFinite(expectation.maxCalls)) { |
| 206 |
for (var k = j + 1; k < expectations.length; k++) { |
| 207 |
var laterExpectation = expectations[k]; |
| 208 |
if (laterExpectation.minCalls > 0 && |
| 209 |
goog.array.equals( |
| 210 |
expectation.argumentList, laterExpectation.argumentList)) { |
| 211 |
var name = expectation.name; |
| 212 |
var argsString = this.$argumentsAsString(expectation.argumentList); |
| 213 |
this.$throwException([ |
| 214 |
'Expected call to ', name, ' with arguments ', argsString, |
| 215 |
' has an infinite max number of calls; can\'t expect an', |
| 216 |
' identical call later with a positive min number of calls' |
| 217 |
].join('')); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
/** @override */ |
| 227 |
goog.testing.LooseMock.prototype.$verify = function() { |
| 228 |
goog.testing.LooseMock.superClass_.$verify.call(this); |
| 229 |
var collections = this.$expectations_.getValues(); |
| 230 |
|
| 231 |
for (var i = 0; i < collections.length; i++) { |
| 232 |
var expectations = collections[i].getExpectations(); |
| 233 |
for (var j = 0; j < expectations.length; j++) { |
| 234 |
var expectation = expectations[j]; |
| 235 |
if (expectation.actualCalls > expectation.maxCalls) { |
| 236 |
this.$throwException( |
| 237 |
'Too many calls to ' + expectation.name + '\nExpected: ' + |
| 238 |
expectation.maxCalls + ' but was: ' + expectation.actualCalls); |
| 239 |
} else if (expectation.actualCalls < expectation.minCalls) { |
| 240 |
this.$throwException( |
| 241 |
'Not enough calls to ' + expectation.name + '\nExpected: ' + |
| 242 |
expectation.minCalls + ' but was: ' + expectation.actualCalls); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
}; |