Runtime assertion system with type checking, existence verification, and debugging utilities for development and testing environments.
Core assertion functions for runtime validation.
/**
* Basic assertion that throws error if condition is false
* @param {*} condition - Condition to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assert = function(condition, opt_message, var_args) {};
/**
* Unconditionally throws assertion error
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @throws {goog.asserts.AssertionError} Always throws
*/
goog.asserts.fail = function(opt_message, var_args) {};Functions for asserting specific types with runtime validation.
/**
* Asserts value is a number
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {number} The value as number
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertNumber = function(value, opt_message, var_args) {};
/**
* Asserts value is a string
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {string} The value as string
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertString = function(value, opt_message, var_args) {};
/**
* Asserts value is a function
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {Function} The value as function
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertFunction = function(value, opt_message, var_args) {};
/**
* Asserts value is an object
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {Object} The value as object
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertObject = function(value, opt_message, var_args) {};
/**
* Asserts value is an array
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {Array} The value as array
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertArray = function(value, opt_message, var_args) {};
/**
* Asserts value is a boolean
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {boolean} The value as boolean
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertBoolean = function(value, opt_message, var_args) {};
/**
* Asserts value is a DOM element
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {Element} The value as DOM element
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertElement = function(value, opt_message, var_args) {};
/**
* Asserts value is an instance of specified type
* @param {*} value - Value to test
* @param {Function} type - Constructor function to test against
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {*} The value cast to specified type
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertInstanceof = function(value, type, opt_message, var_args) {};
/**
* Asserts value is a finite number
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {number} The value as finite number
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertFinite = function(value, opt_message, var_args) {};Functions for asserting value existence and optional types.
/**
* Asserts value is not null or undefined
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {*} The non-null value
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertExists = function(value, opt_message, var_args) {};
/**
* Asserts value is a string or null/undefined
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {string|null|undefined} The value as optional string
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertOptionalString = function(value, opt_message, var_args) {};
/**
* Asserts value is a number or null/undefined
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {number|null|undefined} The value as optional number
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertOptionalNumber = function(value, opt_message, var_args) {};
/**
* Asserts value is a boolean or null/undefined
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {boolean|null|undefined} The value as optional boolean
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertOptionalBoolean = function(value, opt_message, var_args) {};
/**
* Asserts value is a DOM element or null/undefined
* @param {*} value - Value to test
* @param {string=} opt_message - Optional error message
* @param {...*} var_args - Message format arguments
* @return {Element|null|undefined} The value as optional element
* @throws {goog.asserts.AssertionError} When assertion fails
*/
goog.asserts.assertOptionalElement = function(value, opt_message, var_args) {};Functions for asserting object and prototype integrity.
/**
* Asserts that Object.prototype has not been tampered with
* @param {*} value - Value with prototype to check
* @throws {goog.asserts.AssertionError} When prototype is corrupted
*/
goog.asserts.assertObjectPrototypeIsIntact = function(value) {};Usage Examples:
// Basic assertions
goog.asserts.assert(user.isLoggedIn, 'User must be logged in');
goog.asserts.assert(items.length > 0, 'Items array cannot be empty');
// Type assertions with return value
var userName = goog.asserts.assertString(user.name, 'User name must be string');
var userAge = goog.asserts.assertNumber(user.age, 'User age must be number');
// Optional type assertions
function processUserName(name) {
var validName = goog.asserts.assertOptionalString(name);
if (validName) {
return validName.toUpperCase();
}
return 'Anonymous';
}
// DOM element assertions
function highlightElement(elementId) {
var element = goog.dom.getElement(elementId);
goog.asserts.assertElement(element, 'Element not found: ' + elementId);
element.style.backgroundColor = 'yellow';
}
// Instance assertions
var dialog = goog.asserts.assertInstanceof(widget, goog.ui.Dialog,
'Widget must be a Dialog instance');