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