Merge: model: is_accessor
[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
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 test
22
23 redef fun create_reader do return new XophonReader
24
25 fun test_empty is test do
26 before_test
27 parse_string("<foo />")
28 expected.document_locator = new SAXLocatorImpl
29 expected.start_document
30 expected.start_element("", "foo", "foo", new AttributesImpl)
31 expected.end_element("", "foo", "foo")
32 expected.end_document
33 assert_equals
34 end
35
36 fun test_simple_element is test do
37 before_test
38 parse_string("<foo>bar</foo>")
39 expected.document_locator = new SAXLocatorImpl
40 expected.start_document
41 expected.start_element("", "foo", "foo", new AttributesImpl)
42 expected.characters("bar")
43 expected.end_element("", "foo", "foo")
44 expected.end_document
45 assert_equals
46 end
47
48 fun test_type_mismatch is test do
49 before_test
50 parse_string("<a></b>")
51 expected.document_locator = new SAXLocatorImpl
52 expected.start_document
53 expected.start_element("", "a", "a", new AttributesImpl)
54 expected.fatal_error(new SAXParseException.with_info(
55 "The type in the closing tag (`b`) does not match the type " +
56 "in the opening tag (`a`).", null, null, 1, 8))
57 expected.end_document
58 assert_equals
59 end
60
61 fun test_attributes is test do
62 var atts = new AttributesImpl
63
64 before_test
65 parse_string("<foo bar=\"baz\" answer='&amp;42' />")
66 expected.document_locator = new SAXLocatorImpl
67 expected.start_document
68 atts.add("", "bar", "bar", "CDATA", "baz")
69 atts.add("", "answer", "answer", "CDATA", "&42")
70 expected.start_element("", "foo", "foo", atts)
71 expected.end_element("", "foo", "foo")
72 expected.end_document
73 assert_equals
74 end
75
76 fun test_nested is test do
77 var atts = new AttributesImpl
78
79 before_test
80 parse_string("<foo><bar /><a>b</a></foo>")
81 expected.document_locator = new SAXLocatorImpl
82 expected.start_document
83 expected.start_element("", "foo", "foo", atts)
84 expected.start_element("", "bar", "bar", atts)
85 expected.end_element("", "bar", "bar")
86 expected.start_element("", "a", "a", atts)
87 expected.characters("b")
88 expected.end_element("", "a", "a")
89 expected.end_element("", "foo", "foo")
90 expected.end_document
91 assert_equals
92 end
93
94 fun test_xmldecl is test do
95 before_test
96 parse_string("<?xml version='1.0'?><foo />")
97 expected.document_locator = new SAXLocatorImpl
98 expected.start_document
99 expected.start_element("", "foo", "foo", new AttributesImpl)
100 expected.end_element("", "foo", "foo")
101 expected.end_document
102 assert_equals
103 end
104
105 fun test_xmldecl_encoding is test do
106 before_test
107 parse_string("<?xml version=\"1.0\" encoding='utf-8'?><foo />")
108 expected.document_locator = new SAXLocatorImpl
109 expected.start_document
110 expected.start_element("", "foo", "foo", new AttributesImpl)
111 expected.end_element("", "foo", "foo")
112 expected.end_document
113 assert_equals
114 end
115
116 fun test_xmldecl_standalone is test do
117 before_test
118 parse_string("<?xml version='1.0' standalone=\"yes\"?><foo />")
119 expected.document_locator = new SAXLocatorImpl
120 expected.start_document
121 expected.start_element("", "foo", "foo", new AttributesImpl)
122 expected.end_element("", "foo", "foo")
123 expected.end_document
124 assert_equals
125 end
126
127 fun test_xmldecl_both is test do
128 before_test
129 parse_string("<?xml version='1.0' encoding='utf-8' standalone=\"yes\"?><foo />")
130 expected.document_locator = new SAXLocatorImpl
131 expected.start_document
132 expected.start_element("", "foo", "foo", new AttributesImpl)
133 expected.end_element("", "foo", "foo")
134 expected.end_document
135 assert_equals
136 end
137
138 fun test_reference_builtin is test do
139 before_test
140 parse_string("<foo>&amp;&quot;&apos;&lt;&gt;&#48;&#x30;&#x03A;</foo>")
141 expected.document_locator = new SAXLocatorImpl
142 expected.start_document
143 expected.start_element("", "foo", "foo", new AttributesImpl)
144 expected.characters("&")
145 expected.characters("\"")
146 expected.characters("'")
147 expected.characters("<")
148 expected.characters(">")
149 expected.characters("0")
150 expected.characters("0")
151 expected.characters(":")
152 expected.end_element("", "foo", "foo")
153 expected.end_document
154 assert_equals
155 end
156
157 fun test_comments is test do
158 # TODO For the moment, comments are simply ignored.
159 before_test
160 parse_string("<!-- I--><foo>bar<!--l-i-k-e--></foo><!--comments -->")
161 expected.document_locator = new SAXLocatorImpl
162 expected.start_document
163 expected.start_element("", "foo", "foo", new AttributesImpl)
164 expected.characters("bar")
165 expected.end_element("", "foo", "foo")
166 expected.end_document
167 assert_equals
168 end
169
170 fun test_ns_simple is test do
171 before_test
172 parse_string("<foo:bar xmlns:foo='https://s.exemple.org' />")
173 expected.document_locator = new SAXLocatorImpl
174 expected.start_document
175 expected.start_prefix_mapping("foo", "https://s.exemple.org")
176 expected.start_element("https://s.exemple.org", "bar", "foo:bar", new AttributesImpl)
177 expected.end_element("https://s.exemple.org", "bar", "foo:bar")
178 expected.end_document
179 assert_equals
180 end
181
182 fun test_ns_prefix is test do
183 var atts = new AttributesImpl
184
185 before_test
186 actual.feature("http://xml.org/sax/features/namespace-prefixes") = true
187 parse_string("<foo:bar xmlns:foo='https://s.exemple.org' />")
188 expected.document_locator = new SAXLocatorImpl
189 expected.start_document
190 expected.start_prefix_mapping("foo", "https://s.exemple.org")
191 atts.add("http://www.w3.org/xmlns/2000/", "foo", "xmlns:foo", "CDATA", "https://s.exemple.org")
192 expected.start_element("https://s.exemple.org", "bar", "foo:bar", atts)
193 expected.end_element("https://s.exemple.org", "bar", "foo:bar")
194 expected.end_document
195 assert_equals
196 end
197
198 fun test_mixed is test do
199 var atts = new AttributesImpl
200
201 # TODO For the moment, ignorable white space is not detected.
202 before_test
203 parse_string("<foo> \r\n\n<bar> baz </bar></foo>")
204 expected.document_locator = new SAXLocatorImpl
205 expected.start_document
206 expected.start_element("", "foo", "foo", atts)
207 expected.characters(" \n\n")
208 expected.start_element("", "bar", "bar", atts)
209 expected.characters(" baz ")
210 expected.end_element("", "bar", "bar")
211 expected.end_element("", "foo", "foo")
212 expected.end_document
213 assert_equals
214 end
215 end