Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Unit testing
function(
a, // a object holding test functions
b, // a logging function, taking multiple arguments
c, // placeholder
d, // placeholder
e, // placeholder
f // placeholder
){
c = d = e = 0; // initialize asserts, failures and exception counts to 0
for ( // iterate
f // over all tests
in // in
a // the test object
)
try { // for each test, catch any exceptions
a[f]( // run the test function
function( // argument is our "assert" function
g, // expression to assert
h // memo/log entry to print if assert fails
) {
g ? // check if the expression is true
c++ : // if so, increment the assert count
(
d++, // if not, increment the failure count
b(f,'F',h) // log failure as test name F memo
)
}
)
}
catch (i) // if an exception occurs during the test function
{
e++; // increment exception count
b(f,'E',i) // log exception as test name E exception description
}
b( // log final results
c+'A', // number of assertions
d+'F', // number of failures
e+'E' // number of exceptions
)
}
var test = function(a,b,c,d,e,f){c=d=e=0;for(f in a)try{a[f](function(g,h){g?c++:(d++,b(f,'F',h))})}catch(i){e++;b(f,'E',i)}b(c+'A',d+'F',e+'E')};
test({
'testSomething': function(assert){
assert(1==1, 'testing the obvious');
assert(1==1, 'testing the obvious');
assert(1==2, 'testing if 1 equals 2');
},
'testException': function(assert){
DOES-NOT-WORK
}
}, function(a,b,c){ console.log(a,b,c) });
/*
RESULTS:
testSomething F testing if 1 equals 2
testException E ReferenceError: Can't find variable: DOES
2A 1F 1E
*/
function(a,b,c,d,e,f){c=d=e=0;for(f in a)try{a[f](function(g,h){g?c++:(d++,b(f,'F',h))})}catch(i){e++;b(f,'E',i)}b(c+'A',d+'F',e+'E')}
Copyright (c) 2011 Thomas Fuchs, http://mir.aculo.us
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "unittest",
"keywords": [ "testing" ]
}

Just a little typo fix in annotated.js:

line 16: t => a
line 36: l => b

Owner

madrobby commented May 18, 2011

@peutetre: thanks, fixed!

Walfisch commented Nov 6, 2011

Really good gist. Thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment