#!perl -w # # $Id: xml_node_func.t,v 1.4 2000/10/05 17:50:47 kmacleod Exp $ # use strict; use Test; BEGIN { plan tests => 29 } use XML::Orchard; # [I2:1] Create a Document with an Element between PIs or Comments, # verify that doc.Root is the one Element my $doc = XML::Orchard::Document->new(); push @{ $doc->{Contents} }, XML::Orchard::Comment->new(); my $el = XML::Orchard::Element->new(); push @{ $doc->{Contents} }, $el; push @{ $doc->{Contents} }, XML::Orchard::ProcessingInstruction->new(); ok($doc->{Root}{NodeType} eq 'element'); # 1 # [I2:1] doc.Root is readonly my $new_el = XML::Orchard::Element->new(Contents => [ XML::Orchard::Element->new() ] ); eval { $doc->{Root} = $new_el }; ok($@); # 2 # [I2:1] In Element, Name is made up of Prefix ":" LocalName, changing # any of Name, Prefix, or LocalName should be reflected in the others. $el->{Name} = 'Fu:Bar'; ok($el->{Prefix} eq 'Fu'); #3 ok($el->{LocalName} eq 'Bar'); #4 $el->{Prefix} = 'Bravo'; ok($el->{Name} eq 'Bravo:Bar'); #5 $el->{LocalName} = 'Charlie'; ok($el->{Name} eq 'Bravo:Charlie'); #6 # [12:1] In Attribute, Name is made up of Prefix ":" LocalName, # changing any of Name, Prefix, or LocalName should be reflected in # the others. my $attr = XML::Orchard::Attribute->new(); $attr->{Name} = 'Fu:Bar'; ok($attr->{Prefix} eq 'Fu'); #7 ok($attr->{LocalName} eq 'Bar'); #8 $attr->{Prefix} = 'Bravo'; ok($attr->{Name} eq 'Bravo:Bar'); #9 $attr->{LocalName} = 'Charlie'; ok($attr->{Name} eq 'Bravo:Charlie'); #10 # [I2:1] Creating nodes should accept a list of Property => Value # pairs my $attr2 = XML::Orchard::Attribute->new( Name => 'Alpha:Beta', NamespaceURI => 'a:uri' ); ok($attr2->{Name} eq 'Alpha:Beta'); #11 ok($attr2->{Prefix} eq 'Alpha'); #12 ok($attr2->{LocalName} eq 'Beta'); #13 ok($attr2->{NamespaceURI} eq 'a:uri'); #14 $el = XML::Orchard::Element->new( Name => 'Fu:Bar', NamespaceURI => 'some:uri', Contents => [ ], Attributes => [ $attr, $attr2 ] ); ok($el->{Name} eq 'Fu:Bar'); #15 ok($el->{Prefix} eq 'Fu'); #16 ok($el->{LocalName} eq 'Bar'); #17 ok($el->{NamespaceURI} eq 'some:uri'); #18 ok($#{ $el->{Contents} } == -1); #19 # [I2:2] el.Attributes['Foo'] is el.Attributes[(None, 'Foo')] $el->{Attributes}{Foo} = XML::Orchard::Attribute->new(Value => 'fubar'); ok($el->{Attributes}{[undef, 'Foo']}{Value} eq 'fubar'); #20 # [I2:2] el.Attributes['Fu:Bar'] is el.Attributes[('FuURI', 'Bar')], # iff el.Attributes[('FuURI', 'Bar')]['Prefix'] is 'Fu' my $wierd = $el->{Attributes}{'Alpha:Beta'}; ok($wierd->{NamespaceURI} eq 'a:uri'); #21 $el->{Attributes}{['a:uri', 'Beta']}{Prefix} = 'Gamma'; ok(!defined $el->{Attributes}{'Alpha:Beta'}); #22 # [I2:2] an Attribute with a namespace but without a defined Prefix is # not accessible via a string index $el->{Attributes}{Zap} = XML::Orchard::Attribute->new(NamespaceURI => 'z:uri'); ok(!defined $el->{Attributes}{Zap}); #23 # [I2:2] len(el.Attributes) is the number of attributes, not indexes # -- {a:uri}Beta, {an:uri}Foo, Bravo:Charlie, {z:uri}Zap ok(keys %{ $el->{Attributes} } == 4); #24 # [I2:2] keys(el.Attributes) returns Name for elements without a # NamespaceURI, and a (URI, LocalName) tuple for those with my %found; while (my ($key, $value) = each %{ $el->{Attributes} }) { if (ref($key) eq 'ARRAY' and $key->[0] eq 'a:uri' and $key->[1] eq 'Beta') { $found{Beta} = 1; } elsif (ref($key) eq 'ARRAY' and $key->[0] eq 'z:uri' and $key->[1] eq 'Zap') { $found{Zap} = 1; } elsif (!ref($key) and $key eq 'Foo') { $found{Foo} = 1; } elsif (!ref($key) and $key eq 'Charlie') { $found{Charlie} = 1; } else { $found{SomethingElse} = 1; } } ok($found{Beta}); #25 ok($found{Zap}); #26 ok($found{Foo}); #27 ok($found{Charlie}); #28 ok(!$found{SomethingElse}); #29