TestCase.js revision 70ed32201807e3e04e8285d8718bf97239ac142d
/**
* 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 Test
* @constructor
*/
YUITest.TestCase = function (template) {
/**
* 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
for (var prop in template) {
this[prop] = template[prop];
}
//check for a valid name
if (typeof this.name != "string"){
this.name = "testCase" + (+new Date());
}
};
YUITest.TestCase.prototype = {
//restore constructor
constructor: YUITest.TestCase,
/**
* 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(){
return YUITest.TestRunner.callback.apply(YUITest.TestRunner,arguments);
},
/**
* 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
*/
resume : function (segment) {
YUITest.TestRunner.resume(segment);
},
/**
* 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
*/
wait : function (segment, delay){
var actualDelay = (typeof segment == "number" ? segment : delay);
actualDelay = (typeof actualDelay == "number" ? actualDelay : 10000);
if (typeof segment == "function"){
throw new YUITest.Wait(segment, actualDelay);
} else {
throw new YUITest.Wait(function(){
YUITest.Assert.fail("Timeout: wait() called but resume() never called.");
}, 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.
*/
assert : function (condition, message){
YUITest.Assert._increment();
if (!condition){
throw new YUITest.AssertionError(YUITest.Assert._formatMessage(message, "Assertion failed."));
}
},
/**
* Forces an assertion error to occur. Shortcut for YUITest.Assert.fail().
* @method fail
* @param {String} message (Optional) The message to display with the failure.
*/
fail: function (message) {
YUITest.Assert.fail(message);
},
//-------------------------------------------------------------------------
// 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
}
};