#!perl -w # # $Id: xml_parser_func.t,v 1.3 2000/11/10 02:19:17 kmacleod Exp $ # use strict; use Test; my $xml = <<'EOF;'; text5 EOF; my $num_tests = 22; BEGIN { plan tests => 29 } package MyHandler; use vars qw{ $xmlns_ns }; $xmlns_ns = "http://www.w3.org/2000/xmlns/"; sub new { my $type = shift; return bless {}, $type; } sub start_document { my ($self, $document) = @_; if ( $document->{NodeType} eq 'document' ) { $self->{Tests}[21] = 1; } else { $self->{Fail_start_document} = 1; } } sub end_document { my ($self, $document) = @_; if ( $document->{NodeType} eq 'document' ) { $self->{Tests}[22] = 1; } else { $self->{Fail_end_document} = 1; } } sub start_element { my ($self, $element) = @_; if ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el1' and !defined($element->{NamespaceURI}) and !defined($element->{Prefix}) and $element->{LocalName} eq 'el1' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{attr1}{Name} eq 'attr1' and $element->{Attributes}{attr1}{Value} eq 'text1' and !defined($element->{Attributes}{attr1}{Prefix}) and !defined($element->{Attributes}{attr1}{NamespaceURI}) ) { $self->{Tests}[1] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el2' and $element->{NamespaceURI} eq 'uri:ns1' and !defined($element->{Prefix}) and $element->{LocalName} eq 'el2' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{xmlns}{Name} eq 'xmlns' and $element->{Attributes}{xmlns}{Value} eq 'uri:ns1' and !defined($element->{Attributes}{xmlns}{Prefix}) and !defined($element->{Attributes}{xmlns}{NamespaceURI}) ) { $self->{Tests}[2] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el3' and $element->{NamespaceURI} eq 'uri:ns1' and !defined($element->{Prefix}) and $element->{LocalName} eq 'el3' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{['uri:ns1', 'attr2']}{Name} eq 'attr2' and $element->{Attributes}{['uri:ns1', 'attr2']}{Value} eq 'text2' and !defined($element->{Attributes}{['uri:ns1', 'attr2']}{Prefix}) and $element->{Attributes}{['uri:ns1', 'attr2']}{NamespaceURI} eq 'uri:ns1' ) { $self->{Tests}[3] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el4' and !defined($element->{NamespaceURI}) and !defined($element->{Prefix}) and $element->{LocalName} eq 'el4' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{[$xmlns_ns, 'ns2']}{Name} eq 'xmlns:ns2' and $element->{Attributes}{[$xmlns_ns, 'ns2']}{Value} eq 'uri:ns2' and $element->{Attributes}{[$xmlns_ns, 'ns2']}{NamespaceURI} eq $xmlns_ns and $element->{Attributes}{[$xmlns_ns, 'ns2']}{Prefix} eq 'xmlns' ) { $self->{Tests}[4] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'ns2:el5' and $element->{NamespaceURI} eq 'uri:ns2' and $element->{Prefix} eq 'ns2' and $element->{LocalName} eq 'el5' and (keys %{ $element->{Attributes} } ) == 2 and $element->{Attributes}{attr3}{Name} eq 'attr3' and $element->{Attributes}{attr3}{Value} eq 'text3' and !defined($element->{Attributes}{attr3}{NamespaceURI}) and !defined($element->{Attributes}{attr3}{Prefix}) and $element->{Attributes}{['uri:ns2', 'attr4']}{Name} eq 'ns2:attr4' and $element->{Attributes}{['uri:ns2', 'attr4']}{Value} eq 'text4' and $element->{Attributes}{['uri:ns2', 'attr4']}{NamespaceURI} eq 'uri:ns2' and $element->{Attributes}{['uri:ns2', 'attr4']}{Prefix} eq 'ns2' ) { $self->{Tests}[5] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el6' and !defined($element->{NamespaceURI}) and !defined($element->{Prefix}) and $element->{LocalName} eq 'el6' and (keys %{ $element->{Attributes} } ) == 0 ) { $self->{Tests}[6] = 1; } else { $self->{Fail_start_element} = 1; } } sub end_element { my ($self, $element) = @_; if ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el1', and !defined($element->{NamespaceURI}) and !defined($element->{Prefix}) and $element->{LocalName} eq 'el1' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{attr1}{Name} eq 'attr1' and $element->{Attributes}{attr1}{Value} eq 'text1' and !defined($element->{Attributes}{attr1}{Prefix}) and !defined($element->{Attributes}{attr1}{NamespaceURI}) ) { $self->{Tests}[7] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el2' and $element->{NamespaceURI} eq 'uri:ns1' and !defined($element->{Prefix}) and $element->{LocalName} eq 'el2' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{xmlns}{Name} eq 'xmlns' and $element->{Attributes}{xmlns}{Value} eq 'uri:ns1' and !defined($element->{Attributes}{xmlns}{Prefix}) and !defined($element->{Attributes}{xmlns}{NamespaceURI}) ) { $self->{Tests}[8] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el3' and $element->{NamespaceURI} eq 'uri:ns1' and !defined($element->{Prefix}) and $element->{LocalName} eq 'el3' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{['uri:ns1', 'attr2']}{Name} eq 'attr2' and $element->{Attributes}{['uri:ns1', 'attr2']}{Value} eq 'text2' and !defined($element->{Attributes}{['uri:ns1', 'attr2']}{Prefix}) and $element->{Attributes}{['uri:ns1', 'attr2']}{NamespaceURI} eq 'uri:ns1' ) { $self->{Tests}[9] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el4' and !defined($element->{NamespaceURI}) and !defined($element->{Prefix}) and $element->{LocalName} eq 'el4' and (keys %{ $element->{Attributes} } ) == 1 and $element->{Attributes}{[$xmlns_ns, 'ns2']}{Name} eq 'xmlns:ns2' and $element->{Attributes}{[$xmlns_ns, 'ns2']}{Value} eq 'uri:ns2' and $element->{Attributes}{[$xmlns_ns, 'ns2']}{NamespaceURI} eq $xmlns_ns and $element->{Attributes}{[$xmlns_ns, 'ns2']}{Prefix} eq 'xmlns' ) { $self->{Tests}[10] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'ns2:el5' and $element->{NamespaceURI} eq 'uri:ns2' and $element->{Prefix} eq 'ns2' and $element->{LocalName} eq 'el5' and (keys %{ $element->{Attributes} } ) == 2 and $element->{Attributes}{attr3}{Name} eq 'attr3' and $element->{Attributes}{attr3}{Value} eq 'text3' and !defined($element->{Attributes}{attr3}{NamespaceURI}) and !defined($element->{Attributes}{attr3}{Prefix}) and $element->{Attributes}{['uri:ns2', 'attr4']}{Name} eq 'ns2:attr4' and $element->{Attributes}{['uri:ns2', 'attr4']}{Value} eq 'text4' and $element->{Attributes}{['uri:ns2', 'attr4']}{NamespaceURI} eq 'uri:ns2' and $element->{Attributes}{['uri:ns2', 'attr4']}{Prefix} eq 'ns2' ) { $self->{Tests}[11] = 1; } elsif ( $element->{NodeType} eq 'element' and $element->{Name} eq 'el6' and !defined($element->{NamespaceURI}) and !defined($element->{Prefix}) and $element->{LocalName} eq 'el6' and (keys %{ $element->{Attributes} } ) == 0 ) { $self->{Tests}[12] = 1; } else { $self->{Fail_end_element} = 1; } } sub characters { my ($self, $characters) = @_; if ($characters->{NodeType} eq 'characters' and $characters->{Data} =~ /^\s*$/) { $self->{Tests}[13] = 1; } elsif ($characters->{NodeType} eq 'characters' and $characters->{Data} eq 'text5') { $self->{Tests}[14] = 1; } else { $self->{Fail_characters} = 1; } } sub comment { my ($self, $comment) = @_; if ($comment->{NodeType} eq 'comment' and $comment->{Data} eq ' comment1 ') { $self->{Tests}[15] = 1; } elsif ($comment->{NodeType} eq 'comment' and $comment->{Data} eq ' comment2 ') { $self->{Tests}[16] = 1; } elsif ($comment->{NodeType} eq 'comment' and $comment->{Data} eq ' comment3 ') { $self->{Tests}[17] = 1; } else { $self->{Fail_comment} = 1; } } sub processing_instruction { my ($self, $pi) = @_; if ( $pi->{NodeType} eq 'processing_instruction' and $pi->{Target} eq 'pi1' and $pi->{Data} eq 'data1') { $self->{Tests}[18] = 1; } elsif ( $pi->{NodeType} eq 'processing_instruction' and $pi->{Target} eq 'pi2' and $pi->{Data} eq 'data2' ) { $self->{Tests}[19] = 1; } elsif ( $pi->{NodeType} eq 'processing_instruction' and $pi->{Target} eq 'pi3' and $pi->{Data} eq 'data3' ) { $self->{Tests}[20] = 1; } else { $self->{Fail_processing_instruction} = 1; } } package main; use SAXDriver::XMLParser; # [3]Given an XML fragment with all node and property combinations, # verify the expected SAX event calls my $handler = MyHandler->new(); my $parser = SAXDriver::XMLParser->new( Handler => $handler ); $parser->parse_string($xml); for(my $ii = 1; $ii <= $num_tests; $ii ++) { ok($handler->{Tests}[$ii]); } ok(!$handler->{Fail_start_document}); ok(!$handler->{Fail_end_document}); ok(!$handler->{Fail_start_element}); ok(!$handler->{Fail_end_element}); ok(!$handler->{Fail_characters}); ok(!$handler->{Fail_comment}); ok(!$handler->{Fail_processing_instruction});