File size: 2,912 Bytes
4cadbaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<html>
  <head>
    <title>JSONSelect JS parser tests</title>
    <link rel="stylesheet" type="text/css" href="js/doctest.css" />
    <script src="js/doctest.js"></script>
    <script src="../jsonselect.js"></script>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  </head>
  <body>

<div>
  <button onclick="doctest()" type="button">run tests</button>
  <pre id="doctestOutput"></pre>
</div>

    <h2> Tests of the JSONSelect parser </h2>

<div class="test">
Selectors
<pre class="doctest">
$ JSONSelect._parse(".foo");
[{id: "foo"}]
$ JSONSelect._parse('." foo "');
[{id: " foo "}]
$ JSONSelect._parse("string.foo:last-child");
[{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");
Error: multiple ids not allowed
$ JSONSelect._parse("string:last-child.foo:first-child.bar");
Error: multiple pseudo classes (:xxx) not allowed
$ JSONSelect._parse("string.");
Error: string required after '.'
$ JSONSelect._parse("string:bogus");
Error: unrecognized pseudo class
$ JSONSelect._parse("string.xxx\\@yyy");
[{id: "xxx@yyy", type: "string"}]
$ JSONSelect._parse(" ");
Error: selector expected
$ JSONSelect._parse("");
Error: selector expected
</pre>
</div>

<div class="test">
Combinators
<pre class="doctest">
$ JSONSelect._parse(".foo .bar");
[{id: "foo"}, {id: "bar"}]
$ JSONSelect._parse("string.foo , number.foo");
[",", [{id: "foo", type: "string"}], [{id: "foo", type: "number"}]]
$ JSONSelect._parse("string > .foo number.bar");
[{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"}]]
$ 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"}]
]
</pre>
</div>

<div class="test">
Expressions
<pre class="doctest">
$ JSONSelect._parse(":nth-child(1)");
[{a: 0, b: 1, pf: ":nth-child"}]
$ JSONSelect._parse(":nth-child(2n+1)");
[{a: 2, b: 1, pf: ":nth-child"}]
$ JSONSelect._parse(":nth-child ( 2n + 1 )");
[{a: 2, b: 1, pf: ":nth-child"}]
$ JSONSelect._parse(":nth-child(odd)");
[{a: 2, b: 1, pf: ":nth-child"}]
$ JSONSelect._parse(":nth-child(even)");
[{a: 2, b: 0, pf: ":nth-child"}]
$ JSONSelect._parse(":nth-child(-n+6)");
[{a: -1, b: 6, pf: ":nth-child"}]
$ JSONSelect._parse(":nth-child(2n)");
[{a: 2, b: 0, pf: ":nth-child"}]
$ JSONSelect._parse(":nth-last-child(-3n - 3)");
[{a: -3, b: -3, pf: ":nth-last-child"}]
$ JSONSelect._parse(":first-child");
[{a: 0, b: 1, pf: ":nth-child"}]
$ JSONSelect._parse(":last-child");
[{a: 0, b: 1, pf: ":nth-last-child"}]
</pre>
</div>

  </body>
</html>