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 # Test suite for `namespace_support`.
12 module test_namespace_support
is test_suite
15 import sax
::helpers
::namespace_support
17 class TestNamespaceSupport
20 private fun sample
: NamespaceSupport do
21 var subject
= new NamespaceSupport
24 subject
.declare_prefix
("", "http://www.w3.org/1999/xhtml")
25 subject
.declare_prefix
("dc", "http://www.purl.org/dc#")
33 assert subject
.xmlns
== subject
.uri
("xml")
34 assert subject
.nsdecl
== subject
.uri
("xmlns")
35 assert subject
.prefix
("http://www.w3.org/1999/xhtml") == null
36 assert 2 == subject
.declared_prefixes
.length
37 assert 2 == subject
.prefixes
.length
40 fun test_push_context_override_default
do
44 subject
.declare_prefix
("", "this:is:a:test")
45 assert "this:is:a:test" == subject
.uri
("")
46 assert "http://www.purl.org/dc#" == subject
.uri
("dc")
47 assert subject
.xmlns
== subject
.uri
("xml")
48 assert subject
.nsdecl
== subject
.uri
("xmlns")
49 assert subject
.prefix
("http://www.w3.org/1999/xhtml") == null
50 assert 1 == subject
.declared_prefixes
.length
51 assert 3 == subject
.prefixes
.length
54 fun test_push_context_override_dc
do
58 subject
.declare_prefix
("dc", "this:is:a:test")
59 assert "this:is:a:test" == subject
.uri
("dc")
60 assert "http://www.w3.org/1999/xhtml" == subject
.uri
("")
61 assert subject
.xmlns
== subject
.uri
("xml")
62 assert subject
.prefix
("http://www.purl.org/dc#") == null
63 assert 1 == subject
.declared_prefixes
.length
64 assert 3 == subject
.prefixes
.length
67 fun test_push_context_undeclare
do
71 subject
.declare_prefix
("dc", "")
72 assert subject
.uri
("dc") == null
73 assert 1 == subject
.declared_prefixes
.length
74 assert 2 == subject
.prefixes
.length
77 fun test_pop_context
do
81 assert subject
.xmlns
== subject
.uri
("xml")
82 assert subject
.prefix
("http://www.w3.org/1999/xhtml") == null
83 assert 2 == subject
.declared_prefixes
.length
84 assert 2 == subject
.prefixes
.length
87 #fun test_declare_prefix # SEE: test_push_context_*
89 fun test_process_name
do
91 var parts
= new Array[String]
93 assert ["http://www.w3.org/1999/xhtml", "p", "p"] == subject
.process_name
("p", parts
, false)
94 assert ["", "p", "p"] == subject
.process_name
("p", parts
, true)
95 assert ["http://www.purl.org/dc#", "title", "dc:title"] == subject
.process_name
("dc:title", parts
, false)
96 assert ["http://www.purl.org/dc#", "title", "dc:title"] == subject
.process_name
("dc:title", parts
, true)
97 assert [subject
.xmlns
, "lang", "xml:lang"] == subject
.process_name
("xml:lang", parts
, false)
98 assert ["http://www.w3.org/1999/xhtml", "p", ":p"] == subject
.process_name
(":p", parts
, false)
99 assert ["", "p", ":p"] == subject
.process_name
(":p", parts
, true)
100 assert subject
.process_name
("foo:bar", parts
, false) == null
101 assert subject
.process_name
("foo:bar", parts
, true) == null
103 assert ["", "p", "p"] == subject
.process_name
("p", parts
, false)
104 assert ["", "p", "p"] == subject
.process_name
("p", parts
, true)
107 fun test_process_name_xmlns
do
109 var parts
= new Array[String].with_capacity
(3)
111 assert [subject
.nsdecl
, "", "xmlns"] == subject
.process_name
("xmlns", parts
, true)
112 assert ["http://www.w3.org/1999/xhtml", "xmlns", "xmlns"] == subject
.process_name
("xmlns", parts
, false)
115 fun test_declare_prefix_illegal
do
118 assert not subject
.declare_prefix
("xml", "http://example.org")
119 assert not subject
.declare_prefix
("xmlns", "http://www2.example.org")
120 assert not subject
.declare_prefix
("foo", subject
.xmlns
)
121 assert not subject
.declare_prefix
("bar", subject
.nsdecl
)
122 assert 2 == subject
.declared_prefixes
.length
128 assert "http://www.w3.org/1999/xhtml" == subject
.uri
("")
129 assert "http://www.purl.org/dc#" == subject
.uri
("dc")
130 assert subject
.xmlns
== subject
.uri
("xml")
131 assert subject
.uri
("foo") == null
135 var res
= sample
.prefixes
137 assert 3 == res
.length
else
138 sys
.stderr
.write
("Expected 3; got {res.length}.\n")
141 assert res
.has
("xml")
142 assert res
.has
("xmlns")
148 assert subject
.prefix
("http://www.w3.org/1999/xhtml") == null
149 assert "dc" == subject
.prefix
("http://www.purl.org/dc#")
150 assert "xml" == subject
.prefix
(subject
.xmlns
)
151 assert subject
.prefix
("https://example.org/serious") == null
154 fun test_prefixes_of
do
156 var res
: Collection[String]
158 assert new Array[String] == subject
.prefixes_of
("http://www.w3.org/1999/xhtml")
159 assert ["dc"] == subject
.prefixes_of
("http://www.purl.org/dc#")
160 assert ["xml"] == subject
.prefixes_of
(subject
.xmlns
)
161 assert new Array[String] == subject
.prefixes_of
("https://example.org/serious")
162 subject
.declare_prefix
("html", "http://www.w3.org/1999/xhtml")
163 assert ["html"] == subject
.prefixes_of
("http://www.w3.org/1999/xhtml")
164 subject
.declare_prefix
("dc2", "http://www.purl.org/dc#")
165 res
= subject
.prefixes_of
("http://www.purl.org/dc#")
166 assert 2 == res
.length
else
167 sys
.stderr
.write
("Expected 2; got {res.length}.\n")
169 assert res
.has_all
(["dc", "dc2"])
172 fun test_declared_prefixes
do
173 var res
= sample
.declared_prefixes
175 assert 2 == res
.length
else
176 sys
.stderr
.write
("Expected 2; got {res.length}.\n")