aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/recordfunction_test.html
blob: bb78aeecefcb4ae651bc6056f43f0f727e416eb1 (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
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.testing.recordFunction</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.functions');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.recordConstructor');
  goog.require('goog.testing.recordFunction');
</script>
</head>
<body>
<script>

var stubs = new goog.testing.PropertyReplacer();

function tearDown() {
  stubs.reset();
}

function testNoCalls() {
  var f = goog.testing.recordFunction(goog.functions.identity);
  assertEquals('call count', 0, f.getCallCount());
  assertNull('last call', f.getLastCall());
  assertArrayEquals('all calls', [], f.getCalls());
}

function testWithoutArguments() {
  var f = goog.testing.recordFunction();
  assertUndefined('f(1)', f(1));
  assertEquals('call count', 1, f.getCallCount());
  var lastCall = f.getLastCall();
  assertEquals('original function', goog.nullFunction, lastCall.getFunction());
  assertEquals('this context', window, lastCall.getThis());
  assertArrayEquals('arguments', [1], lastCall.getArguments());
  assertEquals('arguments[0]', 1, lastCall.getArgument(0));
  assertUndefined('arguments[1]', lastCall.getArgument(1));
  assertUndefined('return value', lastCall.getReturnValue());
}

function testWithIdentityFunction() {
  var f = goog.testing.recordFunction(goog.functions.identity);
  var dummyThis = {};
  assertEquals('f(1)', 1, f(1));
  assertEquals('f.call(dummyThis, 2)', 2, f.call(dummyThis, 2));

  var calls = f.getCalls();
  var firstCall = calls[0];
  var lastCall = f.getLastCall();
  assertEquals('call count', 2, f.getCallCount());
  assertEquals('last call', calls[1], lastCall);
  assertEquals('original function', goog.functions.identity,
      lastCall.getFunction());
  assertEquals('this context of first call', window, firstCall.getThis());
  assertEquals('this context of last call', dummyThis, lastCall.getThis());
  assertArrayEquals('arguments of the first call', [1],
      firstCall.getArguments());
  assertArrayEquals('arguments of the last call', [2], lastCall.getArguments());
  assertEquals('return value of the first call', 1, firstCall.getReturnValue());
  assertEquals('return value of the last call', 2, lastCall.getReturnValue());
  assertNull('error thrown by the first call', firstCall.getError());
  assertNull('error thrown by the last call', lastCall.getError());
}

function testWithErrorFunction() {
  var f = goog.testing.recordFunction(goog.functions.error('error'));

  var error = assertThrows('f(1) should throw an error', function() {
    f(1);
  });
  assertEquals('error message', 'error', error.message);
  assertEquals('call count', 1, f.getCallCount());
  var lastCall = f.getLastCall();
  assertEquals('this context', window, lastCall.getThis());
  assertArrayEquals('arguments', [1], lastCall.getArguments());
  assertUndefined('return value', lastCall.getReturnValue());
  assertEquals('recorded error message', 'error', lastCall.getError().message);
}

function testWithClass() {
  var ns = {};
  /** @constructor */
  ns.TestClass = function(num) {
    this.setX(ns.TestClass.identity(1) + num);
  }
  ns.TestClass.prototype.setX = function(x) {
    this.x = x;
  };
  ns.TestClass.identity = function(x) {
    return x;
  };
  var originalNsTestClass = ns.TestClass;

  stubs.set(ns, 'TestClass', goog.testing.recordConstructor(ns.TestClass));
  stubs.set(ns.TestClass.prototype, 'setX',
      goog.testing.recordFunction(ns.TestClass.prototype.setX));
  stubs.set(ns.TestClass, 'identity',
      goog.testing.recordFunction(ns.TestClass.identity));

  var obj = new ns.TestClass(2);
  assertEquals('constructor is called once', 1, ns.TestClass.getCallCount());
  var lastConstructorCall = ns.TestClass.getLastCall();
  assertArrayEquals('... with argument 2', [2],
      lastConstructorCall.getArguments());
  assertEquals('the created object', obj, lastConstructorCall.getThis());
  assertEquals('type of the created object', originalNsTestClass,
      obj.constructor);

  assertEquals('setX is called once', 1, obj.setX.getCallCount());
  assertArrayEquals('... with argument 3', [3],
      obj.setX.getLastCall().getArguments());
  assertEquals('The x field is properly set', 3, obj.x);

  assertEquals('identity is called once', 1,
      ns.TestClass.identity.getCallCount());
  assertArrayEquals('... with argument 1', [1],
      ns.TestClass.identity.getLastCall().getArguments());
}

function testPopLastCall() {
  var f = goog.testing.recordFunction();
  f(0);
  f(1);

  var firstCall = f.getCalls()[0];
  var lastCall = f.getCalls()[1];
  assertEquals('return value of popLastCall', lastCall, f.popLastCall());
  assertArrayEquals('1 call remains', [firstCall], f.getCalls());
  assertEquals('next return value of popLastCall', firstCall, f.popLastCall());
  assertArrayEquals('0 calls remain', [], f.getCalls());
  assertNull('no more calls to pop', f.popLastCall());
}

function testReset() {
  var f = goog.testing.recordFunction();
  f(0);
  f(1);

  assertEquals('Should be two calls.', 2, f.getCallCount());

  f.reset();

  assertEquals('Call count should reset.', 0, f.getCallCount());
}

</script>
</body>
</html>