XML-LibXML
view release on metacpan or search on metacpan
t/02parse.t view on Meta::CPAN
my $doc = $parser->parse_string( $goodXInclude );
isa_ok($doc, 'XML::LibXML::Document');
my $i;
eval { $i = $parser->processXIncludes($doc); };
is( $i, "1", "return value from processXIncludes == 1");
$doc = $parser->parse_string( $badXInclude );
$i= undef;
eval { $i = $parser->processXIncludes($doc); };
like($@, qr/$badfile1:3: parser error : Extra content at the end of the document/, "error parsing a bad include");
# auto expand
$parser->expand_xinclude(1);
$doc = $parser->parse_string( $goodXInclude );
isa_ok($doc, 'XML::LibXML::Document');
$doc = undef;
eval { $doc = $parser->parse_string( $badXInclude ); };
like($@, qr/$badfile1:3: parser error : Extra content at the end of the document/, "error parsing $badfile1 in include");
is($doc, undef, "no doc returned");
# some bad stuff
eval{ $parser->processXIncludes(undef); };
like($@, qr/^No document to process! at/, "Error parsing undef include");
eval{ $parser->processXIncludes("blahblah"); };
like($@, qr/^No document to process! at/, "Error parsing bogus include");
}
# 2 PUSH PARSER
{
my $pparser = XML::LibXML->new();
# 2.1 PARSING WELLFORMED DOCUMENTS
foreach my $key ( qw(single1 single2 single3 single4 single5 single6
single7 single8 single9 multiple1 multiple2 multiple3
multiple4 multiple5 multiple6 multiple7 multiple8
multiple9 multiple10 comment1 comment2 comment3
comment4 comment5 attr1 attr2 attr3
ns1 ns2 ns3 ns4 ns5 ns6 dtd1 dtd2) ) {
foreach ( @{$goodPushWF{$key}} ) {
$pparser->parse_chunk( $_ );
}
my $doc;
eval {$doc = $pparser->parse_chunk("",1); };
is($@, '', "No error parsing $key");
isa_ok($doc, 'XML::LibXML::Document', "Document came back parsing chunk: ");
}
my @good_strings = ("<foo>", "bar", "</foo>" );
my %bad_strings = (
predocend1 => ["<A>" ],
predocend2 => ["<A>", "B"],
predocend3 => ["<A>", "<C>"],
predocend4 => ["<A>", "<C/>"],
postdocend1 => ["<A/>", "<C/>"],
# use with libxml2 2.4.26: postdocend2 => ["<A/>", "B"], # libxml2 < 2.4.26 bug
postdocend3 => ["<A/>", "BB"],
badcdata => ["<A> ","<!","[CDATA[B]","</A>"],
badending1 => ["<A> ","B","</C>"],
badending2 => ["<A> ","</C>","</A>"],
);
my $parser = XML::LibXML->new;
{
for ( @good_strings ) {
$parser->parse_chunk( $_ );
}
my $doc = $parser->parse_chunk("",1);
isa_ok($doc, 'XML::LibXML::Document');
}
{
# 2.2 PARSING BROKEN DOCUMENTS
my $doc;
foreach my $key ( keys %bad_strings ) {
$doc = undef;
my $bad_chunk;
foreach ( @{$bad_strings{$key}} ) {
eval { $parser->parse_chunk( $_ );};
if ( $@ ) {
# if we won't stop here, we will lose the error :|
$bad_chunk = $_;
last;
}
}
if ( $@ ) {
isnt($@, '', "Error found parsing chunk $bad_chunk");
# $parser->parse_chunk("",1); # will cause no harm anymore, but is still needed
next;
}
eval {
$doc = $parser->parse_chunk("",1);
};
isnt($@, '', "Got an error parsing empty chunk after chunks for $key");
}
}
{
# 2.3 RECOVERING PUSH PARSER
$parser->init_push;
foreach ( "<A>", "B" ) {
$parser->push( $_);
}
my $doc;
eval {
local $SIG{'__WARN__'} = sub { };
$doc = $parser->finish_push(1);
};
isa_ok( $doc, 'XML::LibXML::Document' );
}
}
# 3 SAX PARSER
( run in 0.827 second using v1.01-cache-2.11-cpan-39bf76dae61 )