Last active
December 11, 2015 01:59
-
-
Save jacobsandlund/4527480 to your computer and use it in GitHub Desktop.
Run simple unit tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lineWidth = 80 - 'PASS'.length - 4 * 3; // 4 * 3 is for indenting 4 times | |
var suites = { | |
'test foo': require('./test/test_foo'), | |
'test bar': require('./test/test_bar'), | |
}; | |
var padding = function(length, pad) { | |
return (new Array(length + 1)).join(pad || ' '); | |
}; | |
var runTest = function(descr, depth, test) { | |
var indent = padding(depth * 3), | |
description = indent + descr, | |
pass; | |
if (typeof test === 'function') { | |
pass = test(); | |
outputPassOrFail(description, depth, pass); | |
} else { | |
console.log(description); | |
pass = true; | |
Object.keys(test).forEach(function(subDesc) { | |
if (!runTest(subDesc, depth + 1, test[subDesc])) pass = false; | |
}); | |
outputPassOrFail(description, depth, pass); | |
} | |
return pass; | |
}; | |
var outputPassOrFail = function(description, depth, pass) { | |
var dots = padding(lineWidth - description.length + depth * 3, '.'), | |
inner = pass ? '32mPASS' : '31mFAIL'; | |
console.log(description + dots + '\033[' + inner + '\033[0m'); | |
}; | |
runTest('all tests', 0, suites); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment