// 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.provide('goog.testing.PropertyReplacer');
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.set(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:
*
*
DOM subclasses aren't supported.
*
The value of the objects' constructor property must either be equal to
* the real constructor or kept untouched.
*
*
* @constructor
*/
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.