// 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. /** * @fileoverview Utilities to check the preconditions, postconditions and * invariants runtime. * * Methods in this package should be given special treatment by the compiler * for type-inference. For example, goog.asserts.assert(foo) * will restrict foo to a truthy value. * * The compiler has an option to disable asserts. So code like: * * var x = goog.asserts.assert(foo()); goog.asserts.assert(bar()); * * will be transformed into: * * var x = foo(); * * The compiler will leave in foo() (because its return value is used), * but it will remove bar() because it assumes it does not have side-effects. * */ goog.provide('goog.asserts'); goog.provide('goog.asserts.AssertionError'); goog.require('goog.debug.Error'); goog.require('goog.string'); /** * @define {boolean} Whether to strip out asserts or to leave them in. */ goog.define('goog.asserts.ENABLE_ASSERTS', goog.DEBUG); /** * Error object for failed assertions. * @param {string} messagePattern The pattern that was used to form message. * @param {!Array.<*>} messageArgs The items to substitute into the pattern. * @constructor * @extends {goog.debug.Error} */ goog.asserts.AssertionError = function(messagePattern, messageArgs) { messageArgs.unshift(messagePattern); goog.debug.Error.call(this, goog.string.subs.apply(null, messageArgs)); // Remove the messagePattern afterwards to avoid permenantly modifying the // passed in array. messageArgs.shift(); /** * The message pattern used to format the error message. Error handlers can * use this to uniquely identify the assertion. * @type {string} */ this.messagePattern = messagePattern; }; goog.inherits(goog.asserts.AssertionError, goog.debug.Error); /** @override */ goog.asserts.AssertionError.prototype.name = 'AssertionError'; /** * Throws an exception with the given message and "Assertion failed" prefixed * onto it. * @param {string} defaultMessage The message to use if givenMessage is empty. * @param {Array.<*>} defaultArgs The substitution arguments for defaultMessage. * @param {string|undefined} givenMessage Message supplied by the caller. * @param {Array.<*>} givenArgs The substitution arguments for givenMessage. * @throws {goog.asserts.AssertionError} When the value is not a number. * @private */ goog.asserts.doAssertFailure_ = function(defaultMessage, defaultArgs, givenMessage, givenArgs) { var message = 'Assertion failed'; if (givenMessage) { message += ': ' + givenMessage; var args = givenArgs; } else if (defaultMessage) { message += ': ' + defaultMessage; args = defaultArgs; } // The '' + works around an Opera 10 bug in the unit tests. Without it, // a stack trace is added to var message above. With this, a stack trace is // not added until this line (it causes the extra garbage to be added after // the assertion message instead of in the middle of it). throw new goog.asserts.AssertionError('' + message, args || []); }; /** * Checks if the condition evaluates to true if goog.asserts.ENABLE_ASSERTS is * true. * @param {*} condition The condition to check. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @return {*} The value of the condition. * @throws {goog.asserts.AssertionError} When the condition evaluates to false. */ goog.asserts.assert = function(condition, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !condition) { goog.asserts.doAssertFailure_('', null, opt_message, Array.prototype.slice.call(arguments, 2)); } return condition; }; /** * Fails if goog.asserts.ENABLE_ASSERTS is true. This function is useful in case * when we want to add a check in the unreachable area like switch-case * statement: * *
 *  switch(type) {
 *    case FOO: doSomething(); break;
 *    case BAR: doSomethingElse(); break;
 *    default: goog.assert.fail('Unrecognized type: ' + type);
 *      // We have only 2 types - "default:" section is unreachable code.
 *  }
 * 
* * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @throws {goog.asserts.AssertionError} Failure. */ goog.asserts.fail = function(opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS) { throw new goog.asserts.AssertionError( 'Failure' + (opt_message ? ': ' + opt_message : ''), Array.prototype.slice.call(arguments, 1)); } }; /** * Checks if the value is a number if goog.asserts.ENABLE_ASSERTS is true. * @param {*} value The value to check. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @return {number} The value, guaranteed to be a number when asserts enabled. * @throws {goog.asserts.AssertionError} When the value is not a number. */ goog.asserts.assertNumber = function(value, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !goog.isNumber(value)) { goog.asserts.doAssertFailure_('Expected number but got %s: %s.', [goog.typeOf(value), value], opt_message, Array.prototype.slice.call(arguments, 2)); } return /** @type {number} */ (value); }; /** * Checks if the value is a string if goog.asserts.ENABLE_ASSERTS is true. * @param {*} value The value to check. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @return {string} The value, guaranteed to be a string when asserts enabled. * @throws {goog.asserts.AssertionError} When the value is not a string. */ goog.asserts.assertString = function(value, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !goog.isString(value)) { goog.asserts.doAssertFailure_('Expected string but got %s: %s.', [goog.typeOf(value), value], opt_message, Array.prototype.slice.call(arguments, 2)); } return /** @type {string} */ (value); }; /** * Checks if the value is a function if goog.asserts.ENABLE_ASSERTS is true. * @param {*} value The value to check. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @return {!Function} The value, guaranteed to be a function when asserts * enabled. * @throws {goog.asserts.AssertionError} When the value is not a function. */ goog.asserts.assertFunction = function(value, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !goog.isFunction(value)) { goog.asserts.doAssertFailure_('Expected function but got %s: %s.', [goog.typeOf(value), value], opt_message, Array.prototype.slice.call(arguments, 2)); } return /** @type {!Function} */ (value); }; /** * Checks if the value is an Object if goog.asserts.ENABLE_ASSERTS is true. * @param {*} value The value to check. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @return {!Object} The value, guaranteed to be a non-null object. * @throws {goog.asserts.AssertionError} When the value is not an object. */ goog.asserts.assertObject = function(value, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !goog.isObject(value)) { goog.asserts.doAssertFailure_('Expected object but got %s: %s.', [goog.typeOf(value), value], opt_message, Array.prototype.slice.call(arguments, 2)); } return /** @type {!Object} */ (value); }; /** * Checks if the value is an Array if goog.asserts.ENABLE_ASSERTS is true. * @param {*} value The value to check. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @return {!Array} The value, guaranteed to be a non-null array. * @throws {goog.asserts.AssertionError} When the value is not an array. */ goog.asserts.assertArray = function(value, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !goog.isArray(value)) { goog.asserts.doAssertFailure_('Expected array but got %s: %s.', [goog.typeOf(value), value], opt_message, Array.prototype.slice.call(arguments, 2)); } return /** @type {!Array} */ (value); }; /** * Checks if the value is a boolean if goog.asserts.ENABLE_ASSERTS is true. * @param {*} value The value to check. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @return {boolean} The value, guaranteed to be a boolean when asserts are * enabled. * @throws {goog.asserts.AssertionError} When the value is not a boolean. */ goog.asserts.assertBoolean = function(value, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !goog.isBoolean(value)) { goog.asserts.doAssertFailure_('Expected boolean but got %s: %s.', [goog.typeOf(value), value], opt_message, Array.prototype.slice.call(arguments, 2)); } return /** @type {boolean} */ (value); }; /** * Checks if the value is an instance of the user-defined type if * goog.asserts.ENABLE_ASSERTS is true. * * The compiler may tighten the type returned by this function. * * @param {*} value The value to check. * @param {function(new: T, ...)} type A user-defined constructor. * @param {string=} opt_message Error message in case of failure. * @param {...*} var_args The items to substitute into the failure message. * @throws {goog.asserts.AssertionError} When the value is not an instance of * type. * @return {!T} * @template T */ goog.asserts.assertInstanceof = function(value, type, opt_message, var_args) { if (goog.asserts.ENABLE_ASSERTS && !(value instanceof type)) { goog.asserts.doAssertFailure_('instanceof check failed.', null, opt_message, Array.prototype.slice.call(arguments, 3)); } return value; }; /** * Checks that no enumerable keys are present in Object.prototype. Such keys * would break most code that use {@code for (var ... in ...)} loops. */ goog.asserts.assertObjectPrototypeIsIntact = function() { for (var key in Object.prototype) { goog.asserts.fail(key + ' should not be enumerable in Object.prototype.'); } };