summaryrefslogtreecommitdiff
path: root/t/01simple.t
blob: e73b9f953389f0b253e5eeeb359fb2014d6323ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use Test::More tests => 218;
use strict;
use warnings;
use lib 't/';
BEGIN { use_ok('Simple') };
 
# construct a small tree 
my $root = Simple->new_root(name => 'root');
isa_ok($root'Simple');
can_ok($root'match''xpath');
$root->add_kid(
    name => 'some:page'foo => 10, bar => 'bif')->add_kid(
        name => 'kidfoo'data => 10);
$root->add_kid(
    name => 'some:page'foo => 20, bar => 'bof')->add_kid(
        name => 'kidfoo'data => 20);
$root->add_kid(
    name => 'some:page'foo => 30, bar => 'bongo')->add_kid(
        name => 'kidfoo'data => 30);
my @pages = $root->kids;
for my $page (@pages) {
    isa_ok($page'Simple');
    can_ok($page'match''xpath');
    for (0 .. 9) {
        $page->add_kid(name => 'paragraph'data => "$page->{bar}$_" );
        $page->add_kid(name => 'image'if $_ % 2;
    }
}
#use Data::Dumper; 
#warn "tree:",Dumper($root),"\n"; 
 
# root's xpath should be / 
is($root->xpath(), '/');
 
# page xpath tests 
is($pages[0]->xpath, '/some:page[0]');
is($pages[1]->xpath, '/some:page[1]');
is($pages[2]->xpath, '/some:page[2]');
 
# paragraph xpath tests 
foreach my $page (@pages) {
    my @para = grep { $_->name eq 'paragraph' } $page->kids;
    for (my $x = 0; $x < $#para$x++) {
        is($para[$x]->xpath, $page->xpath . "/paragraph[$x]");
    }
    my @images = grep { $_->name eq 'image' } $page->kids;
    for (my $x = 0; $x < $#images$x++) {
        is($images[$x]->xpath, $page->xpath . "/image[$x]");
    }
}
 
# test match against returned xpaths 
is($root->match($pages[0]->xpath), 1);
is(($root->match($pages[0]->xpath))[0], $pages[0]);
is($root->match($pages[1]->xpath), 1);
is(($root->match($pages[1]->xpath))[0], $pages[1]);
is($root->match($pages[2]->xpath), 1);
is(($root->match($pages[2]->xpath))[0], $pages[2]);
 
# test paragraph xpath matching, both from the page and the root 
foreach my $page (@pages) {
    my @para = grep { $_->name eq 'paragraph' } $page->kids;
    for (my $x = 0; $x < $#para$x++) {
        is($para[$x]->match($page->xpath), 1);
        is(($para[$x]->match($page->xpath))[0], $page);
        is(($root->match($page->xpath))[0], $page);
    }
}
 
# test local name query 
is($root->match('some:page'), 3);
is(($root->match('some:page'))[0]->match('paragraph'), 10);
 
# test global  name query 
is($root->match('//paragraph'), 30);
 
# test parent context 
foreach my $page (@pages) {
    my @para = grep { $_->name eq 'paragraph' } $page->kids;
    for (my $x = 0; $x < $#para$x++) {
        is(($para[$x]->match("../paragraph[$x]"))[0], $para[$x]);
    }
}
 
# test string attribute matching 
is($root->match('some:page[@bar="bif"]'), 1);
is(($root->match('some:page[@bar="bif"]'))[0], $pages[0]);
is($root->match('some:page[@bar="bof"]'), 1);
is(($root->match('some:page[@bar="bof"]'))[0], $pages[1]);
is($root->match("some:page[\@bar='bongo']"), 1);
is(($root->match("some:page[\@bar='bongo']"))[0], $pages[2]);
 
# test numeric attribute matching 
is($root->match('some:page[@foo=10]'), 1);
is(($root->match('some:page[@foo=10]'))[0], $pages[0]);
is($root->match('some:page[@foo=20]'), 1);
is(($root->match('some:page[@foo=20]'))[0], $pages[1]);
is($root->match('some:page[@foo=30]'), 1);
is(($root->match('some:page[@foo=30]'))[0], $pages[2]);
 
is($root->match('some:page[@foo>10]'), 2);
is(($root->match('some:page[@foo>10]'))[0], $pages[1]);
is(($root->match('some:page[@foo>10]'))[1], $pages[2]);
 
is($root->match('some:page[@foo<10]'), 0);
 
is($root->match('some:page[@foo!=10]'), 2);
 
is($root->match('some:page[@foo<=10]'), 1);
 
is($root->match('some:page[@foo>=10]'), 3);
 
# test attribute value retrieval 
is($root->match('/some:page[0]/@foo'), 1);
eq_array([$root->match('/some:page/@foo')], [qw( 10 20 30 )]);
is(($root->match('/some:page[-1]/@bar'))[0], 'bongo');
eq_array([$root->match('/some:page/@bar')], [qw( bif bof bongo )]);
 
# make sure bad use of @foo is caught 
eval { $root->match('/some:page[0]/@foo/bar'); };
like($@qr/Bad call.*contains an attribute selector in the middle of the expression/);
 
# test string child matching 
is($root->match('some:page[paragraph="bif0"]'), 1, "Child node string match");
is(($root->match('some:page[paragraph="bif0"]'))[0], $pages[0]);
is($root->match('some:page[paragraph="bif3"]'), 1, "Child node string match");
is(($root->match('some:page[paragraph="bif3"]'))[0], $pages[0]);
 
is($root->match('some:page[paragraph="bof0"]'), 1, "Child node string match");
is(($root->match('some:page[paragraph="bof0"]'))[0], $pages[1]);
is($root->match('some:page[paragraph="bof3"]'), 1, "Child node string match");
is(($root->match('some:page[paragraph="bof3"]'))[0], $pages[1]);
 
is($root->match('some:page[paragraph="bongo0"]'), 1, "Child node string match");
is(($root->match('some:page[paragraph="bongo0"]'))[0], $pages[2]);
is($root->match('some:page[paragraph="bongo3"]'), 1, "Child node string match");
is(($root->match('some:page[paragraph="bongo3"]'))[0], $pages[2]);
 
# test numeric child matching 
is($root->match('some:page[kidfoo=10]'), 1, "Child node = match");
is(($root->match('some:page[kidfoo=10]'))[0], $pages[0]);
is($root->match('some:page[kidfoo=20]'), 1, "Child node = match");
is(($root->match('some:page[kidfoo=20]'))[0], $pages[1]);
is($root->match('some:page[kidfoo=30]'), 1, "Child node = match");
is(($root->match('some:page[kidfoo=30]'))[0], $pages[2]);
 
is($root->match('some:page[kidfoo>10]'), 2, "Child node > match");
is(($root->match('some:page[kidfoo>10]'))[0], $pages[1]);
is(($root->match('some:page[kidfoo>10]'))[1], $pages[2]);
 
is($root->match('some:page[kidfoo<10]'), 0, "Child node < match");
 
is($root->match('some:page[kidfoo!=10]'), 2, "Child node != match");
 
is($root->match('some:page[kidfoo<=10]'), 1, "Child node <= match");
 
is($root->match('some:page[kidfoo>=10]'), 3, "Child node >= match");
 
is($root->match('some:page[.="10bif0bif1bif2bif3bif4bif5bif6bif7bif8bif9"]'), 1,
"Complex child node string match");
is(($root->match('some:page[.="10bif0bif1bif2bif3bif4bif5bif6bif7bif8bif9"]'))[0], $pages[0]);