TestRunner.js revision 70ed32201807e3e04e8285d8718bf97239ac142d
/**
* Runs test suites and test cases, providing events to allowing for the
* interpretation of test results.
* @namespace Test
* @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
* @private
* @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 {Test.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 {Test.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 {Test.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();
}();