use Test::More tests => 67; use XML::Parser::LiteCopy; use Data::Dumper; my($s, $c, $e, $a); # # start, char, end # ($s, $c, $e) = (0) x 3; my $p1 = XML::Parser::LiteCopy->new(); $p1->setHandlers( Start => sub { $s++; }, Char => sub { $c++; }, End => sub { $e++; }, ); $p1->parse('Hello World!'); is($s, 1); is($c, 1); is($e, 1); # # attributes from start event # ($s, $c, $e) = (0) x 3; my %foo; my $p2 = new XML::Parser::LiteCopy Handlers => { Start => sub { shift; $s++; %foo = @_[1..$#_] if $_[0] eq 'foo'; }, Char => sub { $c++; }, End => sub { $e++; }, } ; $p2->parse('Hello cruel World!'); is($s, 3); is($c, 4); is($e, 3); is($foo{id}, 'me'); ok(defined $foo{root}); is($foo{root}, '0'); ok(defined $foo{empty}); is($foo{empty}, ''); # # Char & CDATA # sub test_chars { my @chars; my $p = new XML::Parser::LiteCopy Handlers => { Char => sub { push @chars, $_[1]; }, CData => sub { push @chars, 'CDATA:'.$_[1]; }, } ; my $in = shift; $p->parse($in); is(scalar @chars, scalar @_); is_deeply(\@chars, \@_); } &test_chars('', ()); &test_chars('', ()); &test_chars('hey', ('hey')); &test_chars('hey<', ('hey<')); &test_chars('&hey', ('&hey')); &test_chars('', ('CDATA:yo')); &test_chars('', ('CDATA: yo ')); &test_chars('', ('CDATA:foo]bar')); &test_chars('', ('CDATA:foo]]bar')); &test_chars('bar]]>', ('CDATA:foo]>bar')); &test_chars('', ('CDATA:foo]')); &test_chars('wooyay', ('woo','CDATA:foo','CDATA:bar','yay')); # # comments # sub test_comments { my @comments; my $p = new XML::Parser::LiteCopy Handlers => { Comment => sub { push @comments, $_[1]; }, } ; my $in = shift; $p->parse($in); is(scalar @comments, scalar @_); is_deeply(\@comments, \@_); } # >>> A note about comments: # An XML comment opens with a "" delimiter. An explicitly stated exception is that a double # hyphen is not permitted within the body of a comment. This rule ensures that unterminated # comments are detected if a new comment opening delimiter is encountered. There is an # additional restriction that comments cannot be terminated with the "--->" sequence, that is, # that the body of the comment cannot terminate with a hyphen &test_comments('', ()); &test_comments('', ('a')); &test_comments('', (' b ')); &test_comments('', (' c-d ')); &test_comments('', (' e- ')); &test_comments('', (' - ')); &test_comments('', ('fg','h')); &test_comments('', ('i-j')); # # processing instructions (PI) # sub test_pi { my @instructions; my $p = new XML::Parser::LiteCopy Handlers => { PI => sub { push @instructions, $_[1]; }, } ; my $in = shift; $p->parse($in); is(scalar @instructions, scalar @_); is_deeply(\@instructions, \@_); } &test_pi('', ()); &test_pi('', ('name pidata')); &test_pi('', ('xml version="1.0"? encoding="UTF-8"')); &test_pi(qq||, (qq|php\nexit;\n|)); &test_pi('', ('yay woo?')); # technically allowed... # # error conditions # $p2->setHandlers; # check for junk before eval { $p2->parse('fooHello World!') }; ok($@ =~ /^junk .+ before/); # check for junk after eval { $p2->parse('Hello World!bar') }; ok($@ =~ /^junk .+ after/); # check for non-closed tag eval { $p2->parse('Hello World!') }; ok($@ =~ /^not properly closed tag 'foo'/); # check for non properly closed tag eval { $p2->parse('Hello World!') }; ok($@ =~ /^mismatched tag 'foo'/); # check for unwanted tag eval { $p2->parse('Hello World!') }; ok($@ =~ /^multiple roots, wrong element 'bar'/); # check for string without elements eval { $p2->parse(' ') }; ok($@ =~ /^no element found/); # TODO tests # check for unclosed PI: $p2->parse(''); # check for unclosed CDATA # check for bad doctype # check for bad comments (various kinds)