/*
Copyright (c) 2007, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
version: 2.3.0
*/
//-----------------------------------------------------------------------------
// TestLogger object
//-----------------------------------------------------------------------------
/**
* Displays test execution progress and results, providing filters based on
* different key events.
* @namespace YAHOO.tool
* @class TestLogger
* @constructor
* @param {HTMLElement} element (Optional) The element to create the logger in.
* @param {Object} config (Optional) Configuration options for the logger.
*/
this.init();
};
footerEnabled : true,
newestOnTop : false,
/**
* Formats message string to HTML for output to console.
* @private
* @method formatMsg
* @param oLogMsg {Object} Log message object.
* @return {String} HTML-formatted message for output to console.
*/
return "<pre><p><span class=\"" + category + "\">" + category.toUpperCase() + "</span> " + text + "</p></pre>";
},
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/*
* Initializes the logger.
* @private
*/
init : function () {
//attach to any available TestRunner
}
//hide useless sources
this.hideSource("global");
this.hideSource("LogReader");
//hide useless message categories
this.hideCategory("warn");
this.hideCategory("window");
this.hideCategory("time");
//reset the logger
this.clearConsole();
},
/**
* Clears the reference to the TestRunner from previous operations. This
* unsubscribes all events and removes the object reference.
* @return {Void}
* @static
*/
clearTestRunner : function () /*:Void*/ {
if (this._runner){
this._runner.unsubscribeAll();
this._runner = null;
}
},
/**
* Sets the source test runner that the logger should monitor.
* @param {YAHOO.tool.TestRunner} testRunner The TestRunner to observe.
* @return {Void}
* @static
*/
if (this._runner){
this.clearTestRunner();
}
this._runner = testRunner;
//setup event _handlers
testRunner.subscribe(testRunner.TEST_SUITE_COMPLETE_EVENT, this._handleTestRunnerEvent, this, true);
},
//-------------------------------------------------------------------------
// Event Handlers
//-------------------------------------------------------------------------
/**
* Handles all TestRunner events, outputting appropriate data into the console.
* @param {Object} data The event data object.
* @return {Void}
* @private
*/
//shortcut variables
//data variables
case TestRunner.BEGIN_EVENT:
messageType = "info";
break;
case TestRunner.COMPLETE_EVENT:
messageType = "info";
break;
case TestRunner.TEST_FAIL_EVENT:
messageType = "fail";
break;
case TestRunner.TEST_IGNORE_EVENT:
messageType = "ignore";
break;
case TestRunner.TEST_PASS_EVENT:
messageType = "pass";
break;
messageType = "info";
break;
messageType = "info";
break;
messageType = "info";
break;
messageType = "info";
break;
default:
message = "info";
}
}
});
/**
* The YUI test tool
* @module yuitest
* @namespace YAHOO.tool
* @requires yahoo,dom,event,logger
*/
//-----------------------------------------------------------------------------
// TestRunner object
//-----------------------------------------------------------------------------
/**
* Runs test suites and test cases, providing events to allowing for the
* interpretation of test results.
* @namespace YAHOO.tool
* @class TestRunner
* @static
*/
function TestRunner(){
//inherit from EventProvider
/**
* The test objects to run.
* @type Array
* @private
*/
this.items /*:Array*/ = [];
//create events
var events /*:Array*/ = [
this.TEST_CASE_BEGIN_EVENT,
this.TEST_CASE_COMPLETE_EVENT,
this.TEST_SUITE_BEGIN_EVENT,
this.TEST_PASS_EVENT,
this.TEST_FAIL_EVENT,
this.TEST_IGNORE_EVENT,
this.COMPLETE_EVENT,
this.BEGIN_EVENT
];
}
}
//-------------------------------------------------------------------------
// Constants
//-------------------------------------------------------------------------
/**
* Fires when a test case is opened but before the first
* test is executed.
* @event testcasebegin
*/
/**
* Fires when all tests in a test case have been executed.
* @event testcasecomplete
*/
/**
* Fires when a test suite is opened but before the first
* test is executed.
* @event testsuitebegin
*/
/**
* Fires when all test cases in a test suite have been
* completed.
* @event testsuitecomplete
*/
/**
* Fires when a test has passed.
* @event pass
*/
/**
* Fires when a test has failed.
* @event fail
*/
/**
* Fires when a test has been ignored.
* @event ignore
*/
/**
* Fires when all test suites and test cases have been completed.
* @event complete
*/
/**
* Fires when the run() method is called.
* @event begin
*/
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/**
* Runs a given test case.
* @param {YAHOO.tool.TestCase} testCase The test case to run.
* @return {Object} Results of the execution with properties passed, failed, and total.
* @method _runTestCase
* @private
* @static
*/
//object to store results
var results /*:Object*/ = {};
//test case begins
//gather the test functions
var tests /*:Array*/ = [];
}
}
//get the "should" test cases
//test counts
//run each test
//figure out if the test should be ignored or not
if (shouldIgnore[tests[i]]){
continue;
}
//variable to hold whether or not the test failed
var failed /*:Boolean*/ = false;
var error /*:Error*/ = null;
//run the setup
//try the test
try {
//run the test
//if it should fail, and it got here, then it's a fail because it didn't
if (shouldFail[tests[i]]){
failed = true;
} else if (shouldError[tests[i]]){
failed = true;
}
} catch (thrown /*:Error*/){
if (!shouldFail[tests[i]]){
failed = true;
}
} else {
//first check to see if it should error
if (!shouldError[tests[i]]) {
failed = true;
} else {
//check to see what type of data we have
//if it's a string, check the error message
failed = true;
}
//if it's an object, check the instance and message
failed = true;
}
}
}
}
} finally {
//fireEvent appropriate event
if (failed) {
} else {
}
}
//run the tear down
//update results
};
//update counts
runCount++;
}
//add test counts to results
//test case is done
//return results
return results;
},
/**
* Runs all the tests in a test suite.
* @param {YAHOO.tool.TestSuite} testSuite The test suite to run.
* @return {Object} Results of the execution with properties passed, failed, and total.
* @method _runTestSuite
* @private
* @static
*/
//object to store results
var results /*:Object*/ = {
passed: 0,
failed: 0,
total: 0
};
//fireEvent event for beginning of test suite run
//iterate over the test suite items
var result = null;
}
if (result !== null){
}
}
//fireEvent event for completion of test suite run
//return the results
return results;
},
/**
* Runs a test case or test suite, returning the results.
* @param {YAHOO.tool.TestCase|YAHOO.tool.TestSuite} testObject The test case or test suite to run.
* @return {Object} Results of the execution with properties passed, failed, and total.
* @private
* @method _run
* @static
*/
return this._runTestSuite(testObject);
return this._runTestCase(testObject);
} else {
throw new TypeError("_run(): Expected either YAHOO.tool.TestCase or YAHOO.tool.TestSuite.");
}
}
},
//-------------------------------------------------------------------------
// Protected Methods
//-------------------------------------------------------------------------
/*
* Fires events for the TestRunner. This overrides the default fireEvent()
* method from EventProvider to add the type property to the data that is
* passed through on each event call.
* @param {String} type The type of event to fire.
* @param {Object} data (Optional) Data for the event.
* @method fireEvent
* @static
* @protected
*/
},
//-------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------
/**
* Adds a test suite or test case to the list of test objects to run.
* @param testObject Either a TestCase or a TestSuite that should be run.
*/
},
/**
* Removes all test objects from the runner.
*/
clear : function () /*:Void*/ {
}
},
/**
* Runs the test suite.
*/
var results = null;
this.fireEvent(this.BEGIN_EVENT);
//an object passed in overrides everything else
} else {
results = {
passed: 0,
failed: 0,
total: 0
};
}
}
}
});
return new TestRunner();
})();
//-----------------------------------------------------------------------------
// TestSuite object
//-----------------------------------------------------------------------------
/**
* A test suite that can contain a collection of TestCase and TestSuite objects.
* @param {String} name The name of the test fixture.
* @namespace YAHOO.tool
* @class TestSuite
* @constructor
*/
/**
* The name of the test suite.
*/
/**
* Array of test suites and
* @private
*/
this.items /*:Array*/ = [];
};
/**
* Adds a test suite or test case to the test suite.
* @param {YAHOO.tool.TestSuite||YAHOO.tool.TestCase} testObject The test suite or test case to add.
*/
}
}
};
//-----------------------------------------------------------------------------
// TestCase object
//-----------------------------------------------------------------------------
/**
* Test case containing various tests to run.
* @param template An object containing any number of test methods, other methods,
* an optional name, and anything else the test case needs.
* @class TestCase
* @namespace YAHOO.tool
* @constructor
*/
/**
* Special rules for the test case. Possible subobjects
* are fail, for tests that should fail, and error, for
* tests that should throw an error.
*/
this._should /*:Object*/ = {};
//copy over all properties from the template to this object
}
//check for a valid name
/**
* Name for the test case.
*/
}
};
//-------------------------------------------------------------------------
// Test Methods
//-------------------------------------------------------------------------
/**
* Function to run before each test is executed.
*/
setUp : function () /*:Void*/ {
},
/**
* Function to run after each test is executed.
*/
tearDown: function () /*:Void*/ {
}
};
//-----------------------------------------------------------------------------
// Assert object
//-----------------------------------------------------------------------------
/**
* The Assert object provides functions to test JavaScript values against
* known and expected results. Whenever a comparison (assertion) fails,
* an error is thrown.
*
* @namespace YAHOO.util
* @class Assert
* @static
*/
//-------------------------------------------------------------------------
// Generic Assertion Methods
//-------------------------------------------------------------------------
/**
* Forces an assertion error to occur.
* @param {String} message (Optional) The message to display with the failure.
* @method fail
* @static
*/
},
//-------------------------------------------------------------------------
// Equality Assertion Methods
//-------------------------------------------------------------------------
/**
* Asserts that a value is equal to another. This uses the double equals sign
* so type cohersion may occur.
* @param {Object} expected The expected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areEqual
* @static
*/
}
},
/**
* Asserts that a value is not equal to another. This uses the double equals sign
* so type cohersion may occur.
* @param {Object} unexpected The unexpected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areNotEqual
* @static
*/
if (unexpected == actual) {
}
},
/**
* Asserts that a value is not the same as another. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} unexpected The unexpected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areNotSame
* @static
*/
if (unexpected === actual) {
}
},
/**
* Asserts that a value is the same as another. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} expected The expected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areSame
* @static
*/
}
},
//-------------------------------------------------------------------------
// Boolean Assertion Methods
//-------------------------------------------------------------------------
/**
* Asserts that a value is false. 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 isFalse
* @static
*/
if (false !== actual) {
}
},
/**
* Asserts that a value is true. 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 isTrue
* @static
*/
if (true !== actual) {
}
},
//-------------------------------------------------------------------------
// Special Value Assertion Methods
//-------------------------------------------------------------------------
/**
* Asserts that a value is not a number.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNaN
* @static
*/
}
},
/**
* Asserts that a value is not the special NaN value.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNotNaN
* @static
*/
}
},
/**
* Asserts that a value is not 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 isNotNull
* @static
*/
}
},
/**
* 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
*/
}
},
/**
* 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
*/
}
},
/**
* Asserts that a value is undefined. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} expected The expected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isUndefined
* @static
*/
}
},
//--------------------------------------------------------------------------
// 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
*/
}
},
/**
* 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
*/
}
},
/**
* 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
*/
}
},
/**
* 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*/ {
throw new YAHOO.util.ComparisonFailure(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
*/
}
},
/**
* 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
*/
}
},
/**
* 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
*/
}
},
/**
* 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(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
*/
//call superclass
/*
* Error message. Must be duplicated to ensure browser receives it.
* @type String
* @property message
*/
/**
* The name of the error that occurred.
* @type String
* @property name
*/
};
//inherit methods
/**
* 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*/ {
},
/**
* 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
/**
* The expected value.
* @type Object
* @property expected
*/
/**
* The actual value.
* @type Object
* @property actual
*/
/**
* The name of the error that occurred.
* @type String
* @property name
*/
};
//inherit methods
/**
* 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*/ {
}
});
/**
* 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
*/
//call superclass
/**
* The unexpected value.
* @type Object
* @property unexpected
*/
/**
* The name of the error that occurred.
* @type String
* @property name
*/
};
//inherit methods
/**
* 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*/ {
}
});
/**
* 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
*/
//call superclass
/**
* The name of the error that occurred.
* @type String
* @property name
*/
};
//inherit methods
/**
* ShouldError is subclass of AssertionError that is thrown whenever
* a test is expected to throw an error but doesn't.
*
* @param {String} message The message to display when the error occurs.
* @namespace YAHOO.util
* @extends YAHOO.util.AssertionError
* @class ShouldError
* @constructor
*/
//call superclass
arguments.callee.superclass.constructor.call(this, message || "This test should have thrown an error but didn't.");
/**
* The name of the error that occurred.
* @type String
* @property name
*/
};
//inherit methods
/**
* UnexpectedError is subclass of AssertionError that is thrown whenever
* an error occurs within the course of a test and the test was not expected
* to throw an error.
*
* @param {Error} cause The unexpected error that caused this error to be
* thrown.
* @namespace YAHOO.util
* @extends YAHOO.util.AssertionError
* @class UnexpectedError
* @constructor
*/
//call superclass
/**
* The unexpected error that occurred.
* @type Error
* @property cause
*/
/**
* The name of the error that occurred.
* @type String
* @property name
*/
};
//inherit methods
//-----------------------------------------------------------------------------
// ArrayAssert object
//-----------------------------------------------------------------------------
/**
* The ArrayAssert object provides functions to test JavaScript array objects
* for a variety of cases.
*
* @namespace YAHOO.util
* @class ArrayAssert
* @static
*/
/**
* Asserts that a value is present in an array. This uses the triple equals
* sign so no type cohersion may occur.
* @param {Object} needle The value that is expected in the array.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method contains
* @static
*/
var found /*:Boolean*/ = false;
//begin checking values
found = true;
}
}
if (!found){
}
},
/**
* Asserts that a set of values are present in an array. This uses the triple equals
* sign so no type cohersion may occur. For this assertion to pass, all values must
* be found.
* @param {Object[]} needles An array of values that are expected in the array.
* @param {Array} haystack An array of values to check.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method containsItems
* @static
*/
//begin checking values
}
if (!found){
}
},
/**
* Asserts that a value matching some condition is present in an array. This uses
* a function to determine a match.
* @param {Function} matcher A function that returns true if the items matches or false if not.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method containsMatch
* @static
*/
//check for valid matcher
if (typeof matcher != "function"){
throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
}
var found /*:Boolean*/ = false;
//begin checking values
found = true;
}
}
if (!found){
}
},
/**
* Asserts that a value is not present in an array. This uses the triple equals
* sign so no type cohersion may occur.
* @param {Object} needle The value that is expected in the array.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method doesNotContain
* @static
*/
var found /*:Boolean*/ = false;
//begin checking values
found = true;
}
}
if (found){
}
},
/**
* Asserts that a set of values are not present in an array. This uses the triple equals
* sign so no type cohersion may occur. For this assertion to pass, all values must
* not be found.
* @param {Object[]} needles An array of values that are not expected in the array.
* @param {Array} haystack An array of values to check.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method doesNotContainItems
* @static
*/
}
},
/**
* Asserts that no values matching a condition are present in an array. This uses
* a function to determine a match.
* @param {Function} matcher A function that returns true if the items matches or false if not.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method doesNotContainMatch
* @static
*/
//check for valid matcher
if (typeof matcher != "function"){
throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
}
var found /*:Boolean*/ = false;
//begin checking values
found = true;
}
}
if (found){
}
},
/**
* Asserts that the given value is contained in an array at the specified index.
* This uses the triple equals sign so no type cohersion will occur.
* @param {Object} needle The value to look for.
* @param {Array} haystack The array to search in.
* @param {int} index The index at which the value should exist.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method indexOf
* @static
*/
indexOf : function (needle /*:Object*/, haystack /*:Array*/, index /*:int*/, message /*:String*/) /*:Void*/ {
//try to find the value in the array
YAHOO.util.Assert.areEqual(index, i, message || "Value exists at index " + i + " but should be at index " + index + ".");
return;
}
}
//if it makes it here, it wasn't found at all
},
/**
* Asserts that the values in an array are equal, and in the same position,
* as values in another array. This uses the double equals sign
* so type cohersion may occur. Note that the array objects themselves
* need not be the same for this test to pass.
* @param {Array} expected An array of the expected values.
* @param {Array} actual Any array of the actual values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method itemsAreEqual
* @static
*/
//one may be longer than the other, so get the maximum length
//begin checking values
for (var i=0; i < len; i++){
"Values in position " + i + " are not equal.");
}
},
/**
* Asserts that the values in an array are equivalent, and in the same position,
* as values in another array. This uses a function to determine if the values
* are equivalent. Note that the array objects themselves
* need not be the same for this test to pass.
* @param {Array} expected An array of the expected values.
* @param {Array} actual Any array of the actual values.
* @param {Function} comparator A function that returns true if the values are equivalent
* or false if not.
* @param {String} message (Optional) The message to display if the assertion fails.
* @return {Void}
* @method itemsAreEquivalent
* @static
*/
//make sure the comparator is valid
if (typeof comparator != "function"){
throw new TypeError("ArrayAssert.itemsAreEquivalent(): Third argument must be a function.");
}
//one may be longer than the other, so get the maximum length
//begin checking values
for (var i=0; i < len; i++){
throw new YAHOO.util.ComparisonFailure(message || "Values in position " + i + " are not equivalent.", expected[i], actual[i]);
}
}
},
/**
* Asserts that an array is empty.
* @param {Array} actual The array to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isEmpty
* @static
*/
}
},
/**
* Asserts that an array is not empty.
* @param {Array} actual The array to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNotEmpty
* @static
*/
}
},
/**
* Asserts that the values in an array are the same, and in the same position,
* as values in another array. This uses the triple equals sign
* so no type cohersion will occur. Note that the array objects themselves
* need not be the same for this test to pass.
* @param {Array} expected An array of the expected values.
* @param {Array} actual Any array of the actual values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method itemsAreSame
* @static
*/
//one may be longer than the other, so get the maximum length
//begin checking values
for (var i=0; i < len; i++){
}
},
/**
* Asserts that the given value is contained in an array at the specified index,
* starting from the back of the array.
* This uses the triple equals sign so no type cohersion will occur.
* @param {Object} needle The value to look for.
* @param {Array} haystack The array to search in.
* @param {int} index The index at which the value should exist.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method lastIndexOf
* @static
*/
lastIndexOf : function (needle /*:Object*/, haystack /*:Array*/, index /*:int*/, message /*:String*/) /*:Void*/ {
//try to find the value in the array
YAHOO.util.Assert.areEqual(index, i, message || "Value exists at index " + i + " but should be at index " + index + ".");
return;
}
}
//if it makes it here, it wasn't found at all
}
};
//-----------------------------------------------------------------------------
// ObjectAssert object
//-----------------------------------------------------------------------------
/**
* The ObjectAssert object provides functions to test JavaScript objects
* for a variety of cases.
*
* @namespace YAHOO.util
* @class ObjectAssert
* @static
*/
/**
* Asserts that all properties in the object exist in another object.
* @param {Object} expected An object with the expected properties.
* @param {Object} actual An object with the actual properties.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method propertiesAreEqual
* @static
*/
//get all properties in the object
var properties /*:Array*/ = [];
}
//see if the properties are in the expected object
}
},
/**
* Asserts that an object has a property with the given name.
* @param {String} propertyName The name of the property to test.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method hasProperty
* @static
*/
hasProperty : function (propertyName /*:String*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
}
},
/**
* Asserts that a property with the given name exists on an object instance (not on its prototype).
* @param {String} propertyName The name of the property to test.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method hasProperty
* @static
*/
hasOwnProperty : function (propertyName /*:String*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
}
}
};
//-----------------------------------------------------------------------------
// DateAssert object
//-----------------------------------------------------------------------------
/**
* The DateAssert object provides functions to test JavaScript Date objects
* for a variety of cases.
*
* @namespace YAHOO.util
* @class DateAssert
* @static
*/
/**
* Asserts that a date's month, day, and year are equal to another date's.
* @param {Date} expected The expected date.
* @param {Date} actual The actual date to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areEqual
* @static
*/
YAHOO.util.Assert.areEqual(expected.getFullYear(), actual.getFullYear(), message || "Years should be equal.");
YAHOO.util.Assert.areEqual(expected.getMonth(), actual.getMonth(), message || "Months should be equal.");
YAHOO.util.Assert.areEqual(expected.getDate(), actual.getDate(), message || "Day of month should be equal.");
} else {
throw new TypeError("DateAssert.datesAreEqual(): Expected and actual values must be Date objects.");
}
},
/**
* Asserts that a date's hour, minutes, and seconds are equal to another date's.
* @param {Date} expected The expected date.
* @param {Date} actual The actual date to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areEqual
* @static
*/
YAHOO.util.Assert.areEqual(expected.getHours(), actual.getHours(), message || "Hours should be equal.");
YAHOO.util.Assert.areEqual(expected.getMinutes(), actual.getMinutes(), message || "Minutes should be equal.");
YAHOO.util.Assert.areEqual(expected.getSeconds(), actual.getSeconds(), message || "Seconds should be equal.");
} else {
throw new TypeError("DateAssert.timesAreEqual(): Expected and actual values must be Date objects.");
}
}
};
/**
* The UserAction object provides functions that simulate events occurring in
* the browser. Since these are simulated events, they do not behave exactly
* as regular, user-initiated events do, but can be used to test simple
* user interactions safely.
*
* @namespace YAHOO.util
* @class UserAction
* @static
*/
//--------------------------------------------------------------------------
// Generic event methods
//--------------------------------------------------------------------------
/**
* Simulates a key event using the given event information to populate
* the generated event object. This method does browser-equalizing
* calculations to account for differences in the DOM and IE event models
* as well as different browser quirks. Note: keydown causes Safari 2.x to
* crash.
* @method simulateKeyEvent
* @private
* @static
* @param {HTMLElement} target The target of the given event.
* @param {String} type The type of event to fire. This can be any one of
* the following: keyup, keydown, and keypress.
* @param {Boolean} bubbles (Optional) Indicates if the event can be
* bubbled up. DOM Level 3 specifies that all key events bubble by
* default. The default is true.
* @param {Boolean} cancelable (Optional) Indicates if the event can be
* canceled using preventDefault(). DOM Level 3 specifies that all
* key events can be cancelled. The default
* is true.
* @param {Window} view (Optional) The view containing the target. This is
* typically the window object. The default is window.
* @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys
* is pressed while the event is firing. The default is false.
* @param {Boolean} altKey (Optional) Indicates if one of the ALT keys
* is pressed while the event is firing. The default is false.
* @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys
* is pressed while the event is firing. The default is false.
* @param {Boolean} metaKey (Optional) Indicates if one of the META keys
* is pressed while the event is firing. The default is false.
* @param {int} keyCode (Optional) The code for the key that is in use.
* The default is 0.
* @param {int} charCode (Optional) The Unicode code for the character
* associated with the key being used. The default is 0.
*/
view /*:Window*/,
{
//check target
if (!target){
throw new Error("simulateKeyEvent(): Invalid target.");
}
//check event type
switch(type){
case "keyup":
case "keydown":
case "keypress":
break;
case "textevent": //DOM Level 3
type = "keypress";
break;
// @TODO was the fallthrough intentional, if so throw error
default:
}
} else {
throw new Error("simulateKeyEvent(): Event type must be a string.");
}
//setup default values
bubbles = true; //all key events bubble
}
cancelable = true; //all key events can be cancelled
}
}
ctrlKey = false;
}
altKey = false;
}
shiftKey = false;
}
metaKey = false;
}
keyCode = 0;
}
charCode = 0;
}
//check for DOM-compliant browsers first
//try to create a mouse event
var event /*:MouseEvent*/ = null;
try {
//try to create key event
/*
* Interesting problem: Firefox implemented a non-standard
* version of initKeyEvent() based on DOM Level 2 specs.
* Key event was removed from DOM Level 2 and re-introduced
* in DOM Level 3 with a different interface. Firefox is the
* only browser with any implementation of Key Events, so for
* now, assume it's Firefox if the above line doesn't error.
*/
//TODO: Decipher between Firefox's implementation and a correct one.
} catch (ex /*:Error*/){
/*
* If it got here, that means key events aren't officially supported.
* set keyCode, charCode, or other properties if you use a
* UIEvent, so we first must try to create a generic event. The
* fun part is that this will throw an error on Safari 2.x. The
* end result is that we need another try...catch statement just to
* deal with this mess.
*/
try {
//try to create generic event - will fail in Safari 2.x
} catch (uierror /*:Error*/){
//the above failed, so create a UIEvent for Safari 2.x
} finally {
//initialize
}
}
//fire the event
//create an IE event object
//assign available properties
/*
* IE doesn't support charCode explicitly. CharCode should
* take precedence over any keyCode value for accurate
* representation.
*/
//fire the event
} else {
throw new Error("simulateKeyEvent(): No event simulation framework present.");
}
},
/**
* Simulates a mouse event using the given event information to populate
* the generated event object. This method does browser-equalizing
* calculations to account for differences in the DOM and IE event models
* as well as different browser quirks.
* @method simulateMouseEvent
* @private
* @static
* @param {HTMLElement} target The target of the given event.
* @param {String} type The type of event to fire. This can be any one of
* the following: click, dblclick, mousedown, mouseup, mouseout,
* mouseover, and mousemove.
* @param {Boolean} bubbles (Optional) Indicates if the event can be
* bubbled up. DOM Level 2 specifies that all mouse events bubble by
* default. The default is true.
* @param {Boolean} cancelable (Optional) Indicates if the event can be
* canceled using preventDefault(). DOM Level 2 specifies that all
* mouse events except mousemove can be cancelled. The default
* is true for all events except mousemove, for which the default
* is false.
* @param {Window} view (Optional) The view containing the target. This is
* typically the window object. The default is window.
* @param {int} detail (Optional) The number of times the mouse button has
* been used. The default value is 1.
* @param {int} screenX (Optional) The x-coordinate on the screen at which
* point the event occured. The default is 0.
* @param {int} screenY (Optional) The y-coordinate on the screen at which
* point the event occured. The default is 0.
* @param {int} clientX (Optional) The x-coordinate on the client at which
* point the event occured. The default is 0.
* @param {int} clientY (Optional) The y-coordinate on the client at which
* point the event occured. The default is 0.
* @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys
* is pressed while the event is firing. The default is false.
* @param {Boolean} altKey (Optional) Indicates if one of the ALT keys
* is pressed while the event is firing. The default is false.
* @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys
* is pressed while the event is firing. The default is false.
* @param {Boolean} metaKey (Optional) Indicates if one of the META keys
* is pressed while the event is firing. The default is false.
* @param {int} button (Optional) The button being pressed while the event
* is executing. The value should be 0 for the primary mouse button
* (typically the left button), 1 for the terciary mouse button
* (typically the middle button), and 2 for the secondary mouse button
* (typically the right button). The default is 0.
* @param {HTMLElement} relatedTarget (Optional) For mouseout events,
* this is the element that the mouse has moved to. For mouseover
* events, this is the element that the mouse has moved from. This
* argument is ignored for all other events. The default is null.
*/
{
//check target
if (!target){
throw new Error("simulateMouseEvent(): Invalid target.");
}
//check event type
switch(type){
case "mouseover":
case "mouseout":
case "mousedown":
case "mouseup":
case "click":
case "dblclick":
case "mousemove":
break;
default:
}
} else {
throw new Error("simulateMouseEvent(): Event type must be a string.");
}
//setup default values
bubbles = true; //all mouse events bubble
}
}
}
}
screenX = 0;
}
screenY = 0;
}
clientX = 0;
}
clientY = 0;
}
ctrlKey = false;
}
altKey = false;
}
shiftKey = false;
}
metaKey = false;
}
button = 0;
}
//check for DOM-compliant browsers first
//try to create a mouse event
//Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()
if (event.initMouseEvent){
} else { //Safari
//the closest thing available in Safari 2.x is UIEvents
}
/*
* Check to see if relatedTarget has been assigned. Firefox
* versions less than 2.0 don't allow it to be assigned via
* initMouseEvent() and the property is readonly after event
* creation, so in order to keep YAHOO.util.getRelatedTarget()
* working, assign to the IE proprietary toElement property
* for mouseout event and fromElement property for mouseover
* event.
*/
if (type == "mouseout"){
} else if (type == "mouseover"){
}
}
//fire the event
//create an IE event object
//assign available properties
//fix button property for IE's wacky implementation
switch(button){
case 0:
break;
case 1:
break;
case 2:
//leave as is
break;
default:
}
/*
* Have to use relatedTarget because IE won't allow assignment
* to toElement or fromElement on generic events. This keeps
* YAHOO.util.Event.getRelatedTarget() functional.
*/
//fire the event
} else {
throw new Error("simulateMouseEvent(): No event simulation framework present.");
}
},
//--------------------------------------------------------------------------
// Mouse events
//--------------------------------------------------------------------------
/**
* Simulates a mouse event on a particular element.
* @param {HTMLElement} target The element to click on.
* @param {String} type The type of event to fire. This can be any one of
* the following: click, dblclick, mousedown, mouseup, mouseout,
* mouseover, and mousemove.
* @param {Object} options Additional event options (use DOM standard names).
* @method mouseEvent
* @static
*/
{
},
/**
* Simulates a click on a particular element.
* @param {HTMLElement} target The element to click on.
* @param {Object} options Additional event options (use DOM standard names).
* @method click
* @static
*/
},
/**
* Simulates a double click on a particular element.
* @param {HTMLElement} target The element to double click on.
* @param {Object} options Additional event options (use DOM standard names).
* @method dblclick
* @static
*/
},
/**
* Simulates a mousedown on a particular element.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method mousedown
* @static
*/
},
/**
* Simulates a mousemove on a particular element.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method mousemove
* @static
*/
},
/**
* Simulates a mouseout event on a particular element. Use "relatedTarget"
* on the options object to specify where the mouse moved to.
* Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so
* toElement is assigned in its place. IE doesn't allow toElement to be
* be assigned, so relatedTarget is assigned in its place. Both of these
* concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly
* in both browsers.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method mouseout
* @static
*/
},
/**
* Simulates a mouseover event on a particular element. Use "relatedTarget"
* on the options object to specify where the mouse moved from.
* Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so
* fromElement is assigned in its place. IE doesn't allow fromElement to be
* be assigned, so relatedTarget is assigned in its place. Both of these
* concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly
* in both browsers.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method mouseover
* @static
*/
},
/**
* Simulates a mouseup on a particular element.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method mouseup
* @static
*/
},
//--------------------------------------------------------------------------
// Key events
//--------------------------------------------------------------------------
/**
* Fires an event that normally would be fired by the keyboard (keyup,
* keydown, keypress). Make sure to specify either keyCode or charCode as
* an option.
* @private
* @param {String} type The type of event ("keyup", "keydown" or "keypress").
* @param {HTMLElement} target The target of the event.
* @param {Object} options Options for the event. Either keyCode or charCode
* are required.
* @method fireKeyEvent
* @static
*/
{
},
/**
* Simulates a keydown event on a particular element.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method keydown
* @static
*/
},
/**
* Simulates a keypress on a particular element.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method keypress
* @static
*/
},
/**
* Simulates a keyup event on a particular element.
* @param {HTMLElement} target The element to act on.
* @param {Object} options Additional event options (use DOM standard names).
* @method keyup
* @static
*/
}
};
//-----------------------------------------------------------------------------
// TestManager object
//-----------------------------------------------------------------------------
/**
* Runs pages containing test suite definitions.
* @namespace YAHOO.tool
* @class TestManager
* @static
*/
/**
* Constant for the testpagebegin custom event
* @property TEST_PAGE_BEGIN_EVENT
* @static
* @type string
* @final
*/
/**
* Constant for the testpagecomplete custom event
* @property TEST_PAGE_COMPLETE_EVENT
* @static
* @type string
* @final
*/
/**
* Constant for the testmanagerbegin custom event
* @property TEST_MANAGER_BEGIN_EVENT
* @static
* @type string
* @final
*/
/**
* Constant for the testmanagercomplete custom event
* @property TEST_MANAGER_COMPLETE_EVENT
* @static
* @type string
* @final
*/
//-------------------------------------------------------------------------
// Private Properties
//-------------------------------------------------------------------------
/**
* The URL of the page currently being executed.
* @type String
* @private
* @property _curPage
* @static
*/
_curPage /*:String*/ : null,
/**
* The frame used to load and run tests.
* @type Window
* @private
* @property _frame
* @static
*/
_frame /*:Window*/ : null,
/**
* The logger used to output results from the various tests.
* @type YAHOO.tool.TestLogger
* @private
* @property _logger
* @static
*/
_logger : null,
/**
* The timeout ID for the next iteration through the tests.
* @type int
* @private
* @property _timeoutId
* @static
*/
/**
* Array of pages to load.
* @type String[]
* @private
* @property _pages
* @static
*/
_pages /*:String[]*/ : [],
/**
* Aggregated results
* @type Object
* @private
* @property _results
* @static
*/
_results: null,
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/**
* Handles TestRunner.COMPLETE_EVENT, storing the results and beginning
* the loop again.
* @param {Object} data Data about the event.
* @return {Void}
* @private
* @static
*/
this.fireEvent(this.TEST_PAGE_COMPLETE_EVENT, {
});
//save results
//this._results[this.curPage] = data.results;
//process 'em
this._logger.clearTestRunner();
//if there's more to do, set a timeout to begin again
this._timeoutId = setTimeout(function(){
}, 1000);
}
},
/**
* Processes the results of a test page run, outputting log messages
* for failed tests.
* @return {Void}
* @private
* @static
*/
var r = this._results;
r.pages_passed++;
}
r.pages_failed++;
} else {
}
}
},
/**
* Loads the next test page into the iframe.
* @return {Void}
* @static
* @private
*/
_run : function () /*:Void*/ {
//set the current page
//load the frame - destroy history in case there are other iframes that
//need testing
},
//-------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------
/**
* Signals that a test page has been loaded. This should be called from
* within the test page itself to notify the TestManager that it is ready.
* @return {Void}
* @static
*/
load : function () /*:Void*/ {
} else {
if (this._frame) {
//assign event handling
//run it
TestRunner.run();
}
}
},
/**
* Sets the pages to be loaded.
* @param {String[]} pages An array of URLs to load.
* @return {Void}
* @static
*/
},
/**
* Begins the process of running the tests.
* @return {Void}
* @static
*/
start : function () /*:Void*/ {
if (!this._initialized) {
/**
* Fires when loading a test page
* @event testpagebegin
* @param curPage {string} the page being loaded
* @static
*/
this.createEvent(this.TEST_PAGE_BEGIN_EVENT);
/**
* Fires when a test page is complete
* @event testpagecomplete
* @param obj {page: string, results: object} the name of the
* page that was loaded, and the test suite results
* @static
*/
this.createEvent(this.TEST_PAGE_COMPLETE_EVENT);
/**
* Fires when the test manager starts running all test pages
* @event testmanagerbegin
* @static
*/
this.createEvent(this.TEST_MANAGER_BEGIN_EVENT);
/**
* Fires when the test manager finishes running all test pages. External
* test runners should subscribe to this event in order to get the
* aggregated test results.
* @event testmanagercomplete
* @param obj { pages_passed: int, pages_failed: int, tests_passed: int
* tests_failed: int, passed: string[], failed: string[],
* page_results: {} }
* @static
*/
this.createEvent(this.TEST_MANAGER_COMPLETE_EVENT);
//create iframe if not already available
if (!this._frame){
}
//create test logger if not already available
if (!this._logger){
}
this._initialized = true;
}
// reset the results cache
this._results = {
// number of pages that pass
pages_passed: 0,
// number of pages that fail
pages_failed: 0,
// total number of tests passed
tests_passed: 0,
// total number of tests failed
tests_failed: 0,
// array of pages that passed
passed: [],
// array of pages that failed
failed: [],
// map of full results for each page
page_results: {}
};
this.fireEvent(this.TEST_MANAGER_BEGIN_EVENT, null);
this._run();
},
/**
* Stops the execution of tests.
* @return {Void}
* @static
*/
stop : function () /*:Void*/ {
clearTimeout(this._timeoutId);
}
};