summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/misc/oktavia/test/test-sax.jsx
blob: 8310bf0a784a830d0f74ae67e68cc83beb9cdf68 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import "sax.jsx";
import "test-case.jsx";


class TestHandler extends SAXHandler
{
    var events : string[];
    var param1 : string[];
    var param2 : string[];

    function constructor (events : string[], param1 : string[], param2 : string[])
    {
        this.events = events;
        this.param1 = param1;
        this.param2 = param2;
    }
    override function ontext (text : string) : void
    {
        this.events.push("ontext");
        this.param1.push(text);
        this.param2.push(null);
    }
    override function ondoctype (doctype : string) : void
    {
        this.events.push("ondoctype");
        this.param1.push(doctype);
        this.param2.push(null);
    }
    override function onopentag (tagname : string, attributes : Map.<string>) : void
    {
        this.events.push("onopentag");
        this.param1.push(tagname);
        this.param2.push(null);
    }
    override function onclosetag (tagname : string) : void
    {
        this.events.push("onclosetag");
        this.param1.push(tagname);
        this.param2.push(null);
    }
    override function onattribute (name : string, value : string) : void
    {
        this.events.push("onattribute");
        this.param1.push(name);
        this.param2.push(value);
    }
    override function oncomment (comment : string) : void
    {
        this.events.push("oncomment");
        this.param1.push(comment);
        this.param2.push(null);
    }
    override function onend () : void
    {
        this.events.push("onend");
        this.param1.push(null);
        this.param2.push(null);
    }
    override function onready () : void
    {
        this.events.push("onready");
        this.param1.push(null);
        this.param2.push(null);
    }
    override function onscript (script : string) : void
    {
        this.events.push("onscript");
        this.param1.push(script);
        this.param2.push(null);
    }
}

class _Test extends TestCase
{
    var handler : TestHandler;
    var parser : SAXParser;

    var events : string[];
    var param1 : string[];
    var param2 : string[];

    override function setUp () : void
    {
        this.events = [] : string[];
        this.param1 = [] : string[];
        this.param2 = [] : string[];
        this.handler = new TestHandler(this.events, this.param1, this.param2);
        this.parser = new SAXParser(this.handler);
    }

    function test_empty_input () : void
    {
        this.parser.parse('');
        this.expect(this.events[0]).toBe('onready');
        this.expect(this.events[1]).toBe('onend');
    }

    function test_doctype () : void
    {
        this.parser.parse('<!DOCTYPE html>');
        this.expect(this.events[1]).toBe('ondoctype');
        this.expect(this.param1[1]).toBe('html');
    }

    function test_tag1 () : void
    {
        this.parser.parse('<html></html>');
        this.expect(this.events[1]).toBe('onopentag');
        this.expect(this.param1[1]).toBe('html');
        this.expect(this.events[2]).toBe('onclosetag');
        this.expect(this.param1[2]).toBe('html');
    }

    function test_tag2 () : void
    {
        this.parser.parse('<html/>');
        this.expect(this.events[1]).toBe('onopentag');
        this.expect(this.param1[1]).toBe('html');
        this.expect(this.events[2]).toBe('onclosetag');
        this.expect(this.param1[2]).toBe('html');
    }

    function test_attribute () : void
    {
        this.parser.parse('<html lang="ja"></html>');
        this.expect(this.events[1]).toBe('onattribute');
        this.expect(this.param1[1]).toBe('lang');
        this.expect(this.param2[1]).toBe('ja');
        this.expect(this.events[2]).toBe('onopentag');
        this.expect(this.param1[2]).toBe('html');
    }

    function test_text () : void
    {
        this.parser.parse('<html><body>hello world</body></html>');
        this.expect(this.events[3]).toBe('ontext');
        this.expect(this.param1[3]).toBe('hello world');
    }

    function test_comment () : void
    {
        this.parser.parse('<html><body><!-- comment --></body></html>');
        this.expect(this.events[3]).toBe('oncomment');
        this.expect(this.param1[3]).toBe('comment');
    }
}