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
|
#!/usr/bin/env perl
use warnings;
use strict;
use lib 'lib','t';
use TestTools;
use XML::Compile::Schema;
use XML::Compile::Tester;
use Math::BigFloat;
use Test::More tests => 175;
use utf8;
my $schema = XML::Compile::Schema->new( <<__SCHEMA__ );
<schema xmlns="$SchemaNS"
targetNamespace="$TestNS">
<element name="test1" type="int" />
<element name="test2" type="boolean" />
<element name="test3" type="float" />
<element name="test4" type="NMTOKENS" />
<element name="test5" type="positiveInteger" />
<element name="test6" type="base64Binary" />
<element name="test7" type="dateTime" />
<element name="test8" type="duration" />
<element name="test9" type="hexBinary" />
<element name="testA" type="string" />
</schema>
__SCHEMA__
ok(defined $schema);
set_compile_defaults
elements_qualified => 'NONE';
###
### int
###
test_rw($schema, test1 => '<test1>0</test1>', 0);
test_rw($schema, test1 => '<test1>3</test1>', 3);
###
### Boolean
###
test_rw($schema, test2 => '<test2>0</test2>', 0);
test_rw($schema, test2 => '<test2>false</test2>', 0
, '<test2>false</test2>', 'false');
test_rw($schema, test2 => '<test2>1</test2>', 1);
test_rw($schema, test2 => '<test2>true</test2>', 1
, '<test2>true</test2>', 'true');
###
### Float
###
test_rw($schema, test3 => '<test3>0</test3>', 0);
test_rw($schema, test3 => '<test3>9</test3>', 9);
test_rw($schema, test3 => '<test3>INF</test3>', Math::BigFloat->binf);
test_rw($schema, test3 => '<test3>-INF</test3>', Math::BigFloat->binf('-'));
test_rw($schema, test3 => '<test3>NaN</test3>', Math::BigFloat->bnan);
my $error = error_r($schema, test3 => '<test3></test3>');
is($error, "illegal value `' for type {http://www.w3.org/2001/XMLSchema}float at {http://test-types}test3");
$error = error_w($schema, test3 => 'aap');
is($error, "illegal value `aap' for type {http://www.w3.org/2001/XMLSchema}float at {http://test-types}test3");
$error = error_w($schema, test3 => '');
is($error, "illegal value `' for type {http://www.w3.org/2001/XMLSchema}float at {http://test-types}test3");
###
test_rw($schema, test4 => '<test4>A bc D</test4>', [ qw/A bc D/ ]);
###
### Integers
###
test_rw($schema, test5 => '<test5>4320239</test5>', 4320239);
###
### Base64Binary
###
test_rw($schema,test6 => '<test6>SGVsbG8sIFdvcmxkIQ==</test6>','Hello, World!');
$error = error_w($schema, test6 => "€");
is($error, 'use Encode::encode() for base64Binary field at {http://test-types}test6');
###
### dateTime validation
###
my $d = '2010-02-11T08:52:47';
test_rw($schema, test7 => "<test7>$d</test7>", $d);
###
### duration validation
###
my $e = 'PT5M';
test_rw($schema, test8 => "<test8>$e</test8>", $e);
###
### hexBinary
###
my $f = pack "N", 0x12345678;
test_rw($schema, test9 => "<test9>12345678</test9>", $f);
###
### string
###
test_rw($schema, testA => "<testA>abc</testA>", 'abc');
my $r1 = reader_create $schema, "CDATA reader" => "{$TestNS}testA";
my $cd = '<testA><![CDATA[abc]]></testA>';
my $r1a = $r1->($cd);
cmp_ok($r1a, 'eq', 'abc');
my $cdata = XML::LibXML::CDATASection->new('abc');
is('<![CDATA[abc]]>', $cdata->toString(1));
#XXX MO 20120815: XML::LibXML crashed on cleanup of double refs to CDATA
# object (as done in clone of test_rw). Other XML::LibXML objects do not
# crash on this.
#test_rw($schema, testA => $cd, 'abc', $cd, $cdata);
test_rw($schema, testA => '<testA></testA>', '');
|