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
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More qw(no_plan);
use XML::Validator::Schema;
use XML::Validator::Schema::ElementNode;
use XML::Validator::Schema::ModelNode;
# create test elements
my $foo = XML::Validator::Schema::ElementNode->parse(
{ Attributes => { '{}name' => { Value => 'foo' } } });
my $bar = XML::Validator::Schema::ElementNode->parse(
{ Attributes => { '{}name' => { Value => 'bar' } } });
my $baz = XML::Validator::Schema::ElementNode->parse(
{ Attributes => { '{}name' => { Value => 'baz' } } });
# foo contains a sequence of (bar, baz)
my $sequence = XML::Validator::Schema::ModelNode->parse({ LocalName => 'sequence' });
$foo->add_daughter($sequence);
$sequence->add_daughters($bar, $baz);
is($sequence->daughters(), 2);
# compile sequence into $foo
$sequence->compile();
is($foo->daughters, 2);
is(($foo->daughters())[0]->name, $bar->name);
is(($foo->daughters())[1]->name, $baz->name);
isa_ok($foo->{model}, 'XML::Validator::Schema::ModelNode');
# check the description
is($foo->{model}->{description}, "(bar,baz)");
# check a sequence of nodes against the model
eval { $foo->{model}->check_final_model('', ['bar', 'baz']) };
is($@, "");
eval { $foo->{model}->check_model('', ['bar']) };
is($@, "");
eval { $foo->{model}->check_final_model('', []) };
like($@, qr/do not match content model/);
eval { $foo->{model}->check_model('', ['baz']) };
like($@, qr/does not match content model/);
# foo contains a choice of (bar|baz)
my $choice = XML::Validator::Schema::ModelNode->parse({ LocalName => 'choice' });
$foo->clear_daughters();
$foo->{model} = undef;
$foo->add_daughter($choice);
$choice->add_daughters($baz, $bar);
is($choice->daughters(), 2);
# compile model into $foo
$choice->compile();
is($foo->daughters, 2);
is(($foo->daughters())[0]->name, $baz->name);
is(($foo->daughters())[1]->name, $bar->name);
isa_ok($foo->{model}, 'XML::Validator::Schema::ModelNode');
# check the description
is($foo->{model}->{description}, "(baz|bar)");
# check a sequence of nodes against the model
eval { $foo->{model}->check_final_model('', ['bar']) };
is($@, "");
eval { $foo->{model}->check_model('', ['baz']) };
is($@, "");
eval { $foo->{model}->check_final_model('', []) };
like($@, qr/do not match content model/);
eval { $foo->{model}->check_model('', ['bar', 'baz']) };
like($@, qr/does not match content model/);
# foo contains an 'all' of (bar&baz)
my $all = XML::Validator::Schema::ModelNode->parse({ LocalName => 'all' });
$foo->clear_daughters();
$foo->{model} = undef;
$foo->add_daughter($all);
$all->add_daughters($bar, $baz);
is($all->daughters(), 2);
# compile model into $foo
$all->compile();
is($foo->daughters, 2);
is(($foo->daughters())[0]->name, $bar->name);
is(($foo->daughters())[1]->name, $baz->name);
isa_ok($foo->{model}, 'XML::Validator::Schema::ModelNode');
# check the description
is($foo->{model}->{description}, "(bar&baz)");
# check a sequence of nodes against the model
eval { $foo->{model}->check_final_model('', ['bar', 'baz']) };
is($@, "");
eval { $foo->{model}->check_final_model('', ['baz', 'bar']) };
is($@, "");
eval { $foo->{model}->check_final_model('', []) };
like($@, qr/do not match content model/);
my $bang = XML::Validator::Schema::ElementNode->parse(
{ Attributes => { '{}name' => { Value => 'bang' } } });
my $bop = XML::Validator::Schema::ElementNode->parse(
{ Attributes => { '{}name' => { Value => 'bop' } } });
# foo contains a sequence with a choice of (bar,(bang|bop),baz)
$sequence = XML::Validator::Schema::ModelNode->parse({ LocalName => 'sequence' });
$foo->clear_daughters();
$foo->{model} = undef;
$foo->add_daughter($sequence);
$choice = XML::Validator::Schema::ModelNode->parse({ LocalName => 'choice' });
$choice->add_daughters($bang, $bop);
$sequence->add_daughters($bar, $choice, $baz);
is($sequence->daughters(), 3);
# compile sequence into $foo
$sequence->compile();
# all daughters should end up in $foo
is($foo->daughters, 4);
# check the description
is($foo->{model}->{description}, "(bar,(bang|bop),baz)");
# check a sequence of nodes against the model
eval { $foo->{model}->check_final_model('', ['bar', 'bang', 'baz']) };
is($@, "");
eval { $foo->{model}->check_final_model('', ['bar', 'bop', 'baz']) };
is($@, "");
eval { $foo->{model}->check_model('', ['bar']) };
is($@, "");
eval { $foo->{model}->check_model('', ['bar', 'bang']) };
is($@, "");
eval { $foo->{model}->check_final_model('', ['bar', 'bang', 'bop', 'baz']) };
like($@, qr/do not match content model/);
eval { $foo->{model}->check_model('', ['baz']) };
like($@, qr/does not match content model/);
|