root/lang/perl/XML-XPathEngine/trunk/t/01_basic.t @ 9314

Revision 9314, 4.2 kB (checked in by mattn, 7 years ago)

lang/perl/XML-XPathEngine/trunk/t/01_basic.t,
lang/perl/XML-XPathEngine/trunk/lib/XML/XPathEngine.pm:

  • fixed findnodes to return count value.

ex: findnodes('count(//kid1[@name="foo"])') eq 3

Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 15;
7use XML::XPathEngine;
8
9BEGIN { push @INC, './t'; }
10
11my $tree = init_tree();
12my $xp   = XML::XPathEngine->new;
13
14#warn $tree->as_xml, "\n\n";
15{
16my @root_nodes= $xp->findnodes( '/root', $tree);
17is( join( ':', map { $_->value } @root_nodes), 'root_value', q{findnodes( '/root', $tree)});
18}
19{
20my @kid_nodes= $xp->findnodes( '/root/kid0', $tree);
21is( scalar @kid_nodes, 2, q{findnodes( '/root/kid0', $tree)});
22}
23{
24my $kid_nodes= $xp->findvalue( '/root/kid0', $tree);
25is( $kid_nodes, 'vkid2vkid4', q{findvalue( '/root/kid0', $tree)});
26}
27{
28is( $xp->findvalue( '//*[@att2="vv"]', $tree), 'gvkid1gvkid2gvkid3gvkid4gvkid5',
29    q{findvalue( '//*[@att2="vv"]', $tree)}
30  );
31is( $xp->findvalue( '//*[@att2]', $tree), 'gvkid1gkid2 1gvkid2gkid2 2gvkid3gkid2 3gvkid4gkid2 4gvkid5gkid2 5',
32    q{findvalue( '//*[@att2]', $tree)}
33  );
34}
35
36is( $xp->findvalue( '//kid1[@att1=~/v[345]/]', $tree), 'vkid3vkid5', "match on attributes");
37
38is( $xp->findvalue( '//@*', $tree), 'v1v1vvvxv2vvvxv3vvvxv4vvvxv5vvvx', 'match all attributes');
39is( $xp->findvalue( '//@*[parent::*/@att1=~/v[345]/]', $tree), 'v3v4v5', 'match all attributes with a test');
40
41is( $xp->findvalue( '//kid1[@att1="v3"]/following::gkid2[1]', $tree), 'gkid2 4', "following axis[1]");
42is( $xp->findvalue( '//kid1[@att1="v3"]/following::gkid2[2]', $tree), 'gkid2 5', "following axis[2]");
43is( $xp->findvalue( '//kid1[@att1="v3"]/following::kid1/*', $tree), 'gvkid5gkid2 5', "following axis");
44is( $xp->findvalue( '//kid1[@att1="v3"]/preceding::gkid2[1]', $tree), 'gkid2 2', "preceding axis[1]");
45is( $xp->findvalue( '//kid1[@att1="v3"]/preceding::gkid2[2]', $tree), 'gkid2 1', "preceding axis[1]");
46is( $xp->findvalue( '//kid1[@att1="v3"]/preceding::gkid2', $tree), 'gkid2 1gkid2 2', "preceding axis");
47is( $xp->findvalue( 'count(//kid1)', $tree), '3', 'preceding count');
48
49sub init_tree
50  { my $tree  = tree->new( 'att', name => 'tree', value => 'tree');
51    my $root  = tree->new( 'att', name => 'root', value => 'root_value', att1 => 'v1');
52    $root->add_as_last_child_of( $tree);
53
54    foreach (1..5)
55      { my $kid= tree->new( 'att', name => 'kid' . $_ % 2, value => "vkid$_", att1 => "v$_");
56        $kid->add_as_last_child_of( $root);
57        my $gkid1= tree->new( 'att', name => 'gkid' . $_ % 2, value => "gvkid$_", att2 => "vv");
58        $gkid1->add_as_last_child_of( $kid);
59        my $gkid2= tree->new( 'att', name => 'gkid2', value => "gkid2 $_", att2 => "vx");
60        $gkid2->add_as_last_child_of( $kid);
61      }
62
63    $tree->set_pos;
64
65    return $tree;
66  }
67
68
69package tree;
70use base 'minitree';
71
72sub getName            { return shift->name;  }
73sub getValue           { return shift->value; }
74sub string_value       { return shift->value; }
75sub getRootNode        { return shift->root;                }
76sub getParentNode      { return shift->parent;              }
77sub getChildNodes      { return wantarray ? shift->children : [shift->children]; }
78sub getFirstChild      { return shift->first_child;         }
79sub getLastChild       { return shift->last_child;         }
80sub getNextSibling     { return shift->next_sibling;        }
81sub getPreviousSibling { return shift->previous_sibling;    }
82sub isElementNode      { return 1;                          }
83sub get_pos            { return shift->pos;          }
84sub getAttributes      { return wantarray ? @{shift->attributes} : shift->attributes; }
85sub as_xml
86  { my $elt= shift;
87    return "<" . $elt->getName . join( "", map { " " . $_->getName . '="' . $_->getValue . '"' } $elt->getAttributes) . '>'
88           . (join( "\n", map { $_->as_xml } $elt->getChildNodes) || $elt->getValue)
89           . "</" . $elt->getName . ">"
90           ;
91  }
92
93sub cmp { my( $a, $b)= @_; return $a->pos <=> $b->pos; }
94
951;
96
97package att;
98use base 'attribute';
99
100sub getName            { return shift->name;                }
101sub getValue           { return shift->value;               }
102sub string_value       { return shift->value; }
103sub getRootNode        { return shift->parent->root;        }
104sub getParentNode      { return shift->parent;              }
105sub isAttributeNode    { return 1;                          }
106sub getChildNodes      { return; }
107
108sub cmp { my( $a, $b)= @_; return $a->pos <=> $b->pos; }
109
1101;
111
Note: See TracBrowser for help on using the browser.