mootest.js

The MooTest system.

License

MIT License

Summary
mootest.jsThe MooTest system.
MooTestThe MooTest namespace.
MooTest.TestFailedException thrown by tests failed.
Variables
messageThe description of the error.
Functions
MooTest.ClassEnhancerAdds to every method of the class definition a ‘functionName’ attribute which is the name of the function in the class definition.
MooTest.TestBlockA block of tests, either suites or testsets.
Functions
initializeCreate a new test block.
runRun the test(s) of this test block and report results to a callback function.
_testsMethod to override to return a map between test names and callables; each callable is a test.
_errorThrow a new error caused by a test failed.
MooTest.TestSetA single test set.
Functions
initializeCreate a new TestSet.
assertAssert a condition.
assertEqualsAssert two values are the same (according to the ‘==’ operator).
assertZeroAssert a value is zero.
assertNotEqualsAssert two values are the same (according to the ‘!=’ operator).
assertGreaterAssert the first value is greater than the second (according to the ‘>’ operator).
assertGreaterOrEqualAssert the first value is greater or equal than the second (according to the ‘>=’ operator).
assertLesserAssert the first value is greater than the second (according to the ‘<’ operator).
assertLesserOrEqualAssert the first value is greater or equal than the second (according to the ‘<=’ operator).
assertDefinedAssert a value is defined (according to the MooTools ‘$defined’ function; that is, it’s neither null nor undefined).
assertNotDefinedAssert a value is not defined (according to the MooTools ‘$defined’ function; that is, it’s either null or undefined).
assertThrowExceptionAssert a piece of code throw an exception.
assertThrowNotExceptionAssert a piece of code throw not an exception.
MooTest.SuiteA suite of tests.
Functions
initializeCreate a new suite.
MooTest.RunnerA test runner.
Functions
runRun a block of tests, showing the results.
MooTest.DefaultRunnerThe default test runner for the base test page given in the distribution.

MooTest

The MooTest namespace.

MooTest.TestFailed

Exception thrown by tests failed.

Summary
Variables
messageThe description of the error.
Functions
MooTest.ClassEnhancerAdds to every method of the class definition a ‘functionName’ attribute which is the name of the function in the class definition.

Variables

message

The description of the error.

Functions

MooTest.ClassEnhancer

MooTest.ClassEnhancer = function(className,
classDefinition)

Adds to every method of the class definition a ‘functionName’ attribute which is the name of the function in the class definition.  This way the stack trace method can extract a name even for the generated anonymous functions.

Parameters

classNameThe name of the class.
classDefinitionThe class definition as should be passed to the Class constructor.

Return

A class definition with the function members enriched with a ‘functionName’ attribute.

Example

var MyClass = new Class(MooTest.ClassEnhancer({
    myMethod: function(a, b) { return a+b; }
}));
var m = new MyClass();
alert(m.MyMethod.functionName); // alerts 'myMethod'

MooTest.TestBlock

A block of tests, either suites or testsets.  This is a base class for both of Suite and TestSet and serves the purpose to provide the base definitions for both.

Summary
Functions
initializeCreate a new test block.
runRun the test(s) of this test block and report results to a callback function.
_testsMethod to override to return a map between test names and callables; each callable is a test.
_errorThrow a new error caused by a test failed.

Functions

initialize

initialize: function()

Create a new test block.

run

run: function(callback)

Run the test(s) of this test block and report results to a callback function.

Between the tests you can put special-named elements which are considered separately.  These are:

  • init.  This function is called at the beginning of the test block.
  • testStart.  This function is called when a test is started, and its argument is the name of the test start.
  • testPassed.  This function is called when a test succeeds, and its argument is the name of the test succeeded.
  • testFailed.  This function is called when a test fails, and its argument is the name of the test succeeded.
  • testEnd.  This function is called when a test ends, regardless if successful or not, and its argument is the name of the test succeeded.
  • dispose.  This function is called when the test block ends.

Parameters

callbackThe callback function; when the test is completed it is called with a dictionary containing the following keys: name (name of the test), success (boolean telling whether test succeeded or not), error (the description of the error as an object containing the keys ‘message’, ‘source’, ‘line’ and ‘stack’); when the test starts is called with the same dictionary where success is null.

_tests

_tests: function()

Method to override to return a map between test names and callables; each callable is a test.  Tests report errors usually through the internal method _error.  Any other exception thrown is considered an error too.

_error

_error: function(description)

Throw a new error caused by a test failed.

MooTest.TestSet

A single test set.  A test set is a set of tests performed on the same data and which can have a setup and tear-down processing for every test.

Summary
Functions
initializeCreate a new TestSet.
assertAssert a condition.
assertEqualsAssert two values are the same (according to the ‘==’ operator).
assertZeroAssert a value is zero.
assertNotEqualsAssert two values are the same (according to the ‘!=’ operator).
assertGreaterAssert the first value is greater than the second (according to the ‘>’ operator).
assertGreaterOrEqualAssert the first value is greater or equal than the second (according to the ‘>=’ operator).
assertLesserAssert the first value is greater than the second (according to the ‘<’ operator).
assertLesserOrEqualAssert the first value is greater or equal than the second (according to the ‘<=’ operator).
assertDefinedAssert a value is defined (according to the MooTools ‘$defined’ function; that is, it’s neither null nor undefined).
assertNotDefinedAssert a value is not defined (according to the MooTools ‘$defined’ function; that is, it’s either null or undefined).
assertThrowExceptionAssert a piece of code throw an exception.
assertThrowNotExceptionAssert a piece of code throw not an exception.

Functions

initialize

initialize: function(tests)

Create a new TestSet.

Parameters

testsA map between test names and callable (tests); these tests are bind with this instance before being run.

assert

assert: function(condition,
message)

Assert a condition.

Parameters

conditionThe condition to assert.  In case of failure, the test fails.
messageThe message to display in case the assertion fails; can be omitted.

assertEquals

assertEquals: function(v1,
v2,
message)

Assert two values are the same (according to the ‘==’ operator).

Parameters

v1First value.
v2Second value.
messageThe message to display in case the assertion fails; can be omitted.

assertZero

assertZero: function(v,
message)

Assert a value is zero.

Parameters

vThe value to check.
messageThe message to display in case the assertion fails; can be omitted.

assertNotEquals

assertNotEquals: function(v1,
v2,
message)

Assert two values are the same (according to the ‘!=’ operator).

Parameters

v1First value.
v2Second value.
messageThe message to display in case the assertion fails; can be omitted.

assertGreater

assertGreater: function(v1,
v2,
message)

Assert the first value is greater than the second (according to the ‘>’ operator).

Parameters

v1Greater value.
v2Lesser value.
messageThe message to display in case the assertion fails; can be omitted.

assertGreaterOrEqual

assertGreaterOrEqual: function(v1,
v2,
message)

Assert the first value is greater or equal than the second (according to the ‘>=’ operator).

Parameters

v1Greater or equal value.
v2Lesser or equal value.
messageThe message to display in case the assertion fails; can be omitted.

assertLesser

assertLesser: function(v1,
v2,
message)

Assert the first value is greater than the second (according to the ‘<’ operator).

Parameters

v1Lesser value.
v2Greater value.
messageThe message to display in case the assertion fails; can be omitted.

assertLesserOrEqual

assertLesserOrEqual: function(v1,
v2,
message)

Assert the first value is greater or equal than the second (according to the ‘<=’ operator).

Parameters

v1Lesser or equal value.
v2Greater or equal value.
messageThe message to display in case the assertion fails; can be omitted.

assertDefined

assertDefined: function(v,
message)

Assert a value is defined (according to the MooTools ‘$defined’ function; that is, it’s neither null nor undefined).

Parameters

vThe value to check.
messageThe message to display in case the assertion fails; can be omitted.

assertNotDefined

assertNotDefined: function(v,
message)

Assert a value is not defined (according to the MooTools ‘$defined’ function; that is, it’s either null or undefined).

Parameters

vThe value to check.
messageThe message to display in case the assertion fails; can be omitted.

assertThrowException

assertThrowException: function(f,
message)

Assert a piece of code throw an exception.

Parameters

fPiece of code to run and check.
messageThe message to display in case the assertion fails; can be omitted.

assertThrowNotException

assertThrowNotException: function(f,
message)

Assert a piece of code throw not an exception.

Parameters

fPiece of code to run and check.
messageThe message to display in case the assertion fails; can be omitted.

MooTest.Suite

A suite of tests.  A test suite is a collection of other Suites or TestSets.

Summary
Functions
initializeCreate a new suite.

Functions

initialize

initialize: function(testBlocks)

Create a new suite.

Parameters

testBlocksA map between test block names and test blocks.

MooTest.Runner

A test runner.  This class run a TestBlock (TestSet or Suite) and report results.

Events

initGenerated at the beginning of the test block run.
testStartGenerated when a test is started; has the name of the test as argument.
testPassedGenerated when a test pass; has the name of the test as argument.
testFailedGenerated when a test fail; has the following arguments: the name of the test, the error description, the source file/url where the error happened, the line at which the error happened, the full stack.
disposeGenerated at the end of the test block.
Summary
Functions
runRun a block of tests, showing the results.

Functions

run

run: function(testBlock)

Run a block of tests, showing the results.

Parameters

testBlockthe test block to run.

MooTest.DefaultRunner

The default test runner for the base test page given in the distribution.

MooTest.ClassEnhancer = function(className,
classDefinition)
Adds to every method of the class definition a ‘functionName’ attribute which is the name of the function in the class definition.
initialize: function()
Create a new test block.
run: function(callback)
Run the test(s) of this test block and report results to a callback function.
_tests: function()
Method to override to return a map between test names and callables; each callable is a test.
_error: function(description)
Throw a new error caused by a test failed.
initialize: function(tests)
Create a new TestSet.
assert: function(condition,
message)
Assert a condition.
assertEquals: function(v1,
v2,
message)
Assert two values are the same (according to the ‘==’ operator).
assertZero: function(v,
message)
Assert a value is zero.
assertNotEquals: function(v1,
v2,
message)
Assert two values are the same (according to the ‘!=’ operator).
assertGreater: function(v1,
v2,
message)
Assert the first value is greater than the second (according to the ‘>’ operator).
assertGreaterOrEqual: function(v1,
v2,
message)
Assert the first value is greater or equal than the second (according to the ‘>=’ operator).
assertLesser: function(v1,
v2,
message)
Assert the first value is greater than the second (according to the ‘<’ operator).
assertLesserOrEqual: function(v1,
v2,
message)
Assert the first value is greater or equal than the second (according to the ‘<=’ operator).
assertDefined: function(v,
message)
Assert a value is defined (according to the MooTools ‘$defined’ function; that is, it’s neither null nor undefined).
assertNotDefined: function(v,
message)
Assert a value is not defined (according to the MooTools ‘$defined’ function; that is, it’s either null or undefined).
assertThrowException: function(f,
message)
Assert a piece of code throw an exception.
assertThrowNotException: function(f,
message)
Assert a piece of code throw not an exception.
initialize: function(testBlocks)
Create a new suite.
run: function(testBlock)
Run a block of tests, showing the results.
Close