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 => DEFAULT_PFX, |
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<http://uri.example/b>, |
299 |
prefix => q:b:, |
300 |
}, |
301 |
{ |
302 |
name => q<http://uri.example/bc>, |
303 |
prefix => q:bc:, |
304 |
}, |
305 |
{ |
306 |
name => q<http://uri.example/01w>, |
307 |
prefix => q:w:, |
308 |
}, |
309 |
{ |
310 |
name => q<test/0name/xmlns>, |
311 |
prefix => q:name:, |
312 |
}, |
313 |
{ |
314 |
name => q<test/0name/xmlns>, |
315 |
prefix => q:test:, |
316 |
n2p => q:name:, |
317 |
}, |
318 |
{ |
319 |
reset => 1, |
320 |
name => q<:///:04465612@&>, |
321 |
prefix => q:ns0:, |
322 |
}, |
323 |
{ |
324 |
reset => 1, |
325 |
name => NULL_URI, |
326 |
prefix => DEFAULT_PFX, |
327 |
opt => {use_prefix_default => 1}, |
328 |
}, |
329 |
{ |
330 |
reset => 0, |
331 |
name => NULL_URI, |
332 |
prefix => DEFAULT_PFX, |
333 |
opt => {use_prefix_default => 1}, |
334 |
}, |
335 |
{ |
336 |
reset => 1, |
337 |
name => NULL_URI, |
338 |
prefix => DEFAULT_PFX, |
339 |
opt => {use_prefix_default => 0}, |
340 |
}, |
341 |
); |
342 |
|
343 |
my @expand = ( |
344 |
{ |
345 |
reset => 1, |
346 |
ns => {foo => q<http://foo.test/>}, |
347 |
qname => q"foo:bar", |
348 |
xname => [q<http://foo.test/> => q:bar:], |
349 |
}, |
350 |
{ |
351 |
reset => 0, |
352 |
qname => q"foo:bar", |
353 |
xname => [q<http://foo.test/> => q:bar:], |
354 |
}, |
355 |
{ |
356 |
reset => 0, |
357 |
qname => q"bar", |
358 |
xname => [NULL_URI, q:bar:], |
359 |
opt => {use_prefix_default => 1, use_name_null => 1}, |
360 |
}, |
361 |
); |
362 |
|
363 |
Test::Simple->import (tests => scalar (@reg_p2n) |
364 |
+ scalar (@get_p2n) * 2 |
365 |
+ scalar (@qname) * 2 |
366 |
+ scalar (@gen_pfx) * 2 |
367 |
+ scalar (@expand) * 2); |
368 |
eval q{ |
369 |
sub ok ($;$) { |
370 |
my ($cond, $desc) = @_; |
371 |
if ($cond) { |
372 |
Test::Simple::ok (1); |
373 |
} else { |
374 |
Test::Simple::ok (0, $desc); |
375 |
} |
376 |
}}; |
377 |
|
378 |
for (@reg_p2n) { |
379 |
my $chk = Message::Markup::XML::QName::register_prefix_to_name |
380 |
($e, $_->{prefix} => $_->{name}, %{$_->{opt}||{}}); |
381 |
ok ($chk->{success} == $_->{result}, 'Register pfx->URI: '.$chk->{reason}); |
382 |
} |
383 |
|
384 |
for (@get_p2n) { |
385 |
$e->{ns} = {}; |
386 |
my $chk = Message::Markup::XML::QName::register_prefix_to_name |
387 |
($e, $_->{prefix} => $_->{name}, %{$_->{opt}||{}}) |
388 |
unless $_->{opt}->{___dont_register}; |
389 |
ok (Message::Markup::XML::QName::prefix_to_name ($e, $_->{prefix}, |
390 |
%{$_->{opt}||{}}) |
391 |
->{name} |
392 |
eq ($_->{result} ? ($_->{result_name} || $_->{name}) : undef), |
393 |
":$_->{prefix}: => <@{[$_->{result} ? ($_->{result_name} || $_->{name}) : undef]}>"); |
394 |
ok (Message::Markup::XML::QName::name_to_prefix ($e, $_->{name}, |
395 |
%{$_->{opt}||{}}) |
396 |
->{prefix} |
397 |
eq ($_->{result} ? ($_->{result_prefix} || $_->{prefix}) : undef), |
398 |
"<$_->{name}> => :@{[$_->{result} ? ($_->{result_prefix} || $_->{prefix}) : undef]}:"); |
399 |
} |
400 |
|
401 |
|
402 |
for (@qname) { |
403 |
my $chk = Message::Markup::XML::QName::split_qname ($_->{qname}, |
404 |
%{$_->{opt}||{}}); |
405 |
if ($_->{result}) { |
406 |
ok $chk->{prefix}.':'.($chk->{local_name_star} ? '*' : $chk->{local_name}) |
407 |
eq $_->{prefix} . ':' . $_->{lname}, |
408 |
"Split 1: $_->{qname} => $chk->{prefix}:$chk->{local_name}($chk->{local_name_star})"; |
409 |
} else { |
410 |
ok $chk->{success} == $_->{result}, |
411 |
"Split 0: $_->{qname} => $chk->{prefix}:$chk->{local_name}($chk->{local_name_star})"; |
412 |
} |
413 |
|
414 |
$chk = Message::Markup::XML::QName::join_qname |
415 |
($_->{prefix}, $_->{lname}, %{$_->{opt}||{}}); |
416 |
if (defined $_->{join_result} ? $_->{join_result} : $_->{result}) { |
417 |
ok $chk->{qname} eq ($_->{join_qname} || $_->{qname}), qq(Join 1: "$_->{prefix}":"$_->{lname}" => "$chk->{qname}" ("@{[$_->{join_qname} || $_->{qname}]}" expected)); |
418 |
} else { |
419 |
ok $chk->{success} == ((defined $_->{join_result} and $_->{join_result}) or $_->{result}), |
420 |
"Join 0: $chk->{success}; $_->{prefix}, $_->{lname} => $chk->{qname}"; |
421 |
} |
422 |
} |
423 |
|
424 |
for (@gen_pfx) { |
425 |
$e->{ns} = {} if $_->{reset}; |
426 |
my $pfx = Message::Markup::XML::QName::generate_prefix ($e, $_->{name}, |
427 |
%{$_->{opt}||{}}); |
428 |
if ($pfx eq $_->{prefix}) { |
429 |
my $chk = Message::Markup::XML::QName::register_prefix_to_name |
430 |
($e, $pfx => $_->{name}); |
431 |
ok $chk->{success}, 'Generate pfx: '.$chk->{reason}; |
432 |
} else { |
433 |
ok $pfx eq $_->{prefix}, "Generate pfx: :$pfx: (expected :$_->{prefix}:"; |
434 |
} |
435 |
} |
436 |
|
437 |
for (@gen_pfx) { |
438 |
$e->{ns} = {} if $_->{reset}; |
439 |
my $chk = Message::Markup::XML::QName::name_to_prefix ($e, $_->{name}, |
440 |
%{$_->{opt}||{}}, |
441 |
make_new_prefix => 1); |
442 |
ok $chk->{success} && ($chk->{prefix} eq ($_->{n2p} || $_->{prefix})), |
443 |
"URI->Pfx: $chk->{prefix} (@{[$_->{n2p} || $_->{prefix}]} is expected; $chk->{reason})"; |
444 |
} |
445 |
|
446 |
for (@expand) { |
447 |
$e->{ns} = {} if $_->{reset}; |
448 |
for my $pfx (keys %{$_->{ns}||{}}) { |
449 |
Message::Markup::XML::QName::register_prefix_to_name |
450 |
($e, $pfx => $_->{ns}->{$pfx}, %{$_->{opt}||{}}); |
451 |
} |
452 |
my $chk = Message::Markup::XML::QName::qname_to_expanded_name |
453 |
($e, $_->{qname}, %{$_->{opt}||{}}); |
454 |
ok $chk->{success} && ($_->{xname}->[0] eq $chk->{name} |
455 |
&& $_->{xname}->[1] eq $chk->{local_name}), |
456 |
qq(QName->expand: <$chk->{name}> (should be <$_->{xname}->[0]>), "$chk->{local_name}" (should be "$_->{xname}->[1]") (prefix "$chk->{prefix}"; $chk->{reason})); |
457 |
} |
458 |
|
459 |
for (@expand) { |
460 |
$e->{ns} = {} if $_->{reset}; |
461 |
for my $pfx (keys %{$_->{ns}||{}}) { |
462 |
Message::Markup::XML::QName::register_prefix_to_name |
463 |
($e, $pfx => $_->{ns}->{$pfx}, %{$_->{opt}||{}}); |
464 |
} |
465 |
my $chk = Message::Markup::XML::QName::expanded_name_to_qname |
466 |
($e, $_->{xname}->[0], $_->{xname}->[1], %{$_->{opt}||{}}); |
467 |
ok $chk->{success} && ($_->{qname} eq $chk->{qname}), 'Expand->QName: '.$chk->{reason}; |
468 |
} |
469 |
|
470 |
print $e; |