Debian Codegraph @

closure-library/closure/goog/testing/expectedfailures_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.provide('goog.testing.ExpectedFailuresTest');
16 goog.setTestOnly('goog.testing.ExpectedFailuresTest');
17
18 goog.require('goog.debug.Logger');
19 goog.require('goog.testing.ExpectedFailures');
20 goog.require('goog.testing.JsUnitException');
21 goog.require('goog.testing.TestCase');
22 goog.require('goog.testing.jsunit');
23
24 var count, expectedFailures, lastLevel, lastMessage;
25
26 // Stub out the logger.
27 goog.testing.ExpectedFailures.prototype.logger_.log = function(level, message) {
28 lastLevel = level;
29 lastMessage = message;
30 count++;
31 };
32
33 function setUp() {
34 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
35 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
36
37 expectedFailures = new goog.testing.ExpectedFailures();
38 count = 0;
39 lastLevel = lastMessage = '';
40 }
41
42 // Individual test methods.
43
44 function testNoExpectedFailure() {
45 expectedFailures.handleTearDown();
46 }
47
48 function testPreventExpectedFailure() {
49 expectedFailures.expectFailureFor(true);
50
51 expectedFailures.handleException(new goog.testing.JsUnitException('', ''));
52 assertEquals('Should have logged a message', 1, count);
53 assertEquals(
54 'Should have logged an info message', goog.debug.Logger.Level.INFO,
55 lastLevel);
56 assertContains(
57 'Should log a suppression message', 'Suppressing test failure',
58 lastMessage);
59
60 expectedFailures.handleTearDown();
61 assertEquals('Should not have logged another message', 1, count);
62 }
63
64 function testDoNotPreventException() {
65 var ex = 'exception';
66 expectedFailures.expectFailureFor(false);
67 var e = assertThrows('Should have rethrown exception', function() {
68 expectedFailures.handleException(ex);
69 });
70 assertEquals('Should rethrow same exception', ex, e);
71 }
72
73 function testExpectedFailureDidNotOccur() {
74 expectedFailures.expectFailureFor(true);
75
76 expectedFailures.handleTearDown();
77 assertEquals('Should have logged a message', 1, count);
78 assertEquals(
79 'Should have logged a warning', goog.debug.Logger.Level.WARNING,
80 lastLevel);
81 assertContains(
82 'Should log a suppression message', 'Expected a test failure',
83 lastMessage);
84 }
85
86 function testRun() {
87 expectedFailures.expectFailureFor(true);
88
89 expectedFailures.run(function() { fail('Expected failure'); });
90
91 assertEquals('Should have logged a message', 1, count);
92 assertEquals(
93 'Should have logged an info message', goog.debug.Logger.Level.INFO,
94 lastLevel);
95 assertContains(
96 'Should log a suppression message', 'Suppressing test failure',
97 lastMessage);
98
99 expectedFailures.handleTearDown();
100 assertEquals('Should not have logged another message', 1, count);
101 }
102
103 function testRunStrict() {
104 expectedFailures.expectFailureFor(true);
105
106 var ex = assertThrows(function() {
107 expectedFailures.run(function() {
108 // Doesn't fail!
109 });
110 });
111 assertContains(
112 "Expected a test failure in 'testRunStrict' but the test passed.",
113 ex.message);
114 }
115
116 function testRunLenient() {
117 expectedFailures.expectFailureFor(true);
118
119 expectedFailures.run(function() {
120 // Doesn't fail!
121 }, true);
122 expectedFailures.handleTearDown();
123 assertEquals('Should have logged a message', 1, count);
124 assertEquals(
125 'Should have logged a warning', goog.debug.Logger.Level.WARNING,
126 lastLevel);
127 assertContains(
128 'Should log a suppression message', 'Expected a test failure',
129 lastMessage);
130 }