XML-Twig

 view release on metacpan or  search on metacpan

t/xmlxpath_test1.t  view on Meta::CPAN

stest( ref $new_elt, 'XML::Twig::XPath::Elt', "new"); 
my $new_elt1= new XML::Twig::XPath::Elt( 'subclass');
stest( ref $new_elt, 'XML::Twig::XPath::Elt', "new subclass"); 

my $new_elt2= new XML::Twig::XPath::Elt;
stest( ref $new_elt2, 'XML::Twig::XPath::Elt', "create no gi"); 

my $new_elt3= new XML::Twig::XPath::Elt( 'elt3');
$new_elt3->set_id( 'elt3');
etest( $new_elt3, 'elt3', 'elt3', "create with gi"); 

my $new_elt4= new XML::Twig::XPath::Elt( 'elt4', 'text of elt4');
ttest( $new_elt4, 'text of elt4', "create with gi and text"); 

my $new_elt5= new XML::Twig::XPath::Elt( 'elt5', 'text of elt5 ', $new_elt4);
ttest( $new_elt5, 'text of elt5 text of elt4', "create with gi and content"); 

my $new_elt6= new XML::Twig::XPath::Elt( PCDATA, 'text of elt6');
ttest( $new_elt6, 'text of elt6', "create PCDATA"); 

# test CDATA
my $st1='<doc><![CDATA[<br><b>bold</b>]]></doc>';
my $t1= new XML::Twig::XPath;
$t1->parse( $st1); 
sttest( $t1->root, $st1, "CDATA Section"); 


my $st2='<doc>text <![CDATA[<br><b>bold</b>]]> more text</doc>';
my $t2= new XML::Twig::XPath;
$t2->parse( $st2); 
sttest( $t2->root, $st2, "CDATA Section"); 

my $st3='<doc><![CDATA[<br><b>bold</b>]]> text</doc>';
my $t3= new XML::Twig::XPath;
$t3->parse( $st3); 
sttest( $t3->root, $st3, "CDATA Section"); 

my $st4='<doc><el>text</el><![CDATA[<br><b>bold</b>]]><el>more text</el></doc>';
my $t4= new XML::Twig::XPath;
$t4->parse( $st4); 
sttest( $t4->root, $st4, "CDATA Section"); 

my $st5='<doc>text <![CDATA[ text ]]&lt; ]]><el>more text</el></doc>';
my $t5= new XML::Twig::XPath;
$t5->parse( $st5); 
sttest( $t5->root, $st5, "CDATA Section with ]]&lt;"); 

# test prefix
my $st6='<doc><el1>text</el1><el2>more text</el2></doc>';
my $t6= new XML::Twig::XPath;
$t6->parse( $st6); 
$doc= $t6->root;
$doc->prefix( 'p1:');
sttest( $t6->root,'<doc>p1:<el1>text</el1><el2>more text</el2></doc>', 
        "prefix doc"); 
my $el1= $doc->first_child( 'el1');
$el1->prefix( 'p2:');
sttest( $t6->root,'<doc>p1:<el1>p2:text</el1><el2>more text</el2></doc>',
        "prefix el1"); 
my $el2= $doc->first_child( 'el2');
my $pcdata= $el2->first_child( PCDATA);
$pcdata->prefix( 'p3:');
sttest( $t6->root,'<doc>p1:<el1>p2:text</el1><el2>p3:more text</el2></doc>', 
        "prefix pcdata"); 

is( $t6->node_cmp( 1), -1, "compare twig with scalar");
ok( UNIVERSAL::isa( $t->root->getParentNode, 'XML::Twig::XPath'), 'getParentNode on the root');
ok( UNIVERSAL::isa( $t->root->first_child->getParentNode, 'XML::Twig::XPath::Elt'), 'getParentNode on an elt');
eval '$t6->root->node_cmp( []);';
matches( $@, "^unknown node type ", "compare elt with scalar");
my $elt= XML::Twig::XPath::Elt->new( elt => { att1 => 1, att2 => 2 }, "99");
my( $att1, $att2)= $elt->getAttributes;
is( $att1->node_cmp( $att2), -1, "attribute comparison");
is( $att2->node_cmp( $att1),  1, "attribute comparison (reverse order)");
is( $att2->node_cmp( $elt),   1, "compare attribute with elt");
is( $att2->node_cmp( $t6),   1, "compare attribute with elt");
is( $elt->node_cmp( $att1),  -1, "compare elt with attribute");
is( $att1->node_cmp( $att1),  0, "compare attribute with itself");
is( $elt->node_cmp( $elt),    0, "compare elt with itself");
eval( '$att1->node_cmp( 1)');
matches( $@, "^unknown node type ", "compare att with scalar");
$elt->set_att( att3 => 3);
my $att3= XML::Twig::XPath::Attribute->new( $elt => 'att3');
is( $att1->node_cmp( $att3), -1, "attribute comparison");
ok( $att2->to_number == 2, "to_number on att");
ok( $elt->to_number == 99, "to_number on elt");

exit 0;



( run in 0.901 second using v1.01-cache-2.11-cpan-39bf76dae61 )