mirror of
https://github.com/codedread/bitjs
synced 2025-10-06 02:39:55 +02:00
Use modern function property syntax in the test harness
This commit is contained in:
parent
fcbf024848
commit
ed15993286
1 changed files with 40 additions and 40 deletions
|
@ -5,46 +5,46 @@
|
||||||
*
|
*
|
||||||
* Copyright(c) 2014, Google Inc.
|
* 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; } };
|
$(id) {
|
||||||
muther.assertEquals = function(a, b, err) { muther.assert(a === b, err); };
|
let el = document.querySelector('#' + id);
|
||||||
muther.assertThrows = function(fn, err) {
|
if (!el) {
|
||||||
let threw = false;
|
el = document.createElement('div');
|
||||||
try { fn(); } catch (e) { threw = true; }
|
el.id = id;
|
||||||
muther.assert(threw, err);
|
document.body.appendChild(el);
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue