harness: Support more assertion types

This commit is contained in:
Keegan McAllister 2013-10-14 16:38:42 -07:00
parent e701ba4d43
commit 4bb2a70e08
7 changed files with 38 additions and 26 deletions

View file

@ -12,16 +12,28 @@ function _pass(s, m) {
window.alert(_oneline("TEST-PASS | " + s + ": " + m)); window.alert(_oneline("TEST-PASS | " + s + ": " + m));
} }
function is(a, b, c) { function _printer(opstr, op) {
let f = a != b ? _fail : _pass; return function (a, b, msg) {
let m = !c ? "" : c; let f = op(a,b) ? _pass : _fail;
f(a + " == " + b, m); if (!msg) msg = "";
f(a + " " + opstr + " " + b, msg);
};
} }
function isnot(a, b, c) { var is = _printer("==", function (a,b) { return a == b; });
let f = (a != b) ? _pass : _fail; var is_a = _printer("is a", function (a,b) { return a instanceof b; });
let m = !c ? "" : c; var is_in = _printer("is in", function (a,b) { return a in b; });
f(a + " != " + b, m); var is_not_in = _printer("is not in", function (a,b) { return !(a in b); });
var as_str_is = _printer("as string is", function (a,b) { return String(a) == b; });
var isnot = _printer("!=", function (a,b) { return a != b; });
var lt = _printer("<", function (a,b) { return a < b; });
var gt = _printer(">", function (a,b) { return a > b; });
var leq = _printer("<=", function (a,b) { return a <= b; });
var geq = _printer(">=", function (a,b) { return a >= b; });
var starts_with = _printer("starts with", function (a,b) { return a.indexOf(b) == 0; });
function is_function(val, name) {
starts_with(String(val), "function " + name + "(");
} }
var _test_complete = false; var _test_complete = false;

View file

@ -18,11 +18,11 @@
var child2 = document.createElement("p"); var child2 = document.createElement("p");
elem.appendChild(child2); elem.appendChild(child2);
is(1 in children, true); is_in(1, children);
is(children.length, 2); is(children.length, 2);
is(children.item(1), child2); is(children.item(1), child2);
is(!(2 in children), true); is_not_in(2, children);
is(children.item(2), null); is(children.item(2), null);
finish(); finish();

View file

@ -8,11 +8,11 @@
var elem = document.createElement("foo"); var elem = document.createElement("foo");
is(elem.tagName, "FOO"); is(elem.tagName, "FOO");
var elem = document.createElement("p"); var elem = document.createElement("p");
is(elem instanceof HTMLParagraphElement, true); is_a(elem, HTMLParagraphElement);
var elem = document.createElement("sPAn"); var elem = document.createElement("sPAn");
is(elem instanceof HTMLSpanElement, true); is_a(elem, HTMLSpanElement);
var text = document.createTextNode("hello"); var text = document.createTextNode("hello");
is(text instanceof Text, true); is_a(text, Text);
finish(); finish();
</script> </script>
</body> </body>

View file

@ -13,12 +13,12 @@
let foo = document.getElementById("foo"); let foo = document.getElementById("foo");
isnot(foo, null, "test-1-0, on static page"); isnot(foo, null, "test-1-0, on static page");
is(foo && foo.tagName, "HEAD", "test1-1, on static page"); is(foo && foo.tagName, "HEAD", "test1-1, on static page");
is(foo instanceof HTMLHeadElement, true, "test1-2, on static page"); is_a(foo, HTMLHeadElement, "test1-2, on static page");
let bar = document.getElementById("bar"); let bar = document.getElementById("bar");
isnot(bar, null, "test1-3, on static page"); isnot(bar, null, "test1-3, on static page");
is(bar && bar.tagName, "DIV", "test1-4, on static page"); is(bar && bar.tagName, "DIV", "test1-4, on static page");
is(bar instanceof HTMLDivElement, true, "test1-5, on static page"); is_a(bar, HTMLDivElement, "test1-5, on static page");
} }
// test2: scripted element // test2: scripted element
@ -32,7 +32,7 @@
let appended = document.getElementById(TEST_ID); let appended = document.getElementById(TEST_ID);
isnot(appended, null, "test2-0, appended element"); isnot(appended, null, "test2-0, appended element");
is(appended && appended.tagName, "DIV", "test2-1, appended element"); is(appended && appended.tagName, "DIV", "test2-1, appended element");
is(appended instanceof HTMLDivElement, true, "test2-2, appended element"); is_a(appended, HTMLDivElement, "test2-2, appended element");
// test: removed element // test: removed element
gBody.removeChild(test); gBody.removeChild(test);

View file

@ -4,14 +4,14 @@
<script> <script>
var rect = window.document.head.getBoundingClientRect(); var rect = window.document.head.getBoundingClientRect();
var rects = window.document.head.getClientRects(); var rects = window.document.head.getClientRects();
is(rect instanceof ClientRect, true); is_a(rect, ClientRect);
is(rect.top, 0); is(rect.top, 0);
is(rect.bottom, 0); is(rect.bottom, 0);
is(rect.left, 0); is(rect.left, 0);
is(rect.right, 0); is(rect.right, 0);
is(rect.width, 0); is(rect.width, 0);
is(rect.height, 0); is(rect.height, 0);
is(rects instanceof ClientRectList, true); is_a(rects, ClientRectList);
is(rects.length, 0); is(rects.length, 0);
finish(); finish();
</script> </script>

View file

@ -6,7 +6,7 @@
<body> <body>
<script> <script>
is(window.navigator, window.navigator); is(window.navigator, window.navigator);
is(String(window.navigator), '[object Navigator]'); is_a(window.navigator, Navigator);
var nav = window.navigator; var nav = window.navigator;
is(nav.doNotTrack, "unspecified"); is(nav.doNotTrack, "unspecified");

View file

@ -5,14 +5,14 @@
<body> <body>
<foo-á>foo</foo-á> <foo-á>foo</foo-á>
<script> <script>
is(window.document.documentElement instanceof Node, true); is_a(window.document.documentElement, Node);
is(window.document.documentElement instanceof Element, true); is_a(window.document.documentElement, Element);
is(window.document.documentElement instanceof HTMLElement, true); is_a(window.document.documentElement, HTMLElement);
is(window.document.documentElement instanceof HTMLHtmlElement, true); is_a(window.document.documentElement, HTMLHtmlElement);
is(window.document instanceof Document, true); is_a(window.document, Document);
is(window.document instanceof HTMLDocument, true); is_a(window.document, HTMLDocument);
is(window.document.documentElement.tagName, "HTML"); is(window.document.documentElement.tagName, "HTML");
is(window.document.getElementsByTagName('foo-á')[0] instanceof HTMLUnknownElement, true); is_a(window.document.getElementsByTagName('foo-á')[0], HTMLUnknownElement);
is(window.document.getElementsByTagName('foo-á')[0].tagName, "FOO-á"); is(window.document.getElementsByTagName('foo-á')[0].tagName, "FOO-á");
finish(); finish();
</script> </script>