/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Utilities to check the preconditions, postconditions and
* invariants runtime.
*
* Methods in this package are given special treatment by the compiler
* for type-inference. For example, <code>goog.asserts.assert(foo)</code>
* will make the compiler treat <code>foo</code> as non-nullable. Similarly,
* <code>goog.asserts.assertNumber(foo)</code> informs the compiler about the
* type of <code>foo</code>. Where applicable, such assertions are preferable to
* casts by jsdoc with <code>@type</code>.
*
* The compiler has an option to disable asserts. So code like:
* <code>
* var x = goog.asserts.assert(foo());
* goog.asserts.assert(bar());
* </code>
* will be transformed into:
* <code>
* var x = foo();
* </code>
* 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.
*
* Additionally, note the compiler will consider the type to be "tightened" for
* all statements <em>after</em> the assertion. For example:
* <code>
* const /** ?Object &#ast;/ value = foo();
* goog.asserts.assert(value);
* // "value" is of type {!Object} at this point.
* </code>
*/
goog.provide('goog.asserts');
goog.provide('goog.asserts.AssertionError');
goog.require('goog.debug.Error');
goog.require('goog.dom.NodeType');
/**
* @define {boolean} Whether to strip out asserts or to leave them in.
*/
goog.asserts.ENABLE_ASSERTS =
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}
* @final
*/
goog.asserts.AssertionError = function(messagePattern, messageArgs) {
'use strict';
goog.debug.Error.call(this, goog.asserts.subs_(messagePattern, messageArgs));
/**
* 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 @type {string} */
goog.asserts.AssertionError.prototype.name = 'AssertionError';
/**
* The default error handler.
* @param {!goog.asserts.AssertionError} e The exception to be handled.
* @return {void}
*/
goog.asserts.DEFAULT_ERROR_HANDLER = function(e) {
'use strict';
throw e;
};
/**
* The handler responsible for throwing or logging assertion errors.
* @private {function(!goog.asserts.AssertionError)}
*/
goog.asserts.errorHandler_ = goog.asserts.DEFAULT_ERROR_HANDLER;
/**
* Does simple python-style string substitution.
* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
* @param {string} pattern The string containing the pattern.
* @param {!Array<*>} subs The items to substitute into the pattern.
* @return {string} A copy of `str` in which each occurrence of
* {@code %s} has been replaced an argument from `var_args`.
* @private
*/
goog.asserts.subs_ = function(pattern, subs) {
'use strict';
var splitParts = pattern.split('%s');
var returnString = '';
// Replace up to the last split part. We are inserting in the
// positions between split parts.
var subLast = splitParts.length - 1;
for (var i = 0; i < subLast; i++) {
// keep unsupplied as '%s'
var sub = (i < subs.length) ? subs[i] : '%s';
returnString += splitParts[i] + sub;
}
return returnString + splitParts[subLast];
};
/**
* 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) {
'use strict';
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).
var e = new goog.asserts.AssertionError('' + message, args || []);
goog.asserts.errorHandler_(e);
};
/**
* Sets a custom error handler that can be used to customize the behavior of
* assertion failures, for example by turning all assertion failures into log
* messages.
* @param {function(!goog.asserts.AssertionError)} errorHandler
* @return {void}
*/
goog.asserts.setErrorHandler = function(errorHandler) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS) {
goog.asserts.errorHandler_ = errorHandler;
}
};
/**
* Checks if the condition evaluates to true if goog.asserts.ENABLE_ASSERTS is
* true.
* @template T
* @param {T} 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 {T} The value of the condition.
* @throws {goog.asserts.AssertionError} When the condition evaluates to false.
* @closurePrimitive {asserts.truthy}
*/
goog.asserts.assert = function(condition, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && !condition) {
goog.asserts.doAssertFailure_(
'', null, opt_message, Array.prototype.slice.call(arguments, 2));
}
return condition;
};
/**
* Checks if `value` is `null` or `undefined` if goog.asserts.ENABLE_ASSERTS is
* true.
*
* @param {T} 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 {R} `value` with its type narrowed to exclude `null` and `undefined`.
*
* @template T
* @template R :=
* mapunion(T, (V) =>
* cond(eq(V, 'null'),
* none(),
* cond(eq(V, 'undefined'),
* none(),
* V)))
* =:
*
* @throws {!goog.asserts.AssertionError} When `value` is `null` or `undefined`.
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertExists = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && value == null) {
goog.asserts.doAssertFailure_(
'Expected to exist: %s.', [value], opt_message,
Array.prototype.slice.call(arguments, 2));
}
return value;
};
/**
* 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:
*
* <pre>
* switch(type) {
* case FOO: doSomething(); break;
* case BAR: doSomethingElse(); break;
* default: goog.asserts.fail('Unrecognized type: ' + type);
* // We have only 2 types - "default:" section is unreachable code.
* }
* </pre>
*
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {void}
* @throws {goog.asserts.AssertionError} Failure.
* @closurePrimitive {asserts.fail}
*/
goog.asserts.fail = function(opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS) {
goog.asserts.errorHandler_(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.
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertNumber = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && typeof value !== 'number') {
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.
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertString = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && typeof value !== 'string') {
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.
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertFunction = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && typeof value !== 'function') {
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.
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertObject = function(value, opt_message, var_args) {
'use strict';
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.
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertArray = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && !Array.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.
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertBoolean = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && typeof value !== 'boolean') {
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 a DOM Element 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 {!Element} The value, likely to be a DOM Element when asserts are
* enabled.
* @throws {goog.asserts.AssertionError} When the value is not an Element.
* @closurePrimitive {asserts.matchesReturn}
* @deprecated Use goog.asserts.dom.assertIsElement instead.
*/
goog.asserts.assertElement = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS &&
(!goog.isObject(value) ||
/** @type {!Node} */ (value).nodeType != goog.dom.NodeType.ELEMENT)) {
goog.asserts.doAssertFailure_(
'Expected Element but got %s: %s.', [goog.typeOf(value), value],
opt_message, Array.prototype.slice.call(arguments, 2));
}
return /** @type {!Element} */ (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.
*
* Do not use this to ensure a value is an HTMLElement or a subclass! Cross-
* document DOM inherits from separate - though identical - browser classes, and
* such a check will unexpectedly fail. Please use the methods in
* goog.asserts.dom for these purposes.
*
* @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
* @closurePrimitive {asserts.matchesReturn}
*/
goog.asserts.assertInstanceof = function(value, type, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS && !(value instanceof type)) {
goog.asserts.doAssertFailure_(
'Expected instanceof %s but got %s.',
[goog.asserts.getType_(type), goog.asserts.getType_(value)],
opt_message, Array.prototype.slice.call(arguments, 3));
}
return value;
};
/**
* Checks whether the value is a finite 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.
* @throws {goog.asserts.AssertionError} When the value is not a number, or is
* a non-finite number such as NaN, Infinity or -Infinity.
* @return {number} The value initially passed in.
*/
goog.asserts.assertFinite = function(value, opt_message, var_args) {
'use strict';
if (goog.asserts.ENABLE_ASSERTS &&
(typeof value != 'number' || !isFinite(value))) {
goog.asserts.doAssertFailure_(
'Expected %s to be a finite number but it is not.', [value],
opt_message, Array.prototype.slice.call(arguments, 2));
}
return /** @type {number} */ (value);
};
/**
* Returns the type of a value. If a constructor is passed, and a suitable
* string cannot be found, 'unknown type name' will be returned.
* @param {*} value A constructor, object, or primitive.
* @return {string} The best display name for the value, or 'unknown type name'.
* @private
*/
goog.asserts.getType_ = function(value) {
'use strict';
if (value instanceof Function) {
return value.displayName || value.name || 'unknown type name';
} else if (value instanceof Object) {
return /** @type {string} */ (value.constructor.displayName) ||
value.constructor.name || Object.prototype.toString.call(value);
} else {
return value === null ? 'null' : typeof value;
}
};