From c0c4593d5cfb7ae07d05f8fd87b7d0efcde0048d Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Tue, 7 Oct 2014 11:04:34 -0400 Subject: [PATCH] Fix most unit tests and the conformance test runner. --- src/test/alltests.txt | 47 ++++++++++++++++++++++++++++++++++ src/test/js/conf_tests.js | 23 ++++++++++------- src/test/parse_test.html | 53 +++++++++++++++++++++------------------ 3 files changed, 89 insertions(+), 34 deletions(-) create mode 100644 src/test/alltests.txt diff --git a/src/test/alltests.txt b/src/test/alltests.txt new file mode 100644 index 0000000..a743f98 --- /dev/null +++ b/src/test/alltests.txt @@ -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 diff --git a/src/test/js/conf_tests.js b/src/test/js/conf_tests.js index 42711ea..dea648f 100644 --- a/src/test/js/conf_tests.js +++ b/src/test/js/conf_tests.js @@ -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"); @@ -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"); @@ -27,7 +32,7 @@ $(document).ready(function() { function fetchFile(p, c) { $.get(p, function (data) { $("." + c).text($.trim(data)); - }); + }, 'text'); } function renderTests() { @@ -38,16 +43,16 @@ $(document).ready(function() { var c = $("
"); for (var k in tests) { c.append($("

").text("document: " + k)); - var cl = k + "_document"; + var cl = classify(k) + "_document"; c.append($("
").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 = $("
").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 = $("").append($("").append(
                     $("
").append($("
").addClass(cl).addClass("before").text("loading output..."))).append(
                     $("
").append($("
").addClass(cl).addClass("after").text("... test output ..."))));
@@ -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]);
diff --git a/src/test/parse_test.html b/src/test/parse_test.html
index 9744457..002877b 100644
--- a/src/test/parse_test.html
+++ b/src/test/parse_test.html
@@ -19,11 +19,11 @@ 

Tests of the JSONSelect parser

Selectors
 $ 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");
@@ -35,9 +35,9 @@ 

Tests of the JSONSelect parser

$ 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
@@ -47,21 +47,24 @@

Tests of the JSONSelect parser

Combinators
 $ 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"}]
+  ]
 ]
 
@@ -70,25 +73,25 @@

Tests of the JSONSelect parser

Expressions
 $ 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"}]]