Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix most unit tests and the conformance test runner. #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/test/alltests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
level_1/basic_first-child.selector
level_1/basic_grouping.selector
level_1/basic_id.selector
level_1/basic_id_multiple.selector
level_1/basic_id_quotes.selector
level_1/basic_id_with_type.selector
level_1/basic_last-child.selector
level_1/basic_nth-child-2.selector
level_1/basic_nth-child.selector
level_1/basic_nth-last-child.selector
level_1/basic_root_pseudo.selector
level_1/basic_type.selector
level_1/basic_type2.selector
level_1/basic_type3.selector
level_1/basic_universal.selector
level_1/collision_nested.selector
level_1/collision_quoted-string.selector
level_1/collision_string.selector
level_2/sibling_childof.selector
level_2/sibling_descendantof.selector
level_2/sibling_unrooted.selector
level_3/basic_has-multiple.selector
level_3/basic_has-root-in-expr.selector
level_3/basic_has-sans-first-paren.selector
level_3/basic_has-sans-paren.selector
level_3/basic_has-whitespace.selector
level_3/basic_has-with-comma.selector
level_3/basic_has.selector
level_3/basic_multiple-has-with-strings.selector
level_3/expr_div.selector
level_3/expr_ends-with.selector
level_3/expr_false-eq.selector
level_3/expr_greater-than.selector
level_3/expr_less-than.selector
level_3/expr_mod.selector
level_3/expr_mult.selector
level_3/expr_null-eq.selector
level_3/expr_number-eq.selector
level_3/expr_precedence-1.selector
level_3/expr_precedence-2.selector
level_3/expr_simple-false.selector
level_3/expr_simple.selector
level_3/expr_starts-with.selector
level_3/expr_string-eq.selector
level_3/expr_true-eq.selector
level_3/polykids_has-with_descendant.selector
level_3/polykids_val.selector
23 changes: 14 additions & 9 deletions src/test/js/conf_tests.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
$(document).ready(function() {
var tests = {};

// Turn k into a valid CSS class name.
function classify(k) {
return k.replace(/\//g, '-');
}

$("#runit").click(function() {
for (var k in tests) {
var obj = JSON.parse($("." + k + "_document").text());
var obj = JSON.parse($("." + classify(k) + "_document").text());
for (var i = 0; i < tests[k].length; i++) {
var n = tests[k][i];
var cl = k + "_" + n;
var cl = classify(k) + "_" + n;
var b = $("." + cl + "_output.before");
var a = $("." + cl + "_output.after");
var s = $("." + cl + "_selector.selector");
Expand All @@ -16,7 +21,7 @@ $(document).ready(function() {
a.text($.trim(a.text() + "\n" + JSON.stringify(m, null, " ")));
});
} catch(e) {
a.text("Error: " + e);
a.text("" + e);
}
if (a.text() === b.text()) s.addClass("success").removeClass("failure");
else s.addClass("failure").removeClass("success");
Expand All @@ -27,7 +32,7 @@ $(document).ready(function() {
function fetchFile(p, c) {
$.get(p, function (data) {
$("." + c).text($.trim(data));
});
}, 'text');
}

function renderTests() {
Expand All @@ -38,16 +43,16 @@ $(document).ready(function() {
var c = $("<div/>");
for (var k in tests) {
c.append($("<h1/>").text("document: " + k));
var cl = k + "_document";
var cl = classify(k) + "_document";
c.append($("<pre/>").addClass(cl).addClass("document").text("loading document..."));
fetchFile("tests/" + k + ".json", cl);
for (var i = 0; i < tests[k].length; i++) {
var n = tests[k][i];
var cl = k + "_" + n + "_selector";
var cl = classify(k) + "_" + n + "_selector";
var s = $("<pre/>").addClass(cl).addClass("selector").text("loading selector...");
c.append(s);
fetchFile("tests/" + k + "_" + n + ".selector", cl);
cl = k + "_" + n + "_output";
cl = classify(k) + "_" + n + "_output";
var t = $("<table/>").append($("<tr/>").append(
$("<td/>").append($("<pre/>").addClass(cl).addClass("before").text("loading output..."))).append(
$("<td/>").append($("<pre/>").addClass(cl).addClass("after").text("... test output ..."))));
Expand All @@ -61,12 +66,12 @@ $(document).ready(function() {
c.appendTo($("#tests"));
}

$.get("tests/alltests.txt", function (data) {
$.get("alltests.txt", function (data) {
var lines = data.split("\n");
for (var i = 0; i < lines.length; i++) {
var f = $.trim(lines[i]);
if (f.length == 0) continue;
var m = /^([A-Za-z]+)_(.+)\.selector$/.exec(f);
var m = /^([A-Za-z_0-9]+\/[A-Za-z0-9]+)_(.+)\.selector$/.exec(f);
if (m) {
if (!tests.hasOwnProperty(m[1])) tests[m[1]] = [];
tests[m[1]].push(m[2]);
Expand Down
53 changes: 28 additions & 25 deletions src/test/parse_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ <h2> Tests of the JSONSelect parser </h2>
Selectors
<pre class="doctest">
$ JSONSelect._parse(".foo");
[{id: "foo"}]
[4, [{id: "foo"}]]
$ JSONSelect._parse('." foo "');
[{id: " foo "}]
[8, [{id: " foo "}]]
$ JSONSelect._parse("string.foo:last-child");
[{a: 0, b: 1, id: "foo", pf: ":nth-last-child", type: "string"}]
[21, [{a: 0, b: 1, id: "foo", pf: ":nth-last-child", type: "string"}]]
$ JSONSelect._parse("string.foo.bar");
Error: multiple ids not allowed
$ JSONSelect._parse("string.foo:first-child.bar");
Expand All @@ -35,9 +35,9 @@ <h2> Tests of the JSONSelect parser </h2>
$ JSONSelect._parse("string:bogus");
Error: unrecognized pseudo class
$ JSONSelect._parse("string.xxx\\@yyy");
[{id: "xxx@yyy", type: "string"}]
[15, [{id: "xxx@yyy", type: "string"}]]
$ JSONSelect._parse(" ");
Error: selector expected
Error: selector expected in ' '
$ JSONSelect._parse("");
Error: selector expected
</pre>
Expand All @@ -47,21 +47,24 @@ <h2> Tests of the JSONSelect parser </h2>
Combinators
<pre class="doctest">
$ JSONSelect._parse(".foo .bar");
[{id: "foo"}, {id: "bar"}]
[9, [{id: "foo"}, {id: "bar"}]]
$ JSONSelect._parse("string.foo , number.foo");
[",", [{id: "foo", type: "string"}], [{id: "foo", type: "number"}]]
[23, [",", [{id: "foo", type: "string"}], [{id: "foo", type: "number"}]]]
$ JSONSelect._parse("string > .foo number.bar");
[{type: "string"}, ">", {id: "foo"}, {id: "bar", type: "number"}]
[24, [{type: "string"}, ">", {id: "foo"}, {id: "bar", type: "number"}]]
$ JSONSelect._parse("string > .foo number.bar, object");
[",", [{type: "string"}, ">", {id: "foo"}, {id: "bar", type: "number"}], [{type: "object"}]]
[32, [",", [{type: "string"}, ">", {id: "foo"}, {id: "bar", type: "number"}], [{type: "object"}]]]
$ JSONSelect._parse("string > .foo number.bar, object, string, .\"baz bing\", :root");
[
",",
[{type: "string"}, ">", {id: "foo"}, {id: "bar", type: "number"}],
[{type: "object"}],
[{type: "string"}],
[{id: "baz bing"}],
[{pc: ":root"}]
60,
[
",",
[{type: "string"}, ">", {id: "foo"}, {id: "bar", type: "number"}],
[{type: "object"}],
[{type: "string"}],
[{id: "baz bing"}],
[{pc: ":root"}]
]
]
</pre>
</div>
Expand All @@ -70,25 +73,25 @@ <h2> Tests of the JSONSelect parser </h2>
Expressions
<pre class="doctest">
$ JSONSelect._parse(":nth-child(1)");
[{a: 0, b: 1, pf: ":nth-child"}]
[13, [{a: 0, b: 1, pf: ":nth-child"}]]
$ JSONSelect._parse(":nth-child(2n+1)");
[{a: 2, b: 1, pf: ":nth-child"}]
[16, [{a: 2, b: 1, pf: ":nth-child"}]]
$ JSONSelect._parse(":nth-child ( 2n + 1 )");
[{a: 2, b: 1, pf: ":nth-child"}]
[21, [{a: 2, b: 1, pf: ":nth-child"}]]
$ JSONSelect._parse(":nth-child(odd)");
[{a: 2, b: 1, pf: ":nth-child"}]
[15, [{a: 2, b: 1, pf: ":nth-child"}]]
$ JSONSelect._parse(":nth-child(even)");
[{a: 2, b: 0, pf: ":nth-child"}]
[16, [{a: 2, b: 0, pf: ":nth-child"}]]
$ JSONSelect._parse(":nth-child(-n+6)");
[{a: -1, b: 6, pf: ":nth-child"}]
[16, [{a: -1, b: 6, pf: ":nth-child"}]]
$ JSONSelect._parse(":nth-child(2n)");
[{a: 2, b: 0, pf: ":nth-child"}]
[14, [{a: 2, b: 0, pf: ":nth-child"}]]
$ JSONSelect._parse(":nth-last-child(-3n - 3)");
[{a: -3, b: -3, pf: ":nth-last-child"}]
[24, [{a: -3, b: -3, pf: ":nth-last-child"}]]
$ JSONSelect._parse(":first-child");
[{a: 0, b: 1, pf: ":nth-child"}]
[12, [{a: 0, b: 1, pf: ":nth-child"}]]
$ JSONSelect._parse(":last-child");
[{a: 0, b: 1, pf: ":nth-last-child"}]
[11, [{a: 0, b: 1, pf: ":nth-last-child"}]]
</pre>
</div>

Expand Down