1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-04 10:09:16 +02:00
bitjs/tests/muther.js

40 lines
1.3 KiB
JavaScript

/**
* Minimal Unit Test Harness
*
* Licensed under the MIT License
*
* Copyright(c) 2014, Google Inc.
*/
var muther = muther || {};
muther.$ = function(s) { return document.querySelector(s) || {}; }
muther.assert = function(cond, err) { if (!cond) { throw err; } };
muther.assertEquals = function(a, b, err) { muther.assert(a === b, err); };
muther.set_ = function(id, style, innerHTML) {
muther.$('#' + id).innerHTML = '';
document.body.innerHTML += '<div id="' + id + '" style="' + style + '">' + innerHTML + '</div>';
};
muther.go = function(spec) {
Object.keys(spec['tests']).forEach(function(testName) {
const test = spec['tests'][testName];
if (test instanceof Promise) {
muther.set_(testName, 'color:#F90', 'RUNNING: ' + testName);
test.then(function() {
muther.set_(testName, 'color:#090', 'PASS: ' + testName);
}, function(err) {
muther.set_(testName, 'color:#900', 'FAIL: ' + testName + ': ' + err);
});
} else if (test instanceof Function) {
const setup = spec['setUp'] || function(){};
const tearDown = spec['tearDown'] || function(){};
try {
setup(); test(); tearDown();
muther.set_(testName, 'color:#090', 'PASS: ' + testName);
} catch (err) {
muther.set_(testName, 'color:#900', 'FAIL: ' + testName + ': ' + err);
}
}
});
};