1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
require Test::Simple; |
4 |
require Message::Markup::XML; |
5 |
use Message::Markup::XML::QName qw/UNDEF_URI NULL_URI DEFAULT_PFX/; |
6 |
sub ok ($;$); |
7 |
my $e = new Message::Markup::XML (type => '#element', local_name => 'test', |
8 |
namespace_uri => 'http://something.test/'); |
9 |
use Carp q(verbose); |
10 |
my @reg_p2n = ( |
11 |
{ |
12 |
prefix => q(test1), |
13 |
name => q(http://test1.example/), |
14 |
result => 1, |
15 |
}, |
16 |
{ |
17 |
prefix => q(test1), |
18 |
name => q(http://test1.example/), |
19 |
result => 1, |
20 |
}, |
21 |
{ |
22 |
prefix => q(test1), |
23 |
name => q(http://test1.example/), |
24 |
opt => {check_registered => 1}, |
25 |
result => 1, |
26 |
}, |
27 |
{ |
28 |
prefix => q(test1), |
29 |
name => q(http://test2.example/), |
30 |
opt => {check_registered => 1}, |
31 |
result => 0, |
32 |
}, |
33 |
{ |
34 |
prefix => q(test1), |
35 |
name => q(http://test2.example/), |
36 |
result => 1, |
37 |
}, |
38 |
{ |
39 |
prefix => q(test1), |
40 |
name => q(), |
41 |
result => 0, |
42 |
}, |
43 |
{ |
44 |
prefix => q(test1), |
45 |
name => NULL_URI, |
46 |
result => 0, |
47 |
}, |
48 |
{ |
49 |
prefix => q(#default), |
50 |
name => q(http://test2.example/), |
51 |
result => 1, |
52 |
}, |
53 |
{ |
54 |
prefix => DEFAULT_PFX, |
55 |
name => q(http://test2.example/), |
56 |
result => 1, |
57 |
}, |
58 |
{ |
59 |
prefix => DEFAULT_PFX, |
60 |
name => NULL_URI, |
61 |
result => 1, |
62 |
}, |
63 |
{ |
64 |
prefix => q:some-prefix:, |
65 |
name => UNDEF_URI, |
66 |
result => 0, |
67 |
}, |
68 |
{ |
69 |
prefix => q:some-prefix:, |
70 |
name => q<http://uri.test/>, |
71 |
opt => {check_prefix => 1}, |
72 |
result => 1, |
73 |
}, |
74 |
{ |
75 |
prefix => q:some invalid prefix:, |
76 |
name => q<http://uri.test/>, |
77 |
opt => {check_prefix => 1}, |
78 |
result => 0, |
79 |
}, |
80 |
{ |
81 |
prefix => q:some-prefix-11:, |
82 |
name => q<http://uri.test/>, |
83 |
opt => {check_name => 1}, |
84 |
result => 1, |
85 |
}, |
86 |
{ |
87 |
prefix => q:some-prefix-21:, |
88 |
name => q<relative-uri>, |
89 |
opt => {check_name => 1, check_name_uri_relative => 1}, |
90 |
result => 0, |
91 |
}, |
92 |
{ |
93 |
prefix => q:some-prefix-31:, |
94 |
name => q<relative-uri>, |
95 |
opt => {check_name => 1, resolve_name_uri_relative => 1}, |
96 |
result => 1, |
97 |
}, |
98 |
); |
99 |
|
100 |
my @get_p2n = ( |
101 |
{ |
102 |
prefix => q:prefix-1.:, |
103 |
name => q<http://foo.test/>, |
104 |
result => 1, |
105 |
}, |
106 |
{ |
107 |
prefix => q:prefix-2.:, |
108 |
name => q<http://foo2.test/>, |
109 |
opt => {___dont_register => 1}, |
110 |
result => 0, |
111 |
}, |
112 |
{ |
113 |
prefix => q:prefix-2.:, |
114 |
name => NULL_URI, |
115 |
opt => {___dont_register => 1}, |
116 |
result => 0, |
117 |
}, |
118 |
{ |
119 |
prefix => q:prefix-2.:, |
120 |
name => UNDEF_URI, |
121 |
opt => {___dont_register => 1}, |
122 |
result => 0, |
123 |
}, |
124 |
{ |
125 |
prefix => DEFAULT_PFX, |
126 |
name => UNDEF_URI, |
127 |
opt => {___dont_register => 1}, |
128 |
result => 0, |
129 |
}, |
130 |
{ |
131 |
prefix => DEFAULT_PFX, |
132 |
name => NULL_URI, |
133 |
result => 0, |
134 |
}, |
135 |
{ |
136 |
prefix => DEFAULT_PFX, |
137 |
name => NULL_URI, |
138 |
opt => {use_name_null => 1, use_prefix_default => 1}, |
139 |
result => 1, |
140 |
}, |
141 |
{ |
142 |
prefix => DEFAULT_PFX, |
143 |
name => NULL_URI, |
144 |
opt => {___dont_register => 1}, |
145 |
result => 0, |
146 |
}, |
147 |
{ |
148 |
prefix => DEFAULT_PFX, |
149 |
name => NULL_URI, |
150 |
opt => {use_name_null => 1, use_prefix_default => 1, |
151 |
___dont_register => 1}, |
152 |
result => 1, |
153 |
}, |
154 |
); |
155 |
|
156 |
my @qname = ( |
157 |
{ |
158 |
qname => q"foo", |
159 |
prefix => DEFAULT_PFX, |
160 |
lname => q"foo", |
161 |
result => 1, |
162 |
opt => {check_qname => 1, check_prefix => 1, |
163 |
check_local_name => 1}, |
164 |
}, |
165 |
{ |
166 |
qname => q"foo:bar", |
167 |
prefix => q"foo", |
168 |
lname => q"bar", |
169 |
result => 1, |
170 |
opt => {check_qname => 1, check_prefix => 1, |
171 |
check_local_name => 1}, |
172 |
}, |
173 |
{ |
174 |
qname => q"foo:0", |
175 |
prefix => q"foo", |
176 |
lname => q"0", |
177 |
result => 0, |
178 |
opt => {check_qname => 1, check_prefix => 1, |
179 |
check_local_name => 1}, |
180 |
}, |
181 |
{ |
182 |
qname => q"0:bar", |
183 |
prefix => q"0", |
184 |
lname => q"bar", |
185 |
result => 0, |
186 |
opt => {check_qname => 1, check_prefix => 1, |
187 |
check_local_name => 1}, |
188 |
}, |
189 |
{ |
190 |
qname => q"foo:", |
191 |
prefix => q"foo", |
192 |
lname => q"", |
193 |
result => 0, |
194 |
opt => {check_qname => 1, check_prefix => 1, |
195 |
check_local_name => 1}, |
196 |
}, |
197 |
{ |
198 |
qname => q":bar", |
199 |
join_qname => q"bar", |
200 |
prefix => q"", |
201 |
lname => q"bar", |
202 |
result => 0, |
203 |
join_result => 1, |
204 |
opt => {check_qname => 1, check_prefix => 1, |
205 |
check_local_name => 1}, |
206 |
}, |
207 |
{ |
208 |
qname => q"*", |
209 |
prefix => DEFAULT_PFX, |
210 |
lname => q"*", |
211 |
result => 1, |
212 |
opt => {check_qname => 1, check_prefix => 1, |
213 |
check_local_name => 1, use_local_name_star => 1}, |
214 |
}, |
215 |
{ |
216 |
qname => q"foo:*", |
217 |
prefix => q"foo", |
218 |
lname => q"*", |
219 |
result => 1, |
220 |
opt => {check_qname => 1, check_prefix => 1, |
221 |
check_local_name => 1, use_local_name_star => 1}, |
222 |
}, |
223 |
{ |
224 |
qname => q"foo|bar", |
225 |
prefix => q:foo:, |
226 |
lname => q:bar:, |
227 |
result => 1, |
228 |
opt => {check_qname => 1, check_prefix => 1, |
229 |
check_local_name => 1, qname_separator => '|'}, |
230 |
}, |
231 |
{ |
232 |
qname => q"foo|*", |
233 |
prefix => q:foo:, |
234 |
lname => q:*:, |
235 |
result => 1, |
236 |
opt => {check_qname => 1, check_prefix => 1, |
237 |
check_local_name => 1, qname_separator => '|', |
238 |
use_local_name_star => 1}, |
239 |
}, |
240 |
); |
241 |
|
242 |
my @gen_pfx = ( |
243 |
{ |
244 |
reset => 1, |
245 |
name => q<http://www.w3.org/1999/xhtml>, |
246 |
prefix => DEFAULT_PFX, |
247 |
opt => {use_prefix_default => 1}, |
248 |
}, |
249 |
{ |
250 |
reset => 0, |
251 |
name => q<http://www.w3.org/1999/xhtml>, |
252 |
prefix => q:h:, |
253 |
n2p => DEFAULT_PFX, |
254 |
opt => {use_prefix_default => 1}, |
255 |
}, |
256 |
{ |
257 |
reset => 1, |
258 |
name => q<http://www.w3.org/1999/xhtml>, |
259 |
prefix => q:h:, |
260 |
opt => {use_prefix_default => 0}, |
261 |
}, |
262 |
{ |
263 |
name => q<http://www.w3.org/1999/xhtml>, |
264 |
prefix => q:h1:, |
265 |
n2p => q:h:, |
266 |
}, |
267 |
{ |
268 |
name => q<http://www.w3.org/1999/xhtml>, |
269 |
prefix => q:xhtml:, |
270 |
n2p => q:h:, |
271 |
}, |
272 |
{ |
273 |
name => q<http://www.w3.org/1999/xhtml>, |
274 |
prefix => q:xhtml1:, |
275 |
n2p => q:h:, |
276 |
}, |
277 |
{ |
278 |
name => q<http://www.w3.org/1999/xhtml>, |
279 |
prefix => q:www.w3.org:, |
280 |
n2p => q:h:, |
281 |
}, |
282 |
{ |
283 |
name => q<http://www.w3.org/1999/xhtml>, |
284 |
prefix => q:http:, |
285 |
n2p => q:h:, |
286 |
}, |
287 |
{ |
288 |
name => q<http://www.w3.org/1999/xhtml>, |
289 |
prefix => q:ns0:, |
290 |
n2p => q:h:, |
291 |
}, |
292 |
{ |
293 |
name => q<http://www.w3.org/1999/xhtml>, |
294 |
prefix => q:ns1:, |
295 |
n2p => q:h:, |
296 |
}, |
297 |
{ |
298 |
name => q<test/0name/xmlns>, |
299 |
prefix => q:name:, |
300 |
}, |
301 |
{ |
302 |
name => q<test/0name/xmlns>, |
303 |
prefix => q:test:, |
304 |
n2p => q:name:, |
305 |
}, |
306 |
{ |
307 |
reset => 1, |
308 |
name => q<:///:04465612@&>, |
309 |
prefix => q:ns0:, |
310 |
}, |
311 |
{ |
312 |
reset => 1, |
313 |
name => NULL_URI, |
314 |
prefix => DEFAULT_PFX, |
315 |
opt => {use_prefix_default => 1}, |
316 |
}, |
317 |
{ |
318 |
reset => 0, |
319 |
name => NULL_URI, |
320 |
prefix => DEFAULT_PFX, |
321 |
opt => {use_prefix_default => 1}, |
322 |
}, |
323 |
{ |
324 |
reset => 1, |
325 |
name => NULL_URI, |
326 |
prefix => DEFAULT_PFX, |
327 |
opt => {use_prefix_default => 0}, |
328 |
}, |
329 |
); |
330 |
|
331 |
my @expand = ( |
332 |
{ |
333 |
reset => 1, |
334 |
ns => {foo => q<http://foo.test/>}, |
335 |
qname => q"foo:bar", |
336 |
xname => [q<http://foo.test/> => q:bar:], |
337 |
}, |
338 |
{ |
339 |
reset => 0, |
340 |
qname => q"foo:bar", |
341 |
xname => [q<http://foo.test/> => q:bar:], |
342 |
}, |
343 |
{ |
344 |
reset => 0, |
345 |
qname => q"bar", |
346 |
xname => [NULL_URI, q:bar:], |
347 |
opt => {use_prefix_default => 1, use_name_null => 1}, |
348 |
}, |
349 |
); |
350 |
|
351 |
Test::Simple->import (tests => scalar (@reg_p2n) |
352 |
+ scalar (@get_p2n) * 2 |
353 |
+ scalar (@qname) * 2 |
354 |
+ scalar (@gen_pfx) * 2 |
355 |
+ scalar (@expand) * 2); |
356 |
eval q{ |
357 |
sub ok ($;$) { |
358 |
my ($cond, $desc) = @_; |
359 |
if ($cond) { |
360 |
Test::Simple::ok (1); |
361 |
} else { |
362 |
Test::Simple::ok (0, $desc); |
363 |
} |
364 |
}}; |
365 |
|
366 |
for (@reg_p2n) { |
367 |
my $chk = Message::Markup::XML::QName::register_prefix_to_name |
368 |
($e, $_->{prefix} => $_->{name}, %{$_->{opt}||{}}); |
369 |
ok ($chk->{success} == $_->{result}, 'Register pfx->URI: '.$chk->{reason}); |
370 |
} |
371 |
|
372 |
for (@get_p2n) { |
373 |
$e->{ns} = {}; |
374 |
my $chk = Message::Markup::XML::QName::register_prefix_to_name |
375 |
($e, $_->{prefix} => $_->{name}, %{$_->{opt}||{}}) |
376 |
unless $_->{opt}->{___dont_register}; |
377 |
ok (Message::Markup::XML::QName::prefix_to_name ($e, $_->{prefix}, |
378 |
%{$_->{opt}||{}}) |
379 |
->{name} |
380 |
eq ($_->{result} ? ($_->{result_name} || $_->{name}) : undef), |
381 |
":$_->{prefix}: => <@{[$_->{result} ? ($_->{result_name} || $_->{name}) : undef]}>"); |
382 |
ok (Message::Markup::XML::QName::name_to_prefix ($e, $_->{name}, |
383 |
%{$_->{opt}||{}}) |
384 |
->{prefix} |
385 |
eq ($_->{result} ? ($_->{result_prefix} || $_->{prefix}) : undef), |
386 |
"<$_->{name}> => :@{[$_->{result} ? ($_->{result_prefix} || $_->{prefix}) : undef]}:"); |
387 |
} |
388 |
|
389 |
|
390 |
for (@qname) { |
391 |
my $chk = Message::Markup::XML::QName::split_qname ($_->{qname}, |
392 |
%{$_->{opt}||{}}); |
393 |
if ($_->{result}) { |
394 |
ok $chk->{prefix}.':'.($chk->{local_name_star} ? '*' : $chk->{local_name}) |
395 |
eq $_->{prefix} . ':' . $_->{lname}, |
396 |
"Split 1: $_->{qname} => $chk->{prefix}:$chk->{local_name}($chk->{local_name_star})"; |
397 |
} else { |
398 |
ok $chk->{success} == $_->{result}, |
399 |
"Split 0: $_->{qname} => $chk->{prefix}:$chk->{local_name}($chk->{local_name_star})"; |
400 |
} |
401 |
|
402 |
$chk = Message::Markup::XML::QName::join_qname |
403 |
($_->{prefix}, $_->{lname}, %{$_->{opt}||{}}); |
404 |
if ((defined $_->{join_result} and $_->{join_result}) or $_->{result}) { |
405 |
ok $chk->{qname} eq ($_->{join_qname} || $_->{qname}), "Join 1: $_->{prefix}, $_->{lname} => $chk->{qname}"; |
406 |
} else { |
407 |
ok $chk->{success} == ((defined $_->{join_result} and $_->{join_result}) or $_->{result}), |
408 |
"Join 0: $chk->{success}; $_->{prefix}, $_->{lname} => $chk->{qname}"; |
409 |
} |
410 |
} |
411 |
|
412 |
for (@gen_pfx) { |
413 |
$e->{ns} = {} if $_->{reset}; |
414 |
my $pfx = Message::Markup::XML::QName::generate_prefix ($e, $_->{name}, |
415 |
%{$_->{opt}||{}}); |
416 |
if ($pfx eq $_->{prefix}) { |
417 |
my $chk = Message::Markup::XML::QName::register_prefix_to_name |
418 |
($e, $pfx => $_->{name}); |
419 |
ok $chk->{success}, 'Generate pfx: '.$chk->{reason}; |
420 |
} else { |
421 |
ok $pfx eq $_->{prefix}, "Generate pfx: :$pfx: (expected :$_->{prefix}:"; |
422 |
} |
423 |
} |
424 |
|
425 |
for (@gen_pfx) { |
426 |
$e->{ns} = {} if $_->{reset}; |
427 |
my $chk = Message::Markup::XML::QName::name_to_prefix ($e, $_->{name}, |
428 |
%{$_->{opt}||{}}, |
429 |
make_new_prefix => 1); |
430 |
ok $chk->{success} && ($chk->{prefix} eq ($_->{n2p} || $_->{prefix})), |
431 |
"URI->Pfx: $chk->{prefix} (@{[$_->{n2p} || $_->{prefix}]} is expected; $chk->{reason})"; |
432 |
} |
433 |
|
434 |
for (@expand) { |
435 |
$e->{ns} = {} if $_->{reset}; |
436 |
for my $pfx (keys %{$_->{ns}||{}}) { |
437 |
Message::Markup::XML::QName::register_prefix_to_name |
438 |
($e, $pfx => $_->{ns}->{$pfx}, %{$_->{opt}||{}}); |
439 |
} |
440 |
my $chk = Message::Markup::XML::QName::qname_to_expanded_name |
441 |
($e, $_->{qname}, %{$_->{opt}||{}}); |
442 |
ok $chk->{success} && ($_->{xname}->[0] eq $chk->{name} |
443 |
&& $_->{xname}->[1] eq $chk->{local_name}), |
444 |
qq(QName->expand: <$chk->{name}> (should be <$_->{xname}->[0]>), "$chk->{local_name}" (should be "$_->{xname}->[1]") (prefix "$chk->{prefix}"; $chk->{reason})); |
445 |
} |
446 |
|
447 |
for (@expand) { |
448 |
$e->{ns} = {} if $_->{reset}; |
449 |
for my $pfx (keys %{$_->{ns}||{}}) { |
450 |
Message::Markup::XML::QName::register_prefix_to_name |
451 |
($e, $pfx => $_->{ns}->{$pfx}, %{$_->{opt}||{}}); |
452 |
} |
453 |
my $chk = Message::Markup::XML::QName::expanded_name_to_qname |
454 |
($e, $_->{xname}->[0], $_->{xname}->[1], %{$_->{opt}||{}}); |
455 |
ok $chk->{success} && ($_->{qname} eq $chk->{qname}), 'Expand->QName: '.$chk->{reason}; |
456 |
} |
457 |
|
458 |
print $e; |