fix: Working browser harness

This commit is contained in:
Jeremy Wall 2024-04-02 19:41:50 -04:00
parent d26f60241f
commit f84a7c1bcc
5 changed files with 206 additions and 167 deletions

View File

@ -36,12 +36,44 @@ class NodeRenderer {
/** @implements TapRenderer */ /** @implements TapRenderer */
class BrowserRenderer { class BrowserRenderer {
#target = document.body;
/** @param {HtmlElement=} target */
constructor(target) {
if (target) {
this.#target = target;
}
}
/** @returns TextNode */
#createText(text) {
return document.createTextNode(text);
}
/**
* @param {Node} nodes
* @returns HTMLDivElement
*/
#createDiv(nodes) {
const div = document.createElement("div");
div.append(...nodes);
return div;
}
out(text) { out(text) {
// TODO(jeremy): const textNode = this.#createText(text);
this.#target.appendChild(this.#createDiv([textNode]));
} }
comment(lines) { comment(lines) {
// TODO(jeremy): // TODO(jeremy):
var elems = [];
for (var line of lines) {
elems.push(this.#createText(line), document.createElement("br"));
}
var commentDiv = this.#createDiv(elems);
commentDiv.setAttribute("class", "tap-comment");
this.#target.appendChild(commentDiv);
} }
} }
@ -379,5 +411,5 @@ function runSuite(t, suite) {
summarize(t); summarize(t);
} }
export { Tap, runTest, runSuite }; export { Tap, runTest, runSuite, BrowserRenderer, NodeRenderer };

View File

@ -10,7 +10,7 @@ class TapRenderer {
* *
* @param {Array<string>} lines * @param {Array<string>} lines
*/ */
diag(lines) { comment(lines) {
} }
} }

View File

@ -14,149 +14,6 @@ class FakeRenderer {
} }
} }
import('../src/TAP.mjs').then(m => { import('./suite.mjs').then(m => {
function tapSuite(t) { m.runTest(m.Tap.Node(), "Tap dogfood test suite", m.tapSuite);
t.plan(23);
var renderer = new FakeRenderer();
var testCan = function () {
// setup fake test object
var f = new m.Tap(renderer); // the TAP thats failing
f.plan(4);
//mock a fake object to run test against
var obj = new Object;
obj.run = function() {};
var method = 'run';
// begin real tests!
f.can_ok(obj, 'not_there');
t.like(renderer.output, /not ok 1 - object can \[ not_there \]/, 'can_ok failed');
f.can_ok(obj, method);
diag = '';
t.like(renderer.output, /ok 2 - object can \[ run \]/, 'can_ok passed');
//Now we need to test the whole prototype method assignment thing
function MockObj() {
this.attr = 1;
}
MockObj.prototype.fakeme = function () {};
f.can_ok(MockObj, 'fakeme');
renderer.commentOutput = '';
t.like(renderer.output, /^ok .* \[ fakeme \]/,
'can_ok recognized prototype methods');
f.can_ok(MockObj, 'fakeme2');
renderer.commentOutput = '';
t.like(renderer.output, /^not ok .* \[ fakeme2 \]/,
'can_ok prototype recognization doesnt break methods');
};
testCan();
var testLike = function() {
// setup fake test object
var f = new m.Tap(renderer); // the TAP that's failing
f.plan(1);
// begin real tests!
f.like("hello", /hello/, "hello matches hello");
t.like(renderer.output, /ok 1 - hello matches hello/, 'got description in TAP output');
};
testLike()
var testDiag = function() {
// setup fake test object
var f = new m.Tap(renderer); // the TAP that's failing
f.plan(10);
// begin real tests!
f.comment("hello");
t.comment(renderer.commentOutput);
t.like(renderer.commentOutput, /hello/, 'got hello');
};
testDiag();
var testException = function() {
// setup fake test object
var f = new m.Tap(renderer); // the TAP that's failing
f.plan(2);
// begin real tests!
f.throws_ok(function() {throw new Error('I made a boo boo')}, 'I made a boo boo');
//t.comment(renderer.output);
t.like(renderer.output, /ok 1 - code threw \[Error: I made a boo boo\]/, 'uncaught exception');
f.throws_ok(function() {}, 'I made a boo boo');
//t.comment(renderer.output);
t.like(renderer.output, /not ok 2 - code threw \[ \]/, 'false failed');
};
testException();
var testFails = function() {
// setup fake test object
var f = new m.Tap(renderer); // the TAP that's failing
f.plan(3);
// begin real tests!
f.ok(false, 'false fails');
t.like(renderer.output, /not ok 1 - false fails/, 'false failed');
f.ok(0, 'zero fails');
t.like(renderer.output, /not ok 2 - zero fails/, '0 failed');
f.is(0, 1, 'zero is one');
t.like(renderer.output, /not ok 3 - zero is one/, '0 != 1');
};
testFails();
var testPass = function() {
t.ok(true, 'true is true');
t.is(1,1, '1 is 1');
t.pass('pass passes');
t.like("hello world", /hel+o/, 'regexen work');
t.unlike("hello there", /world/, 'no world');
};
testPass();
var testPlan = function() {
// setup fake test object
var f = new m.Tap(renderer); // the TAP that's failing
f.plan(2);
// begin real tests!
f.ok(false, 'false fails');
t.is(f.counter, 1, 'counter increments by one');
t.is(f.planned, 2, 'planned = 2');
};
testPlan();
var testTodoSkip = function() {
t.can_ok(m.Tap, 'todo', 'skip');
var f = new m.Tap(renderer); // the TAP that's failing
f.plan(4);
f.todo(function() {
f.ok(true, 'true is true');
});
t.like(renderer.output, /ok 1 - # TODO: true is true/g,
'the non todo output is suitably formatted');
f.ok(!false, 'not false is true');
t.like(renderer.output, /ok 2 -/g, 'the regular output is suitably formatted');
f.skip(true, 'because I said so', 1,
function() {
f.is(1, 2, 'one is two');
}
);
t.like(renderer.output, /^not ok 3 - # SKIP because I said so$/,
'the skipped output is suitably formatted');
f.is(1, 1, 'one is one');
t.like(renderer.output, /ok 4 - one is one/,
'the non skipped output is suitable formatted');
};
testTodoSkip();
return t;
}
m.runTest(m.Tap.Node(), "Tap dogfood test suite", tapSuite);
}); });

