Debian Codegraph @

closure-library/closure/goog/testing/async/mockcontrol_test.js

raw
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.async.MockControlTest');
16 goog.setTestOnly('goog.testing.async.MockControlTest');
17
18 goog.require('goog.async.Deferred');
19 goog.require('goog.testing.MockControl');
20 goog.require('goog.testing.TestCase');
21 goog.require('goog.testing.asserts');
22 goog.require('goog.testing.async.MockControl');
23 goog.require('goog.testing.jsunit');
24
25 var mockControl;
26 var asyncMockControl;
27
28 var mockControl2;
29 var asyncMockControl2;
30
31 function setUp() {
32 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
33 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
34
35 mockControl = new goog.testing.MockControl();
36 asyncMockControl = new goog.testing.async.MockControl(mockControl);
37
38 // We need two of these for the tests where we need to verify our meta-test
39 // assertions without verifying our tested assertions.
40 mockControl2 = new goog.testing.MockControl();
41 asyncMockControl2 = new goog.testing.async.MockControl(mockControl2);
42 }
43
44 function assertVerifyFails() {
45 assertThrowsJsUnitException(function() { mockControl.$verifyAll(); });
46 }
47
48 function testCreateCallbackMockFailure() {
49 asyncMockControl.createCallbackMock('failingCallbackMock', function() {});
50 assertVerifyFails();
51 }
52
53 function testCreateCallbackMockSuccess() {
54 var callback = asyncMockControl.createCallbackMock(
55 'succeedingCallbackMock', function() {});
56 callback();
57 mockControl.$verifyAll();
58 }
59
60 function testCreateCallbackMockSuccessWithArg() {
61 var callback = asyncMockControl.createCallbackMock(
62 'succeedingCallbackMockWithArg',
63 asyncMockControl.createCallbackMock(
64 'metaCallbackMock', function(val) { assertEquals(10, val); }));
65 callback(10);
66 mockControl.$verifyAll();
67 }
68
69 function testCreateCallbackMockSuccessWithArgs() {
70 var callback = asyncMockControl.createCallbackMock(
71 'succeedingCallbackMockWithArgs',
72 asyncMockControl.createCallbackMock(
73 'metaCallbackMock', function(val1, val2, val3) {
74 assertEquals(10, val1);
75 assertEquals('foo', val2);
76 assertObjectEquals({foo: 'bar'}, val3);
77 }));
78 callback(10, 'foo', {foo: 'bar'});
79 mockControl.$verifyAll();
80 }
81
82 function testAsyncAssertEqualsFailureNeverCalled() {
83 asyncMockControl.asyncAssertEquals('never called', 12);
84 assertVerifyFails();
85 }
86
87 function testAsyncAssertEqualsFailureNumberOfArgs() {
88 assertThrowsJsUnitException(function() {
89 asyncMockControl.asyncAssertEquals('wrong number of args', 12)();
90 });
91 }
92
93 function testAsyncAssertEqualsFailureOneArg() {
94 assertThrowsJsUnitException(function() {
95 asyncMockControl.asyncAssertEquals('wrong arg value', 12)(13);
96 });
97 }
98
99 function testAsyncAssertEqualsFailureThreeArgs() {
100 assertThrowsJsUnitException(function() {
101 asyncMockControl.asyncAssertEquals('wrong arg values', 1, 2, 15)(2, 2, 15);
102 });
103 }
104
105 function testAsyncAssertEqualsSuccessNoArgs() {
106 asyncMockControl.asyncAssertEquals('should be called')();
107 mockControl.$verifyAll();
108 }
109
110 function testAsyncAssertEqualsSuccessThreeArgs() {
111 asyncMockControl.asyncAssertEquals('should have args', 1, 2, 3)(1, 2, 3);
112 mockControl.$verifyAll();
113 }
114
115 function testAssertDeferredErrorFailureNoError() {
116 var deferred = new goog.async.Deferred();
117 asyncMockControl.assertDeferredError(deferred, function() {});
118 assertVerifyFails();
119 }
120
121 function testAssertDeferredErrorSuccess() {
122 var deferred = new goog.async.Deferred();
123 asyncMockControl.assertDeferredError(
124 deferred, function() { deferred.errback(new Error('FAIL')); });
125 mockControl.$verifyAll();
126 }
127
128 function testAssertDeferredEqualsFailureActualDeferredNeverResolves() {
129 var actual = new goog.async.Deferred();
130 asyncMockControl.assertDeferredEquals('doesn\'t resolve', 12, actual);
131 assertVerifyFails();
132 }
133
134 function testAssertDeferredEqualsFailureActualDeferredNeverResolvesBoth() {
135 var actualDeferred = new goog.async.Deferred();
136 var expectedDeferred = new goog.async.Deferred();
137 expectedDeferred.callback(12);
138 asyncMockControl.assertDeferredEquals(
139 'doesn\'t resolve', expectedDeferred, actualDeferred);
140 assertVerifyFails();
141 }
142
143 function testAssertDeferredEqualsFailureExpectedDeferredNeverResolves() {
144 var expected = new goog.async.Deferred();
145 asyncMockControl.assertDeferredEquals('doesn\'t resolve', expected, 12);
146 assertVerifyFails();
147 }
148
149 function testAssertDeferredEqualsFailureExpectedDeferredNeverResolvesBoth() {
150 var actualDeferred = new goog.async.Deferred();
151 var expectedDeferred = new goog.async.Deferred();
152 actualDeferred.callback(12);
153 asyncMockControl.assertDeferredEquals(
154 'doesn\'t resolve', expectedDeferred, actualDeferred);
155 assertVerifyFails();
156 }
157
158 function testAssertDeferredEqualsFailureWrongValueActualDeferred() {
159 var actual = new goog.async.Deferred();
160 asyncMockControl.assertDeferredEquals('doesn\'t resolve', 12, actual);
161 asyncMockControl2.assertDeferredError(
162 actual, function() { actual.callback(13); });
163 mockControl2.$verifyAll();
164 }
165
166 function testAssertDeferredEqualsFailureWrongValueExpectedDeferred() {
167 var expected = new goog.async.Deferred();
168 asyncMockControl.assertDeferredEquals('doesn\'t resolve', expected, 12);
169 asyncMockControl2.assertDeferredError(
170 expected, function() { expected.callback(13); });
171 mockControl2.$verifyAll();
172 }
173
174 function testAssertDeferredEqualsFailureWongValueBothDeferred() {
175 var actualDeferred = new goog.async.Deferred();
176 var expectedDeferred = new goog.async.Deferred();
177 asyncMockControl.assertDeferredEquals(
178 'different values', expectedDeferred, actualDeferred);
179 expectedDeferred.callback(12);
180 asyncMockControl2.assertDeferredError(
181 actualDeferred, function() { actualDeferred.callback(13); });
182 assertVerifyFails();
183 mockControl2.$verifyAll();
184 }
185
186 function testAssertDeferredEqualsFailureNeitherDeferredEverResolves() {
187 var actualDeferred = new goog.async.Deferred();
188 var expectedDeferred = new goog.async.Deferred();
189 asyncMockControl.assertDeferredEquals(
190 'doesn\'t resolve', expectedDeferred, actualDeferred);
191 assertVerifyFails();
192 }
193
194 function testAssertDeferredEqualsSuccessActualDeferred() {
195 var actual = new goog.async.Deferred();
196 asyncMockControl.assertDeferredEquals('should succeed', 12, actual);
197 actual.callback(12);
198 mockControl.$verifyAll();
199 }
200
201 function testAssertDeferredEqualsSuccessExpectedDeferred() {
202 var expected = new goog.async.Deferred();
203 asyncMockControl.assertDeferredEquals('should succeed', expected, 12);
204 expected.callback(12);
205 mockControl.$verifyAll();
206 }
207
208 function testAssertDeferredEqualsSuccessBothDeferred() {
209 var actualDeferred = new goog.async.Deferred();
210 var expectedDeferred = new goog.async.Deferred();
211 asyncMockControl.assertDeferredEquals(
212 'should succeed', expectedDeferred, actualDeferred);
213 expectedDeferred.callback(12);
214 actualDeferred.callback(12);
215 mockControl.$verifyAll();
216 }