Debian Codegraph @

closure-library/closure/goog/testing/mockclassfactory_test.js

raw
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 goog.setTestOnly('goog.testing.MockClassFactoryTest');
16 goog.require('goog.testing');
17 goog.require('goog.testing.MockClassFactory');
18 goog.require('goog.testing.jsunit');
19 goog.provide('fake.BaseClass');
20 goog.provide('fake.ChildClass');
21 goog.provide('goog.testing.MockClassFactoryTest');
22
23 // Classes that will be mocked. A base class and child class are used to
24 // test inheritance.
25 fake.BaseClass = function(a) {
26 fail('real object should never be called');
27 };
28
29 fake.BaseClass.prototype.foo = function() {
30 fail('real object should never be called');
31 };
32
33 fake.BaseClass.prototype.toString = function() {
34 return 'foo';
35 };
36
37 fake.BaseClass.prototype.toLocaleString = function() {
38 return 'bar';
39 };
40
41 fake.BaseClass.prototype.overridden = function() {
42 return 42;
43 };
44
45 fake.ChildClass = function(a) {
46 fail('real object should never be called');
47 };
48 goog.inherits(fake.ChildClass, fake.BaseClass);
49
50 fake.ChildClass.staticFoo = function() {
51 fail('real object should never be called');
52 };
53
54 fake.ChildClass.prototype.bar = function() {
55 fail('real object should never be called');
56 };
57
58 fake.ChildClass.staticProperty = 'staticPropertyOnClass';
59
60 function TopLevelBaseClass() {}
61
62 fake.ChildClass.prototype.overridden = function() {
63 var superResult = fake.ChildClass.base(this, 'overridden');
64 if (superResult != 42) {
65 fail('super method not invoked or returned wrong value');
66 }
67 return superResult + 1;
68 };
69
70 var mockClassFactory = new goog.testing.MockClassFactory();
71 var matchers = goog.testing.mockmatchers;
72
73 function tearDown() {
74 mockClassFactory.reset();
75 }
76
77 function testGetStrictMockClass() {
78 var mock1 = mockClassFactory.getStrictMockClass(fake, fake.BaseClass, 1);
79 mock1.foo();
80 mock1.$replay();
81
82 var mock2 = mockClassFactory.getStrictMockClass(fake, fake.BaseClass, 2);
83 mock2.foo();
84 mock2.$replay();
85
86 var mock3 = mockClassFactory.getStrictMockClass(fake, fake.ChildClass, 3);
87 mock3.foo();
88 mock3.bar();
89 mock3.$replay();
90
91 var instance1 = new fake.BaseClass(1);
92 instance1.foo();
93 mock1.$verify();
94
95 var instance2 = new fake.BaseClass(2);
96 instance2.foo();
97 mock2.$verify();
98
99 var instance3 = new fake.ChildClass(3);
100 instance3.foo();
101 instance3.bar();
102 mock3.$verify();
103
104 assertThrows(function() { new fake.BaseClass(-1) });
105 assertTrue(instance1 instanceof fake.BaseClass);
106 assertTrue(instance2 instanceof fake.BaseClass);
107 assertTrue(instance3 instanceof fake.ChildClass);
108 }
109
110 function testGetStrictMockClassCreatesAllProxies() {
111 var mock1 = mockClassFactory.getStrictMockClass(fake, fake.BaseClass, 1);
112 // toString(), toLocaleString() and others are treaded specially in
113 // createProxy_().
114 mock1.toString();
115 mock1.toLocaleString();
116 mock1.$replay();
117
118 var instance1 = new fake.BaseClass(1);
119 instance1.toString();
120 instance1.toLocaleString();
121 mock1.$verify();
122 }
123
124 function testGetLooseMockClass() {
125 var mock1 = mockClassFactory.getLooseMockClass(fake, fake.BaseClass, 1);
126 mock1.foo().$anyTimes().$returns(3);
127 mock1.$replay();
128
129 var mock2 = mockClassFactory.getLooseMockClass(fake, fake.BaseClass, 2);
130 mock2.foo().$times(3);
131 mock2.$replay();
132
133 var mock3 = mockClassFactory.getLooseMockClass(fake, fake.ChildClass, 3);
134 mock3.foo().$atLeastOnce().$returns(5);
135 mock3.bar().$atLeastOnce();
136 mock3.$replay();
137
138 var instance1 = new fake.BaseClass(1);
139 assertEquals(3, instance1.foo());
140 assertEquals(3, instance1.foo());
141 assertEquals(3, instance1.foo());
142 assertEquals(3, instance1.foo());
143 assertEquals(3, instance1.foo());
144 mock1.$verify();
145
146 var instance2 = new fake.BaseClass(2);
147 instance2.foo();
148 instance2.foo();
149 instance2.foo();
150 mock2.$verify();
151
152 var instance3 = new fake.ChildClass(3);
153 assertEquals(5, instance3.foo());
154 assertEquals(5, instance3.foo());
155 instance3.bar();
156 mock3.$verify();
157
158 assertThrows(function() { new fake.BaseClass(-1) });
159 assertTrue(instance1 instanceof fake.BaseClass);
160 assertTrue(instance2 instanceof fake.BaseClass);
161 assertTrue(instance3 instanceof fake.ChildClass);
162 }
163
164 function testGetStrictStaticMock() {
165 var staticMock = mockClassFactory.getStrictStaticMock(fake, fake.ChildClass);
166 var mock = mockClassFactory.getStrictMockClass(fake, fake.ChildClass, 1);
167
168 mock.foo();
169 mock.bar();
170 staticMock.staticFoo();
171 mock.$replay();
172 staticMock.$replay();
173
174 var instance = new fake.ChildClass(1);
175 instance.foo();
176 instance.bar();
177 fake.ChildClass.staticFoo();
178 mock.$verify();
179 staticMock.$verify();
180
181 assertTrue(instance instanceof fake.BaseClass);
182 assertTrue(instance instanceof fake.ChildClass);
183 assertThrows(function() {
184 mockClassFactory.getLooseStaticMock(fake, fake.ChildClass);
185 });
186 }
187
188 function testGetStrictStaticMockKeepsStaticProperties() {
189 var OriginalChildClass = fake.ChildClass;
190 var staticMock = mockClassFactory.getStrictStaticMock(fake, fake.ChildClass);
191 assertEquals(
192 OriginalChildClass.staticProperty, fake.ChildClass.staticProperty);
193 }
194
195 function testGetLooseStaticMockKeepsStaticProperties() {
196 var OriginalChildClass = fake.ChildClass;
197 var staticMock = mockClassFactory.getLooseStaticMock(fake, fake.ChildClass);
198 assertEquals(
199 OriginalChildClass.staticProperty, fake.ChildClass.staticProperty);
200 }
201
202 function testGetLooseStaticMock() {
203 var staticMock = mockClassFactory.getLooseStaticMock(fake, fake.ChildClass);
204 var mock = mockClassFactory.getStrictMockClass(fake, fake.ChildClass, 1);
205
206 mock.foo();
207 mock.bar();
208 staticMock.staticFoo().$atLeastOnce();
209 mock.$replay();
210 staticMock.$replay();
211
212 var instance = new fake.ChildClass(1);
213 instance.foo();
214 instance.bar();
215 fake.ChildClass.staticFoo();
216 fake.ChildClass.staticFoo();
217 mock.$verify();
218 staticMock.$verify();
219
220 assertTrue(instance instanceof fake.BaseClass);
221 assertTrue(instance instanceof fake.ChildClass);
222 assertThrows(function() {
223 mockClassFactory.getStrictStaticMock(fake, fake.ChildClass);
224 });
225 }
226
227 function testFlexibleClassMockInstantiation() {
228 // This mock should be returned for all instances created with a number
229 // as the first argument.
230 var mock = mockClassFactory.getStrictMockClass(
231 fake, fake.ChildClass, matchers.isNumber);
232 mock.foo(); // Will be called by the first mock instance.
233 mock.foo(); // Will be called by the second mock instance.
234 mock.$replay();
235
236 var instance1 = new fake.ChildClass(1);
237 var instance2 = new fake.ChildClass(2);
238 instance1.foo();
239 instance2.foo();
240 assertThrows(function() { new fake.ChildClass('foo'); });
241 mock.$verify();
242 }
243
244 function testMockTopLevelClass() {
245 var mock = mockClassFactory.getStrictMockClass(
246 goog.global, goog.global.TopLevelBaseClass);
247 }
248
249 function testGoogBaseCall() {
250 var overriddenFn = fake.ChildClass.prototype.overridden;
251 var mock = mockClassFactory.getLooseMockClass(fake, fake.ChildClass, 1);
252 var instance1 = new fake.ChildClass(1);
253 assertTrue(43 == overriddenFn.call(instance1));
254 }