1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-05 18:34:17 +02:00

Use modern function property syntax in the test harness

This commit is contained in:
codedread 2018-01-20 09:53:20 -08:00
parent fcbf024848
commit ed15993286

View file

@ -5,46 +5,46 @@
*
* Copyright(c) 2014, Google Inc.
*/
var muther = {};
var muther = {
assert(cond, err) { if (!cond) { throw err; } },
assertEquals(a, b, err) { muther.assert(a === b, err); },
assertThrows(fn, err) {
let threw = false;
try { fn(); } catch (e) { threw = true; }
muther.assert(threw, err);
},
muther.assert = function(cond, err) { if (!cond) { throw err; } };
muther.assertEquals = function(a, b, err) { muther.assert(a === b, err); };
muther.assertThrows = function(fn, err) {
let threw = false;
try { fn(); } catch (e) { threw = true; }
muther.assert(threw, err);
};
muther.$ = function(id) {
let el = document.querySelector('#' + id);
if (!el) {
el = document.createElement('div');
el.id = id;
document.body.appendChild(el);
}
return el;
};
muther.set_ = function(id, style, innerHTML) {
muther.$(id).setAttribute('style', style);
muther.$(id).innerHTML = innerHTML;
};
muther.go = function(spec) {
let prevResult = Promise.resolve(true);
for (let testName in spec['tests']) {
muther.set_(testName, 'color:#F90', 'RUNNING: ' + testName);
try {
prevResult = prevResult.then(() => {
if (spec['setUp']) spec['setUp']();
const thisResult = spec['tests'][testName]() || Promise.resolve(true);
return thisResult.then(() => {
if (spec['tearDown']) spec['tearDown']();
muther.set_(testName, 'color:#090', 'PASS: ' + testName);
});
}).catch(err => muther.set_(testName, 'color:#900', 'FAIL: ' + testName + ': ' + err));
} catch (err) {
muther.set_(testName, 'color:#900', 'FAIL: ' + testName + ': ' + err);
$(id) {
let el = document.querySelector('#' + id);
if (!el) {
el = document.createElement('div');
el.id = id;
document.body.appendChild(el);
}
}
return el;
},
set_(id, style, innerHTML) {
muther.$(id).setAttribute('style', style);
muther.$(id).innerHTML = innerHTML;
},
go(spec) {
let prevResult = Promise.resolve(true);
for (let testName in spec['tests']) {
muther.set_(testName, 'color:#F90', 'RUNNING: ' + testName);
try {
prevResult = prevResult.then(() => {
if (spec['setUp']) spec['setUp']();
const thisResult = spec['tests'][testName]() || Promise.resolve(true);
return thisResult.then(() => {
if (spec['tearDown']) spec['tearDown']();
muther.set_(testName, 'color:#090', 'PASS: ' + testName);
});
}).catch(err => muther.set_(testName, 'color:#900', 'FAIL: ' + testName + ': ' + err));
} catch (err) {
muther.set_(testName, 'color:#900', 'FAIL: ' + testName + ': ' + err);
}
}
},
};