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
|
use warnings;
use strict;
use Encode qw(decode);
use IO::File ();
use Params::Classify qw(scalar_class);
use Scalar::Util qw(blessed reftype);
use t::ErrorCases qw(COUNT_error_text test_error_text);
use utf8 ();
use Test::More tests => 1 + 2*504 + 2 + COUNT_error_text*6 + 5;
BEGIN { $SIG{__WARN__} = sub { die "WARNING: $_[0]" }; }
BEGIN { use_ok "XML::Easy::Text", qw(
xml10_read_content_object xml10_read_content_twine
xml10_read_element
xml10_read_document
xml10_read_extparsedent_object xml10_read_extparsedent_twine
); }
sub deep_match($$);
sub deep_match($$) {
my($a, $b) = @_;
my $ac = scalar_class($a);
my $bc = scalar_class($b);
return 0 unless $ac eq $bc;
if($ac eq "STRING") {
return $a eq $b;
} elsif($ac eq "BLESSED" || $ac eq "REF") {
return 0 if $ac eq "BLESSED" && blessed($a) ne blessed($b);
my $at = reftype($a);
my $bt = reftype($b);
return 0 unless $at eq $bt;
if($at =~ /\A(?:REF|SCALAR|LVALUE|GLOB)\z/) {
return deep_match($$a, $$b);
} elsif($at eq "ARRAY") {
return 0 unless @$a == @$b;
foreach(my $i = @$a; $i--; ) {
return 0 unless deep_match($a->[$i], $b->[$i]);
}
return 1;
} elsif($at eq "HASH") {
my @keys = keys %$a;
foreach(@keys) {
return 0 unless exists $b->{$_};
return 0 unless deep_match($a->{$_}, $b->{$_});
}
foreach(keys %$b) {
return 0 unless exists $a->{$_};
}
return 1;
} else {
return 1;
}
} else {
return 1;
}
}
sub upgraded($) {
my($str) = @_;
utf8::upgrade($str);
return $str;
}
sub downgraded($) {
my($str) = @_;
utf8::downgrade($str, 1);
return $str;
}
my %reader = (
c => \&xml10_read_content_object,
e => \&xml10_read_element,
d => \&xml10_read_document,
x => \&xml10_read_extparsedent_object,
);
sub try_read($$) {
my $result = eval { $reader{$_[0]}->($_[1]) };
return $@ ne "" ? [ "error", $@ ] : [ "ok", $result ];
}
my $data_in = IO::File->new("t/read.data", "r") or die;
my $line = $data_in->getline;
while(1) {
$line =~ /\A###([a-z])?-?\n\z/ or die;
last unless defined $1;
my $prod = $1;
$line = $data_in->getline;
last unless defined $line;
my $input = "";
while($line ne "#\n") {
die if $line =~ /\A###/;
$input .= $line;
$line = $data_in->getline;
die unless defined $line;
}
die if $input eq "";
chomp($input);
$input =~ tr/~/\r/;
$input =~ s/\$\((.*?)\)/$1 x 40000/seg;
$input =~ s/\$\{(.*?)\}/$1 x 32764/seg;
$input = decode("UTF-8", $input);
my $correct = "";
while(1) {
$line = $data_in->getline;
die unless defined $line;
last if $line =~ /\A###/;
$correct .= $line;
}
chomp $correct;
$correct = $correct =~ /\A[:'A-Za-z ]+\z/ ?
[ "error", "$correct\n" ] :
[ "ok", do { no warnings "utf8"; eval($correct) } ];
ok deep_match(try_read($prod, upgraded($input)), $correct);
ok deep_match(try_read($prod, downgraded($input)), $correct);
}
is_deeply
xml10_read_content_object("foo<q>bar</q>baz")->twine,
xml10_read_content_twine("foo<q>bar</q>baz");
is_deeply
xml10_read_extparsedent_object("foo<q>bar</q>baz")->twine,
xml10_read_extparsedent_twine("foo<q>bar</q>baz");
foreach my $func (
(values %reader),
\&xml10_read_content_twine,
\&xml10_read_extparsedent_twine,
) {
test_error_text($func);
}
ok defined(&{"XML::Easy::Text::xml10_read_content"});
ok \&{"XML::Easy::Text::xml10_read_content"} == \&{"XML::Easy::Text::xml10_read_content_twine"};
ok defined(&{"XML::Easy::Text::xml10_read_extparsedent"});
ok \&{"XML::Easy::Text::xml10_read_extparsedent"} == \&{"XML::Easy::Text::xml10_read_extparsedent_twine"};
use_ok "XML::Easy::Text", qw(xml10_read_content xml10_read_extparsedent);
1;
|