1
0
Fork 0

[#29] Added @each() support to test cases

- A little sloppy, but it gets the job done
closure/master
Mike Gerwitz 2011-11-19 14:09:23 -05:00
parent a022b62f8f
commit 164b6a925b
1 changed files with 79 additions and 28 deletions

View File

@ -89,10 +89,7 @@ function incAssertCount()
module.exports = function( test_case )
{
var context = prepareCaseContext(),
setUp = test_case.setUp,
acount_last = 0
;
setUp = test_case.setUp;
// if we're not running a suite, clear out the failures
if ( !( suite ) )
@ -116,11 +113,73 @@ module.exports = function( test_case )
setUp.call( context );
}
acount_last = acount;
var data = test.match( /^(?:@(.*?)\((.*?)\))?(.*)$/ ),
method = data[ 1 ],
prop = data[ 2 ],
name = data[ 3 ],
count = 1,
args = [ [] ]
;
if ( method === 'each' )
{
count = context[ prop ].length;
args = [];
for ( var i = 0; i < count; i++ )
{
args.push( [ context[ prop ][ i ] ] );
}
}
else if ( method !== undefined )
{
throw Error( "Unknown test method: " + method );
}
// perform the appropriate number of tests
for ( var i = 0; i < count; i++ )
{
tryTest(
test_case,
test,
name + ( ( count > 1 )
? ( ' (' + i + ')' )
: ''
),
context,
args[ i ]
);
}
}
// only output statistics if we're not running a suite (otherwise they'll be
// output at the end of the suite)
if ( !( suite ) )
{
endStats();
}
};
/**
* Attempt a test
*
* @param {Object} test_case object containing all test cases
* @param {string} test complete key of test to run
* @param {string} test_str text to use on failure
* @param {Object} context context to bind to test function
* @param {Array} args arguments to pass to test function
*
* @return {undefined}
*/
function tryTest( test_case, test, test_str, context, args )
{
var acount_last = acount;
try
{
test_case[ test ].call( context );
test_case[ test ].apply( context, args );
// if there were no assertions, then the test should be marked as
// incomplete
@ -145,19 +204,11 @@ module.exports = function( test_case )
else
{
testPrint( 'F' );
failures.push( [ test, e ] );
failures.push( [ test_str, e ] );
}
}
}
// only output statistics if we're not running a suite (otherwise they'll be
// output at the end of the suite)
if ( !( suite ) )
{
endStats();
}
};
/**
* Reset counters