Skip to content

Instantly share code, notes, and snippets.

@rogerbraun
Created October 23, 2011 20:00
Show Gist options
  • Save rogerbraun/1307811 to your computer and use it in GitHub Desktop.
Save rogerbraun/1307811 to your computer and use it in GitHub Desktop.
Simple Javascript testing
var assert_equal = function(a, b) {
if(a === b){
return {passed: true};
} else {
throw { message: ("Expected:\n\"" + a + "\"\nGot:\n\"" + b + "\""), passed: false}
;
}
}
var indent = function(text) {
return " " + text.replace(/\n/g, "\n ");
}
var it = function(name, code) {
var res = {passed: true}
try {
code();
} catch(er) {
res = er;
}
if(res.passed){
console.log("\033[1;32mPassed: " + name + "\033[0m");
return true;
} else {
console.log("\033[1;31mFailed: " + name + "\n\033[0m");
console.log(indent(res.message));
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment