1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
#!/usr/bin/env perl
# test facets
use warnings;
use strict;
use lib 'lib','t';
use TestTools;
use XML::Compile::Schema;
use XML::Compile::Tester;
use XML::Compile::Util qw/pack_type/;
use Test::More tests => 392;
set_compile_defaults
elements_qualified => 'NONE';
my $schema = XML::Compile::Schema->new( <<__SCHEMA__ );
<schema targetNamespace="$TestNS"
xmlns="$SchemaNS"
xmlns:me="$TestNS"
elementFormDefault="qualified">
<simpleType name="t1">
<restriction base="int" />
</simpleType>
<simpleType name="t2">
<restriction base="int">
<maxInclusive value="42" />
<minInclusive value="12" />
</restriction>
</simpleType>
<element name="test1" type="me:t1" />
<element name="test2" type="me:t2" />
<element name="test3">
<simpleType>
<restriction base="int">
<maxExclusive value="45" />
<minExclusive value="13" />
</restriction>
</simpleType>
</element>
<element name="test4">
<simpleType>
<restriction base="string">
<length value="3" />
</restriction>
</simpleType>
</element>
<element name="test5">
<simpleType>
<restriction base="string">
<whiteSpace value="preserve" />
</restriction>
</simpleType>
</element>
<element name="test6">
<simpleType>
<restriction base="string">
<whiteSpace value="replace" />
</restriction>
</simpleType>
</element>
<element name="test7">
<simpleType>
<restriction base="string">
<whiteSpace value="collapse" />
</restriction>
</simpleType>
</element>
<element name="test8">
<simpleType>
<restriction base="string">
<enumeration value="one" />
<enumeration value="two" />
</restriction>
</simpleType>
</element>
<element name="test9">
<simpleType>
<restriction base="long">
<totalDigits value="4" />
</restriction>
</simpleType>
</element>
<element name="test10">
<simpleType>
<restriction base="float">
<totalDigits value="4" />
</restriction>
</simpleType>
</element>
<element name="test11">
<complexType>
<sequence>
<element name="t11" type="me:t2" />
</sequence>
</complexType>
</element>
<!-- rt.cpan.org#39224, order of pattern match and value decoding -->
<simpleType name="DecimalType">
<restriction base="decimal">
<pattern value="[0-9]{1,13}\\.[0-9]{0,2}"/>
</restriction>
</simpleType>
<element name="test12" type="me:DecimalType" />
<element name="test13">
<simpleType>
<restriction base="base64Binary">
<length value="5"/>
</restriction>
</simpleType>
</element>
<!-- rt.cpan.org#62237, enumeration of qname -->
<element name="test14">
<simpleType>
<restriction base="QName">
<enumeration value="me:DataEncodingUnknown"/>
<enumeration value="me:MustUnderstand"/>
<enumeration value="me:Receiver"/>
<enumeration value="me:Sender"/>
<enumeration value="me:VersionMismatch"/>
</restriction>
</simpleType>
</element>
<!-- from KML 2.2 -->
<element name="test15" type="me:colorType" />
<simpleType name="colorType">
<annotation>
<documentation><![CDATA[
aabbggrr
ffffffff: opaque white
ff000000: opaque black
]]></documentation>
</annotation>
<restriction base="hexBinary">
<length value="4"/>
</restriction>
</simpleType>
<!-- rt.cpan.org#63464, combined totalDigits and fracDigits -->
<element name="test16">
<simpleType>
<restriction base="decimal">
<fractionDigits value="2" />
</restriction>
</simpleType>
</element>
<element name="test17">
<simpleType>
<restriction base="decimal">
<totalDigits value="5" />
<fractionDigits value="2" />
</restriction>
</simpleType>
</element>
<!-- rt.cpan.org#121946 canonicalization and pattern -->
<element name="test18">
<simpleType>
<restriction base="integer">
<pattern value="-?[0-9]{4,4}" />
</restriction>
</simpleType>
</element>
<!-- reported by Wesley Schengle -->
<element name="test19">
<simpleType name="test19">
<restriction base="integer">
<enumeration value="03" />
<enumeration value="14" />
</restriction>
</simpleType>
</element>
</schema>
__SCHEMA__
ok(defined $schema);
#<minInclusive value="0"/>
#<maxInclusive value="99"/>
##
### Integers
##
test_rw($schema, test1 => <<__XML, 12);
<test1>12</test1>
__XML
test_rw($schema, test2 => <<__XML, 13);
<test2>13</test2>
__XML
test_rw($schema, test2 => <<__XML, 42);
<test2>42</test2>
__XML
my $error = error_r($schema, test2 => <<__XML);
<test2>43</test2>
__XML
is($error, "too large inclusive 43, max 42 at {http://test-types}test2#facet");
$error = error_w($schema, test2 => 43);
is($error, "too large inclusive 43, max 42 at {http://test-types}test2#facet");
$error = error_r($schema, test2 => <<__XML);
<test2>11</test2>
__XML
is($error, "too small inclusive 11, min 12 at {http://test-types}test2#facet");
$error = error_w($schema, test2 => 11);
is($error, "too small inclusive 11, min 12 at {http://test-types}test2#facet");
test_rw($schema, "test3" => <<__XML, 44);
<test3>44</test3>
__XML
$error = error_r($schema, test3 => <<__XML);
<test3>45</test3>
__XML
is($error, "too large exclusive 45, smaller 45 at {http://test-types}test3#facet");
$error = error_w($schema, test3 => 45);
is($error, "too large exclusive 45, smaller 45 at {http://test-types}test3#facet");
$error = error_r($schema, test3 => <<__XML);
<test3>13</test3>
__XML
is($error, "too small exclusive 13, larger 13 at {http://test-types}test3#facet");
$error = error_w($schema, test3 => 13);
is($error, "too small exclusive 13, larger 13 at {http://test-types}test3#facet");
##
### strings
##
test_rw($schema, "test4" => <<__XML, "aap");
<test4>aap</test4>
__XML
$error = error_r($schema, test4 => <<__XML);
<test4>noot</test4>
__XML
is($error, "string `noot' does not have required length 3 but 4 at {http://test-types}test4\#facet");
$error = error_w($schema, test4 => 'noot');
is($error, "string `noot' does not have required length 3 but 4 at {http://test-types}test4\#facet");
$error = error_r($schema, test4 => <<__XML);
<test4>ik</test4>
__XML
is($error, "string `ik' does not have required length 3 but 2 at {http://test-types}test4#facet");
$error = error_w($schema, test4 => "ik");
is($error, "string `ik' does not have required length 3 but 2 at {http://test-types}test4#facet");
test_rw($schema, "test5" => <<__XML, "\ \ \t\n\tmies \t");
<test5>\ \ \t
\tmies \t</test5>
__XML
test_rw($schema, "test6" => <<__XML, " mies ", <<__XML, "\ \ \t \tmies \t");
<test6>\ \ \t
\tmies \t</test6>
__XML
<test6> mies </test6>
__XML
test_rw($schema, "test7" => <<__XML, 'mies', <<__XML, "\ \ \t \tmies \t");
<test7>\ \ \t
\tmies \t</test7>
__XML
<test7>mies</test7>
__XML
test_rw($schema, "test8" => <<__XML, 'one');
<test8>one</test8>
__XML
test_rw($schema, "test8" => <<__XML, 'two');
<test8>two</test8>
__XML
$error = error_r($schema, test8 => <<__XML);
<test8>three</test8>
__XML
is($error, "invalid enumerate `three' at {http://test-types}test8#facet");
$error = error_r($schema, test8 => <<__XML);
<test8/>
__XML
is($error, "invalid enumerate `' at {http://test-types}test8#facet");
### test9 (bug reported by Gert Doering)
set_compile_defaults
sloppy_integers => 1
, sloppy_floats => 1
, elements_qualified => 'NONE';
test_rw($schema, test9 => '<test9>0</test9>', 0);
test_rw($schema, test9 => '<test9>12</test9>', 12);
test_rw($schema, test9 => '<test9>123</test9>', 123);
test_rw($schema, test9 => '<test9>1234</test9>', 1234);
$error = error_w($schema, test9 => 12345);
is($error, 'decimal too long, got 5 digits max 4 at {http://test-types}test9#facet');
$error = error_r($schema, test9 => '<test9>12345</test9>');
is($error, 'decimal too long, got 5 digits max 4 at {http://test-types}test9#facet');
### test10 (same bug reported by Gert Doering)
test_rw($schema, test10 => '<test10>0</test10>', 0);
test_rw($schema, test10 => '<test10>1.2</test10>', 1.2);
test_rw($schema, test10 => '<test10>1.23</test10>', 1.23);
test_rw($schema, test10 => '<test10>12.3</test10>', 12.3);
test_rw($schema, test10 => '<test10>1.234</test10>', 1.234);
test_rw($schema, test10 => '<test10>12.34</test10>', 12.34);
test_rw($schema, test10 => '<test10>123.4</test10>', 123.4);
test_rw($schema, test10 => '<test10>1234</test10>', 1234);
### test11 (from bug reported by Allan Wind)
$error = error_w($schema, test11 => {t11 => 3});
is($error, 'too small inclusive 3, min 12 at {http://test-types}test11/t11#facet');
### test12 rt.cpan.org#39224
test_rw($schema, test12 => '<test12>1.12</test12>', '1.12');
test_rw($schema, test12 => '<test12>1.10</test12>', '1.10');
test_rw($schema, test12 => '<test12>1.00</test12>', '1.00');
test_rw($schema, test12 => '<test12>1.2</test12>', '1.2');
test_rw($schema, test12 => '<test12>1.</test12>', '1.');
$error = error_r($schema, test12 => '<test12>1</test12>');
like($error, qr/^string \`1' does not match pattern /);
# dot problem with regex '.'
$error = error_r($schema, test12 => '<test12>42</test12>');
like($error, qr/^string \`42' does not match pattern /);
### test13 length on base64
test_rw($schema, test13 => '<test13>YWJjZGU=</test13>', 'abcde');
$error = error_r($schema, test13 => '<test13>YWJjYWJjZGU=</test13>');
is($error, "string `abcabcde' does not have required length 5 but 8 at {http://test-types}test13#facet");
$error = error_w($schema, test13 => 'abcdef');
is($error, "string `abcdef' does not have required length 5 but 6 at {http://test-types}test13#facet");
### test14 enumeration of qnames [Aleksey Mashanov]
set_compile_defaults
include_namespaces => 1
, use_default_namespace => 0
, prefixes => [ a => $TestNS ];
test_rw($schema, test14 => qq{<a:test14 xmlns:a="$TestNS">a:Sender</a:test14>}
, "{$TestNS}Sender");
### test15 length of hexBinary
test_rw($schema, test15 => qq{<a:test15 xmlns:a="$TestNS">DEADBEEF</a:test15>},
pack('N', 0xdeadbeef));
$error = error_r($schema, test15 => qq{<a:test15 xmlns:a="$TestNS">345678</a:test15>});
is($error, "string `4Vx' does not have required length 4 but 3 at a:test15#facet");
$error = error_w($schema, test15 => 'abc');
is($error, "string `abc' does not have required length 4 but 3 at a:test15#facet");
$error = error_w($schema, test15 => 'anything');
is($error, "string `anything' does not have required length 4 but 8 at a:test15#facet");
### test16 fracDigits
# max 2 digits
my $t16 = pack_type $TestNS, 'test16';
my $r16 = reader_create $schema, "frac 2r", $t16;
is($r16->(qq{<test16 xmlns="$TestNS">2.14</test16>}), "2.14");
is($r16->(qq{<test16 xmlns="$TestNS">3.1</test16>}), "3.1");
is($r16->(qq{<test16 xmlns="$TestNS">3.14152</test16>}), "3.14");
my $w16 = writer_create $schema, 'frac 2w', $t16;
my $x16 = writer_test $w16, '3.141526';
compare_xml($x16, qq{<a:test16 xmlns:a="$TestNS">3.14</a:test16>});
### test17, totalFracDigits [mimon-cz]
my $t17 = pack_type $TestNS, 'test17';
my $r17 = reader_create $schema, "total 5, frac 2r", $t17;
is($r17->(qq{<test17 xmlns="$TestNS">2.14</test17>}), "2.14");
is($r17->(qq{<test17 xmlns="$TestNS">3.1</test17>}), "3.1");
$error = error_r($schema, test17 => qq{<test17 xmlns="$TestNS">3.14152</test17>});
is($error, 'fractional part for 3.14152 too long, got 5 digits max 2 at a:test17#facet');
my $w17 = writer_create $schema, 'total 5, frac 2w', $t17;
my $x17 = writer_test $w17, '3.14';
compare_xml($x17, qq{<a:test17 xmlns:a="$TestNS">3.14</a:test17>});
### test18 canonicalization and patterns
my $t18 = pack_type $TestNS, 'test18';
my $r18 = reader_create $schema, "canon patterns", $t18;
is($r18->(qq{<test18 xmlns="$TestNS">0519</test18>}), '0519');
my $w18 = writer_create $schema, 'canon patterns', $t18;
my $x18 = writer_test $w18, '0519';
compare_xml($x18, qq{<a:test18 xmlns:a="$TestNS">0519</a:test18>});
### test19 canonicalization and patterns, multiple facets
my $t19 = pack_type $TestNS, 'test19';
my $r19 = reader_create $schema, "canon patterns multi", $t19;
is($r19->(qq{<test19 xmlns="$TestNS">14</test19>}), '14');
is($r19->(qq{<test19 xmlns="$TestNS">03</test19>}), '03');
$error = error_r($schema, test19 => qq{<test19 xmlns="$TestNS">124</test19>});
is($error, "invalid enumerate `124' at a:test19#facet");
my $w19 = writer_create $schema, 'canon patterns multi', $t19;
my $x19a = writer_test $w19, '14';
compare_xml($x19a, qq{<a:test19 xmlns:a="$TestNS">14</a:test19>});
my $x19b = writer_test $w19, '03';
compare_xml($x19b, qq{<a:test19 xmlns:a="$TestNS">03</a:test19>});
|