View File

@ -1,11 +1,6 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="../lib/Test/TAP.js"></script>
<script type="text/javascript" src="../lib/Test/TAP/Runner.js"></script>
<script type="text/javascript" src="../lib/Test/TAP/Class.js"></script>
</head> </head>
<body> <body>
<style type="text/css"> <style type="text/css">
@ -30,26 +25,19 @@
</style> </style>
<script type="text/javascript"> <script type="text/javascript">
var top = this;
/** Configuration options /** Configuration options
* *
*/ */
var tests = [ var tests = [
'01_tap.t.js', '01_tap.t.js',
]; ];
var testlib = '.';
/** Setup
*
*/
</script> </script>
This is the Test.TAP.Class and company test harness for the browser. This is the Tap test harness for the browser.
<script type="text/javascript" src="../lib/Test/TAPBrowser.js"></script> <script type="module">
<script type="text/javascript"> import { Tap, runTest } from '../src/TAP.mjs';
window.onload = function() { import { tapSuite } from './suite.mjs';
var browser = new Test.TAPBrowser(testlib, tests);
browser.run(); runTest(Tap.Browser(), 'Tap Dogfood Tests', tapSuite);
};
</script> </script>
</body> </body>
</html> </html>

162
tests/suite.mjs Normal file
View File

