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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
// 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 Helper class for creating stubs for testing.
*
*/
goog.setTestOnly('goog.testing.PropertyReplacer');
goog.provide('goog.testing.PropertyReplacer');
/** @suppress {extraRequire} Needed for some tests to compile. */
goog.require('goog.testing.ObjectPropertyString');
goog.require('goog.userAgent');
/**
* Helper class for stubbing out variables and object properties for unit tests.
* This class can change the value of some variables before running the test
* cases, and to reset them in the tearDown phase.
* See googletest.StubOutForTesting as an analogy in Python:
* http://protobuf.googlecode.com/svn/trunk/python/stubout.py
*
* Example usage:
*
* var stubs = new goog.testing.PropertyReplacer();
*
* function setUp() {
* // Mock functions used in all test cases.
* stubs.replace(Math, 'random', function() {
* return 4; // Chosen by fair dice roll. Guaranteed to be random.
* });
* }
*
* function tearDown() {
* stubs.reset();
* }
*
* function testThreeDice() {
* // Mock a constant used only in this test case.
* stubs.set(goog.global, 'DICE_COUNT', 3);
* assertEquals(12, rollAllDice());
* }
*
* Constraints on altered objects:
* <ul>
* <li>DOM subclasses aren't supported.
* <li>The value of the objects' constructor property must either be equal to
* the real constructor or kept untouched.
* </ul>
*
* Code compiled with property renaming may need to use {@code
* goog.reflect.objectProperty} instead of simply naming the property to
* replace.
*
* @constructor
* @final
*/
goog.testing.PropertyReplacer = function() {
/**
* Stores the values changed by the set() method in chronological order.
* Its items are objects with 3 fields: 'object', 'key', 'value'. The
* original value for the given key in the given object is stored under the
* 'value' key.
* @type {Array<{ object: ?, key: string, value: ? }>}
* @private
*/
this.original_ = [];
};
/**
* Indicates that a key didn't exist before having been set by the set() method.
* @private @const
*/
goog.testing.PropertyReplacer.NO_SUCH_KEY_ = {};
/**
* Tells if the given key exists in the object. Ignores inherited fields.
* @param {Object|Function} obj The JavaScript or native object or function
* whose key is to be checked.
* @param {string} key The key to check.
* @return {boolean} Whether the object has the key as own key.
* @private
* @suppress {unusedLocalVariables}
*/
goog.testing.PropertyReplacer.hasKey_ = function(obj, key) {
if (!(key in obj)) {
return false;
}
// hasOwnProperty is only reliable with JavaScript objects. It returns false
// for built-in DOM attributes.
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return true;
}
// In all browsers except Opera obj.constructor never equals to Object if
// obj is an instance of a native class. In Opera we have to fall back on
// examining obj.toString().
if (obj.constructor == Object &&
(!goog.userAgent.OPERA ||
Object.prototype.toString.call(obj) == '[object Object]')) {
return false;
}
try {
// Firefox hack to consider "className" part of the HTML elements or
// "body" part of document. Although they are defined in the prototype of
// HTMLElement or Document, accessing them this way throws an exception.
// <pre>
// var dummy = document.body.constructor.prototype.className
// [Exception... "Cannot modify properties of a WrappedNative"]
// </pre>
var dummy = obj.constructor.prototype[key];
} catch (e) {
return true;
}
return !(key in obj.constructor.prototype);
};
/**
* Deletes a key from an object. Sets it to undefined or empty string if the
* delete failed.
* @param {Object|Function} obj The object or function to delete a key from.
* @param {string} key The key to delete.
* @throws {Error} In case of trying to set a read-only property
* @private
*/
goog.testing.PropertyReplacer.deleteKey_ = function(obj, key) {
try {
delete obj[key];
// Delete has no effect for built-in properties of DOM nodes in FF.
if (!goog.testing.PropertyReplacer.hasKey_(obj, key)) {
return;
}
} catch (e) {
// IE throws TypeError when trying to delete properties of native objects
// (e.g. DOM nodes or window), even if they have been added by JavaScript.
}
obj[key] = undefined;
if (obj[key] == 'undefined') {
// Some properties such as className in IE are always evaluated as string
// so undefined will become 'undefined'.
obj[key] = '';
}
if (obj[key]) {
throw new Error(
'Cannot delete non configurable property "' + key + '" in ' + obj);
}
};
/**
* Restore the original state of a key in an object.
* @param {{ object: ?, key: string, value: ? }} original Original state
* @private
*/
goog.testing.PropertyReplacer.restoreOriginal_ = function(original) {
if (original.value == goog.testing.PropertyReplacer.NO_SUCH_KEY_) {
goog.testing.PropertyReplacer.deleteKey_(original.object, original.key);
} else {
original.object[original.key] = original.value;
}
};
/**
* Adds or changes a value in an object while saving its original state.
* @param {Object|Function} obj The JavaScript or native object or function to
* alter. See the constraints in the class description.
* @param {string} key The key to change the value for.
* @param {*} value The new value to set.
* @throws {Error} In case of trying to set a read-only property.
*/
goog.testing.PropertyReplacer.prototype.set = function(obj, key, value) {
var origValue = goog.testing.PropertyReplacer.hasKey_(obj, key) ?
obj[key] :
goog.testing.PropertyReplacer.NO_SUCH_KEY_;
this.original_.push({object: obj, key: key, value: origValue});
obj[key] = value;
// Check whether obj[key] was a read-only value and the assignment failed.
// Also, check that we're not comparing returned pixel values when "value"
// is 0. In other words, account for this case:
// document.body.style.margin = 0;
// document.body.style.margin; // returns "0px"
if (obj[key] != value && (value + 'px') != obj[key]) {
throw new Error(
'Cannot overwrite read-only property "' + key + '" in ' + obj);
}
};
/**
* Changes an existing value in an object to another one of the same type while
* saving its original state. The advantage of {@code replace} over {@link #set}
* is that {@code replace} protects against typos and erroneously passing tests
* after some members have been renamed during a refactoring.
* @param {Object|Function} obj The JavaScript or native object or function to
* alter. See the constraints in the class description.
* @param {string} key The key to change the value for. It has to be present
* either in {@code obj} or in its prototype chain.
* @param {*} value The new value to set.
* @param {boolean=} opt_allowNullOrUndefined By default, this method requires
* {@code value} to match the type of the existing value, as determined by
* {@link goog.typeOf}. Setting opt_allowNullOrUndefined to {@code true}
* allows an existing value to be replaced by {@code null} or
{@code undefined}, or vice versa.
* @throws {Error} In case of missing key or type mismatch.
*/
goog.testing.PropertyReplacer.prototype.replace = function(
obj, key, value, opt_allowNullOrUndefined) {
if (!(key in obj)) {
throw new Error('Cannot replace missing property "' + key + '" in ' + obj);
}
// If opt_allowNullOrUndefined is true, then we do not check the types if
// either the original or new value is null or undefined.
var shouldCheckTypes = !opt_allowNullOrUndefined ||
(goog.isDefAndNotNull(obj[key]) && goog.isDefAndNotNull(value));
if (shouldCheckTypes) {
var originalType = goog.typeOf(obj[key]);
var newType = goog.typeOf(value);
if (originalType != newType) {
throw new Error(
'Cannot replace property "' + key + '" in ' + obj +
' with a value of different type (expected ' + originalType +
', found ' + newType + ')');
}
}
this.set(obj, key, value);
};
/**
* Builds an object structure for the provided namespace path. Doesn't
* overwrite those prefixes of the path that are already objects or functions.
* @param {string} path The path to create or alter, e.g. 'goog.ui.Menu'.
* @param {*} value The value to set.
*/
goog.testing.PropertyReplacer.prototype.setPath = function(path, value) {
var parts = path.split('.');
var obj = goog.global;
for (var i = 0; i < parts.length - 1; i++) {
var part = parts[i];
if (part == 'prototype' && !obj[part]) {
throw new Error(
'Cannot set the prototype of ' + parts.slice(0, i).join('.'));
}
if (!goog.isObject(obj[part]) && !goog.isFunction(obj[part])) {
this.set(obj, part, {});
}
obj = obj[part];
}
this.set(obj, parts[parts.length - 1], value);
};
/**
* Deletes the key from the object while saving its original value.
* @param {Object|Function} obj The JavaScript or native object or function to
* alter. See the constraints in the class description.
* @param {string} key The key to delete.
*/
goog.testing.PropertyReplacer.prototype.remove = function(obj, key) {
if (goog.testing.PropertyReplacer.hasKey_(obj, key)) {
this.original_.push({object: obj, key: key, value: obj[key]});
goog.testing.PropertyReplacer.deleteKey_(obj, key);
}
};
/**
* Restore the original state of key in an object.
* @param {!Object|!Function} obj The JavaScript or native object whose state
* should be restored.
* @param {string} key The key to restore the original value for.
* @throws {Error} In case the object/key pair hadn't been modified earlier.
*/
goog.testing.PropertyReplacer.prototype.restore = function(obj, key) {
for (var i = this.original_.length - 1; i >= 0; i--) {
var original = this.original_[i];
if (original.object === obj && original.key == key) {
goog.testing.PropertyReplacer.restoreOriginal_(original);
this.original_.splice(i, 1);
return;
}
}
throw new Error('Cannot restore unmodified property "' + key + '" of ' + obj);
};
/**
* Resets all changes made by goog.testing.PropertyReplacer.prototype.set.
*/
goog.testing.PropertyReplacer.prototype.reset = function() {
for (var i = this.original_.length - 1; i >= 0; i--) {
goog.testing.PropertyReplacer.restoreOriginal_(this.original_[i]);
delete this.original_[i];
}
this.original_.length = 0;
};
|