| 1 |
// Copyright 2010 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 |
goog.provide('goog.testing.recordFunctionTest'); |
| 16 |
goog.setTestOnly('goog.testing.recordFunctionTest'); |
| 17 |
|
| 18 |
goog.require('goog.functions'); |
| 19 |
goog.require('goog.testing.PropertyReplacer'); |
| 20 |
goog.require('goog.testing.TestCase'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.testing.recordConstructor'); |
| 23 |
goog.require('goog.testing.recordFunction'); |
| 24 |
|
| 25 |
var stubs = new goog.testing.PropertyReplacer(); |
| 26 |
|
| 27 |
function setUp() { |
| 28 |
// TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts). |
| 29 |
goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false; |
| 30 |
} |
| 31 |
|
| 32 |
function tearDown() { |
| 33 |
stubs.reset(); |
| 34 |
} |
| 35 |
|
| 36 |
function testNoCalls() { |
| 37 |
var f = goog.testing.recordFunction(goog.functions.identity); |
| 38 |
assertEquals('call count', 0, f.getCallCount()); |
| 39 |
assertNull('last call', f.getLastCall()); |
| 40 |
assertArrayEquals('all calls', [], f.getCalls()); |
| 41 |
} |
| 42 |
|
| 43 |
function testWithoutArguments() { |
| 44 |
var f = goog.testing.recordFunction(); |
| 45 |
assertUndefined('f(1)', f(1)); |
| 46 |
assertEquals('call count', 1, f.getCallCount()); |
| 47 |
var lastCall = f.getLastCall(); |
| 48 |
assertEquals('original function', goog.nullFunction, lastCall.getFunction()); |
| 49 |
assertEquals('this context', window, lastCall.getThis()); |
| 50 |
assertArrayEquals('arguments', [1], lastCall.getArguments()); |
| 51 |
assertEquals('arguments[0]', 1, lastCall.getArgument(0)); |
| 52 |
assertUndefined('arguments[1]', lastCall.getArgument(1)); |
| 53 |
assertUndefined('return value', lastCall.getReturnValue()); |
| 54 |
} |
| 55 |
|
| 56 |
function testWithIdentityFunction() { |
| 57 |
var f = goog.testing.recordFunction(goog.functions.identity); |
| 58 |
var dummyThis = {}; |
| 59 |
assertEquals('f(1)', 1, f(1)); |
| 60 |
assertEquals('f.call(dummyThis, 2)', 2, f.call(dummyThis, 2)); |
| 61 |
|
| 62 |
var calls = f.getCalls(); |
| 63 |
var firstCall = calls[0]; |
| 64 |
var lastCall = f.getLastCall(); |
| 65 |
assertEquals('call count', 2, f.getCallCount()); |
| 66 |
assertEquals('last call', calls[1], lastCall); |
| 67 |
assertEquals( |
| 68 |
'original function', goog.functions.identity, lastCall.getFunction()); |
| 69 |
assertEquals('this context of first call', window, firstCall.getThis()); |
| 70 |
assertEquals('this context of last call', dummyThis, lastCall.getThis()); |
| 71 |
assertArrayEquals( |
| 72 |
'arguments of the first call', [1], firstCall.getArguments()); |
| 73 |
assertArrayEquals('arguments of the last call', [2], lastCall.getArguments()); |
| 74 |
assertEquals('return value of the first call', 1, firstCall.getReturnValue()); |
| 75 |
assertEquals('return value of the last call', 2, lastCall.getReturnValue()); |
| 76 |
assertNull('error thrown by the first call', firstCall.getError()); |
| 77 |
assertNull('error thrown by the last call', lastCall.getError()); |
| 78 |
} |
| 79 |
|
| 80 |
function testWithErrorFunction() { |
| 81 |
var f = goog.testing.recordFunction(goog.functions.error('error')); |
| 82 |
|
| 83 |
var error = assertThrows('f(1) should throw an error', function() { f(1); }); |
| 84 |
assertEquals('error message', 'error', error.message); |
| 85 |
assertEquals('call count', 1, f.getCallCount()); |
| 86 |
var lastCall = f.getLastCall(); |
| 87 |
assertEquals('this context', window, lastCall.getThis()); |
| 88 |
assertArrayEquals('arguments', [1], lastCall.getArguments()); |
| 89 |
assertUndefined('return value', lastCall.getReturnValue()); |
| 90 |
assertEquals('recorded error message', 'error', lastCall.getError().message); |
| 91 |
} |
| 92 |
|
| 93 |
function testWithClass() { |
| 94 |
var ns = {}; |
| 95 |
/** @constructor */ |
| 96 |
ns.TestClass = function(num) { this.setX(ns.TestClass.identity(1) + num); }; |
| 97 |
ns.TestClass.prototype.setX = function(x) { this.x = x; }; |
| 98 |
ns.TestClass.identity = function(x) { return x; }; |
| 99 |
var originalNsTestClass = ns.TestClass; |
| 100 |
|
| 101 |
stubs.set(ns, 'TestClass', goog.testing.recordConstructor(ns.TestClass)); |
| 102 |
stubs.set( |
| 103 |
ns.TestClass.prototype, 'setX', |
| 104 |
goog.testing.recordFunction(ns.TestClass.prototype.setX)); |
| 105 |
stubs.set( |
| 106 |
ns.TestClass, 'identity', |
| 107 |
goog.testing.recordFunction(ns.TestClass.identity)); |
| 108 |
|
| 109 |
var obj = new ns.TestClass(2); |
| 110 |
assertEquals('constructor is called once', 1, ns.TestClass.getCallCount()); |
| 111 |
var lastConstructorCall = ns.TestClass.getLastCall(); |
| 112 |
assertArrayEquals( |
| 113 |
'... with argument 2', [2], lastConstructorCall.getArguments()); |
| 114 |
assertEquals('the created object', obj, lastConstructorCall.getThis()); |
| 115 |
assertEquals( |
| 116 |
'type of the created object', originalNsTestClass, obj.constructor); |
| 117 |
|
| 118 |
assertEquals('setX is called once', 1, obj.setX.getCallCount()); |
| 119 |
assertArrayEquals( |
| 120 |
'... with argument 3', [3], obj.setX.getLastCall().getArguments()); |
| 121 |
assertEquals('The x field is properly set', 3, obj.x); |
| 122 |
|
| 123 |
assertEquals( |
| 124 |
'identity is called once', 1, ns.TestClass.identity.getCallCount()); |
| 125 |
assertArrayEquals( |
| 126 |
'... with argument 1', [1], |
| 127 |
ns.TestClass.identity.getLastCall().getArguments()); |
| 128 |
} |
| 129 |
|
| 130 |
function testPopLastCall() { |
| 131 |
var f = goog.testing.recordFunction(); |
| 132 |
f(0); |
| 133 |
f(1); |
| 134 |
|
| 135 |
var firstCall = f.getCalls()[0]; |
| 136 |
var lastCall = f.getCalls()[1]; |
| 137 |
assertEquals('return value of popLastCall', lastCall, f.popLastCall()); |
| 138 |
assertArrayEquals('1 call remains', [firstCall], f.getCalls()); |
| 139 |
assertEquals('next return value of popLastCall', firstCall, f.popLastCall()); |
| 140 |
assertArrayEquals('0 calls remain', [], f.getCalls()); |
| 141 |
assertNull('no more calls to pop', f.popLastCall()); |
| 142 |
} |
| 143 |
|
| 144 |
function testReset() { |
| 145 |
var f = goog.testing.recordFunction(); |
| 146 |
f(0); |
| 147 |
f(1); |
| 148 |
|
| 149 |
assertEquals('Should be two calls.', 2, f.getCallCount()); |
| 150 |
|
| 151 |
f.reset(); |
| 152 |
|
| 153 |
assertEquals('Call count should reset.', 0, f.getCallCount()); |
| 154 |
} |
| 155 |
|
| 156 |
function testAssertCallCount() { |
| 157 |
var f = goog.testing.recordFunction(goog.functions.identity); |
| 158 |
|
| 159 |
f.assertCallCount(0); |
| 160 |
f.assertCallCount('Unexpected failure.', 0); |
| 161 |
|
| 162 |
f('Poodles'); |
| 163 |
f.assertCallCount(1); |
| 164 |
f.assertCallCount('Unexpected failure.', 1); |
| 165 |
|
| 166 |
f('Hopscotch'); |
| 167 |
f.assertCallCount(2); |
| 168 |
|
| 169 |
f.reset(); |
| 170 |
f.assertCallCount(0); |
| 171 |
|
| 172 |
f('Bedazzler'); |
| 173 |
f.assertCallCount(1); |
| 174 |
|
| 175 |
var error = assertThrows(function() { f.assertCallCount(11); }); |
| 176 |
assertEquals(error.comment, 'Expected 11 call(s), but was 1.'); |
| 177 |
|
| 178 |
var comment = 'This application has requested the Runtime to terminate it ' + |
| 179 |
'in an unusual way.'; |
| 180 |
var error2 = assertThrows(function() { f.assertCallCount(comment, 12); }); |
| 181 |
assertEquals(error2.comment, 'Expected 12 call(s), but was 1. ' + comment); |
| 182 |
} |