test-debug.js revision 4e91a2bd12314a991487543b079e91ff17d45ee3
/**
* YUI Test Framework
* @module yuitest
*/
/**
* The root namespace for YUI Test.
* @class YUITest
* @static
*/
var YUITest = {
version: "@VERSION@"
};
Y.namespace('Test');
//Using internal YUI methods here
YUITest.Object = Y.Object;
YUITest.Array = Y.Array;
}
/**
* Simple custom event implementation.
* @namespace YUITest
* @class EventTarget
* @constructor
*/
YUITest.EventTarget = function(){
/**
* Event handlers for the various events.
* @type Object
* @private
* @property _handlers
* @static
*/
this._handlers = {};
};
//restore prototype
//-------------------------------------------------------------------------
// Event Handling
//-------------------------------------------------------------------------
/**
* Adds a listener for a given event type.
* @param {String} type The type of event to add a listener for.
* @param {Function} listener The function to call when the event occurs.
* @return {void}
* @method attach
*/
}
},
/**
* Adds a listener for a given event type.
* @param {String} type The type of event to add a listener for.
* @param {Function} listener The function to call when the event occurs.
* @return {void}
* @method subscribe
* @deprecated
*/
},
/**
* Fires an event based on the passed-in object.
* @param {Object|String} event An object with at least a 'type' attribute
* or a string indicating the event name.
* @return {void}
* @method fire
*/
if (typeof event == "string"){
}
}
throw new Error("Event object missing 'type' property.");
}
}
}
},
/**
* Removes a listener for a given event type.
* @param {String} type The type of event to remove a listener from.
* @param {Function} listener The function to remove from the event.
* @return {void}
* @method detach
*/
break;
}
}
}
},
/**
* Removes a listener for a given event type.
* @param {String} type The type of event to remove a listener from.
* @param {Function} listener The function to remove from the event.
* @return {void}
* @method unsubscribe
* @deprecated
*/
}
};
/**
* A test suite that can contain a collection of TestCase and TestSuite objects.
* @param {String||Object} data The name of the test suite or an object containing
* a name property as well as setUp and tearDown methods.
* @namespace YUITest
* @class TestSuite
* @constructor
*/
/**
* The name of the test suite.
* @type String
* @property name
*/
this.name = "";
/**
* Array of test suites and test cases.
* @type Array
* @property items
* @private
*/
this.items = [];
//initialize the properties
if (typeof data == "string"){
} else if (data instanceof Object){
}
}
}
//double-check name
if (this.name === ""){
this.name = "testSuite" + (+new Date());
}
};
//restore constructor
/**
* Adds a test suite or test case to the test suite.
* @param {YUITest.TestSuite||YUITest.TestCase} testObject The test suite or test case to add.
* @return {Void}
* @method add
*/
add : function (testObject) {
}
return this;
},
//-------------------------------------------------------------------------
// Stub Methods
//-------------------------------------------------------------------------
/**
* Function to run before each test is executed.
* @return {Void}
* @method setUp
*/
setUp : function () {
},
/**
* Function to run after each test is executed.
* @return {Void}
* @method tearDown
*/
tearDown: function () {
}
};
/**
* 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 YUITest
* @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 = {};
//copy over all properties from the template to this object
}
//check for a valid name
if (typeof this.name != "string"){
this.name = "testCase" + (+new Date());
}
};
//restore constructor
/**
* Method to call from an async init method to
* restart the test case. When called, returns a function
* that should be called when tests are ready to continue.
* @method callback
* @return {Function} The function to call as a callback.
*/
callback: function(){
},
/**
* Resumes a paused test and runs the given function.
* @param {Function} segment (Optional) The function to run.
* If omitted, the test automatically passes.
* @return {Void}
* @method resume
*/
},
/**
* Causes the test case to wait a specified amount of time and then
* continue executing the given code.
* @param {Function} segment (Optional) The function to run after the delay.
* If omitted, the TestRunner will wait until resume() is called.
* @param {int} delay (Optional) The number of milliseconds to wait before running
* the function. If omitted, defaults to zero.
* @return {Void}
* @method wait
*/
if (typeof segment == "function"){
} else {
}, actualDelay);
}
},
//-------------------------------------------------------------------------
// Assertion Methods
//-------------------------------------------------------------------------
/**
* Asserts that a given condition is true. If not, then a YUITest.AssertionError object is thrown
* and the test fails.
* @method assert
* @param {Boolean} condition The condition to test.
* @param {String} message The message to display if the assertion fails.
*/
if (!condition){
}
},
/**
* Forces an assertion error to occur. Shortcut for YUITest.Assert.fail().
* @method fail
* @param {String} message (Optional) The message to display with the failure.
*/
},
//-------------------------------------------------------------------------
// Stub Methods
//-------------------------------------------------------------------------
/**
* Function to run once before tests start to run.
* This executes before the first call to setUp().
*/
init: function(){
//noop
},
/**
* Function to run once after tests finish running.
* This executes after the last call to tearDown().
*/
destroy: function(){
//noop
},
/**
* Function to run before each test is executed.
* @return {Void}
* @method setUp
*/
setUp : function () {
//noop
},
/**
* Function to run after each test is executed.
* @return {Void}
* @method tearDown
*/
tearDown: function () {
//noop
}
};
/**
* An object object containing test result formatting methods.
* @namespace YUITest
* @class TestFormat
* @static
*/
YUITest.TestFormat = function(){
/* (intentionally not documented)
* Basic XML escaping method. Replaces quotes, less-than, greater-than,
* apostrophe, and ampersand characters with their corresponding entities.
* @param {String} text The text to encode.
* @return {String} The XML-escaped text.
*/
switch(value){
case "\"": return """;
case "'": return "'";
case "&": return "&";
}
});
}
return {
/**
* Returns test results formatted as a JSON string. Requires JSON utility.
* @param {Object} result The results object created by TestRunner.
* @return {String} A JSON-formatted string of results.
* @method JSON
* @static
*/
},
/**
* Returns test results formatted as an XML string.
* @param {Object} result The results object created by TestRunner.
* @return {String} An XML-formatted string of results.
* @method XML
* @static
*/
function serializeToXML(results){
}
} else {
xml += " passed=\"" + results.passed + "\" failed=\"" + results.failed + "\" ignored=\"" + results.ignored + "\" total=\"" + results.total + "\">";
}
}
}
}
return xml;
}
},
/**
* Returns test results formatted in JUnit XML format.
* @param {Object} result The results object created by TestRunner.
* @return {String} An XML-formatted string of results.
* @method JUnitXML
* @static
*/
function serializeToJUnitXML(results){
var xml = "";
//equivalent to testcase in JUnit
case "test":
xml = "<testcase name=\"" + xmlEscape(results.name) + "\" time=\"" + (results.duration/1000) + "\">";
xml += "<failure message=\"" + xmlEscape(results.message) + "\"><![CDATA[" + results.message + "]]></failure>";
}
xml+= "</testcase>";
}
break;
//equivalent to testsuite in JUnit
case "testcase":
xml = "<testsuite name=\"" + xmlEscape(results.name) + "\" tests=\"" + results.total + "\" failures=\"" + results.failed + "\" time=\"" + (results.duration/1000) + "\">";
}
}
}
xml += "</testsuite>";
break;
//no JUnit equivalent, don't output anything
case "testsuite":
}
}
}
break;
//top-level, equivalent to testsuites in JUnit
case "report":
xml = "<testsuites>";
}
}
}
xml += "</testsuites>";
//no default
}
return xml;
}
},
/**
* Returns test results formatted in TAP format.
* For more information, see <a href="http://testanything.org/">Test Anything Protocol</a>.
* @param {Object} result The results object created by TestRunner.
* @return {String} A TAP-formatted string of results.
* @method TAP
* @static
*/
var currentTestNum = 1;
function serializeToTAP(results){
var text = "";
case "test":
}
text += "\n";
} else {
}
break;
case "testcase":
text = "#Begin testcase " + results.name + "(" + results.failed + " failed of " + results.total + ")\n";
}
}
}
break;
case "testsuite":
text = "#Begin testsuite " + results.name + "(" + results.failed + " failed of " + results.total + ")\n";
}
}
}
break;
case "report":
}
}
}
//no default
}
return text;
}
}
};
}();
/**
* An object capable of sending test results to a server.
* @param {String} url The URL to submit the results to.
* @param {Function} format (Optiona) A function that outputs the results in a specific format.
* Default is YUITest.TestFormat.XML.
* @constructor
* @namespace YUITest
* @class Reporter
*/
/**
* The URL to submit the data to.
* @type String
* @property url
*/
/**
* The formatting function to call when submitting the data.
* @type Function
* @property format
*/
/**
* Extra fields to submit with the request.
* @type Object
* @property _fields
* @private
*/
this._fields = new Object();
/**
* The form element used to submit the results.
* @type HTMLFormElement
* @property _form
* @private
*/
this._form = null;
/**
* Iframe used as a target for form submission.
* @type HTMLIFrameElement
* @property _iframe
* @private
*/
this._iframe = null;
};
//restore missing constructor
/**
* Adds a field to the form that submits the results.
* @param {String} name The name of the field.
* @param {Variant} value The value of the field.
* @return {Void}
* @method addField
*/
},
/**
* Removes all previous defined fields.
* @return {Void}
* @method addField
*/
clearFields : function(){
this._fields = new Object();
},
/**
* Cleans up the memory associated with the TestReporter, removing DOM elements
* that were created.
* @return {Void}
* @method destroy
*/
destroy : function() {
if (this._form){
this._form = null;
}
if (this._iframe){
this._iframe = null;
}
this._fields = null;
},
/**
* Sends the report to the server.
* @param {Object} results The results object created by TestRunner.
* @return {Void}
* @method report
*/
//if the form hasn't been created yet, create it
if (!this._form){
//IE won't let you assign a name using the DOM, must do it the hacky way
try {
} catch (ex){
}
}
//set the form's action
//remove any existing fields
while(this._form.hasChildNodes()){
}
//create default fields
//add fields to the form
}
}
//remove default fields
if (arguments[1] !== false){
}
}
};
/**
* Runs test suites and test cases, providing events to allowing for the
* interpretation of test results.
* @namespace YUITest
* @class TestRunner
* @static
*/
YUITest.TestRunner = function(){
/*(intentionally not documented)
* Determines if any of the array of test groups appears
* in the given TestRunner filter.
* @param {Array} testGroups The array of test groups to
* search for.
* @param {String} filter The TestRunner groups filter.
*/
return true;
} else {
if (testGroups){
return true;
}
}
}
return false;
}
}
/**
* A node in the test tree structure. May represent a TestSuite, TestCase, or
* test function.
* @param {Variant} testObject A TestSuite, TestCase, or the name of a test function.
* @class TestNode
* @constructor
* @private
*/
function TestNode(testObject){
/**
* The TestSuite, TestCase, or test function represented by this node.
* @type Variant
* @property testObject
*/
this.testObject = testObject;
/**
* Pointer to this node's first child.
* @type TestNode
* @property firstChild
*/
this.firstChild = null;
/**
* Pointer to this node's last child.
* @type TestNode
* @property lastChild
*/
this.lastChild = null;
/**
* Pointer to this node's parent.
* @type TestNode
* @property parent
*/
this.parent = null;
/**
* Pointer to this node's next sibling.
* @type TestNode
* @property next
*/
this.next = null;
/**
* Test results for this test object.
* @type object
* @property results
*/
//initialize results
}
}
/**
* Appends a new test object (TestSuite, TestCase, or test function name) as a child
* of this node.
* @param {Variant} testObject A TestSuite, TestCase, or the name of a test function.
* @return {Void}
*/
appendChild : function (testObject){
if (this.firstChild === null){
} else {
}
return node;
}
};
/**
* Runs test suites and test cases, providing events to allowing for the
* interpretation of test results.
* @namespace Test
* @class Runner
* @static
*/
function TestRunner(){
//inherit from EventTarget
/**
* Suite on which to attach all TestSuites and TestCases to be run.
* @type YUITest.TestSuite
* @property masterSuite
* @static
* @private
*/
/**
* Pointer to the current node in the test tree.
* @type TestNode
* @private
* @property _cur
* @static
*/
this._cur = null;
/**
* Pointer to the root node in the test tree.
* @type TestNode
* @private
* @property _root
* @static
*/
this._root = null;
/**
* Indicates if the TestRunner will log events or not.
* @type Boolean
* @property _log
* @private
* @static
*/
this._log = true;
/**
* Indicates if the TestRunner is waiting as a result of
* wait() being called.
* @type Boolean
* @property _waiting
* @private
* @static
*/
this._waiting = false;
/**
* Indicates if the TestRunner is currently running tests.
* @type Boolean
* @private
* @property _running
* @static
*/
this._running = false;
/**
* Holds copy of the results object generated when all tests are
* complete.
* @type Object
* @private
* @property _lastResults
* @static
*/
this._lastResults = null;
/**
* Data object that is passed around from method to method.
* @type Object
* @private
* @property _data
* @static
*/
this._context = null;
/**
* The list of test groups to run. The list is represented
* by a comma delimited string with commas at the start and
* end.
* @type String
* @private
* @property _groups
* @static
*/
this._groups = "";
}
/**
* If true, YUITest will not fire an error for tests with no Asserts.
* @prop ignoreEmpty
* @type Boolean
* @static
*/
ignoreEmpty: false,
//restore prototype
//-------------------------------------------------------------------------
// Constants
//-------------------------------------------------------------------------
/**
* Fires when a test case is opened but before the first
* test is executed.
* @event testcasebegin
* @static
*/
TEST_CASE_BEGIN_EVENT : "testcasebegin",
/**
* Fires when all tests in a test case have been executed.
* @event testcasecomplete
* @static
*/
TEST_CASE_COMPLETE_EVENT : "testcasecomplete",
/**
* Fires when a test suite is opened but before the first
* test is executed.
* @event testsuitebegin
* @static
*/
TEST_SUITE_BEGIN_EVENT : "testsuitebegin",
/**
* Fires when all test cases in a test suite have been
* completed.
* @event testsuitecomplete
* @static
*/
TEST_SUITE_COMPLETE_EVENT : "testsuitecomplete",
/**
* Fires when a test has passed.
* @event pass
* @static
*/
TEST_PASS_EVENT : "pass",
/**
* Fires when a test has failed.
* @event fail
* @static
*/
TEST_FAIL_EVENT : "fail",
/**
* Fires when a non-test method has an error.
* @event error
* @static
*/
ERROR_EVENT : "error",
/**
* Fires when a test has been ignored.
* @event ignore
* @static
*/
TEST_IGNORE_EVENT : "ignore",
/**
* Fires when all test suites and test cases have been completed.
* @event complete
* @static
*/
COMPLETE_EVENT : "complete",
/**
* Fires when the run() method is called.
* @event begin
* @static
*/
BEGIN_EVENT : "begin",
//-------------------------------------------------------------------------
// Test Tree-Related Methods
//-------------------------------------------------------------------------
/**
* Adds a test case to the test tree as a child of the specified node.
* @param {TestNode} parentNode The node to add the test case to as a child.
* @param {YUITest.TestCase} testCase The test case to add.
* @return {Void}
* @static
* @private
* @method _addTestCaseToTestTree
*/
//add the test suite
prop,
//iterate over the items in the test case
}
}
},
/**
* Adds a test suite to the test tree as a child of the specified node.
* @param {TestNode} parentNode The node to add the test suite to as a child.
* @param {YUITest.TestSuite} testSuite The test suite to add.
* @return {Void}
* @static
* @private
* @method _addTestSuiteToTestTree
*/
//add the test suite
//iterate over the items in the master suite
}
}
},
/**
* Builds the test tree based on items in the master suite. The tree is a hierarchical
* representation of the test suites, test cases, and test functions. The resulting tree
* is stored in _root and the pointer _cur is set to the root initially.
* @return {Void}
* @static
* @private
* @method _buildTestTree
*/
_buildTestTree : function () {
//this._cur = this._root;
//iterate over the items in the master suite
}
}
},
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/**
* Handles the completion of a test object's tests. Tallies test results
* from one level up to the next.
* @param {TestNode} node The TestNode representing the test object.
* @return {Void}
* @method _handleTestObjectComplete
* @private
*/
_handleTestObjectComplete : function (node) {
var parentNode;
if (parentNode){
}
this.fire({ type: this.TEST_SUITE_COMPLETE_EVENT, testSuite: node.testObject, results: node.results});
this.fire({ type: this.TEST_CASE_COMPLETE_EVENT, testCase: node.testObject, results: node.results});
}
}
},
//-------------------------------------------------------------------------
// Navigation Methods
//-------------------------------------------------------------------------
/**
* Retrieves the next node in the test tree.
* @return {TestNode} The next node in the test tree or null if the end is reached.
* @private
* @static
* @method _next
*/
_next : function () {
if (this._cur === null){
} else if (this._cur.firstChild) {
} else {
this._handleTestObjectComplete(this._cur);
}
this._handleTestObjectComplete(this._cur);
this._running = false;
this._cur = null;
} else if (this._cur) {
}
}
return this._cur;
},
/**
* Executes a non-test method (init, setUp, tearDown, destroy)
* and traps an errors. If an error occurs, an error event is
* fired.
* @param {Object} node The test node in the testing tree.
* @param {String} methodName The name of the method to execute.
* @param {Boolean} allowAsync Determines if the method can be called asynchronously.
* @return {Boolean} True if an async method was called, false if not.
* @method _execNonTestMethod
* @private
*/
try {
return true;
} else {
}
} catch (ex){
} else {
}
}
return false;
},
/**
* Runs a test case or test suite, returning the results.
* @param {YUITest.TestCase|YUITest.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
*/
_run : function () {
//flag to indicate if the TestRunner should wait before continuing
var shouldWait = false;
//get the next test node
if (node !== null) {
//set flag to say the testrunner is running
this._running = true;
//eliminate last results
this._lastResult = null;
//figure out what to do
//regular or async init
/*try {
if (testObject["async:init"]){
testObject["async:init"](this._context);
return;
} else {
testObject.init(this._context);
}
} catch (ex){
node.results.errors++;
this.fire({ type: this.ERROR_EVENT, error: ex, testCase: testObject, methodName: "init" });
}*/
return;
}
}
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
setTimeout(function(){
}, 0);
} else {
this._run();
}
} else {
}
}
},
_resumeTest : function (segment) {
//get relevant information
//we know there's no more waiting now
this._waiting = false;
//if there's no node, it probably means a wait() was called after resume()
if (!node){
//TODO: Handle in some way?
//console.log("wait() called after resume()");
//this.fire("error", { testCase: "(unknown)", test: "(unknown)", error: new Error("wait() called after resume()")} );
return;
}
//cancel other waits if available
if (testCase.__yui_wait){
delete testCase.__yui_wait;
}
//get the "should" test cases
//variable to hold whether or not the test failed
var failed = false;
var error = null;
//try the test
try {
//run the test
//if the test hasn't already failed and doesn't have any asserts...
}
//if it should fail, and it got here, then it's a fail because it didn't
else if (shouldFail){
failed = true;
} else if (shouldError){
failed = true;
}
} catch (thrown){
//cancel any pending waits, the test already failed
if (testCase.__yui_wait){
delete testCase.__yui_wait;
}
//figure out what type of error it was
if (!shouldFail){
failed = true;
}
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
this._waiting = true;
} else {
throw new Error("Asynchronous tests not supported in this environment.");
}
}
}
return;
} else {
//first check to see if it should error
if (!shouldError) {
failed = true;
} else {
//check to see what type of data we have
if (typeof shouldError == "string"){
//if it's a string, check the error message
failed = true;
}
} else if (typeof shouldError == "function"){
//if it's a function, see if the error is an instance of it
if (!(thrown instanceof shouldError)){
failed = true;
}
//if it's an object, check the instance and message
failed = true;
}
}
}
}
}
//fire appropriate event
if (failed) {
} else {
}
//run the tear down
//reset the assert count
//calculate duration
//update results
type: "test",
};
if (failed){
} else {
}
//set timeout not supported in all environments
if (typeof setTimeout != "undefined"){
setTimeout(function(){
}, 0);
} else {
this._run();
}
},
/**
* Handles an error as if it occurred within the currently executing
* test. This is for mock methods that may be called asynchronously
* and therefore out of the scope of the TestRunner. Previously, this
* error would bubble up to the browser. Now, this method is used
* to tell TestRunner about the error. This should never be called
* by anyplace other than the Mock object.
* @param {Error} error The error object.
* @return {Void}
* @method _handleError
* @private
* @static
*/
_handleError: function(error){
if (this._waiting){
this._resumeTest(function(){
throw error;
});
} else {
throw error;
}
},
/**
* Runs a single test based on the data provided in the node.
* @param {TestNode} node The TestNode representing the test to run.
* @return {Void}
* @static
* @private
* @name _runTest
*/
//get relevant information
//get the "should" test cases
//figure out if the test should be ignored or not
if (shouldIgnore){
//update results
result: "ignore",
message: "Test ignored",
type: "test",
};
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
setTimeout(function(){
}, 0);
} else {
this._run();
}
} else {
//mark the start time
//run the setup
//now call the body of the test
this._resumeTest(test);
}
},
//-------------------------------------------------------------------------
// Misc Methods
//-------------------------------------------------------------------------
/**
* Retrieves the name of the current result set.
* @return {String} The name of the result set.
* @method getName
*/
getName: function(){
return this.masterSuite.name;
},
/**
* The name assigned to the master suite of the TestRunner. This is the name
* that is output as the root's name when results are retrieved.
* @param {String} name The name of the result set.
* @return {Void}
* @method setName
*/
},
//-------------------------------------------------------------------------
// 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.
* @return {Void}
* @method add
* @static
*/
add : function (testObject) {
return this;
},
/**
* Removes all test objects from the runner.
* @return {Void}
* @method clear
* @static
*/
clear : function () {
},
/**
* Indicates if the TestRunner is waiting for a test to resume
* @return {Boolean} True if the TestRunner is waiting, false if not.
* @method isWaiting
* @static
*/
isWaiting: function() {
return this._waiting;
},
/**
* Indicates that the TestRunner is busy running tests and therefore can't
* be stopped and results cannot be gathered.
* @return {Boolean} True if the TestRunner is running, false if not.
* @method isRunning
*/
isRunning: function(){
return this._running;
},
/**
* Returns the last complete results set from the TestRunner. Null is returned
* if the TestRunner is running or no tests have been run.
* @param {Function} format (Optional) A test format to return the results in.
* @return {Object|String} Either the results object or, if a test format is
* passed as the argument, a string representing the results in a specific
* format.
* @method getResults
*/
getResults: function(format){
if (!this._running && this._lastResults){
if (typeof format == "function"){
return format(this._lastResults);
} else {
return this._lastResults;
}
} else {
return null;
}
},
/**
* Returns the coverage report for the files that have been executed.
* This returns only coverage information for files that have been
* instrumented using YUI Test Coverage and only those that were run
* in the same pass.
* @param {Function} format (Optional) A coverage format to return results in.
* @return {Object|String} Either the coverage object or, if a coverage
* format is specified, a string representing the results in that format.
* @method getCoverage
*/
getCoverage: function(format){
if (typeof format == "function"){
return format(_yuitest_coverage);
} else {
return _yuitest_coverage;
}
} else {
return null;
}
},
/**
* Used to continue processing when a method marked with
* "async:" is executed. This should not be used in test
* methods, only in init(). Each argument is a string, and
* when the returned function is executed, the arguments
* are assigned to the context data object using the string
* as the key name (value is the argument itself).
* @private
* @return {Function} A callback function.
*/
callback: function(){
that = this;
return function(){
}
};
},
/**
* Resumes the TestRunner after wait() was called.
* @param {Function} segment The function to run as the rest
* of the haulted test.
* @return {Void}
* @method resume
* @static
*/
if (this._waiting){
this._resumeTest(segment || function(){});
} else {
throw new Error("resume() called without wait().");
}
},
/**
* Runs the test suite.
* @param {Object|Boolean} options (Optional) Options for the runner:
* <code>oldMode</code> indicates the TestRunner should work in the YUI <= 2.8 way
* of internally managing test suites. <code>groups</code> is an array
* of test groups indicating which tests to run.
* @return {Void}
* @method run
* @static
*/
//pointer to runner to avoid scope issues
//if there's only one suite on the masterSuite, move it up
if (!oldMode && this.masterSuite.items.length == 1 && this.masterSuite.items[0] instanceof YUITest.TestSuite){
}
//determine if there are any groups to filter on
//initialize the runner
//fire the begin event
//begin the testing
}
});
return new TestRunner();
}();
/**
* The ArrayAssert object provides functions to test JavaScript array objects
* for a variety of cases.
* @namespace YUITest
* @class ArrayAssert
* @static
*/
YUITest.ArrayAssert = {
//=========================================================================
// Private methods
//=========================================================================
/**
* Simple indexOf() implementation for an array. Defers to native
* if available.
* @param {Array} haystack The array to search.
* @param {Variant} needle The value to locate.
* @return {int} The index of the needle if found or -1 if not.
* @method _indexOf
* @private
*/
} else {
return i;
}
}
return -1;
}
},
/**
* Simple some() implementation for an array. Defers to native
* if available.
* @param {Array} haystack The array to search.
* @param {Function} matcher The function to run on each value.
* @return {Boolean} True if any value, when run through the matcher,
* returns true.
* @method _some
* @private
*/
} else {
return true;
}
}
return false;
}
},
/**
* 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
*/
message) {
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value " + needle + " (" + (typeof needle) + ") not found in array [" + haystack + "]."));
}
},
/**
* 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
*/
message) {
//begin checking values
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value " + needles[i] + " (" + (typeof needles[i]) + ") not found in array [" + haystack + "]."));
}
}
},
/**
* 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
*/
message) {
//check for valid matcher
if (typeof matcher != "function"){
throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
}
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "No match found in array [" + haystack + "]."));
}
},
/**
* Asserts that a value is not present in an array. This uses the triple equals
* 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
*/
message) {
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
},
/**
* 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
*/
message) {
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
}
},
/**
* 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 item 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
*/
message) {
//check for valid matcher
if (typeof matcher != "function"){
throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
}
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
},
/**
* 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
*/
//try to find the value in the array
if (index != i){
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value exists at index " + i + " but should be at index " + index + "."));
}
return;
}
}
//if it makes it here, it wasn't found at all
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value doesn't exist in array [" + haystack + "]."));
},
/**
* 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
*/
message) {
//first make sure they're array-like (this can probably be improved)
}
//next check array length
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length + "."));
}
//begin checking values
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not equal."), expected[i], actual[i]);
}
}
},
/**
* 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
*/
comparator, message) {
//make sure the comparator is valid
if (typeof comparator != "function"){
throw new TypeError("ArrayAssert.itemsAreEquivalent(): Third argument must be a function.");
}
//first check array length
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length));
}
//begin checking values
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(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
*/
message) {
//first check array length
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length));
}
//begin checking values
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not the same."), expected[i], actual[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
*/
//try to find the value in the array
if (index != i){
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value exists at index " + i + " but should be at index " + index + "."));
}
return;
}
}
//if it makes it here, it wasn't found at all
}
};
/**
* The Assert object provides functions to test JavaScript values against
* known and expected results. Whenever a comparison (assertion) fails,
* an error is thrown.
* @namespace YUITest
* @class Assert
* @static
*/
/**
* The number of assertions performed.
* @property _asserts
* @type int
* @private
*/
_asserts: 0,
//-------------------------------------------------------------------------
// Helper Methods
//-------------------------------------------------------------------------
/**
* Formats a message so that it can contain the original assertion message
* in addition to the custom message.
* @param {String} customMessage The message passed in by the developer.
* @param {String} defaultMessage The message created by the error by default.
* @return {String} The final error message, containing either or both.
* @protected
* @static
* @method _formatMessage
*/
} else {
return defaultMessage;
}
},
/**
* Returns the number of assertions that have been performed.
* @method _getCount
* @protected
* @static
*/
_getCount: function(){
return this._asserts;
},
/**
* Increments the number of assertions that have been performed.
* @method _increment
* @protected
* @static
*/
_increment: function(){
this._asserts++;
},
/**
* Resets the number of assertions that have been performed to 0.
* @method _reset
* @protected
* @static
*/
_reset: function(){
this._asserts = 0;
},
//-------------------------------------------------------------------------
// Generic Assertion Methods
//-------------------------------------------------------------------------
/**
* Forces an assertion error to occur.
* @param {String} message (Optional) The message to display with the failure.
* @method fail
* @static
*/
},
/**
* A marker that the test should pass.
* @method pass
* @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
*/
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values should be equal."), expected, actual);
}
},
/**
* 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
*/
message) {
if (unexpected == actual) {
throw new YUITest.UnexpectedValue(YUITest.Assert._formatMessage(message, "Values should not be equal."), unexpected);
}
},
/**
* 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) {
throw new YUITest.UnexpectedValue(YUITest.Assert._formatMessage(message, "Values should not be the same."), unexpected);
}
},
/**
* 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
*/
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values should be the same."), expected, actual);
}
},
//-------------------------------------------------------------------------
// 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) {
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Value should be false."), 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) {
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Value should be true."), 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
*/
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Value should be NaN."), NaN, actual);
}
},
/**
* 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
*/
throw new YUITest.UnexpectedValue(YUITest.Assert._formatMessage(message, "Values should not be NaN."), NaN);
}
},
/**
* 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
*/
if (actual === null) {
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
if (typeof actual == "undefined") {
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
if (actual !== null) {
throw new YUITest.ComparisonFailure(YUITest.Assert._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
*/
if (typeof actual != "undefined") {
throw new YUITest.ComparisonFailure(YUITest.Assert._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
*/
var shouldFail = false;
if (Array.isArray){
} else {
}
if (shouldFail){
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
if (typeof actual != "boolean"){
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
if (!(actual instanceof Function)){
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
throw new YUITest.ComparisonFailure(YUITest.Assert._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
*/
if (typeof actual != "number"){
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
if (typeof actual != "string"){
throw new YUITest.UnexpectedValue(YUITest.Assert._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
*/
if (typeof actualValue != expectedType){
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Value should be of type " + expectedType + "."), expectedType, typeof actualValue);
}
},
//--------------------------------------------------------------------------
// Error Detection Methods
//--------------------------------------------------------------------------
/**
* Asserts that executing a particular method should throw an error of
* a specific type. This is a replacement for _should.error.
* @param {String|Function|Object} expectedError If a string, this
* is the error message that the error must have; if a function, this
* is the constructor that should have been used to create the thrown
* error; if an object, this is an instance of a particular error type
* with a specific error message (both must match).
* @param {Function} method The method to execute that should throw the error.
* @param {String} message (Optional) The message to display if the assertion
* fails.
* @method throwsError
* @return {void}
* @static
*/
var error = false;
try {
method();
} catch (thrown) {
//check to see what type of data we have
if (typeof expectedError == "string"){
//if it's a string, check the error message
error = true;
}
} else if (typeof expectedError == "function"){
//if it's a function, see if the error is an instance of it
if (!(thrown instanceof expectedError)){
error = true;
}
//if it's an object, check the instance and message
error = true;
}
} else { //if it gets here, the argument could be wrong
error = true;
}
if (error){
} else {
return;
}
}
//if it reaches here, the error wasn't thrown, which is a bad thing
throw new YUITest.AssertionError(YUITest.Assert._formatMessage(message, "Error should have been thrown."));
}
};
/**
* Error 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 YUITest
* @class AssertionError
* @constructor
*/
/**
* 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
*/
this.name = "Assert Error";
};
//restore constructor
/**
* 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 () {
return this.message;
},
/**
* Returns a string representation of the error.
* @method toString
* @return {String} A string representation of the error.
*/
toString : function () {
}
};
/**
* ComparisonFailure is subclass of Error 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 YUITest
* @extends AssertionError
* @class ComparisonFailure
* @constructor
*/
//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
*/
this.name = "ComparisonFailure";
};
//inherit from YUITest.AssertionError
//restore constructor
/**
* Returns a fully formatted error for an assertion failure. This message
* provides information about the expected and actual values.
* @method getMessage
* @return {String} A string describing the error.
*/
};
/**
* An object object containing coverage result formatting methods.
* @namespace YUITest
* @class CoverageFormat
* @static
*/
/**
* Returns the coverage report in JSON format. This is the straight
* JSON representation of the native coverage report.
* @param {Object} coverage The coverage report object.
* @return {String} A JSON-formatted string of coverage data.
* @method JSON
* @namespace YUITest.CoverageFormat
*/
},
/**
* Returns the coverage report in a JSON format compatible with
* Xdebug. See <a href="http://www.xdebug.com/docs/code_coverage">Xdebug Documentation</a>
* for more information. Note: function coverage is not available
* in this format.
* @param {Object} coverage The coverage report object.
* @return {String} A JSON-formatted string of coverage data.
* @method XdebugJSON
* @namespace YUITest.CoverageFormat
*/
XdebugJSON: function(coverage){
var report = {};
}
}
}
};
/**
* The DateAssert object provides functions to test JavaScript Date objects
* for a variety of cases.
* @namespace YUITest
* @class DateAssert
* @static
*/
YUITest.DateAssert = {
/**
* 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 datesAreEqual
* @static
*/
var msg = "";
//check years first
msg = "Years should be equal.";
}
//now check months
msg = "Months should be equal.";
}
//last, check the day of the month
msg = "Days of month should be equal.";
}
}
} else {
throw new TypeError("YUITest.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 timesAreEqual
* @static
*/
var msg = "";
//check hours first
msg = "Hours should be equal.";
}
//now check minutes
msg = "Minutes should be equal.";
}
//last, check the seconds
msg = "Seconds should be equal.";
}
}
} else {
throw new TypeError("YUITest.DateAssert.timesAreEqual(): Expected and actual values must be Date objects.");
}
}
};
/**
* Creates a new mock object.
* @namespace YUITest
* @class Mock
* @constructor
* @param {Object} template (Optional) An object whose methods
* should be stubbed out on the mock object.
*/
//use blank object is nothing is passed in
var mock,
name;
//try to create mock that keeps prototype chain intact
//fails in the case of ActiveX objects
try {
function f(){}
mock = new f();
} catch (ex) {
mock = {};
}
//create stubs for all methods
return function(){
};
}(name);
}
}
}
//return it
return mock;
};
/**
* Assigns an expectation to a mock object. This is used to create
* methods and properties on the mock object that are monitored for
* calls and changes, respectively.
* @param {Object} mock The object to add the expectation to.
* @param {Object} expectation An object defining the expectation. For
* a method, the keys "method" and "args" are required with
* an optional "returns" key available. For properties, the keys
* "property" and "value" are required.
* @return {void}
* @method expect
* @static
*/
//make sure there's a place to store the expectations
if (!mock.__expectations) {
mock.__expectations = {};
}
//method expectation
if (expectation.method){
i;
//save expectations
//process arguments
args[i] = YUITest.Mock.Value(YUITest.Assert.areSame, [args[i]], "Argument " + i + " of " + name + "() is incorrect.");
}
}
//if the method is expected to be called
if (callCount > 0){
try {
YUITest.Assert.areEqual(args.length, arguments.length, "Method " + name + "() passed incorrect number of arguments.");
}
if (error){
throw error;
}
} catch (ex){
//route through TestRunner for proper handling
}
return result;
};
} else {
//method should fail if called when not expected
try {
} catch (ex){
//route through TestRunner for proper handling
}
};
}
} else if (expectation.property){
//save expectations
}
};
/**
* Verifies that all expectations of a mock object have been met and
* throws an assertion error if not.
* @param {Object} mock The object to verify..
* @return {void}
* @method verify
* @static
*/
try {
if (expectation.method) {
YUITest.Assert.areEqual(expectation.callCount, expectation.actualCallCount, "Method " + expectation.method + "() wasn't called the expected number of times.");
} else if (expectation.property){
YUITest.Assert.areEqual(expectation.value, mock[expectation.property], "Property " + expectation.property + " wasn't set to the correct value.");
}
}
}
} catch (ex){
//route through TestRunner for proper handling
}
};
/**
* Creates a new value matcher.
* @param {Function} method The function to call on the value.
* @param {Array} originalArgs (Optional) Array of arguments to pass to the method.
* @param {String} message (Optional) Message to display in case of failure.
* @namespace YUITest.Mock
* @class Value
* @constructor
*/
};
} else {
}
};
/**
* Predefined matcher to match any value.
* @property Any
* @static
* @type Function
*/
/**
* Predefined matcher to match boolean values.
* @property Boolean
* @static
* @type Function
*/
/**
* Predefined matcher to match number values.
* @property Number
* @static
* @type Function
*/
/**
* Predefined matcher to match string values.
* @property String
* @static
* @type Function
*/
/**
* Predefined matcher to match object values.
* @property Object
* @static
* @type Function
*/
/**
* Predefined matcher to match function values.
* @property Function
* @static
* @type Function
*/
/**
* The ObjectAssert object provides functions to test JavaScript objects
* for a variety of cases.
* @namespace YUITest
* @class ObjectAssert
* @static
*/
YUITest.ObjectAssert = {
/**
* Asserts that an object has all of the same properties
* and property values as the other.
* @param {Object} expected The object with all expected properties and values.
* @param {Object} actual The object to inspect.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areEqual
* @static
* @deprecated
*/
//first check keys array length
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Object should have " + expectedKeys.length + " keys but has " + actualKeys.length));
}
//then check values
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values should be equal for property " + name), expected[name], actual[name]);
}
}
}
},
/**
* 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 hasKey
* @static
* @deprecated Use ownsOrInheritsKey() instead
*/
},
/**
* Asserts that an object has all properties of a reference object.
* @param {Array} properties An array of property names that should be on the object.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method hasKeys
* @static
* @deprecated Use ownsOrInheritsKeys() instead
*/
},
/**
* Asserts that a property with the given name exists on an object's 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 inheritsKey
* @static
*/
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
}
},
/**
* Asserts that all properties exist on an object prototype.
* @param {Array} properties An array of property names that should be on the object.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method inheritsKeys
* @static
*/
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object instance."));
}
}
},
/**
* 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 ownsKey
* @static
*/
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
}
},
/**
* Asserts that all properties exist on an object instance (not on its prototype).
* @param {Array} properties An array of property names that should be on the object.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method ownsKeys
* @static
*/
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object instance."));
}
}
},
/**
* Asserts that an object owns no properties.
* @param {Object} object The object to check.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method ownsNoKeys
* @static
*/
var count = 0,
name;
count++;
}
}
if (count !== 0){
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Object owns " + count + " properties but should own none."));
}
},
/**
* 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 ownsOrInheritsKey
* @static
*/
if (!(propertyName in object)){
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object."));
}
},
/**
* Asserts that an object has all properties of a reference object.
* @param {Array} properties An array of property names that should be on the object.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method ownsOrInheritsKeys
* @static
*/
if (!(properties[i] in object)){
YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object."));
}
}
}
};
/**
* Convenience type for storing and aggregating
* test result information.
* @private
* @namespace YUITest
* @class Results
* @constructor
* @param {String} name The name of the test.
*/
/**
* Name of the test, test case, or test suite.
* @type String
* @property name
*/
/**
* Number of passed tests.
* @type int
* @property passed
*/
this.passed = 0;
/**
* Number of failed tests.
* @type int
* @property failed
*/
this.failed = 0;
/**
* Number of errors that occur in non-test methods.
* @type int
* @property errors
*/
this.errors = 0;
/**
* Number of ignored tests.
* @type int
* @property ignored
*/
this.ignored = 0;
/**
* Number of total tests.
* @type int
* @property total
*/
this.total = 0;
/**
* Amount of time (ms) it took to complete testing.
* @type int
* @property duration
*/
this.duration = 0;
}
/**
* Includes results from another results object into this one.
* @param {YUITest.Results} result The results object to include.
* @method include
* @return {void}
*/
};
/**
* ShouldError is subclass of Error 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 YUITest
* @extends AssertionError
* @class ShouldError
* @constructor
*/
//call superclass
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name = "ShouldError";
};
//inherit from YUITest.AssertionError
//restore constructor
/**
* 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 YUITest
* @extends YUITest.AssertionError
* @class ShouldFail
* @constructor
*/
//call superclass
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name = "ShouldFail";
};
//inherit from YUITest.AssertionError
//restore constructor
/**
* 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 YUITest
* @extends YUITest.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
*/
this.name = "UnexpectedError";
/**
* Stack information for the error (if provided).
* @type String
* @property stack
*/
};
//inherit from YUITest.AssertionError
//restore constructor
/**
* UnexpectedValue is subclass of Error 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 YUITest
* @extends AssertionError
* @class UnexpectedValue
* @constructor
*/
//call superclass
/**
* The unexpected value.
* @type Object
* @property unexpected
*/
this.unexpected = unexpected;
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name = "UnexpectedValue";
};
//inherit from YUITest.AssertionError
//restore constructor
/**
* Returns a fully formatted error for an assertion failure. This message
* provides information about the expected and actual values.
* @method getMessage
* @return {String} A string describing the error.
*/
};
/**
* Represents a stoppage in test execution to wait for an amount of time before
* continuing.
* @param {Function} segment A function to run when the wait is over.
* @param {int} delay The number of milliseconds to wait before running the code.
* @class Wait
* @namespace Test
* @constructor
*
*/
/**
* The segment of code to run when the wait is over.
* @type Function
* @property segment
*/
/**
* The delay before running the segment of code.
* @type int
* @property delay
*/
};
//Setting up our aliases..
});
/**
* Asserts that a given condition is true. If not, then a Y.Assert.Error object is thrown
* and the test fails.
* @method assert
* @param {Boolean} condition The condition to test.
* @param {String} message The message to display if the assertion fails.
* @for YUI
* @static
*/
Y.Assert._increment();
if (!condition){
}
};
/**
* Forces an assertion error to occur. Shortcut for Y.Assert.fail().
* @method fail
* @param {String} message (Optional) The message to display with the failure.
* @for YUI
* @static
*/
//data variables
var message = "";
var messageType = "";
case this.BEGIN_EVENT:
messageType = "info";
break;
case this.COMPLETE_EVENT:
(new Date()).toString() + ".\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
messageType = "info";
break;
case this.TEST_FAIL_EVENT:
messageType = "fail";
break;
case this.TEST_IGNORE_EVENT:
messageType = "ignore";
break;
case this.TEST_PASS_EVENT:
messageType = "pass";
break;
case this.TEST_SUITE_BEGIN_EVENT:
messageType = "info";
break;
case this.TEST_SUITE_COMPLETE_EVENT:
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
messageType = "info";
break;
case this.TEST_CASE_BEGIN_EVENT:
messageType = "info";
break;
case this.TEST_CASE_COMPLETE_EVENT:
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
messageType = "info";
break;
default:
message = "info";
}
}
}
var i, name;
}
};
};
};
}