use strict ; use warnings FATAL => 'all' ; use Test::More ; use ExtUtils::MakeMaker ; use XML::Smart ; my $DATA = q`
192.168.0.1
192.168.0.2
192.168.1.10
192.168.1.20
`; my $XML = new XML::Smart() ; $XML = new XML::Smart() ; $XML = new XML::Smart() ; my $xml = new XML::Smart() ; $xml = new XML::Smart() ; $xml = new XML::Smart() ; ######################### subtest 'HTML Tests' => sub { my $XML = XML::Smart->new( q` Blah blah


` ); my $data = $XML->data( noheader => 1 ) ; $data =~ s/\s+/ /gs ; cmp_ok($data, 'eq', q` Blah blah


`) ; my @order = $XML->{html}{body}{form}->order ; cmp_ok( join(" ", @order), 'eq', 'input br input br') ; $XML->{html}{body}{form}->set_order( qw(br input input br) ) ; @order = $XML->{html}{body}{form}->order ; cmp_ok( join(" ", @order), 'eq', 'br input input br') ; $data = $XML->data( noheader => 1 ) ; $data =~ s/\s+/ /gs ; cmp_ok( $data, 'eq', q` Blah blah


`) ; done_testing() ; } ; subtest 'Pointer Tests' => sub { my $XML = XML::Smart->new(q` content0 sub_content content1 content2 ` , 'XML::Smart::Parser') ; my $data = $XML->data(noheader => 1) ; cmp_ok($data, 'eq', q` content0 sub_content content1 content2 `) ; my $tmp = tied $XML->{root}->pointer->{CONTENT} ; isnt( tied $XML->{root}->pointer->{CONTENT}, undef ) ; my $cont = $XML->{root}->{CONTENT} ; cmp_ok($cont, 'eq', q` content0 content1 content2 `) ; my $cont_ = $XML->{root}->content ; cmp_ok( $cont_, 'eq', q` content0 content1 content2 `) ; $XML->{root}->content(1,"set1") ; my @cont = $XML->{root}->content ; cmp_ok($cont[0], 'eq', "\ncontent0\n") ; cmp_ok($cont[1], 'eq', "set1") ; cmp_ok($cont[2], 'eq', "\ncontent2\n") ; $XML->{root}->{CONTENT} = 123 ; my $cont_2 = $XML->{root}->content ; subtest 'Perl Version Tests' => sub { if( $] >= 5.007 && $] <= 5.008 ) { plan skip_all => "Skip on $]" ; } cmp_ok( $cont_2, '==', 123) ; is( tied $XML->{root}->pointer->{CONTENT}, undef ) ; done_testing() ; } ; is( tied $XML->{root}{tag1}{sub}->pointer->{CONTENT}, undef, 'Undefined' ) ; my $sub_cont = $XML->{root}{tag1}{sub}->{CONTENT} ; cmp_ok($sub_cont, 'eq', 'sub_content') ; $data = $XML->data(noheader => 1) ; subtest 'Perl Version Tests' => sub { if( $] >= 5.007 && $] <= 5.008 ) { plan skip_all => "Skip on $]" ; } cmp_ok( $data , 'eq', q`123 sub_content ` ) ; done_testing() ; }; done_testing() ; } ; subtest 'Content Set tests' => sub { my $xml = new XML::Smart(q` aaa bbb `) ; $xml = $xml->{root} ; $xml->{phone}->content('XXX') ; $xml->{phone}[1]->content('YYY') ; $xml->{test}->content('ZZZ') ; my $data = $xml->data(noheader => 1) ; cmp_ok($data, 'eq', q` XXX YYY ZZZ `) ; done_testing() ; } ; subtest 'Data Order Tests' => sub { my $xml = new XML::Smart(q` TEXT1 & more aaa bbb `,'html') ; my $data = $xml->data(noident=>1 , noheader => 1 , wild=>1) ; cmp_ok($data, 'eq', q` TEXT1 & more aaa bbb `) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Tests' => sub { my $XML = XML::Smart->new('text1foobartext2' , 'XML::Smart::Parser') ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//g ; cmp_ok($data, 'eq', 'text1foobartext2') ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser args test' => sub { my $XML = XML::Smart->new('' , 'XML::Smart::Parser') ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; cmp_ok($data, 'eq', '') ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser nometagen tests' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; my $data = $XML->data(nometagen => 1) ; $data =~ s/\s//gs ; my $data_org = $DATA ; $data_org =~ s/\s//gs ; cmp_ok( $data, 'eq', $data_org) ; done_testing() ; } ; ######################### subtest 'XML::Smart::HTMLParser Tests' => sub { my $XML = XML::Smart->new('' , 'XML::Smart::HTMLParser') ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; cmp_ok( $data, 'eq', '' ) ; $XML = XML::Smart->new(q` TITLE end" w=q> FOO2-DATA ` , 'HTML') ; $data = $XML->data(noheader => 1 , nospace => 1 ) ; cmp_ok($data, 'eq', q`TITLEFOO2-DATA`) ; $XML = XML::Smart->new(q` TITLE ` , 'HTML') ; $data = $XML->data(noheader => 1 , nospace => 1) ; $data =~ s/\s//gs ; cmp_ok($data, 'eq', q`TITLE`); done_testing() ; } ; ######################### subtest 'XML::Smart::HTMLParser args Tests' => sub { my $XML = XML::Smart->new(q` <.sub1 arg="1" x=1 /> <.sub2 arg="2"/> content ` , 'XML::Smart::HTMLParser') ; my $data = $XML->data(noheader => 1 , wild => 1) ; cmp_ok( $data, 'eq', q` <.sub1 arg="1" x="1"/> <.sub2 arg="2"/> content `); done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Tree tests' => sub { my $XML0 = XML::Smart->new(q`` , 'XML::Smart::Parser') ; my $XML1 = XML::Smart->new(q`` , 'XML::Smart::Parser') ; my $XML = XML::Smart->new() ; $XML->{sub}{sub2} = $XML0->tree ; push(@{$XML->{sub}{sub2}} , $XML1->tree ) ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; cmp_ok( $data, 'eq', '') ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Array Tests' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; $XML = $XML->{hosts} ; my $addr = $XML->{server}[0]{address} ; cmp_ok($addr, 'eq', '192.168.0.1') ; my $addr0 = $XML->{server}[0]{address}[0] ; cmp_ok( $addr, 'eq', $addr0); my $addr1 = $XML->{server}{address}[1] ; cmp_ok( $addr1, 'eq', '192.168.0.2') ; my $addr01 = $XML->{server}[0]{address}[1] ; cmp_ok( $addr1, 'eq', $addr01); my @addrs = @{$XML->{server}{address}} ; cmp_ok( $addrs[0], 'eq', $addr0); cmp_ok( $addrs[1], 'eq', $addr1); @addrs = @{$XML->{server}[0]{address}} ; cmp_ok( $addrs[0], 'eq', $addr0); cmp_ok( $addrs[1], 'eq', $addr1); done_testing() ; } ; ######################### subtest 'XML::Smart::Parser args Tests' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; $XML = $XML->{hosts} ; my $addr = $XML->{'server'}('type','eq','suse'){'address'} ; cmp_ok( $addr, 'eq', '192.168.1.10') ; my $addr0 = $XML->{'server'}('type','eq','suse'){'address'}[0] ; cmp_ok( $addr, 'eq', $addr0) ; my $addr1 = $XML->{'server'}('type','eq','suse'){'address'}[1] ; cmp_ok( $addr1, 'eq', '192.168.1.20') ; my $type = $XML->{'server'}('version','>=','9'){'type'} ; cmp_ok( $type, 'eq', 'conectiva') ; $addr = $XML->{'server'}('version','>=','9'){'address'} ; cmp_ok( $addr, 'eq', '192.168.2.100') ; $addr0 = $XML->{'server'}('version','>=','9'){'address'}[0] ; cmp_ok( $addr0, 'eq', $addr) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Args Array Tests' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; $XML = $XML->{hosts} ; my $newsrv = { os => 'Linux' , type => 'mandrake' , version => 8.9 , address => '192.168.3.201' , } ; push(@{$XML->{server}} , $newsrv) ; my $addr0 = $XML->{'server'}('type','eq','mandrake'){'address'}[0] ; cmp_ok( $addr0, 'eq', '192.168.3.201') ; $XML->{'server'}('type','eq','mandrake'){'address'}[1] = '192.168.3.202' ; my $addr1 = $XML->{'server'}('type','eq','mandrake'){'address'}[1] ; cmp_ok( $addr1, 'eq', '192.168.3.202') ; push(@{$XML->{'server'}('type','eq','conectiva'){'address'}} , '192.168.2.101') ; $addr1 = $XML->{'server'}('type','eq','conectiva'){'address'}[1] ; cmp_ok( $addr1, 'eq', '192.168.2.101') ; $addr1 = $XML->{'server'}[2]{'address'}[1] ; cmp_ok( $addr1, 'eq', '192.168.2.101') ; done_testing() ; } ; ######################### subtest 'Args regex Match Tests' => sub { my $XML = XML::Smart->new(q` ` , 'XML::Smart::Parser') ; my @users = $XML->{users}('email','=~','^jo') ; cmp_ok( $users[0]->{name}, 'eq', 'Joe X') ; cmp_ok( $users[1]->{name}, 'eq', 'JoH Y') ; done_testing() ; } ; ######################### subtest 'Default Parser Array Test' => sub { my $XML = XML::Smart->new() ; $XML->{server} = { os => 'Linux' , type => 'mandrake' , version => 8.9 , address => '192.168.3.201' , } ; $XML->{server}{address}[1] = '192.168.3.202' ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; my $dataok = q`
192.168.3.201
192.168.3.202
`; cmp_ok( $data, 'eq', $dataok ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser tags Test' => sub { my $XML = XML::Smart->new('ctab' , 'XML::Smart::Parser') ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; my $dataok = qq`ctab` ; cmp_ok( $data, 'eq', $dataok ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser data() options tests' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; $XML->{'hosts'}{'server'}('type','eq','conectiva'){'address'}[1] = '' ; my $data = $XML->data( noident => 1 , nospace => 1 , lowtag => 1 , upertag => 1 , uperarg => 1 , noheader => 1 , ) ; $data =~ s/\s//gs ; my $dataok = q`
192.168.0.1
192.168.0.2
192.168.1.10
192.168.1.20
`; cmp_ok( $data, 'eq', $dataok ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Data populate test' => sub { my $XML = XML::Smart->new('' , 'XML::Smart::Parser') ; $XML->{data} = 'aaa' ; $XML->{var } = 10 ; $XML->{addr} = [qw(1 2 3)] ; my $data = $XML->data(length => 1 , nometagen => 1 ) ; $data =~ s/\s//gs ; my $dataok = q`123`; cmp_ok( $data, 'eq', $dataok ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Data Populate Array test' => sub { my $XML = XML::Smart->new('' , 'XML::Smart::Parser') ; $XML->{hosts}{server} = { os => 'lx' , type => 'red' , ver => 123 , } ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; my $dataok = q``; cmp_ok( $data, 'eq', $dataok ) ; $XML->{hosts}[1]{server}[0] = { os => 'LX' , type => 'red' , ver => 123 , } ; $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; $dataok = q``; cmp_ok( $data, 'eq', $dataok ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Array assign test' => sub { my $XML = XML::Smart->new('' , 'XML::Smart::Parser') ; $XML->{hosts}[1]{server}[0] = { os => 'LX' , type => 'red' , ver => 123 , } ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; my $dataok = q``; cmp_ok( $data, 'eq', $dataok ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Hash assign test' => sub { my $XML = XML::Smart->new('' , 'XML::Smart::Parser') ; my $srv = { os => 'lx' , type => 'red' , ver => 123 , } ; push( @{$XML->{hosts}} , {XXXXXX => 1}) ; unshift( @{$XML->{hosts}} , $srv) ; push( @{$XML->{hosts}{more}} , {YYYY => 1}) ; my $data = $XML->data(noheader => 1) ; $data =~ s/\s//gs ; my $dataok = q`` ; cmp_ok( $data, 'eq', $dataok ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Extended Hash assign test' => sub { my $XML = XML::Smart->new('' , 'XML::Smart::Parser') ; $XML->{hosts}{server} = [ { os => 'lx' , type => 'a' , ver => '1' ,} , { os => 'lx ', type => 'b' , ver => '2' ,} , ]; cmp_ok( $XML->{hosts}{server}{type}, 'eq', 'a' ) ; my $srv0 = shift( @{$XML->{hosts}{server}} ) ; cmp_ok( $$srv0{type}, 'eq', 'a' ) ; cmp_ok( $XML->{hosts}{server}{type}, 'eq', 'b' ) ; cmp_ok( $XML->{hosts}{server}{type}[0], 'eq', 'b' ) ; cmp_ok( $XML->{hosts}{server}[0]{type}[0], 'eq', 'b' ) ; cmp_ok( $XML->{hosts}[0]{server}[0]{type}[0], 'eq', 'b' ) ; my $srv1 = pop( @{$XML->{hosts}{server}} ) ; cmp_ok( $$srv1{type}, 'eq', 'b' ) ; my $data = $XML->data(noheader => 1 , nospace=>1) ; cmp_ok($data, 'eq', '' ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser node extraction test' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser' ) ; my @types = $XML->{hosts}{server}('[@]','type') ; cmp_ok("@types", 'eq', 'redhat suse conectiva freebsd' ) ; @types = $XML->{hosts}{server}{type}('<@') ; cmp_ok("@types", 'eq', 'redhat suse conectiva freebsd' ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Extended node extraction test' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; my @srvs = $XML->{hosts}{server}('os','eq','linux') ; my @types ; foreach my $srvs_i ( @srvs ) { push(@types , $srvs_i->{type}) ;} cmp_ok("@types", 'eq', 'redhat suse conectiva' ) ; @srvs = $XML->{hosts}{server}(['os','eq','linux'],['os','eq','bsd']) ; @types = () ; foreach my $srvs_i ( @srvs ) { push(@types , $srvs_i->{type}) ;} cmp_ok("@types", 'eq', 'redhat suse conectiva freebsd' ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Data encode test' => sub { my $wild = pack("C", 127 ) ; my $data = qq`$wild`; my $XML = XML::Smart->new($data , 'XML::Smart::Parser') ; cmp_ok( $XML->{code}, 'eq', $wild ) ; $data = $XML->data() ; $XML = XML::Smart->new($data , 'XML::Smart::Parser') ; cmp_ok( $XML->{code}, 'eq', $wild ) ; my $data2 = $XML->data() ; cmp_ok( $data, 'eq', $data2 ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser cut_root test' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; my $addr1 = $XML->{hosts}{server}{address} ; my $XML2 = $XML->cut_root ; my $addr2 = $XML2->{server}{address} ; cmp_ok( $addr1, 'eq', $addr2 ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Funny Char test' => sub { my $data = q` My Company & Name + x >> plus " + '... `; my $XML = XML::Smart->new($data , 'XML::Smart::Parser') ; cmp_ok( $XML->{root}{foo}, 'eq', q` My Company & Name + x >> plus " + '...` ) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q` My Company & Name + x >> plus " + '...` ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser nodes test' => sub { my $XML = XML::Smart->new(q` cont ` , 'XML::Smart::Parser') ; my @nodes = $XML->{root}{foo}->nodes ; cmp_ok( $nodes[0]->{arg}, 'eq', 'z') ; @nodes = $XML->{root}{foo}->nodes_keys ; cmp_ok( "@nodes", 'eq', 'bar' ) ; isnt( $XML->{root}{foo}{bar}->is_node, undef ) ; my @keys = $XML->{root}{foo}('@keys') ; cmp_ok("@keys", 'eq', 'arg1 arg2 bar' ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser CDATA test' => sub { my $data = qq` end]]> `; my $XML = XML::Smart->new($data , 'XML::Smart::Parser') ; cmp_ok( $XML->{root}{item}{data}, 'eq', q`some CDATA code end` ) ; done_testing() ; } ; ######################### subtest 'Default Parser Hash assign through array Test' => sub { my $XML = XML::Smart->new() ; $XML->{menu}{option}[0] = { name => "Help" , level => {from => 1 , to => 99} , } ; $XML->{menu}{option}[0]{sub}{option}[0] = { name => "Busca" , level => {from => 1 , to => 99} , } ; my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q``) ; done_testing() ; } ; ######################### subtest 'Default Parser integer data tests' => sub { my $XML = XML::Smart->new() ; $XML->{menu}{arg1} = 123 ; $XML->{menu}{arg2} = 456 ; $XML->{menu}{arg2}{subarg} = 999 ; cmp_ok($XML->{menu}{arg1}, '==', 123 ) ; cmp_ok($XML->{menu}{arg2}, '==', 456 ) ; cmp_ok($XML->{menu}{arg2}{subarg}, '==', 999 ) ; my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok($data, 'eq', q`456` ) ; done_testing() ; } ; ######################### subtest 'Default Parser Args get Test' => sub { my $XML = XML::Smart->new() ; $XML->{menu}{arg1} = [1,2,3] ; $XML->{menu}{arg2} = 4 ; my @arg1 = $XML->{menu}{arg1}('@') ; cmp_ok( $#arg1, '==', 2 ) ; my @arg2 = $XML->{menu}{arg2}('@') ; cmp_ok( $#arg2, '==', 0 ) ; my @arg3 = $XML->{menu}{arg3}('@') ; cmp_ok( $#arg3, '==', -1 ) ; done_testing() ; } ; ######################### subtest 'Default Parser set_node and set_order Tests' => sub { my $XML = XML::Smart->new() ; $XML->{menu}{arg2} = 456 ; $XML->{menu}{arg1} = 123 ; my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q`` ) ; $XML->{menu}{arg2}->set_node ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q`456` ) ; $XML->{menu}{arg2}->set_node(0) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q`` ) ; $XML->{menu}->set_order('arg1' , 'arg2') ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q`` ) ; delete $XML->{menu}{arg2}[0] ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q`` ) ; done_testing() ; } ; ######################### subtest 'Default Parser XML Structure verification' => sub { my $XML = XML::Smart->new() ; $XML->{root}{foo} = "bla bla bla"; $XML->{root}{foo}->set_node(1) ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', '1' ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT}, 'eq', "bla bla bla" ) ; cmp_ok( ref $XML->tree->{ root }{ foo }, 'eq', 'HASH' ) ; $XML->{root}{foo}->set_node(0) ; cmp_ok( ref $XML->tree->{ root }{ foo }, 'eq', '' ) ; is( $XML->tree->{root}{'/nodes'}{foo}, undef ) ; $XML->{root}{foo}->set_cdata(1) ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', 'cdata,1,' ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT} , 'eq', "bla bla bla" ) ; $XML->{root}{foo}->set_node(1) ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', 'cdata,1,1' ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT}, 'eq', "bla bla bla" ) ; $XML->{root}{foo}->set_binary(1) ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', 'binary,1,1' ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT}, 'eq', "bla bla bla" ) ; $XML->{root}{foo}->set_binary(0) ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', 'binary,0,1' ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT}, 'eq', "bla bla bla" ) ; $XML->{root}{foo}->set_auto_node ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', 1 ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT}, 'eq', "bla bla bla" ) ; $XML->{root}{foo}->set_cdata(0) ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', 'cdata,0,1' ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT}, 'eq', "bla bla bla" ) ; $XML->{root}{foo}->set_binary(0) ; cmp_ok( $XML->tree->{root}{'/nodes'}{foo}, 'eq', 'binary,0,1' ) ; cmp_ok( $XML->tree->{root}{foo}{CONTENT}, 'eq', "bla bla bla" ) ; cmp_ok( ref( $XML->tree->{root}{foo} ), 'eq', 'HASH' ) ; $XML->{root}{foo}->set_auto ; cmp_ok( ref( $XML->tree->{root}{foo} ), 'eq', '' ) ; isnt( exists $XML->tree->{root}{'/nodes'}{foo}, undef ) ; done_testing() ; } ; ######################### subtest 'Default Parser CDATA and Bin data tests' => sub { my $XML = new XML::Smart ; $XML->{root}{foo} = "bla bla bla bla bla"; my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', ' bla bla]]>' ) ; $XML->{root}{foo}->set_cdata(0) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', 'bla bla bla <tag> bla bla' ) ; $XML->{root}{foo}->set_binary(1) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok ($data, 'eq', 'YmxhIGJsYSBibGEgPHRhZz4gYmxhIGJsYQ==' ) ; done_testing() ; } ; ######################### subtest 'Default Parser Funny Chars, Hex and Bin data Test' => sub { my $XML = new XML::Smart ; $XML->{root}{foo} = "

test \x03

"; my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', 'PGgxPnRlc3QgAzwvaDE+' ) ; $XML->{root}{foo}->set_binary(0) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', "<h1>test \x03\</h1>") ; $XML->{root}{foo}->set_binary(1) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', 'PGgxPnRlc3QgAzwvaDE+' ) ; done_testing() ; } ; ######################### subtest 'Default Parser CDATA test' => sub { my $XML = new XML::Smart ; $XML->{root}{foo} = "simple"; my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', '' ) ; $XML->{root}{foo}->set_cdata(1) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', '' ) ; done_testing() ; } ; ######################### subtest 'Default Parser CDATA and funny chars' => sub { my $XML = new XML::Smart ; $XML->{root}{foo} = "foo bar baz"; my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', 'foo bar baz]]>' ) ; $XML->{root}{foo}->set_cdata(0) ; $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', '<words>foo bar baz</words>' ) ; done_testing() ; } ; ######################### subtest 'Default Parser' => sub { my $XML = XML::Smart->new(q` here's a test `, 'XML::Parser'); my $data = $XML->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', "here's a test") ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Path and XPath Tests' => sub { my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; $XML = $XML->{hosts} ; my $addr = $XML->{'server'}('type','eq','suse'){'address'} ; cmp_ok( $addr->path, 'eq', '/hosts/server[1]/address' ) ; my $addr0 = $XML->{'server'}('type','eq','suse'){'address'}[0] ; cmp_ok( $addr0->path , 'eq', '/hosts/server[1]/address[0]') ; cmp_ok( $addr0->path_as_xpath , 'eq', '/hosts/server[2]/address') ; my $addr1 = $XML->{'server'}('type','eq','suse'){'address'}[1] ; my $type = $XML->{'server'}('version','>=','9'){'type'} ; cmp_ok($type->path , 'eq', '/hosts/server[2]/type') ; $addr = $XML->{'server'}('version','>=','9'){'address'} ; cmp_ok($addr->path , 'eq', '/hosts/server[2]/address') ; $addr0 = $XML->{'server'}('version','>=','9'){'address'}[0] ; cmp_ok($addr0->path , 'eq', '/hosts/server[2]/address[0]') ; cmp_ok($addr0->path_as_xpath , 'eq', '/hosts/server[3]/@address') ; $type = $XML->{'server'}('version','>=','9'){'type'} ; cmp_ok($type->path , 'eq', '/hosts/server[2]/type') ; cmp_ok($type->path_as_xpath , 'eq', '/hosts/server[3]/@type') ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser cut_root array Tests' => sub{ my $XML = new XML::Smart(q` 456 `,'smart'); $XML = $XML->cut_root ; my @frames_123 = @{ $XML->{'output'}('name','eq',123){'frames'} } ; my @formats_123 = map { $_->{'format'} } @frames_123 ; my @frames_456 = @{ $XML->{'output'}('name','eq',456){'frames'} } ; my @formats_456 = map { $_->{format} } @frames_456 ; cmp_ok( join(";", @formats_123) , 'eq', 'a;b' ) ; cmp_ok( join(";", @formats_456) , 'eq', 'c;d' ) ; done_testing() ; } ; ######################### subtest 'XML::Smart::HTMLParser Tests' => sub { my $html = q`

