1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
11 # Tests for SAXophoNit
12 module test_saxophonit
is test
14 import sax
::helpers
::sax_locator_impl
15 import sax
::helpers
::attributes_impl
17 import saxophonit
::testing
23 redef fun create_reader
do return new XophonReader
25 fun test_empty
is test
do
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")
36 fun test_simple_element
is test
do
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")
48 fun test_type_mismatch
is test
do
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))
61 fun test_attributes
is test
do
62 var atts
= new AttributesImpl
65 parse_string
("<foo bar=\"baz\
" answer='&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")
76 fun test_nested
is test
do
77 var atts
= new AttributesImpl
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")
94 fun test_xmldecl
is test
do
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
105 fun test_xmldecl_encoding
is test
do
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
116 fun test_xmldecl_standalone
is test
do
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
127 fun test_xmldecl_both
is test
do
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
138 fun test_reference_builtin
is test
do
140 parse_string
("<foo>&"'<>00:</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
157 fun test_comments is test do
158 # TODO For the moment, comments are simply ignored.
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
170 fun test_ns_simple is test do
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
182 fun test_ns_prefix is test do
183 var atts = new AttributesImpl
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
198 fun test_mixed is test do
199 var atts = new AttributesImpl
201 # TODO For the moment, ignorable white space is not detected.
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