@ -0,0 +1,162 @@
/** @implements TapRenderer */
import {Tap, runTest} from '../src/TAP.mjs';
class FakeRenderer {
output = "nothing yet";
commentOutput = "";
out(text) {
this.output = text;
}
comment(lines) {
for (var line of lines) {
this.commentOutput += line;
}
}
}
function tapSuite(t) {
t.plan(23);
var renderer = new FakeRenderer();
var testCan = function() {
// setup fake test object
var f = new Tap(renderer); // the TAP thats failing
f.plan(4);
//mock a fake object to run test against
var obj = new Object;
obj.run = function() { };
var method = 'run';
// begin real tests!
f.can_ok(obj, 'not_there');
t.like(renderer.output, /not ok 1 - object can \[ not_there \]/, 'can_ok failed');
f.can_ok(obj, method);
t.like(renderer.output, /ok 2 - object can \[ run \]/, 'can_ok passed');
//Now we need to test the whole prototype method assignment thing
function MockObj() {
this.attr = 1;
}
MockObj.prototype.fakeme = function() { };
f.can_ok(MockObj, 'fakeme');
renderer.commentOutput = '';
t.like(renderer.output, /^ok .* \[ fakeme \]/,
'can_ok recognized prototype methods');
f.can_ok(MockObj, 'fakeme2');
renderer.commentOutput = '';
t.like(renderer.output, /^not ok .* \[ fakeme2 \]/,
'can_ok prototype recognization doesnt break methods');
};
testCan();
var testLike = function() {
// setup fake test object
var f = new Tap(renderer); // the TAP that's failing
f.plan(1);
// begin real tests!
f.like("hello", /hello/, "hello matches hello");
t.like(renderer.output, /ok 1 - hello matches hello/, 'got description in TAP output');
};
testLike()
var testDiag = function() {
// setup fake test object
var f = new Tap(renderer); // the TAP that's failing
f.plan(10);
// begin real tests!
f.comment("hello");
t.comment(renderer.commentOutput);
t.like(renderer.commentOutput, /hello/, 'got hello');
};
testDiag();
var testException = function() {
// setup fake test object
var f = new Tap(renderer); // the TAP that's failing
f.plan(2);
// begin real tests!
f.throws_ok(function() { throw new Error('I made a boo boo') }, 'I made a boo boo');
//t.comment(renderer.output);
t.like(renderer.output, /ok 1 - code threw \[Error: I made a boo boo\]/, 'uncaught exception');
f.throws_ok(function() { }, 'I made a boo boo');
//t.comment(renderer.output);
t.like(renderer.output, /not ok 2 - code threw \[ \]/, 'false failed');
};
testException();
var testFails = function() {
// setup fake test object
var f = new Tap(renderer); // the TAP that's failing
f.plan(3);
// begin real tests!
f.ok(false, 'false fails');
t.like(renderer.output, /not ok 1 - false fails/, 'false failed');
f.ok(0, 'zero fails');
t.like(renderer.output, /not ok 2 - zero fails/, '0 failed');
f.is(0, 1, 'zero is one');
t.like(renderer.output, /not ok 3 - zero is one/, '0 != 1');
};
testFails();
var testPass = function() {
t.ok(true, 'true is true');
t.is(1, 1, '1 is 1');
t.pass('pass passes');
t.like("hello world", /hel+o/, 'regexen work');
t.unlike("hello there", /world/, 'no world');
};
testPass();
var testPlan = function() {
// setup fake test object
var f = new Tap(renderer); // the TAP that's failing
f.plan(2);
// begin real tests!
f.ok(false, 'false fails');
t.is(f.counter, 1, 'counter increments by one');
t.is(f.planned, 2, 'planned = 2');
};
testPlan();
var testTodoSkip = function() {
t.can_ok(Tap, 'todo', 'skip');
var f = new Tap(renderer); // the TAP that's failing
f.plan(4);
f.todo(function() {
f.ok(true, 'true is true');
});
t.like(renderer.output, /ok 1 - # TODO: true is true/g,
'the non todo output is suitably formatted');
f.ok(!false, 'not false is true');
t.like(renderer.output, /ok 2 -/g, 'the regular output is suitably formatted');
f.skip(true, 'because I said so', 1,
function() {
f.is(1, 2, 'one is two');
}
);
t.like(renderer.output, /^not ok 3 - # SKIP because I said so$/,
'the skipped output is suitably formatted');
f.is(1, 1, 'one is one');
t.like(renderer.output, /ok 4 - one is one/,
'the non skipped output is suitable formatted');
};
testTodoSkip();
return t;
}
export { tapSuite, runTest, Tap };