📄 yuitest_core.js
字号:
* @static */ isNotNull : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (YAHOO.lang.isNull(actual)) { throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Values should not be null."), null); } }, /** * Asserts that a value is not undefined. This uses the triple equals sign * so no type cohersion may occur. * @param {Object} actual The actual value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isNotUndefined * @static */ isNotUndefined : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (YAHOO.lang.isUndefined(actual)) { throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should not be undefined."), undefined); } }, /** * Asserts that a value is null. This uses the triple equals sign * so no type cohersion may occur. * @param {Object} actual The actual value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isNull * @static */ isNull : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isNull(actual)) { throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be null."), null, actual); } }, /** * Asserts that a value is undefined. This uses the triple equals sign * so no type cohersion may occur. * @param {Object} actual The actual value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isUndefined * @static */ isUndefined : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isUndefined(actual)) { throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be undefined."), undefined, actual); } }, //-------------------------------------------------------------------------- // Instance Assertion Methods //-------------------------------------------------------------------------- /** * Asserts that a value is an array. * @param {Object} actual The value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isArray * @static */ isArray : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isArray(actual)){ throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be an array."), actual); } }, /** * Asserts that a value is a Boolean. * @param {Object} actual The value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isBoolean * @static */ isBoolean : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isBoolean(actual)){ throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a Boolean."), actual); } }, /** * Asserts that a value is a function. * @param {Object} actual The value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isFunction * @static */ isFunction : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isFunction(actual)){ throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a function."), actual); } }, /** * Asserts that a value is an instance of a particular object. This may return * incorrect results when comparing objects from one frame to constructors in * another frame. For best results, don't use in a cross-frame manner. * @param {Function} expected The function that the object should be an instance of. * @param {Object} actual The object to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isInstanceOf * @static */ isInstanceOf : function (expected /*:Function*/, actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!(actual instanceof expected)){ throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value isn't an instance of expected type."), expected, actual); } }, /** * Asserts that a value is a number. * @param {Object} actual The value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isNumber * @static */ isNumber : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isNumber(actual)){ throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a number."), actual); } }, /** * Asserts that a value is an object. * @param {Object} actual The value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isObject * @static */ isObject : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isObject(actual)){ throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be an object."), actual); } }, /** * Asserts that a value is a string. * @param {Object} actual The value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isString * @static */ isString : function (actual /*:Object*/, message /*:String*/) /*:Void*/ { if (!YAHOO.lang.isString(actual)){ throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a string."), actual); } }, /** * Asserts that a value is of a particular type. * @param {String} expectedType The expected type of the variable. * @param {Object} actualValue The actual value to test. * @param {String} message (Optional) The message to display if the assertion fails. * @method isTypeOf * @static */ isTypeOf : function (expectedType /*:String*/, actualValue /*:Object*/, message /*:String*/) /*:Void*/{ if (typeof actualValue != expectedType){ throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be of type " + expected + "."), expected, typeof actual); } }};//-----------------------------------------------------------------------------// Assertion errors//-----------------------------------------------------------------------------/** * AssertionError is thrown whenever an assertion fails. It provides methods * to more easily get at error information and also provides a base class * from which more specific assertion errors can be derived. * * @param {String} message The message to display when the error occurs. * @namespace YAHOO.util * @class AssertionError * @extends Error * @constructor */ YAHOO.util.AssertionError = function (message /*:String*/){ //call superclass arguments.callee.superclass.constructor.call(this, message); /* * Error message. Must be duplicated to ensure browser receives it. * @type String * @property message */ this.message /*:String*/ = message; /** * The name of the error that occurred. * @type String * @property name */ this.name /*:String*/ = "AssertionError";};//inherit methodsYAHOO.lang.extend(YAHOO.util.AssertionError, Error, { /** * Returns a fully formatted error for an assertion failure. This should * be overridden by all subclasses to provide specific information. * @method getMessage * @return {String} A string describing the error. */ getMessage : function () /*:String*/ { return this.message; }, /** * Returns a string representation of the error. * @method toString * @return {String} A string representation of the error. */ toString : function () /*:String*/ { return this.name + ": " + this.getMessage(); }, /** * Returns a primitive value version of the error. Same as toString(). * @method valueOf * @return {String} A primitive value version of the error. */ valueOf : function () /*:String*/ { return this.toString(); }});/** * ComparisonFailure is subclass of AssertionError that is thrown whenever * a comparison between two values fails. It provides mechanisms to retrieve * both the expected and actual value. * * @param {String} message The message to display when the error occurs. * @param {Object} expected The expected value. * @param {Object} actual The actual value that caused the assertion to fail. * @namespace YAHOO.util * @extends YAHOO.util.AssertionError * @class ComparisonFailure * @constructor */ YAHOO.util.ComparisonFailure = function (message /*:String*/, expected /*:Object*/, actual /*:Object*/){ //call superclass arguments.callee.superclass.constructor.call(this, message); /** * The expected value. * @type Object * @property expected */ this.expected /*:Object*/ = expected; /** * The actual value. * @type Object * @property actual */ this.actual /*:Object*/ = actual; /** * The name of the error that occurred. * @type String * @property name */ this.name /*:String*/ = "ComparisonFailure"; };//inherit methodsYAHOO.lang.extend(YAHOO.util.ComparisonFailure, YAHOO.util.AssertionError, { /** * Returns a fully formatted error for an assertion failure. This message * provides information about the expected and actual values. * @method toString * @return {String} A string describing the error. */ getMessage : function () /*:String*/ { return this.message + "\nExpected: " + this.expected + " (" + (typeof this.expected) + ")" + "\nActual:" + this.actual + " (" + (typeof this.actual) + ")"; }});/** * UnexpectedValue is subclass of AssertionError that is thrown whenever * a value was unexpected in its scope. This typically means that a test * was performed to determine that a value was *not* equal to a certain * value. * * @param {String} message The message to display when the error occurs. * @param {Object} unexpected The unexpected value. * @namespace YAHOO.util * @extends YAHOO.util.AssertionError * @class UnexpectedValue * @constructor */ YAHOO.util.UnexpectedValue = function (message /*:String*/, unexpected /*:Object*/){ //call superclass arguments.callee.superclass.constructor.call(this, message); /** * The unexpected value. * @type Object * @property unexpected */ this.unexpected /*:Object*/ = unexpected; /** * The name of the error that occurred. * @type String * @property name */ this.name /*:String*/ = "UnexpectedValue"; };//inherit methodsYAHOO.lang.extend(YAHOO.util.UnexpectedValue, YAHOO.util.AssertionError, { /** * Returns a fully formatted error for an assertion failure. The message * contains information about the unexpected value that was encountered. * @method getMessage * @return {String} A string describing the error. */ getMessage : function () /*:String*/ { return this.message + "\nUnexpected: " + this.unexpected + " (" + (typeof this.unexpected) + ") "; }});/** * ShouldFail is subclass of AssertionError that is thrown whenever * a test was expected to fail but did not. * * @param {String} message The message to display when the error occurs. * @namespace YAHOO.util * @extends YAHOO.util.AssertionError * @class ShouldFail * @constructor */ YAHOO.util.ShouldFail = function (message /*:String*/){ //call superclass arguments.callee.superclass.constructor.call(this, message || "This test should fail but didn't."); /** * The name of the error that occurred. * @type String * @property name */ this.name /*:String*/ = "ShouldFail"; };//inherit methodsYAHOO.lang.extend(YAHOO.util.ShouldFail, YAHOO.util.AssertionError);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -