Debian Codegraph @

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

raw
1 // Copyright 2014 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.TestCaseTest');
16 goog.setTestOnly('goog.testing.TestCaseTest');
17
18 goog.require('goog.Promise');
19 goog.require('goog.Timer');
20 goog.require('goog.functions');
21 goog.require('goog.string');
22 goog.require('goog.testing.ExpectedFailures');
23 goog.require('goog.testing.JsUnitException');
24 goog.require('goog.testing.MethodMock');
25 goog.require('goog.testing.MockRandom');
26 goog.require('goog.testing.PropertyReplacer');
27 goog.require('goog.testing.TestCase');
28 goog.require('goog.testing.jsunit');
29
30
31 // Dual of fail().
32 var ok = function() {
33 assertTrue(true);
34 };
35
36 // Native Promise-based equivalent of ok().
37 var okPromise = function() {
38 return Promise.resolve(null);
39 };
40
41 // Native Promise-based equivalent of fail().
42 var failPromise = function() {
43 return Promise.reject(null);
44 };
45
46 // Native Promise-based test that returns promise which never resolves.
47 var neverResolvedPromise = function() {
48 return new Promise(function() {});
49 };
50
51 // goog.Promise-based equivalent of ok().
52 var okGoogPromise = function() {
53 return goog.Promise.resolve(null);
54 };
55
56 // goog.Promise-based equivalent of fail().
57 var failGoogPromise = function() {
58 return goog.Promise.reject(null);
59 };
60
61 // Native Promise-based test that returns promise which never resolves.
62 var neverResolvedGoogPromise = function() {
63 return new goog.Promise(function() {});
64 };
65
66 /** @type {!Array<string>} */
67 var events;
68
69 function setUp() {
70 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
71 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
72 events = [];
73 }
74
75 /**
76 * @param {string} name
77 * @return {function()}
78 */
79 function event(name) {
80 return function() {
81 events.push(name);
82 };
83 }
84
85 function testEmptyTestCase() {
86 var testCase = new goog.testing.TestCase();
87 testCase.runTests();
88 assertTrue(testCase.isSuccess());
89 var result = testCase.getResult();
90 assertTrue(result.complete);
91 assertEquals(0, result.totalCount);
92 assertEquals(0, result.runCount);
93 assertEquals(0, result.successCount);
94 assertEquals(0, result.errors.length);
95 }
96
97 function testEmptyTestCaseReturningPromise() {
98 return new goog.testing.TestCase().runTestsReturningPromise().then(
99 function(result) {
100 assertTrue(result.complete);
101 assertEquals(0, result.totalCount);
102 assertEquals(0, result.runCount);
103 assertEquals(0, result.successCount);
104 assertEquals(0, result.errors.length);
105 });
106 }
107
108 function testTestCase_SyncSuccess() {
109 var testCase = new goog.testing.TestCase();
110 testCase.addNewTest('foo', ok);
111 testCase.runTests();
112 assertTrue(testCase.isSuccess());
113 var result = testCase.getResult();
114 assertTrue(result.complete);
115 assertEquals(1, result.totalCount);
116 assertEquals(1, result.runCount);
117 assertEquals(1, result.successCount);
118 assertEquals(0, result.errors.length);
119 }
120
121 function testTestCaseReturningPromise_SyncSuccess() {
122 var testCase = new goog.testing.TestCase();
123 testCase.addNewTest('foo', ok);
124 return testCase.runTestsReturningPromise().then(function(result) {
125 assertTrue(result.complete);
126 assertEquals(1, result.totalCount);
127 assertEquals(1, result.runCount);
128 assertEquals(1, result.successCount);
129 assertEquals(0, result.errors.length);
130 });
131 }
132
133 function testTestCaseReturningPromise_GoogPromiseResolve() {
134 var testCase = new goog.testing.TestCase();
135 testCase.addNewTest('foo', okGoogPromise);
136 return testCase.runTestsReturningPromise().then(function(result) {
137 assertTrue(result.complete);
138 assertEquals(1, result.totalCount);
139 assertEquals(1, result.runCount);
140 assertEquals(1, result.successCount);
141 assertEquals(0, result.errors.length);
142 });
143 }
144
145 function testTestCaseReturningPromise_PromiseResolve() {
146 if (!('Promise' in goog.global)) {
147 return;
148 }
149 var testCase = new goog.testing.TestCase();
150 testCase.addNewTest('foo', okPromise);
151 return testCase.runTestsReturningPromise().then(function(result) {
152 assertTrue(result.complete);
153 assertEquals(1, result.totalCount);
154 assertEquals(1, result.runCount);
155 assertEquals(1, result.successCount);
156 assertEquals(0, result.errors.length);
157 });
158 }
159
160 function testTestCase_SyncFailure() {
161 var testCase = new goog.testing.TestCase();
162 testCase.addNewTest('foo', fail);
163 testCase.runTests();
164 assertFalse(testCase.isSuccess());
165 var result = testCase.getResult();
166 assertTrue(result.complete);
167 assertEquals(1, result.totalCount);
168 assertEquals(1, result.runCount);
169 assertEquals(0, result.successCount);
170 assertEquals(1, result.errors.length);
171 assertEquals('foo', result.errors[0].source);
172 }
173
174 function testTestCaseReturningPromise_SyncFailure() {
175 var testCase = new goog.testing.TestCase();
176 testCase.addNewTest('foo', fail);
177 return testCase.runTestsReturningPromise().then(function(result) {
178 assertFalse(testCase.isSuccess());
179 assertTrue(result.complete);
180 assertEquals(1, result.totalCount);
181 assertEquals(1, result.runCount);
182 assertEquals(0, result.successCount);
183 assertEquals(1, result.errors.length);
184 assertEquals('foo', result.errors[0].source);
185 });
186 }
187
188 function testTestCaseReturningPromise_GoogPromiseReject() {
189 var testCase = new goog.testing.TestCase();
190 testCase.addNewTest('foo', failGoogPromise);
191 return testCase.runTestsReturningPromise().then(function(result) {
192 assertFalse(testCase.isSuccess());
193 assertTrue(result.complete);
194 assertEquals(1, result.totalCount);
195 assertEquals(1, result.runCount);
196 assertEquals(0, result.successCount);
197 assertEquals(1, result.errors.length);
198 assertEquals('foo', result.errors[0].source);
199 });
200 }
201
202 function testTestCaseReturningPromise_GoogPromiseTimeout() {
203 var testCase = new goog.testing.TestCase();
204 testCase.addNewTest('foo', neverResolvedGoogPromise);
205 var startTimestamp = new Date().getTime();
206 // We have to decrease timeout for the artificial 'foo' test otherwise current
207 // test will timeout.
208 testCase.promiseTimeout = 500;
209 startTimestamp = new Date().getTime();
210 return testCase.runTestsReturningPromise().then(function(result) {
211 var elapsedTime = new Date().getTime() - startTimestamp;
212 assertFalse(testCase.isSuccess());
213 assertTrue(result.complete);
214 assertEquals(1, result.totalCount);
215 assertEquals(1, result.runCount);
216 assertEquals(0, result.successCount);
217 assertEquals(1, result.errors.length);
218 // Check that error message mentions test name.
219 assertTrue(goog.string.contains(result.errors[0].message, 'foo'));
220 // Check that error message mentions how to change timeout.
221 assertTrue(
222 goog.string.contains(
223 result.errors[0].message,
224 'goog.testing.TestCase.getActiveTestCase().promiseTimeout'));
225 assertTrue(
226 elapsedTime >= testCase.promiseTimeout - 100 &&
227 elapsedTime <= testCase.promiseTimeout + 100);
228 });
229 }
230
231 function testTestCaseReturningPromise_PromiseReject() {
232 if (!('Promise' in goog.global)) {
233 return;
234 }
235 var testCase = new goog.testing.TestCase();
236 testCase.addNewTest('foo', failPromise);
237 return testCase.runTestsReturningPromise().then(function(result) {
238 assertFalse(testCase.isSuccess());
239 assertTrue(result.complete);
240 assertEquals(1, result.totalCount);
241 assertEquals(1, result.runCount);
242 assertEquals(0, result.successCount);
243 assertEquals(1, result.errors.length);
244 assertEquals('foo', result.errors[0].source);
245 });
246 }
247
248 function testTestCaseReturningPromise_PromiseTimeout() {
249 if (!('Promise' in goog.global)) {
250 return;
251 }
252 var testCase = new goog.testing.TestCase();
253 testCase.addNewTest('foo', neverResolvedPromise);
254 // We have to decrease timeout for the artificial 'foo' test otherwise current
255 // test will timeout.
256 testCase.promiseTimeout = 500;
257 var startTimestamp = new Date().getTime();
258 return testCase.runTestsReturningPromise().then(function(result) {
259 var elapsedTime = new Date().getTime() - startTimestamp;
260 assertFalse(testCase.isSuccess());
261 assertTrue(result.complete);
262 assertEquals(1, result.totalCount);
263 assertEquals(1, result.runCount);
264 assertEquals(0, result.successCount);
265 assertEquals(1, result.errors.length);
266 // Check that error message mentions test name.
267 assertTrue(goog.string.contains(result.errors[0].message, 'foo'));
268 // Check that error message mentions how to change timeout.
269 assertTrue(
270 goog.string.contains(
271 result.errors[0].message,
272 'goog.testing.TestCase.getActiveTestCase().promiseTimeout'));
273 assertTrue(
274 elapsedTime >= testCase.promiseTimeout - 100 &&
275 elapsedTime <= testCase.promiseTimeout + 100);
276 });
277 }
278
279 function testTestCase_SyncSuccess_SyncFailure() {
280 var testCase = new goog.testing.TestCase();
281 testCase.addNewTest('foo', ok);
282 testCase.addNewTest('bar', fail);
283 testCase.runTests();
284 assertFalse(testCase.isSuccess());
285 var result = testCase.getResult();
286 assertTrue(result.complete);
287 assertEquals(2, result.totalCount);
288 assertEquals(2, result.runCount);
289 assertEquals(1, result.successCount);
290 assertEquals(1, result.errors.length);
291 assertEquals('bar', result.errors[0].source);
292 }
293
294 function testTestCaseReturningPromise_SyncSuccess_SyncFailure() {
295 var testCase = new goog.testing.TestCase();
296 testCase.addNewTest('foo', ok);
297 testCase.addNewTest('bar', fail);
298 return testCase.runTestsReturningPromise().then(function(result) {
299 assertTrue(result.complete);
300 assertEquals(2, result.totalCount);
301 assertEquals(2, result.runCount);
302 assertEquals(1, result.successCount);
303 assertEquals(1, result.errors.length);
304 assertEquals('bar', result.errors[0].source);
305 });
306 }
307
308 function testTestCaseReturningPromise_GoogPromiseResolve_GoogPromiseReject() {
309 var testCase = new goog.testing.TestCase();
310 testCase.addNewTest('foo', okGoogPromise);
311 testCase.addNewTest('bar', failGoogPromise);
312 return testCase.runTestsReturningPromise().then(function(result) {
313 assertTrue(result.complete);
314 assertEquals(2, result.totalCount);
315 assertEquals(2, result.runCount);
316 assertEquals(1, result.successCount);
317 assertEquals(1, result.errors.length);
318 assertEquals('bar', result.errors[0].source);
319 });
320 }
321
322 function testTestCaseReturningPromise_PromiseResolve_PromiseReject() {
323 if (!('Promise' in goog.global)) {
324 return;
325 }
326 var testCase = new goog.testing.TestCase();
327 testCase.addNewTest('foo', okPromise);
328 testCase.addNewTest('bar', failPromise);
329 return testCase.runTestsReturningPromise().then(function(result) {
330 assertTrue(result.complete);
331 assertEquals(2, result.totalCount);
332 assertEquals(2, result.runCount);
333 assertEquals(1, result.successCount);
334 assertEquals(1, result.errors.length);
335 assertEquals('bar', result.errors[0].source);
336 });
337 }
338
339 function testTestCaseReturningPromise_PromiseResolve_GoogPromiseReject() {
340 if (!('Promise' in goog.global)) {
341 return;
342 }
343 var testCase = new goog.testing.TestCase();
344 testCase.addNewTest('foo', okPromise);
345 testCase.addNewTest('bar', failGoogPromise);
346 return testCase.runTestsReturningPromise().then(function(result) {
347 assertTrue(result.complete);
348 assertEquals(2, result.totalCount);
349 assertEquals(2, result.runCount);
350 assertEquals(1, result.successCount);
351 assertEquals(1, result.errors.length);
352 assertEquals('bar', result.errors[0].source);
353 });
354 }
355
356 function testTestCaseReturningPromise_GoogPromiseResolve_PromiseReject() {
357 if (!('Promise' in goog.global)) {
358 return;
359 }
360 var testCase = new goog.testing.TestCase();
361 testCase.addNewTest('foo', okGoogPromise);
362 testCase.addNewTest('bar', failPromise);
363 return testCase.runTestsReturningPromise().then(function(result) {
364 assertTrue(result.complete);
365 assertEquals(2, result.totalCount);
366 assertEquals(2, result.runCount);
367 assertEquals(1, result.successCount);
368 assertEquals(1, result.errors.length);
369 assertEquals('bar', result.errors[0].source);
370 });
371 }
372
373 function testTestCaseReturningPromise_PromisesInSetUpAndTest() {
374 if (!('Promise' in goog.global)) {
375 return;
376 }
377 var testCase = new goog.testing.TestCase();
378 testCase.setUpPage = function() {
379 event('setUpPage-called')();
380 return goog.Timer.promise().then(function() {
381 event('setUpPage-promiseFinished')();
382 });
383 };
384 testCase.setUp = function() {
385 event('setUp-called')();
386 return goog.Timer.promise().then(function() {
387 event('setUp-promiseFinished')();
388 });
389 };
390 testCase.addNewTest('foo', function() {
391 event('foo-called')();
392 return goog.Timer.promise().then(function() {
393 event('foo-promiseFinished')();
394 });
395 });
396
397 // Initially only setUpPage should have been called.
398 return testCase.runTestsReturningPromise().then(function(result) {
399 assertTrue(result.complete);
400 assertEquals(1, result.totalCount);
401 assertEquals(1, result.runCount);
402 assertEquals(1, result.successCount);
403 assertEquals(0, result.errors.length);
404
405 assertArrayEquals(
406 [
407 'setUpPage-called', 'setUpPage-promiseFinished', 'setUp-called',
408 'setUp-promiseFinished', 'foo-called', 'foo-promiseFinished'
409 ],
410 events);
411 });
412 }
413
414 function testTestCaseNeverRun() {
415 var testCase = new goog.testing.TestCase();
416 testCase.addNewTest('foo', fail);
417 // Missing testCase.runTests()
418 var result = testCase.getResult();
419 assertFalse(result.complete);
420 assertEquals(0, result.totalCount);
421 assertEquals(0, result.runCount);
422 assertEquals(0, result.successCount);
423 assertEquals(0, result.errors.length);
424 }
425
426 function testParseOrder() {
427 assertNull(goog.testing.TestCase.parseOrder_(''));
428 assertNull(goog.testing.TestCase.parseOrder_('?order=invalid'));
429 assertEquals('natural', goog.testing.TestCase.parseOrder_('?order=natural'));
430 assertEquals('sorted', goog.testing.TestCase.parseOrder_('?a&order=sorted'));
431 assertEquals('random', goog.testing.TestCase.parseOrder_('?b&order=random'));
432 assertEquals('random', goog.testing.TestCase.parseOrder_('?ORDER=RANDOM'));
433 }
434
435 function testParseRunTests() {
436 assertNull(goog.testing.TestCase.parseRunTests_(''));
437 assertNull(goog.testing.TestCase.parseRunTests_('?runTests='));
438 assertObjectEquals(
439 {'testOne': true},
440 goog.testing.TestCase.parseRunTests_('?runTests=testOne'));
441 assertObjectEquals(
442 {'testOne': true, 'testTwo': true},
443 goog.testing.TestCase.parseRunTests_(
444 '?foo=bar&runTests=testOne,testTwo'));
445 assertObjectEquals(
446 {
447 '1': true,
448 '2': true,
449 '3': true,
450 'testShouting': true,
451 'TESTSHOUTING': true
452 },
453 goog.testing.TestCase.parseRunTests_(
454 '?RUNTESTS=testShouting,TESTSHOUTING,1,2,3'));
455 }
456
457 function testSortOrder_natural() {
458 var testCase = new goog.testing.TestCase();
459 testCase.setOrder('natural');
460
461 var testIndex = 0;
462 testCase.addNewTest('test_c', function() { assertEquals(0, testIndex++); });
463 testCase.addNewTest('test_a', function() { assertEquals(1, testIndex++); });
464 testCase.addNewTest('test_b', function() { assertEquals(2, testIndex++); });
465 testCase.orderTests_();
466 testCase.runTests();
467
468 assertTrue(testCase.isSuccess());
469 var result = testCase.getResult();
470 assertEquals(3, result.totalCount);
471 assertEquals(3, result.runCount);
472 assertEquals(3, result.successCount);
473 assertEquals(0, result.errors.length);
474 }
475
476 function testSortOrder_random() {
477 var testCase = new goog.testing.TestCase();
478 testCase.setOrder('random');
479
480 var testIndex = 0;
481 testCase.addNewTest('test_c', function() { assertEquals(0, testIndex++); });
482 testCase.addNewTest('test_a', function() { assertEquals(2, testIndex++); });
483 testCase.addNewTest('test_b', function() { assertEquals(1, testIndex++); });
484
485 var mockRandom = new goog.testing.MockRandom([0.5, 0.5]);
486 mockRandom.install();
487 try {
488 testCase.orderTests_();
489 } finally {
490 // Avoid using a global tearDown() for cleanup, since all TestCase instances
491 // auto-detect and share the global life cycle functions.
492 mockRandom.uninstall();
493 }
494
495 testCase.runTests();
496
497 assertTrue(testCase.isSuccess());
498 var result = testCase.getResult();
499 assertEquals(3, result.totalCount);
500 assertEquals(3, result.runCount);
501 assertEquals(3, result.successCount);
502 assertEquals(0, result.errors.length);
503 }
504
505 function testSortOrder_sorted() {
506 var testCase = new goog.testing.TestCase();
507 testCase.setOrder('sorted');
508
509 var testIndex = 0;
510 testCase.addNewTest('test_c', function() { assertEquals(2, testIndex++); });
511 testCase.addNewTest('test_a', function() { assertEquals(0, testIndex++); });
512 testCase.addNewTest('test_b', function() { assertEquals(1, testIndex++); });
513 testCase.orderTests_();
514 testCase.runTests();
515
516 assertTrue(testCase.isSuccess());
517 var result = testCase.getResult();
518 assertEquals(3, result.totalCount);
519 assertEquals(3, result.runCount);
520 assertEquals(3, result.successCount);
521 assertEquals(0, result.errors.length);
522 }
523
524 function testRunTests() {
525 var testCase = new goog.testing.TestCase();
526 testCase.setTestsToRun({'test_a': true, 'test_c': true});
527
528 var testIndex = 0;
529 testCase.addNewTest('test_c', function() { assertEquals(0, testIndex++); });
530 testCase.addNewTest('test_a', function() { assertEquals(1, testIndex++); });
531 testCase.addNewTest('test_b', fail);
532 testCase.runTests();
533
534 assertTrue(testCase.isSuccess());
535 var result = testCase.getResult();
536 assertEquals(3, result.totalCount);
537 assertEquals(2, result.runCount);
538 assertEquals(2, result.successCount);
539 assertEquals(0, result.errors.length);
540 }
541
542 function testRunTests_byIndex() {
543 var testCase = new goog.testing.TestCase();
544 testCase.setTestsToRun({'0': true, '2': true});
545
546 var testIndex = 0;
547 testCase.addNewTest('test_c', function() { assertEquals(0, testIndex++); });
548 testCase.addNewTest('test_a', fail);
549 testCase.addNewTest('test_b', function() { assertEquals(1, testIndex++); });
550 testCase.runTests();
551
552 assertTrue(testCase.isSuccess());
553 var result = testCase.getResult();
554 assertEquals(3, result.totalCount);
555 assertEquals(2, result.runCount);
556 assertEquals(2, result.successCount);
557 assertEquals(0, result.errors.length);
558 }
559
560 function testMaybeFailTestEarly() {
561 var message = 'Error in setUpPage().';
562 var testCase = new goog.testing.TestCase();
563 testCase.setUpPage = function() {
564 throw new Error(message);
565 };
566 testCase.addNewTest('test', ok);
567 testCase.runTests();
568 assertFalse(testCase.isSuccess());
569 var errors = testCase.getResult().errors;
570 assertEquals(1, errors.length);
571 assertEquals(message, errors[0].message);
572 }
573
574 function testSetUpReturnsPromiseThatTimesOut() {
575 var testCase = new goog.testing.TestCase();
576 testCase.promiseTimeout = 500;
577 testCase.setUp = neverResolvedGoogPromise;
578 testCase.addNewTest('test', ok);
579 return testCase.runTestsReturningPromise().then(function(result) {
580 assertFalse(testCase.isSuccess());
581 assertTrue(result.complete);
582 assertEquals(1, result.errors.length);
583 assertTrue(goog.string.contains(result.errors[0].message, 'setUp'));
584 });
585 }
586
587 function testTearDownReturnsPromiseThatTimesOut() {
588 var testCase = new goog.testing.TestCase();
589 testCase.promiseTimeout = 500;
590 testCase.tearDown = neverResolvedGoogPromise;
591 testCase.addNewTest('test', ok);
592 return testCase.runTestsReturningPromise().then(function(result) {
593 assertFalse(testCase.isSuccess());
594 assertTrue(result.complete);
595 assertEquals(1, result.errors.length);
596 assertTrue(goog.string.contains(result.errors[0].message, 'tearDown'));
597 });
598 }
599
600 function testFailOnUnreportedAsserts_EnabledByDefault() {
601 assertTrue(new goog.testing.TestCase().failOnUnreportedAsserts);
602 }
603
604
605 /**
606 * Verifies that:
607 * <ol>
608 * <li>when the {@code failOnUnreportedAsserts} flag is disabled, the test
609 * function passes;
610 * <li>when the {@code failOnUnreportedAsserts} flag is enabled, the test
611 * function passes if {@code shouldPassWithFlagEnabled} is true and fails if
612 * it is false; and that
613 * <li>when the {@code failOnUnreportedAsserts} flag is enabled, and in addition
614 * {@code invalidateAssertionException} is stubbed out to do nothing, the
615 * test function fails.
616 * </ol>
617 * @param {boolean} shouldPassWithFlagEnabled
618 * @param {function(): !goog.Promise} testFunction
619 * @return {!goog.Promise}
620 */
621 function verifyTestOutcomeForFailOnUnreportedAssertsFlag(
622 shouldPassWithFlagEnabled, testFunction) {
623 return verifyWithFlagEnabledAndNoInvalidation(testFunction)
624 .then(function() {
625 return verifyWithFlagEnabled(testFunction, shouldPassWithFlagEnabled);
626 })
627 .then(function() { return verifyWithFlagDisabled(testFunction); });
628 }
629
630 function verifyWithFlagDisabled(testFunction) {
631 // With the flag disabled, the test is expected to pass, as any caught
632 // exception would not be reported.
633 var testCase = new goog.testing.TestCase();
634 var getTestCase = goog.functions.constant(testCase);
635 testCase.addNewTest('test', testFunction);
636 testCase.failOnUnreportedAsserts = false;
637
638 var stubs = new goog.testing.PropertyReplacer();
639 stubs.replace(window, '_getCurrentTestCase', getTestCase);
640 stubs.replace(goog.testing.TestCase, 'getActiveTestCase', getTestCase);
641
642 var promise = new goog
643 .Promise(function(resolve, reject) {
644 testCase.setCompletedCallback(resolve);
645 })
646 .then(function() {
647 assertTrue(testCase.isSuccess());
648 var result = testCase.getResult();
649 assertTrue(result.complete);
650 assertEquals(0, result.errors.length);
651 })
652 .thenAlways(function() { stubs.reset(); });
653
654 testCase.runTests();
655 return promise;
656 }
657
658 function verifyWithFlagEnabled(testFunction, shouldPassWithFlagEnabled) {
659 // With the flag enabled, the test is expected to pass if shouldPassWithFlag
660 // is true, and fail if shouldPassWithFlag is false.
661 var testCase = new goog.testing.TestCase();
662 var getTestCase = goog.functions.constant(testCase);
663 testCase.addNewTest('test', testFunction);
664 testCase.failOnUnreportedAsserts = true;
665
666 var stubs = new goog.testing.PropertyReplacer();
667 stubs.replace(window, '_getCurrentTestCase', getTestCase);
668 stubs.replace(goog.testing.TestCase, 'getActiveTestCase', getTestCase);
669
670 var promise =
671 new goog
672 .Promise(function(resolve, reject) {
673 testCase.setCompletedCallback(resolve);
674 })
675 .then(function() {
676 assertEquals(shouldPassWithFlagEnabled, testCase.isSuccess());
677 var result = testCase.getResult();
678 assertTrue(result.complete);
679 // Expect both the caught assertion and the failOnUnreportedAsserts
680 // error.
681 assertEquals(
682 shouldPassWithFlagEnabled ? 0 : 2, result.errors.length);
683 })
684 .thenAlways(function() { stubs.reset(); });
685
686 testCase.runTests();
687 return promise;
688 }
689
690 function verifyWithFlagEnabledAndNoInvalidation(testFunction) {
691 // With the flag enabled, the test is expected to pass if shouldPassWithFlag
692 // is true, and fail if shouldPassWithFlag is false.
693 var testCase = new goog.testing.TestCase();
694 var getTestCase = goog.functions.constant(testCase);
695 testCase.addNewTest('test', testFunction);
696 testCase.failOnUnreportedAsserts = true;
697
698 var stubs = new goog.testing.PropertyReplacer();
699 stubs.replace(window, '_getCurrentTestCase', getTestCase);
700 stubs.replace(goog.testing.TestCase, 'getActiveTestCase', getTestCase);
701 stubs.replace(
702 goog.testing.TestCase.prototype, 'invalidateAssertionException',
703 goog.nullFunction);
704
705 var promise = new goog
706 .Promise(function(resolve, reject) {
707 testCase.setCompletedCallback(resolve);
708 })
709 .then(function() {
710 assertFalse(testCase.isSuccess());
711 var result = testCase.getResult();
712 assertTrue(result.complete);
713 // Expect both the caught assertion and the
714 // failOnUnreportedAsserts error.
715 assertEquals(2, result.errors.length);
716 })
717 .thenAlways(function() { stubs.reset(); });
718
719 testCase.runTests();
720 return promise;
721 }
722
723 function testFailOnUnreportedAsserts_SwallowedException() {
724 return verifyTestOutcomeForFailOnUnreportedAssertsFlag(false, function() {
725 try {
726 assertTrue(false);
727 } catch (e) {
728 // Swallow the exception generated by the assertion.
729 }
730 });
731 }
732
733 function testFailOnUnreportedAsserts_SwallowedFail() {
734 return verifyTestOutcomeForFailOnUnreportedAssertsFlag(false, function() {
735 try {
736 fail();
737 } catch (e) {
738 // Swallow the exception generated by fail.
739 }
740 });
741 }
742
743 function testFailOnUnreportedAsserts_SwallowedAssertThrowsException() {
744 return verifyTestOutcomeForFailOnUnreportedAssertsFlag(false, function() {
745 try {
746 assertThrows(goog.nullFunction);
747 } catch (e) {
748 // Swallow the exception generated by assertThrows.
749 }
750 });
751 }
752
753 function testFailOnUnreportedAsserts_SwallowedAssertNotThrowsException() {
754 return verifyTestOutcomeForFailOnUnreportedAssertsFlag(false, function() {
755 try {
756 assertNotThrows(goog.functions.error());
757 } catch (e) {
758 // Swallow the exception generated by assertNotThrows.
759 }
760 });
761 }
762
763 function testFailOnUnreportedAsserts_SwallowedExceptionViaPromise() {
764 return verifyTestOutcomeForFailOnUnreportedAssertsFlag(false, function() {
765 return goog.Promise.resolve()
766 .then(function() { assertTrue(false); })
767 .thenCatch(function(e) {
768 // Swallow the exception generated by the assertion.
769 });
770 });
771 }
772
773 function testFailOnUnreportedAsserts_NotForAssertThrowsJsUnitException() {
774 return verifyTestOutcomeForFailOnUnreportedAssertsFlag(true, function() {
775 assertThrowsJsUnitException(function() { assertTrue(false); });
776 });
777 }
778
779 function testFailOnUnreportedAsserts_NotForExpectedFailures() {
780 return verifyTestOutcomeForFailOnUnreportedAssertsFlag(true, function() {
781 var expectedFailures = new goog.testing.ExpectedFailures();
782 expectedFailures.expectFailureFor(true);
783 try {
784 assertTrue(false);
785 } catch (e) {
786 expectedFailures.handleException(e);
787 }
788 });
789 }
790
791 function testFailOnUnreportedAsserts_ReportUnpropagatedAssertionExceptions() {
792 var testCase = new goog.testing.TestCase();
793
794 var e1 = new goog.testing.JsUnitException('foo123');
795 var e2 = new goog.testing.JsUnitException('bar456');
796
797 var mockRecordError = goog.testing.MethodMock(testCase, 'recordError_');
798 mockRecordError('test', e1);
799 mockRecordError('test', e2);
800 mockRecordError.$replay();
801
802 testCase.thrownAssertionExceptions_.push(e1);
803 testCase.thrownAssertionExceptions_.push(e2);
804
805 var exception = testCase.reportUnpropagatedAssertionExceptions_('test');
806 assertContains('One or more assertions were', exception.toString());
807
808 mockRecordError.$verify();
809 mockRecordError.$tearDown();
810 }
811
812
813 function testSetObj() {
814 var testCase = new goog.testing.TestCase();
815 assertEquals(0, testCase.getCount());
816 testCase.setTestObj({testOk: ok, somethingElse: fail});
817 assertEquals(1, testCase.getCount());
818 }
819
820 function testSetObj_Nested() {
821 var testCase = new goog.testing.TestCase();
822 assertEquals(0, testCase.getCount());
823 testCase.setTestObj({
824 setUp: event('setUp1'),
825 testOk: event('testOk'),
826 somethingElse: fail,
827 testNested: {
828 setUp: event('setUp2'),
829 test: event('testNested'),
830 tearDown: event('tearDown2')
831 },
832 testNestedSuite: {
833 setUp: event('setUp3'),
834 testA: event('testNestedSuite_A'),
835 testB: event('testNestedSuite_B'),
836 testSuperNestedSuite: {
837 setUp: event('setUp4'),
838 testC: event('testNestedSuite_SuperNestedSuite_C'),
839 tearDown: event('tearDown4')
840 },
841 tearDown: event('tearDown3')
842 },
843 tearDown: event('tearDown1')
844 });
845 assertEquals(5, testCase.getCount());
846 var tests = testCase.getTests();
847 var names = [];
848 for (var i = 0; i < tests.length; i++) {
849 names.push(tests[i].name);
850 }
851 assertArrayEquals(
852 [
853 'testOk', 'testNested', 'testNestedSuite_A', 'testNestedSuite_B',
854 'testNestedSuite_SuperNestedSuite_C'
855 ],
856 names);
857 testCase.runTests();
858 assertArrayEquals(
859 [
860 'setUp1',
861 'testOk',
862 'tearDown1',
863 'setUp1',
864 'setUp2',
865 'testNested',
866 'tearDown2',
867 'tearDown1',
868 'setUp1',
869 'setUp3',
870 'testNestedSuite_A',
871 'tearDown3',
872 'tearDown1',
873 'setUp1',
874 'setUp3',
875 'testNestedSuite_B',
876 'tearDown3',
877 'tearDown1',
878 'setUp1',
879 'setUp3',
880 'setUp4',
881 'testNestedSuite_SuperNestedSuite_C',
882 'tearDown4',
883 'tearDown3',
884 'tearDown1'
885 ],
886 events);
887 }
888
889 function testSetObj_es6Class() {
890 var FooTest;
891 try {
892 eval(
893 'FooTest = class { testOk() {assertTrue(true); } somethingElse() {} }');
894 } catch (ex) {
895 // IE cannot parse ES6.
896 return;
897 }
898 var testCase = new goog.testing.TestCase();
899 assertEquals(0, testCase.getCount());
900 testCase.setTestObj(new FooTest());
901 assertEquals(1, testCase.getCount());
902 }
903
904
905 function testCurrentTestName() {
906 var currentTestName = goog.testing.TestCase.currentTestName;
907 assertEquals('testCurrentTestName', currentTestName);
908 }
909
910
911 function testCurrentTestNamePromise() {
912 var getAssertSameTest = function() {
913 var expectedTestCase = goog.testing.TestCase.getActiveTestCase();
914 var expectedTestName = (expectedTestCase ? expectedTestCase.getName() :
915 '<no active TestCase>') +
916 '.' +
917 (goog.testing.TestCase.currentTestName || '<no active test name>');
918 var assertSameTest = function() {
919 var currentTestCase = goog.testing.TestCase.getActiveTestCase();
920 var currentTestName = (currentTestCase ? currentTestCase.getName() :
921 '<no active TestCase>') +
922 '.' + goog.testing.TestCase.currentTestName ||
923 '<no active test name>';
924 assertEquals(expectedTestName, currentTestName);
925 assertEquals(expectedTestCase, currentTestCase);
926 };
927 return assertSameTest;
928 };
929 var assertSameTest = getAssertSameTest();
930 // do something asynchronously...
931 return new goog.Promise(function(resolve, reject) {
932 // ... ensure the earlier half runs during the same test ...
933 assertSameTest();
934 setTimeout(function() {
935 // ... and also ensure the later half runs during the same test:
936 try {
937 assertSameTest();
938 resolve();
939 } catch (assertionFailureOrResolveException) {
940 reject(assertionFailureOrResolveException);
941 }
942 });
943 });
944 }
945
946 var testDoneTestsSeen = [];
947 var testDoneErrorsSeen = {};
948 var testDoneRuntime = {};
949 /**
950 * @param {goog.testing.TestCase} test
951 * @param {Array<string>} errors
952 */
953 function storeCallsAndErrors(test, errors) {
954 testDoneTestsSeen.push(test.name);
955 testDoneErrorsSeen[test.name] = [];
956 for (var i = 0; i < errors.length; i++) {
957 testDoneErrorsSeen[test.name].push(errors[i].split('\n')[0]);
958 }
959 testDoneRuntime[test.name] = test.getElapsedTime();
960 }
961 /**
962 * @param {Array<goog.testing.TestCase>} expectedTests
963 * @param {Array<Array<string>>} expectedErrors
964 */
965 function assertStoreCallsAndErrors(expectedTests, expectedErrors) {
966 assertArrayEquals(expectedTests, testDoneTestsSeen);
967 for (var i = 0; i < expectedTests.length; i++) {
968 var name = expectedTests[i];
969 assertArrayEquals(expectedErrors, testDoneErrorsSeen[name]);
970 assertEquals(typeof testDoneRuntime[testDoneTestsSeen[i]], 'number');
971 }
972 }
973
974 function testCallbackToTestDoneOk() {
975 testDoneTestsSeen = [];
976 testDoneErrorsSeen = {};
977 testDoneRuntime = {};
978 var testCase = new goog.testing.TestCase('fooCase');
979 testCase.addNewTest('foo', okGoogPromise);
980 testCase.setTestDoneCallback(storeCallsAndErrors);
981 return testCase.runTestsReturningPromise().then(function() {
982 assertStoreCallsAndErrors(['foo'], []);
983 });
984 }
985
986 function testCallbackToTestDoneFail() {
987 testDoneTestsSeen = [];
988 testDoneErrorsSeen = [];
989 testDoneRuntime = {};
990 var testCase = new goog.testing.TestCase('fooCase');
991 testCase.addNewTest('foo', failGoogPromise);
992 testCase.setTestDoneCallback(storeCallsAndErrors);
993 return testCase.runTestsReturningPromise().then(function() {
994 assertStoreCallsAndErrors(['foo'], ['ERROR in foo']);
995 });
996 }
997
998 /**
999 * @return {!Promise<null>}
1000 */
1001 function mockTestName() {
1002 return failGoogPromise();
1003 }
1004
1005 function testInitializeTestCase() {
1006 testDoneTestsSeen = [];
1007 testDoneErrorsSeen = [];
1008 var testCase = new goog.testing.TestCase('fooCase');
1009 testCase.getAutoDiscoveryPrefix = function() {
1010 return 'mockTestName';
1011 };
1012 var outerTestCase = goog.testing.TestCase.getActiveTestCase();
1013 goog.global['G_testRunner'].testCase = null;
1014 goog.testing.TestCase.initializeTestCase(testCase, storeCallsAndErrors);
1015 var checkAfterInitialize = goog.testing.TestCase.getActiveTestCase();
1016 goog.global['G_testRunner'].testCase = outerTestCase;
1017 // This asserts require G_testRunner to be set.
1018 assertEquals(checkAfterInitialize, testCase);
1019 assertEquals(goog.testing.TestCase.getActiveTestCase(), outerTestCase);
1020 // If the individual test feature is used to selecte this test, erase it.
1021 testCase.setTestsToRun(null);
1022 return testCase.runTestsReturningPromise().then(function() {
1023 assertStoreCallsAndErrors(['mockTestName'], ['ERROR in mockTestName']);
1024 });
1025 }
1026
1027 function testChainSetupTestCase() {
1028 var objectChain = [
1029 {setUp: event('setUp1'), tearDown: event('tearDown1')},
1030 {setUp: event('setUp2'), tearDown: event('tearDown2')}
1031 ];
1032
1033 var testCase = new goog.testing.TestCase('fooCase');
1034 testCase.addNewTest('foo', okGoogPromise, undefined, objectChain);
1035
1036 return testCase.runTestsReturningPromise().then(function() {
1037 assertArrayEquals(['setUp1', 'setUp2', 'tearDown2', 'tearDown1'], events);
1038 });
1039 }