Add testing harness

This commit is contained in:
John Alanbrook 2024-03-14 10:21:44 -05:00
parent f0876825f1
commit 55ab159a85
2 changed files with 49 additions and 13 deletions

View file

@ -121,7 +121,10 @@ Resources.replstrs = function(path)
}
var console = {
print(msg, lvl) {
print(msg) { cmd(91, msg); },
say(msg) { console.print(msg + "\n"); },
pprint(msg, lvl) {
var lg;
if (typeof msg === 'object') {
lg = JSON.stringify(msg, null, 2);
@ -142,34 +145,30 @@ var console = {
},
spam(msg) {
this.print(msg,0);
console.pprint(msg,0);
},
/* this always prints to stdout */
debug(msg) {
this.print(msg,1);
console.pprint(msg,1);
},
info(msg) {
this.print(msg, 2);
console.pprint(msg, 2);
},
warn(msg) {
this.print(msg, 3);
console.pprint(msg, 3);
},
error(msg) {
this.print(msg + "\n" + console.stackstr(3), 4);
console.pprint(msg + "\n" + console.stackstr(3), 4);
},
panic(msg) {
this.print(msg + "\n" + console.stackstr(1), 5);
console.pprint(msg + "\n" + console.stackstr(1), 5);
},
log(msg) {
if (typeof msg === 'object') msg = JSON.stringify(msg,null,1);
cmd(91, msg + '\n');
},
stackstr(skip=0) {
var err = new Error();
var stack = err.stack.split('\n');
@ -181,8 +180,9 @@ var console = {
},
};
var say = console.log;
say.doc = "Print to std out with an appended newline.";
console.log = console.say;
var say = console.say;
var print = console.print;
console.doc = {
level: "Set level to output logging to console.",
@ -599,6 +599,10 @@ Cmdline.register_order("clean", function(argv) {
say("Cleaning not implemented.");
}, "Clean up a given object file.", "JSON ...");
Cmdline.register_order("test", function(argv) {
use("scripts/test.js");
}, "Run tests.");
Cmdline.register_cmd("l", function(n) {
console.level = n;
}, "Set log level.");
@ -607,6 +611,7 @@ return {
console,
Resources,
say,
print,
Cmdline,
cmd_args,
steam

31
scripts/test.js Normal file
View file

@ -0,0 +1,31 @@
/* Tests for prosperon */
var tests = [];
var pass = 0;
var fail = 0;
var test = function(name, fn)
{
tests.push(function() {
print(`${pass+fail}/${tests.length}: ${name} ... `);
var result = fn();
if (result) print(`pass`);
else print(`fail`);
return result;
});
}
test("Pass test", _=>1);
test("Fail test", _=>0);
say(`Testing ${tests.length} tests.`);
for (var t of tests) {
if (t())
pass++;
else
fail++;
print("\n");
}
say(`Passed ${pass} tests and failed ${fail}`);
Game.quit();