diff --git a/test/common.js b/test/common.js index c6967d7..299c474 100644 --- a/test/common.js +++ b/test/common.js @@ -44,3 +44,12 @@ exports.require = function( module ) return require( exports.PATH_LIB + '/' + module ); } + +/** + * Create simple xUnit-style test case + * + * @return {udnefined} + */ +exports.testCase = require( __dirname + '/inc-testcase.js' ); + + diff --git a/test/inc-testcase.js b/test/inc-testcase.js new file mode 100644 index 0000000..f6d434e --- /dev/null +++ b/test/inc-testcase.js @@ -0,0 +1,142 @@ + + +var assert = require( 'assert' ); + + +/** + * Defines and runs a test case + * + * This is a very basic system that provides a more familiar jUnit/phpUnit-style + * output for xUnit tests and allows all tests in the case to be run in the + * event of a test failure. + * + * The test name should be given as the key and the test itself as a function as + * the value. The test will be invoked within the context of the assertion + * module. + * + * This will be evolving throughout the life of the project. Mainly, it cannot + * be run as part of a suite without multiple summary outputs. + * + * @param {Object.} object containing tests + * + * @return {undefined} + */ +module.exports = function( test_case ) +{ + var failures = [], + scount = 0, + context = prepareCaseContext(), + setUp = test_case.setUp; + + delete test_case.setUp; + + // run each test in the case + for ( test in test_case ) + { + // xUnit-style setup + if ( setUp ) + { + setUp.call( context ); + } + + try + { + test_case[ test ].call( context ); + + scount++; + testPrint( '.' ); + } + catch ( e ) + { + testPrint( 'F' ); + failures.push( [ test, e ] ); + } + } + + testPrint( "\n\n" ); + + if ( failures.length ) + { + outputTestFailures( failures ); + } + + // print test case summary + testPrint( + ( ( failures.length ) ? "FAILED" : "OK" ) + " - " + + scount + " successful, " + failures.length + " failure(s), " + + ( scount + failures.length ) + " total\n" + ); + + // exit with non-zero status to indicate failure + failures.length + && typeof process !== 'undefined' + && process.exit( 1 ); +}; + + +/** + * Prepare assertion methods on context + * + * @return {Object} context + */ +function prepareCaseContext() +{ + return { + require: require( __dirname + '/common' ).require, + + fail: assert.fail, + assertOk: assert.ok, + assertEqual: assert.equal, + assertNotEqual: assert.notEqual, + assertDeepEqual: assert.deepEqual, + assertStrictEqual: assert.strictEqual, + assertNotStrictEqual: assert.notStrictEqual, + assertThrows: assert.throws, + assertDoesNotThrow: assert.doesNotThrow, + assertIfError: assert.ifError, + }; +} + + +/** + * Outputs test failures and their stack traces + * + * @param {Array} failures + * + * @return {undefined} + */ +function outputTestFailures( failures ) +{ + var i, cur, name, e; + + for ( i = 0; i < failures.length; i++ ) + { + cur = failures[ i ]; + + name = cur[ 0 ]; + e = cur[ 1 ]; + + // output the name followed by the stack trace + testPrint( + '#' + i + ' ' + name + '\n' + + e.stack + "\n\n" + ); + } +} + + +/** + * Outputs a string if stdout is available (node.js) + * + * @param {string} str string to output + * + * @return {undefined} + */ +var testPrint = ( ( typeof process === 'undefined' ) + || ( typeof process.stdout === 'undefined' ) ) + ? function() {} + : function( str ) + { + process.stdout.write( str ); + }; + diff --git a/tools/combine b/tools/combine index 92d4e57..429cc86 100755 --- a/tools/combine +++ b/tools/combine @@ -135,13 +135,15 @@ if [ "$INC_TEST" ]; then TEST_CASES=$( find $PATH_TEST \ \( -name 'test-*.js' \ -o -name '*Test*.js' \ - -o -name 'inc-*.js' \ \) \ -exec basename {} \; \ | sort \ | grep -v 'test-\(combine\(-pre-es5\)\?\|index\).js' \ ) + # find include files separately so we can output those before the tests + TEST_INC=$( find $PATH_TEST -name 'inc-*.js' -exec basename {} \; ) + # include test combine template cat "$TPL_TEST_PATH" | grep -v '^#' | $RMTRAIL @@ -149,7 +151,7 @@ if [ "$INC_TEST" ]; then echo "ns_exports.runTests = function()" echo "{" - for testcase in $TEST_CASES; do + for testcase in $TEST_INC $TEST_CASES; do filename="$PATH_TEST/$testcase" # generate the module name by removing path and extension, then diff --git a/tools/combine-test.tpl b/tools/combine-test.tpl index 01668f4..89c9119 100644 --- a/tools/combine-test.tpl +++ b/tools/combine-test.tpl @@ -18,10 +18,15 @@ # along with this program. If not, see . # # -module.common = { exports: { +module.common = module['test/common'] = { exports: { require: function ( id ) { return require( id ); + }, + + testCase: function() + { + return require( 'test/inc-testcase' ).apply( this, arguments ); } } };