Skip to content

Instantly share code, notes, and snippets.

@ClementNerma
Created December 21, 2015 18:12
Show Gist options
  • Save ClementNerma/acf8f2c36e266dc4363a to your computer and use it in GitHub Desktop.
Save ClementNerma/acf8f2c36e266dc4363a to your computer and use it in GitHub Desktop.
A micro-library to perform basics unit tests without installing a complete library
var UnitTest = (new (function() {
var testOutput;
this.load = function(name) {
var req, testI, _test = true;
req = new XMLHttpRequest();
req.open('GET', name, false);
req.send(null);
if(req.status !== 200) {
throw new Error('Failed to load unit test : request failed');
return false;
}
if(!testOutput) {
testOutput = document.createElement('div');
testOutput.setAttribute('id', 'tests-output');
testOutput.style.border = '1px solid gray';
testOutput.style.padding = '5px';
document.body.appendChild(testOutput);
} else
testOutput.innerHTML = '';
function describe(name, callback) {
var dom;
if(!_test)
return ;
if((dom = document.getElementsByClassName('describe')).length)
dom[dom.length - 1].style.color = 'green';
UnitTest.output('<pre style="font-size:20px;margin:0;padding:0;" class="describe">' + name + '</pre><br />');
testI = 0;
var durey, started = Date.now();
callback();
durey = Date.now() - started;
if(_test) {
dom = document.getElementsByClassName('describe');
dom[dom.length - 1].style.color = 'green';
dom[dom.length - 1].innerHTML += ' <small>' + durey + ' ms</small>';
}
}
function it(name, callback) {
if(!_test)
return ;
testI += 1;
var msg = '&nbsp;&nbsp;&nbsp;&nbsp;' + testI + ') ' + name, started = Date.now();
try { callback(); }
catch(e) {
UnitTest.output('<pre style="color:red;margin:0;padding:0;" class="log log-failed">' + msg + ' (' + (Date.now() - started) + ' ms)</pre>');
UnitTest.failed(e.stack);
_test = false;
return ;
}
UnitTest.output('<pre style="color:green;margin:0;padding:0;" class="log">' + msg + ' (' + (Date.now() - started) + ' ms)</pre>');
}
function assert(val1, val2, message, nonStrict) {
if(nonStrict ? val1 != val2 : val1 !== val2)
throw new Error('<em>' + (message || 'No description') + '</em>\nAssertion failed. Expected "' + val2 + '", but "' + val1 + '" gived');
}
var durey, started = Date.now();
try { (new Function(['describe', 'it', 'assert'], req.responseText)).apply(window, [describe, it, assert]); }
catch(e) { _test = false; this.failed('Test failed : Runtime error\n' + e.stack); }
durey = Date.now() - started;
if(_test)
testOutput.innerHTML += '<br /><pre style="color:green;margin:0;padding:0;" class="success">Test succeed in ' + durey + ' ms !</pre>'
};
this.output = function(content) {
testOutput.innerHTML += content;
};
this.failed = function(stack) {
var dom = document.getElementsByClassName('describe');
if(dom.length)
dom[dom.length - 1].style.color = 'red';
testOutput.innerHTML += '<br /><pre style="color:red;margin:0;padding:0;" class="failed">' + stack + '</pre>';
};
})())
// Example of using :
UnitTest.load('tests/math.js');
// FILE : tests/math.js
var num = NaN;
while(Number.isNaN(num))
num = parseInt(prompt('Choose a number', '-5'));
describe('Test some maths operations', function() {
it('test basic maths', function() {
assert(1 + 1, 2, 'WTF ???');
assert(2 * 2, 4, 'WTF ???');
assert(0 * 2, 0, 'WTF ???');
});
it('test some functions', function() {
assert(Math.floor(2.3), 2, 'Math.floor doesn\'t seem to work properly');
assert(Math.pow(2, 2), 4, '2^2 != 4');
assert(Math.pow(2, 4), 16, '2^4 != 16');
});
it('test some things', function() {
assert(Math.abs(num), num < 0 ? -num : num, 'Math.abs | absolute function doesn\'t work properly');
});
});
// ENJOY !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment