use strict;
use warnings;
use utf8;
use Encode qw(encode);
use Test::More 0.98;
use XML::Hash::XS qw(xml2hash);
ok(!XML::Hash::XS->can('new_parser'), 'legacy converter factory is not exposed');
my @documents = (
[
'markup boundaries',
'' .
'' .
'- q]]>
',
],
[
'single quoted attribute and references',
q{- AB
},
],
[
'UTF-8 byte boundaries',
encode('UTF-8', qq{Привет 😀}),
],
);
for my $case (@documents) {
my ($name, $xml) = @$case;
my $expected = xml2hash($xml, keep_root => 1);
subtest $name => sub {
for my $at (0 .. length $xml) {
my $parser = XML::Hash::XS::Parser->new(keep_root => 1);
$parser->feed(substr($xml, 0, $at));
$parser->feed('');
$parser->feed(substr($xml, $at));
is_deeply($parser->finish, $expected, "split at byte $at");
}
};
}
{
my $chunk = 'valuenew(keep_root => 1);
$parser->feed($chunk);
# The parser must own the unfinished suffix after feed() returns.
substr($chunk, 0, length($chunk), 'x' x length($chunk));
$parser->feed('m attr="ok">tail');
is_deeply(
$parser->finish,
{ root => { completed => 'value', item => { attr => 'ok', content => 'tail' } } },
'unfinished suffix survives mutation of the source scalar',
);
}
{
my $value = '';
my $parser = XML::Hash::XS::Parser->new(keep_root => 1);
$parser->feed('- ok
');
is_deeply(
$parser->finish,
{ root => { item => { attr => $value, content => 'ok' } } },
'retained suffix remains valid across buffer reallocations',
);
}
{
for my $iteration (1 .. 2000) {
my $parser = XML::Hash::XS::Parser->new;
$parser->feed('