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_suite
14 import sax
::helpers
::sax_locator_impl
15 import sax
::helpers
::attributes_impl
17 import saxophonit
::testing
22 redef fun create_reader
do return new XophonReader
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")
35 fun test_simple_element
do
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")
47 fun test_type_mismatch
do
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_info
(
54 "The type in the closing tag (`b`) does not match the type " +
55 "in the opening tag (`a`).", null, null, 1, 8))
60 fun test_attributes
do
61 var atts
= new AttributesImpl
64 parse_string
("<foo bar=\"baz\
" answer='&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")
76 var atts
= new AttributesImpl
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")
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
104 fun test_xmldecl_encoding
do
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
115 fun test_xmldecl_standalone
do
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
126 fun test_xmldecl_both
do
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
137 fun test_reference_builtin
do
139 parse_string
("<foo>&"'<>00:</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
157 # TODO For the moment, comments are simply ignored.
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
169 fun test_ns_simple do
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
181 fun test_ns_prefix do
182 var atts = new AttributesImpl
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
198 var atts = new AttributesImpl
200 # TODO For the moment, ignorable white space is not detected.
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