270ed22a8724879671a377f21cf265db71043002
[nit.git] / lib / saxophonit / test_saxophonit.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Tests for SAXophoNit
12 module test_saxophonit is test_suite
13
14 import sax::helpers::sax_locator_impl
15 import sax::helpers::attributes_impl
16 import saxophonit
17 import saxophonit::testing
18
19 class TestSaxophonit
20 super SAXTestSuite
21
22 redef fun create_reader do return new XophonReader
23
24 fun test_empty do
25 before_test
26 parse_string("<foo />")
27 expected.document_locator = new SAXLocatorImpl
28 expected.start_document
29 expected.start_element("", "foo", "foo", new AttributesImpl)
30 expected.end_element("", "foo", "foo")
31 expected.end_document
32 assert_equals
33 end
34
35 fun test_simple_element do
36 before_test
37 parse_string("<foo>bar</foo>")
38 expected.document_locator = new SAXLocatorImpl
39 expected.start_document
40 expected.start_element("", "foo", "foo", new AttributesImpl)
41 expected.characters("bar")
42 expected.end_element("", "foo", "foo")
43 expected.end_document
44 assert_equals
45 end
46
47 fun test_type_mismatch do
48 before_test
49 parse_string("<a></b>")
50 expected.document_locator = new SAXLocatorImpl
51 expected.start_document
52 expected.start_element("", "a", "a", new AttributesImpl)
53 expected.fatal_error(new SAXParseException.with(
54 "The type in the closing tag (`b`) does not match the type " +
55 "in the opening tag (`a`).", null, null, 1, 8))
56 expected.end_document
57 assert_equals
58 end
59
60 fun test_attributes do
61 var atts = new AttributesImpl
62
63 before_test
64 parse_string("<foo bar=\"baz\" answer='&amp;42' />")
65 expected.document_locator = new SAXLocatorImpl
66 expected.start_document
67 atts.add("", "bar", "bar", "CDATA", "baz")
68 atts.add("", "answer", "answer", "CDATA", "&42")
69 expected.start_element("", "foo", "foo", atts)
70 expected.end_element("", "foo", "foo")
71 expected.end_document
72 assert_equals
73 end
74
75 fun test_nested do
76 var atts = new AttributesImpl
77
78 before_test
79 parse_string("<foo><bar /><a>b</a></foo>")
80 expected.document_locator = new SAXLocatorImpl
81 expected.start_document
82 expected.start_element("", "foo", "foo", atts)
83 expected.start_element("", "bar", "bar", atts)
84 expected.end_element("", "bar", "bar")
85 expected.start_element("", "a", "a", atts)
86 expected.characters("b")
87 expected.end_element("", "a", "a")
88 expected.end_element("", "foo", "foo")
89 expected.end_document
90 assert_equals
91 end
92
93 fun test_xmldecl do
94 before_test
95 parse_string("<?xml version='1.0'?><foo />")
96 expected.document_locator = new SAXLocatorImpl
97 expected.start_document
98 expected.start_element("", "foo", "foo", new AttributesImpl)
99 expected.end_element("", "foo", "foo")
100 expected.end_document
101 assert_equals
102 end
103
104 fun test_xmldecl_encoding do
105 before_test
106 parse_string("<?xml version=\"1.0\" encoding='utf-8'?><foo />")
107 expected.document_locator = new SAXLocatorImpl
108 expected.start_document
109 expected.start_element("", "foo", "foo", new AttributesImpl)
110 expected.end_element("", "foo", "foo")
111 expected.end_document
112 assert_equals
113 end
114
115 fun test_xmldecl_standalone do
116 before_test
117 parse_string("<?xml version='1.0' standalone=\"yes\"?><foo />")
118 expected.document_locator = new SAXLocatorImpl
119 expected.start_document
120 expected.start_element("", "foo", "foo", new AttributesImpl)
121 expected.end_element("", "foo", "foo")
122 expected.end_document
123 assert_equals
124 end
125
126 fun test_xmldecl_both do
127 before_test
128 parse_string("<?xml version='1.0' encoding='utf-8' standalone=\"yes\"?><foo />")
129 expected.document_locator = new SAXLocatorImpl
130 expected.start_document
131 expected.start_element("", "foo", "foo", new AttributesImpl)
132 expected.end_element("", "foo", "foo")
133 expected.end_document
134 assert_equals
135 end
136
137 fun test_reference_builtin do
138 before_test
139 parse_string("<foo>&amp;&quot;&apos;&lt;&gt;&#48;&#x30;&#x03A;</foo>")
140 expected.document_locator = new SAXLocatorImpl
141 expected.start_document
142 expected.start_element("", "foo", "foo", new AttributesImpl)
143 expected.characters("&")
144 expected.characters("\"")
145 expected.characters("'")
146 expected.characters("<")
147 expected.characters(">")
148 expected.characters("0")
149 expected.characters("0")
150 expected.characters(":")
151 expected.end_element("", "foo", "foo")
152 expected.end_document
153 assert_equals
154 end
155
156 fun test_comments do
157 # TODO For the moment, comments are simply ignored.
158 before_test
159 parse_string("<!-- I--><foo>bar<!--l-i-k-e--></foo><!--comments -->")
160 expected.document_locator = new SAXLocatorImpl
161 expected.start_document
162 expected.start_element("", "foo", "foo", new AttributesImpl)
163 expected.characters("bar")
164 expected.end_element("", "foo", "foo")
165 expected.end_document
166 assert_equals
167 end
168
169 fun test_ns_simple do
170 before_test
171 parse_string("<foo:bar xmlns:foo='https://s.exemple.org' />")
172 expected.document_locator = new SAXLocatorImpl
173 expected.start_document
174 expected.start_prefix_mapping("foo", "https://s.exemple.org")
175 expected.start_element("https://s.exemple.org", "bar", "foo:bar", new AttributesImpl)
176 expected.end_element("https://s.exemple.org", "bar", "foo:bar")
177 expected.end_document
178 assert_equals
179 end
180
181 fun test_ns_prefix do
182 var atts = new AttributesImpl
183
184 before_test
185 actual.feature("http://xml.org/sax/features/namespace-prefixes") = true
186 parse_string("<foo:bar xmlns:foo='https://s.exemple.org' />")
187 expected.document_locator = new SAXLocatorImpl
188 expected.start_document
189 expected.start_prefix_mapping("foo", "https://s.exemple.org")
190 atts.add("http://www.w3.org/xmlns/2000/", "foo", "xmlns:foo", "CDATA", "https://s.exemple.org")
191 expected.start_element("https://s.exemple.org", "bar", "foo:bar", atts)
192 expected.end_element("https://s.exemple.org", "bar", "foo:bar")
193 expected.end_document
194 assert_equals
195 end
196
197 fun test_mixed do
198 var atts = new AttributesImpl
199
200 # TODO For the moment, ignorable white space is not detected.
201 before_test
202 parse_string("<foo> \r\n\n<bar> baz </bar></foo>")
203 expected.document_locator = new SAXLocatorImpl
204 expected.start_document
205 expected.start_element("", "foo", "foo", atts)
206 expected.characters(" \n\n")
207 expected.start_element("", "bar", "bar", atts)
208 expected.characters(" baz ")
209 expected.end_element("", "bar", "bar")
210 expected.end_element("", "foo", "foo")
211 expected.end_document
212 assert_equals
213 end
214 end