| 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 |
/** |
| 16 |
* @fileoverview Utilities to check the preconditions, postconditions and |
| 17 |
* invariants runtime. |
| 18 |
* |
| 19 |
* Methods in this package should be given special treatment by the compiler |
| 20 |
* for type-inference. For example, <code>goog.asserts.assert(foo)</code> |
| 21 |
* will restrict <code>foo</code> to a truthy value. |
| 22 |
* |
| 23 |
* The compiler has an option to disable asserts. So code like: |
| 24 |
* <code> |
| 25 |
* var x = goog.asserts.assert(foo()); goog.asserts.assert(bar()); |
| 26 |
* </code> |
| 27 |
* will be transformed into: |
| 28 |
* <code> |
| 29 |
* var x = foo(); |
| 30 |
* </code> |
| 31 |
* The compiler will leave in foo() (because its return value is used), |
| 32 |
* but it will remove bar() because it assumes it does not have side-effects. |
| 33 |
* |
| 34 |
* @author [email protected] (Andrew Grieve) |
| 35 |
*/ |
| 36 |
|
| 37 |
goog.provide('goog.asserts'); |
| 38 |
goog.provide('goog.asserts.AssertionError'); |
| 39 |
|
| 40 |
goog.require('goog.debug.Error'); |
| 41 |
goog.require('goog.dom.NodeType'); |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* @define {boolean} Whether to strip out asserts or to leave them in. |
| 46 |
*/ |
| 47 |
goog.define('goog.asserts.ENABLE_ASSERTS', goog.DEBUG); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Error object for failed assertions. |
| 53 |
* @param {string} messagePattern The pattern that was used to form message. |
| 54 |
* @param {!Array<*>} messageArgs The items to substitute into the pattern. |
| 55 |
* @constructor |
| 56 |
* @extends {goog.debug.Error} |
| 57 |
* @final |
| 58 |
*/ |
| 59 |
goog.asserts.AssertionError = function(messagePattern, messageArgs) { |
| 60 |
goog.debug.Error.call(this, goog.asserts.subs_(messagePattern, messageArgs)); |
| 61 |
|
| 62 |
/** |
| 63 |
* The message pattern used to format the error message. Error handlers can |
| 64 |
* use this to uniquely identify the assertion. |
| 65 |
* @type {string} |
| 66 |
*/ |
| 67 |
this.messagePattern = messagePattern; |
| 68 |
}; |
| 69 |
goog.inherits(goog.asserts.AssertionError, goog.debug.Error); |
| 70 |
|
| 71 |
|
| 72 |
/** @override */ |
| 73 |
goog.asserts.AssertionError.prototype.name = 'AssertionError'; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* The default error handler. |
| 78 |
* @param {!goog.asserts.AssertionError} e The exception to be handled. |
| 79 |
*/ |
| 80 |
goog.asserts.DEFAULT_ERROR_HANDLER = function(e) { |
| 81 |
throw e; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* The handler responsible for throwing or logging assertion errors. |
| 87 |
* @private {function(!goog.asserts.AssertionError)} |
| 88 |
*/ |
| 89 |
goog.asserts.errorHandler_ = goog.asserts.DEFAULT_ERROR_HANDLER; |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Does simple python-style string substitution. |
| 94 |
* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog". |
| 95 |
* @param {string} pattern The string containing the pattern. |
| 96 |
* @param {!Array<*>} subs The items to substitute into the pattern. |
| 97 |
* @return {string} A copy of {@code str} in which each occurrence of |
| 98 |
* {@code %s} has been replaced an argument from {@code var_args}. |
| 99 |
* @private |
| 100 |
*/ |
| 101 |
goog.asserts.subs_ = function(pattern, subs) { |
| 102 |
var splitParts = pattern.split('%s'); |
| 103 |
var returnString = ''; |
| 104 |
|
| 105 |
// Replace up to the last split part. We are inserting in the |
| 106 |
// positions between split parts. |
| 107 |
var subLast = splitParts.length - 1; |
| 108 |
for (var i = 0; i < subLast; i++) { |
| 109 |
// keep unsupplied as '%s' |
| 110 |
var sub = (i < subs.length) ? subs[i] : '%s'; |
| 111 |
returnString += splitParts[i] + sub; |
| 112 |
} |
| 113 |
return returnString + splitParts[subLast]; |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Throws an exception with the given message and "Assertion failed" prefixed |
| 119 |
* onto it. |
| 120 |
* @param {string} defaultMessage The message to use if givenMessage is empty. |
| 121 |
* @param {Array<*>} defaultArgs The substitution arguments for defaultMessage. |
| 122 |
* @param {string|undefined} givenMessage Message supplied by the caller. |
| 123 |
* @param {Array<*>} givenArgs The substitution arguments for givenMessage. |
| 124 |
* @throws {goog.asserts.AssertionError} When the value is not a number. |
| 125 |
* @private |
| 126 |
*/ |
| 127 |
goog.asserts.doAssertFailure_ = function( |
| 128 |
defaultMessage, defaultArgs, givenMessage, givenArgs) { |
| 129 |
var message = 'Assertion failed'; |
| 130 |
if (givenMessage) { |
| 131 |
message += ': ' + givenMessage; |
| 132 |
var args = givenArgs; |
| 133 |
} else if (defaultMessage) { |
| 134 |
message += ': ' + defaultMessage; |
| 135 |
args = defaultArgs; |
| 136 |
} |
| 137 |
// The '' + works around an Opera 10 bug in the unit tests. Without it, |
| 138 |
// a stack trace is added to var message above. With this, a stack trace is |
| 139 |
// not added until this line (it causes the extra garbage to be added after |
| 140 |
// the assertion message instead of in the middle of it). |
| 141 |
var e = new goog.asserts.AssertionError('' + message, args || []); |
| 142 |
goog.asserts.errorHandler_(e); |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Sets a custom error handler that can be used to customize the behavior of |
| 148 |
* assertion failures, for example by turning all assertion failures into log |
| 149 |
* messages. |
| 150 |
* @param {function(!goog.asserts.AssertionError)} errorHandler |
| 151 |
*/ |
| 152 |
goog.asserts.setErrorHandler = function(errorHandler) { |
| 153 |
if (goog.asserts.ENABLE_ASSERTS) { |
| 154 |
goog.asserts.errorHandler_ = errorHandler; |
| 155 |
} |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Checks if the condition evaluates to true if goog.asserts.ENABLE_ASSERTS is |
| 161 |
* true. |
| 162 |
* @template T |
| 163 |
* @param {T} condition The condition to check. |
| 164 |
* @param {string=} opt_message Error message in case of failure. |
| 165 |
* @param {...*} var_args The items to substitute into the failure message. |
| 166 |
* @return {T} The value of the condition. |
| 167 |
* @throws {goog.asserts.AssertionError} When the condition evaluates to false. |
| 168 |
*/ |
| 169 |
goog.asserts.assert = function(condition, opt_message, var_args) { |
| 170 |
if (goog.asserts.ENABLE_ASSERTS && !condition) { |
| 171 |
goog.asserts.doAssertFailure_( |
| 172 |
'', null, opt_message, Array.prototype.slice.call(arguments, 2)); |
| 173 |
} |
| 174 |
return condition; |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Fails if goog.asserts.ENABLE_ASSERTS is true. This function is useful in case |
| 180 |
* when we want to add a check in the unreachable area like switch-case |
| 181 |
* statement: |
| 182 |
* |
| 183 |
* <pre> |
| 184 |
* switch(type) { |
| 185 |
* case FOO: doSomething(); break; |
| 186 |
* case BAR: doSomethingElse(); break; |
| 187 |
* default: goog.asserts.fail('Unrecognized type: ' + type); |
| 188 |
* // We have only 2 types - "default:" section is unreachable code. |
| 189 |
* } |
| 190 |
* </pre> |
| 191 |
* |
| 192 |
* @param {string=} opt_message Error message in case of failure. |
| 193 |
* @param {...*} var_args The items to substitute into the failure message. |
| 194 |
* @throws {goog.asserts.AssertionError} Failure. |
| 195 |
*/ |
| 196 |
goog.asserts.fail = function(opt_message, var_args) { |
| 197 |
if (goog.asserts.ENABLE_ASSERTS) { |
| 198 |
goog.asserts.errorHandler_( |
| 199 |
new goog.asserts.AssertionError( |
| 200 |
'Failure' + (opt_message ? ': ' + opt_message : ''), |
| 201 |
Array.prototype.slice.call(arguments, 1))); |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Checks if the value is a number if goog.asserts.ENABLE_ASSERTS is true. |
| 208 |
* @param {*} value The value to check. |
| 209 |
* @param {string=} opt_message Error message in case of failure. |
| 210 |
* @param {...*} var_args The items to substitute into the failure message. |
| 211 |
* @return {number} The value, guaranteed to be a number when asserts enabled. |
| 212 |
* @throws {goog.asserts.AssertionError} When the value is not a number. |
| 213 |
*/ |
| 214 |
goog.asserts.assertNumber = function(value, opt_message, var_args) { |
| 215 |
if (goog.asserts.ENABLE_ASSERTS && !goog.isNumber(value)) { |
| 216 |
goog.asserts.doAssertFailure_( |
| 217 |
'Expected number but got %s: %s.', [goog.typeOf(value), value], |
| 218 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 219 |
} |
| 220 |
return /** @type {number} */ (value); |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
/** |
| 225 |
* Checks if the value is a string if goog.asserts.ENABLE_ASSERTS is true. |
| 226 |
* @param {*} value The value to check. |
| 227 |
* @param {string=} opt_message Error message in case of failure. |
| 228 |
* @param {...*} var_args The items to substitute into the failure message. |
| 229 |
* @return {string} The value, guaranteed to be a string when asserts enabled. |
| 230 |
* @throws {goog.asserts.AssertionError} When the value is not a string. |
| 231 |
*/ |
| 232 |
goog.asserts.assertString = function(value, opt_message, var_args) { |
| 233 |
if (goog.asserts.ENABLE_ASSERTS && !goog.isString(value)) { |
| 234 |
goog.asserts.doAssertFailure_( |
| 235 |
'Expected string but got %s: %s.', [goog.typeOf(value), value], |
| 236 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 237 |
} |
| 238 |
return /** @type {string} */ (value); |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
/** |
| 243 |
* Checks if the value is a function if goog.asserts.ENABLE_ASSERTS is true. |
| 244 |
* @param {*} value The value to check. |
| 245 |
* @param {string=} opt_message Error message in case of failure. |
| 246 |
* @param {...*} var_args The items to substitute into the failure message. |
| 247 |
* @return {!Function} The value, guaranteed to be a function when asserts |
| 248 |
* enabled. |
| 249 |
* @throws {goog.asserts.AssertionError} When the value is not a function. |
| 250 |
*/ |
| 251 |
goog.asserts.assertFunction = function(value, opt_message, var_args) { |
| 252 |
if (goog.asserts.ENABLE_ASSERTS && !goog.isFunction(value)) { |
| 253 |
goog.asserts.doAssertFailure_( |
| 254 |
'Expected function but got %s: %s.', [goog.typeOf(value), value], |
| 255 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 256 |
} |
| 257 |
return /** @type {!Function} */ (value); |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
/** |
| 262 |
* Checks if the value is an Object if goog.asserts.ENABLE_ASSERTS is true. |
| 263 |
* @param {*} value The value to check. |
| 264 |
* @param {string=} opt_message Error message in case of failure. |
| 265 |
* @param {...*} var_args The items to substitute into the failure message. |
| 266 |
* @return {!Object} The value, guaranteed to be a non-null object. |
| 267 |
* @throws {goog.asserts.AssertionError} When the value is not an object. |
| 268 |
*/ |
| 269 |
goog.asserts.assertObject = function(value, opt_message, var_args) { |
| 270 |
if (goog.asserts.ENABLE_ASSERTS && !goog.isObject(value)) { |
| 271 |
goog.asserts.doAssertFailure_( |
| 272 |
'Expected object but got %s: %s.', [goog.typeOf(value), value], |
| 273 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 274 |
} |
| 275 |
return /** @type {!Object} */ (value); |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
/** |
| 280 |
* Checks if the value is an Array if goog.asserts.ENABLE_ASSERTS is true. |
| 281 |
* @param {*} value The value to check. |
| 282 |
* @param {string=} opt_message Error message in case of failure. |
| 283 |
* @param {...*} var_args The items to substitute into the failure message. |
| 284 |
* @return {!Array<?>} The value, guaranteed to be a non-null array. |
| 285 |
* @throws {goog.asserts.AssertionError} When the value is not an array. |
| 286 |
*/ |
| 287 |
goog.asserts.assertArray = function(value, opt_message, var_args) { |
| 288 |
if (goog.asserts.ENABLE_ASSERTS && !goog.isArray(value)) { |
| 289 |
goog.asserts.doAssertFailure_( |
| 290 |
'Expected array but got %s: %s.', [goog.typeOf(value), value], |
| 291 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 292 |
} |
| 293 |
return /** @type {!Array<?>} */ (value); |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Checks if the value is a boolean if goog.asserts.ENABLE_ASSERTS is true. |
| 299 |
* @param {*} value The value to check. |
| 300 |
* @param {string=} opt_message Error message in case of failure. |
| 301 |
* @param {...*} var_args The items to substitute into the failure message. |
| 302 |
* @return {boolean} The value, guaranteed to be a boolean when asserts are |
| 303 |
* enabled. |
| 304 |
* @throws {goog.asserts.AssertionError} When the value is not a boolean. |
| 305 |
*/ |
| 306 |
goog.asserts.assertBoolean = function(value, opt_message, var_args) { |
| 307 |
if (goog.asserts.ENABLE_ASSERTS && !goog.isBoolean(value)) { |
| 308 |
goog.asserts.doAssertFailure_( |
| 309 |
'Expected boolean but got %s: %s.', [goog.typeOf(value), value], |
| 310 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 311 |
} |
| 312 |
return /** @type {boolean} */ (value); |
| 313 |
}; |
| 314 |
|
| 315 |
|
| 316 |
/** |
| 317 |
* Checks if the value is a DOM Element if goog.asserts.ENABLE_ASSERTS is true. |
| 318 |
* @param {*} value The value to check. |
| 319 |
* @param {string=} opt_message Error message in case of failure. |
| 320 |
* @param {...*} var_args The items to substitute into the failure message. |
| 321 |
* @return {!Element} The value, likely to be a DOM Element when asserts are |
| 322 |
* enabled. |
| 323 |
* @throws {goog.asserts.AssertionError} When the value is not an Element. |
| 324 |
*/ |
| 325 |
goog.asserts.assertElement = function(value, opt_message, var_args) { |
| 326 |
if (goog.asserts.ENABLE_ASSERTS && |
| 327 |
(!goog.isObject(value) || value.nodeType != goog.dom.NodeType.ELEMENT)) { |
| 328 |
goog.asserts.doAssertFailure_( |
| 329 |
'Expected Element but got %s: %s.', [goog.typeOf(value), value], |
| 330 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 331 |
} |
| 332 |
return /** @type {!Element} */ (value); |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
/** |
| 337 |
* Checks if the value is an instance of the user-defined type if |
| 338 |
* goog.asserts.ENABLE_ASSERTS is true. |
| 339 |
* |
| 340 |
* The compiler may tighten the type returned by this function. |
| 341 |
* |
| 342 |
* @param {?} value The value to check. |
| 343 |
* @param {function(new: T, ...)} type A user-defined constructor. |
| 344 |
* @param {string=} opt_message Error message in case of failure. |
| 345 |
* @param {...*} var_args The items to substitute into the failure message. |
| 346 |
* @throws {goog.asserts.AssertionError} When the value is not an instance of |
| 347 |
* type. |
| 348 |
* @return {T} |
| 349 |
* @template T |
| 350 |
*/ |
| 351 |
goog.asserts.assertInstanceof = function(value, type, opt_message, var_args) { |
| 352 |
if (goog.asserts.ENABLE_ASSERTS && !(value instanceof type)) { |
| 353 |
goog.asserts.doAssertFailure_( |
| 354 |
'Expected instanceof %s but got %s.', |
| 355 |
[goog.asserts.getType_(type), goog.asserts.getType_(value)], |
| 356 |
opt_message, Array.prototype.slice.call(arguments, 3)); |
| 357 |
} |
| 358 |
return value; |
| 359 |
}; |
| 360 |
|
| 361 |
|
| 362 |
/** |
| 363 |
* Checks whether the value is a finite number, if goog.asserts.ENABLE_ASSERTS |
| 364 |
* is true. |
| 365 |
* |
| 366 |
* @param {*} value The value to check. |
| 367 |
* @param {string=} opt_message Error message in case of failure. |
| 368 |
* @param {...*} var_args The items to substitute into the failure message. |
| 369 |
* @throws {goog.asserts.AssertionError} When the value is not a number, or is |
| 370 |
* a non-finite number such as NaN, Infinity or -Infinity. |
| 371 |
* @return {number} The value initially passed in. |
| 372 |
*/ |
| 373 |
goog.asserts.assertFinite = function(value, opt_message, var_args) { |
| 374 |
if (goog.asserts.ENABLE_ASSERTS && |
| 375 |
(typeof value != 'number' || !isFinite(value))) { |
| 376 |
goog.asserts.doAssertFailure_( |
| 377 |
'Expected %s to be a finite number but it is not.', [value], |
| 378 |
opt_message, Array.prototype.slice.call(arguments, 2)); |
| 379 |
} |
| 380 |
return /** @type {number} */ (value); |
| 381 |
}; |
| 382 |
|
| 383 |
/** |
| 384 |
* Checks that no enumerable keys are present in Object.prototype. Such keys |
| 385 |
* would break most code that use {@code for (var ... in ...)} loops. |
| 386 |
*/ |
| 387 |
goog.asserts.assertObjectPrototypeIsIntact = function() { |
| 388 |
for (var key in Object.prototype) { |
| 389 |
goog.asserts.fail(key + ' should not be enumerable in Object.prototype.'); |
| 390 |
} |
| 391 |
}; |
| 392 |
|
| 393 |
|
| 394 |
/** |
| 395 |
* Returns the type of a value. If a constructor is passed, and a suitable |
| 396 |
* string cannot be found, 'unknown type name' will be returned. |
| 397 |
* @param {*} value A constructor, object, or primitive. |
| 398 |
* @return {string} The best display name for the value, or 'unknown type name'. |
| 399 |
* @private |
| 400 |
*/ |
| 401 |
goog.asserts.getType_ = function(value) { |
| 402 |
if (value instanceof Function) { |
| 403 |
return value.displayName || value.name || 'unknown type name'; |
| 404 |
} else if (value instanceof Object) { |
| 405 |
return value.constructor.displayName || value.constructor.name || |
| 406 |
Object.prototype.toString.call(value); |
| 407 |
} else { |
| 408 |
return value === null ? 'null' : typeof value; |
| 409 |
} |
| 410 |
}; |