Debian Codegraph @

closure-library/closure/goog/asserts/asserts_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.assertsTest');
16 goog.setTestOnly('goog.assertsTest');
17
18 goog.require('goog.asserts');
19 goog.require('goog.asserts.AssertionError');
20 goog.require('goog.dom');
21 goog.require('goog.dom.TagName');
22 goog.require('goog.string');
23 goog.require('goog.testing.jsunit');
24 goog.require('goog.userAgent');
25
26 /**
27 * Test that the function throws an error with the given message.
28 * @param {function()} failFunc
29 * @param {string} expectedMsg
30 */
31 function doTestMessage(failFunc, expectedMsg) {
32 var error = assertThrows('failFunc should throw.', failFunc);
33 // Test error message.
34 assertEquals(expectedMsg, error.message);
35 }
36
37 function testAssert() {
38 // None of them may throw exception
39 goog.asserts.assert(true);
40 goog.asserts.assert(1);
41 goog.asserts.assert([]);
42 goog.asserts.assert({});
43
44 assertThrows('assert(false)', goog.partial(goog.asserts.assert, false));
45 assertThrows('assert(0)', goog.partial(goog.asserts.assert, 0));
46 assertThrows('assert(null)', goog.partial(goog.asserts.assert, null));
47 assertThrows(
48 'assert(undefined)', goog.partial(goog.asserts.assert, undefined));
49
50 // Test error messages.
51 doTestMessage(goog.partial(goog.asserts.assert, false), 'Assertion failed');
52 doTestMessage(
53 goog.partial(goog.asserts.assert, false, 'ouch %s', 1),
54 'Assertion failed: ouch 1');
55 }
56
57
58 function testFail() {
59 assertThrows('fail()', goog.asserts.fail);
60 // Test error messages.
61 doTestMessage(goog.partial(goog.asserts.fail, false), 'Failure');
62 doTestMessage(
63 goog.partial(goog.asserts.fail, 'ouch %s', 1), 'Failure: ouch 1');
64 }
65
66 function testNumber() {
67 goog.asserts.assertNumber(1);
68 assertThrows(
69 'assertNumber(null)', goog.partial(goog.asserts.assertNumber, null));
70 // Test error messages.
71 doTestMessage(
72 goog.partial(goog.asserts.assertNumber, null),
73 'Assertion failed: Expected number but got null: null.');
74 doTestMessage(
75 goog.partial(goog.asserts.assertNumber, '1234'),
76 'Assertion failed: Expected number but got string: 1234.');
77 doTestMessage(
78 goog.partial(goog.asserts.assertNumber, null, 'ouch %s', 1),
79 'Assertion failed: ouch 1');
80 }
81
82 function testString() {
83 assertEquals('1', goog.asserts.assertString('1'));
84 assertThrows(
85 'assertString(null)', goog.partial(goog.asserts.assertString, null));
86 // Test error messages.
87 doTestMessage(
88 goog.partial(goog.asserts.assertString, null),
89 'Assertion failed: Expected string but got null: null.');
90 doTestMessage(
91 goog.partial(goog.asserts.assertString, 1234),
92 'Assertion failed: Expected string but got number: 1234.');
93 doTestMessage(
94 goog.partial(goog.asserts.assertString, null, 'ouch %s', 1),
95 'Assertion failed: ouch 1');
96 }
97
98 // jslint:ignore start
99 function testFunction() {
100 function f(){};
101 assertEquals(f, goog.asserts.assertFunction(f));
102 assertThrows(
103 'assertFunction(null)', goog.partial(goog.asserts.assertFunction, null));
104 // Test error messages.
105 doTestMessage(
106 goog.partial(goog.asserts.assertFunction, null),
107 'Assertion failed: Expected function but got null: null.');
108 doTestMessage(
109 goog.partial(goog.asserts.assertFunction, 1234),
110 'Assertion failed: Expected function but got number: 1234.');
111 doTestMessage(
112 goog.partial(goog.asserts.assertFunction, null, 'ouch %s', 1),
113 'Assertion failed: ouch 1');
114 }
115 // jslint:ignore end
116
117 function testObject() {
118 var o = {};
119 assertEquals(o, goog.asserts.assertObject(o));
120 assertThrows(
121 'assertObject(null)', goog.partial(goog.asserts.assertObject, null));
122 // Test error messages.
123 doTestMessage(
124 goog.partial(goog.asserts.assertObject, null),
125 'Assertion failed: Expected object but got null: null.');
126 doTestMessage(
127 goog.partial(goog.asserts.assertObject, 1234),
128 'Assertion failed: Expected object but got number: 1234.');
129 doTestMessage(
130 goog.partial(goog.asserts.assertObject, null, 'ouch %s', 1),
131 'Assertion failed: ouch 1');
132 }
133
134 function testArray() {
135 var a = [];
136 assertEquals(a, goog.asserts.assertArray(a));
137 assertThrows('assertArray({})', goog.partial(goog.asserts.assertArray, {}));
138 // Test error messages.
139 doTestMessage(
140 goog.partial(goog.asserts.assertArray, null),
141 'Assertion failed: Expected array but got null: null.');
142 doTestMessage(
143 goog.partial(goog.asserts.assertArray, 1234),
144 'Assertion failed: Expected array but got number: 1234.');
145 doTestMessage(
146 goog.partial(goog.asserts.assertArray, null, 'ouch %s', 1),
147 'Assertion failed: ouch 1');
148 }
149
150 function testBoolean() {
151 assertEquals(true, goog.asserts.assertBoolean(true));
152 assertEquals(false, goog.asserts.assertBoolean(false));
153 assertThrows(goog.partial(goog.asserts.assertBoolean, null));
154 assertThrows(goog.partial(goog.asserts.assertBoolean, 'foo'));
155
156 // Test error messages.
157 doTestMessage(
158 goog.partial(goog.asserts.assertBoolean, null),
159 'Assertion failed: Expected boolean but got null: null.');
160 doTestMessage(
161 goog.partial(goog.asserts.assertBoolean, 1234),
162 'Assertion failed: Expected boolean but got number: 1234.');
163 doTestMessage(
164 goog.partial(goog.asserts.assertBoolean, null, 'ouch %s', 1),
165 'Assertion failed: ouch 1');
166 }
167
168 function testElement() {
169 assertThrows(goog.partial(goog.asserts.assertElement, null));
170 assertThrows(goog.partial(goog.asserts.assertElement, 'foo'));
171 assertThrows(
172 goog.partial(goog.asserts.assertElement, goog.dom.createTextNode('foo')));
173 var elem = goog.dom.createElement(goog.dom.TagName.DIV);
174 assertEquals(elem, goog.asserts.assertElement(elem));
175 }
176
177 function testInstanceof() {
178 /** @constructor */
179 var F = function() {};
180 goog.asserts.assertInstanceof(new F(), F);
181 var error = assertThrows(
182 'assertInstanceof({}, F)',
183 goog.partial(goog.asserts.assertInstanceof, {}, F));
184 // IE lacks support for function.name and will fallback to toString().
185 var object = /object/.test(error.message) ? '[object Object]' : 'Object';
186 var name = /F/.test(error.message) ? 'F' : 'unknown type name';
187
188 // Test error messages.
189 doTestMessage(
190 goog.partial(goog.asserts.assertInstanceof, {}, F),
191 'Assertion failed: Expected instanceof ' + name + ' but got ' + object +
192 '.');
193 doTestMessage(
194 goog.partial(goog.asserts.assertInstanceof, {}, F, 'a %s', 1),
195 'Assertion failed: a 1');
196 doTestMessage(
197 goog.partial(goog.asserts.assertInstanceof, null, F),
198 'Assertion failed: Expected instanceof ' + name + ' but got null.');
199 doTestMessage(
200 goog.partial(goog.asserts.assertInstanceof, 5, F), 'Assertion failed: ' +
201 'Expected instanceof ' + name + ' but got number.');
202
203 // Test a constructor a with a name (IE does not support function.name).
204 if (!goog.userAgent.IE) {
205 F = function foo() {};
206 doTestMessage(
207 goog.partial(goog.asserts.assertInstanceof, {}, F),
208 'Assertion failed: Expected instanceof foo but got ' + object + '.');
209 }
210
211 // Test a constructor with a displayName.
212 F.displayName = 'bar';
213 doTestMessage(
214 goog.partial(goog.asserts.assertInstanceof, {}, F),
215 'Assertion failed: Expected instanceof bar but got ' + object + '.');
216 }
217
218 function testObjectPrototypeIsIntact() {
219 goog.asserts.assertObjectPrototypeIsIntact();
220 var originalToString = Object.prototype.toString;
221 Object.prototype.toString = goog.nullFunction;
222 try {
223 goog.asserts.assertObjectPrototypeIsIntact();
224 Object.prototype.foo = 1;
225 doTestMessage(
226 goog.asserts.assertObjectPrototypeIsIntact,
227 'Failure: foo should not be enumerable in Object.prototype.');
228 } finally {
229 Object.prototype.toString = originalToString;
230 delete Object.prototype.foo;
231 }
232 }
233
234 function testAssertionError() {
235 var error = new goog.asserts.AssertionError('foo %s %s', [1, 'two']);
236 assertEquals('Wrong message', 'foo 1 two', error.message);
237 assertEquals('Wrong messagePattern', 'foo %s %s', error.messagePattern);
238 }
239
240 function testFailWithCustomErrorHandler() {
241 try {
242 var handledException;
243 goog.asserts.setErrorHandler(function(e) { handledException = e; });
244
245 var expectedMessage = 'Failure: Gevalt!';
246
247 goog.asserts.fail('Gevalt!');
248 assertTrue('handledException is null.', handledException != null);
249 assertTrue(
250 'Message check failed. Expected: ' + expectedMessage + ' Actual: ' +
251 handledException.message,
252 goog.string.startsWith(expectedMessage, handledException.message));
253 } finally {
254 goog.asserts.setErrorHandler(goog.asserts.DEFAULT_ERROR_HANDLER);
255 }
256 }
257
258 function testAssertWithCustomErrorHandler() {
259 try {
260 var handledException;
261 goog.asserts.setErrorHandler(function(e) { handledException = e; });
262
263 var expectedMessage = 'Assertion failed: Gevalt!';
264
265 goog.asserts.assert(false, 'Gevalt!');
266 assertTrue('handledException is null.', handledException != null);
267 assertTrue(
268 'Message check failed. Expected: ' + expectedMessage + ' Actual: ' +
269 handledException.message,
270 goog.string.startsWith(expectedMessage, handledException.message));
271 } finally {
272 goog.asserts.setErrorHandler(goog.asserts.DEFAULT_ERROR_HANDLER);
273 }
274 }
275
276 function testAssertFinite() {
277 assertEquals(9, goog.asserts.assertFinite(9));
278 assertEquals(0, goog.asserts.assertFinite(0));
279 assertThrows(goog.partial(goog.asserts.assertFinite, NaN));
280 assertThrows(goog.partial(goog.asserts.assertFinite, Infinity));
281 assertThrows(goog.partial(goog.asserts.assertFinite, -Infinity));
282 assertThrows(goog.partial(goog.asserts.assertFinite, 'foo'));
283 assertThrows(goog.partial(goog.asserts.assertFinite, true));
284
285 // Test error messages.
286 doTestMessage(
287 goog.partial(goog.asserts.assertFinite, NaN),
288 'Assertion failed: Expected NaN to be a finite number but it is not.');
289 doTestMessage(
290 goog.partial(goog.asserts.assertFinite, NaN, 'ouch %s', 1),
291 'Assertion failed: ouch 1');
292 }