foo

`; my @tag ; my $p = XML::Smart::HTMLParser->new( Start => sub { shift; push(@tag , @_) ;}, Char => sub {}, End => sub {}, ); $p->parse($html) ; cmp_ok($tag[-1] , 'eq', '$s->{supply}->shift') ; done_testing() ; } ; ######################### subtest 'XML::Smart::Parser Array pop Test' => sub { my $xml = new XML::Smart(q` test 1 test 2 test 3 file 1 `); $xml->{doc}{port}[0] = 0; $xml->{doc}{port}[1] = 1; $xml->{doc}{port}[2] = 2; $xml->{doc}{port}[3] = 3; my $data = $xml->data(nospace => 1 , noheader => 1 ) ; cmp_ok($data, 'eq', q`test 1test 2test 3file 10123`) ; pop @{$xml->{doc}{'/order'}} ; $data = $xml->data(nospace => 1 , noheader => 1 ) ; cmp_ok( $data, 'eq', q`test 1test 2test 3file 10123`) ; done_testing() ; } ; ######################### subtest 'XML::XPath Tests' => sub { eval(q`use XML::XPath`) ; if( $@ ) { plan skip_all => 'XML::XPath Unavailable' ; } my $XML = XML::Smart->new($DATA , 'XML::Smart::Parser') ; my $xp1 = $XML->XPath ; my $xp2 = $XML->XPath ; cmp_ok( $xp1, 'eq', $xp2 ) ; $xp1 = $XML->XPath ; $XML->{hosts}{tmp} = 123 ; $xp2 = $XML->XPath ; ## Test cache of the XPath object: cmp_ok( $xp1, '!=', $xp2 ) ; delete $XML->{hosts}{tmp} ; my $data = $XML->XPath->findnodes_as_string('/') ; cmp_ok( $data, 'eq', q`
192.168.0.1
192.168.0.2
192.168.1.10
192.168.1.20
`) ; done_testing() ; } ; ######################### subtest 'XML::Smart::DTD Tests' => sub { use XML::Smart::DTD ; my $dtd = XML::Smart::DTD->new(q` ]> `) ; isnt( $dtd->elem_exists('curso') , undef ) ; isnt( $dtd->elem_exists('objetivo') , undef ) ; isnt( $dtd->elem_exists('curriculo') , undef ) ; isnt( $dtd->elem_exists('disciplina') , undef ) ; isnt( $dtd->elem_exists('descricao') , undef ) ; isnt( $dtd->elem_exists('requisito') , undef ) ; isnt( $dtd->elem_exists('professor') , undef ) ; isnt( $dtd->elem_exists('br') , undef ) ; isnt( $dtd->is_elem_req('requisito') , undef ) ; isnt( $dtd->is_elem_uniq('requisito') , undef ) ; isnt( $dtd->is_elem_opt('curriculo') , undef ) ; isnt( !$dtd->is_elem_req('curriculo') , undef ) ; isnt( $dtd->is_elem_multi('professor') , undef ) ; isnt( $dtd->is_elem_pcdata('professor') , undef ) ; isnt( $dtd->is_elem_empty('br') , undef ) ; isnt( $dtd->attr_exists('curso','centro') , undef ) ; isnt( $dtd->attr_exists('curso','nome') , undef ) ; isnt( $dtd->attr_exists('curso','centro','nome') , undef ) ; is( $dtd->attr_exists('curso','centro','nomes'), undef ) ; my @attrs = $dtd->get_attrs('curso', undef ) ; cmp_ok( join(" ",@attrs), 'eq', 'centro nome age' ) ; @attrs = $dtd->get_attrs_req('curso') ; cmp_ok( join(" ",@attrs) , 'eq', 'centro nome') ; done_testing() ; } ; ######################### subtest 'Default Parser cds Tests' => sub { my $xml = XML::Smart->new()->{cds} ; $xml->{album}[0] = { title => 'foo' , artist => 'the foos' , tracks => 8 , } ; $xml->{album}[1] = { title => 'bar' , artist => 'the barss' , tracks => [qw(6 7)] , time => [qw(60 70)] , type => 'b' , } ; $xml->{album}[2] = { title => 'baz' , artist => undef , tracks => 10 , type => '' , br => 123 , } ; $xml->{creator} = 'Joe' ; $xml->{date} = '2000-01-01' ; $xml->{type} = 'a' ; $xml->{album}[0]{title}->set_node(1); cmp_ok( $xml->data( noheader=>1 , nospace=>1), 'eq', q`foo67`) ; $xml->apply_dtd(q` ]> `); cmp_ok( $xml->data(noheader=>1 , nospace=>1), 'eq', q` ]>the foos8the barss6710
` ); done_testing() ; } ; ######################### subtest 'Default Parser apply_dtd Tests' => sub { my $xml = XML::Smart->new; $xml->{customer}{phone} = "555-1234"; $xml->{customer}{phone}{type} = "home"; $xml->apply_dtd(q` ]> `); cmp_ok( $xml->data(noheader=>1 , nospace=>1 , nodtd=>1), 'eq', q`555-1234` ); done_testing() ; } ; ######################### subtest 'URL Tests' => sub { eval(q`use LWP::UserAgent`) ; if ( $@ ) { plan skip_all => 'URL Tests require LWP::UserAgent' ; done_testing() ; } if( !$ENV{ URL_TESTS } ) { plan skip_all => 'Skipping URL test, Enable by setting ENV variable URL_TESTS' ; done_testing() ; } my $url = 'http://www.perlmonks.org/index.pl?node_id=16046' ; diag( "\nGetting URL... " ) ; my $XML = XML::Smart->new($url , 'XML::Smart::Parser') ; cmp_ok( $XML->{XPINFO}{INFO}{sitename}, 'eq', 'PerlMonks' ) ; done_testing() ; } ; ######################### done_testing() ; exit() ;