| 1 |
# -*- cperl -*- |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 32; |
| 7 |
|
| 8 |
use XML::LibXML; |
| 9 |
use XML::LibXML::XPathContext; |
| 10 |
|
| 11 |
my $doc = XML::LibXML->new->parse_string(<<'XML'); |
| 12 |
<foo><bar a="b">Bla</bar><bar/></foo> |
| 13 |
XML |
| 14 |
# TEST |
| 15 |
ok($doc, ' TODO : Add test name'); |
| 16 |
|
| 17 |
my $xc = XML::LibXML::XPathContext->new($doc); |
| 18 |
$xc->registerNs('foo','urn:foo'); |
| 19 |
|
| 20 |
$xc->registerFunctionNS('copy','urn:foo', |
| 21 |
sub { @_==1 ? $_[0] : die "too many parameters"} |
| 22 |
); |
| 23 |
|
| 24 |
# copy string, real, integer, nodelist |
| 25 |
# TEST |
| 26 |
ok($xc->findvalue('foo:copy("bar")') eq 'bar', ' TODO : Add test name'); |
| 27 |
# TEST |
| 28 |
|
| 29 |
ok($xc->findvalue('foo:copy(3.14)') < 3.141, ' TODO : Add test name'); # can't use == here because of |
| 30 |
# TEST |
| 31 |
|
| 32 |
ok($xc->findvalue('foo:copy(3.14)') > 3.139, ' TODO : Add test name'); # float math |
| 33 |
# TEST |
| 34 |
|
| 35 |
ok($xc->findvalue('foo:copy(7)') == 7, ' TODO : Add test name'); |
| 36 |
# TEST |
| 37 |
|
| 38 |
ok($xc->find('foo:copy(//*)')->size() == 3, ' TODO : Add test name'); |
| 39 |
my ($foo)=$xc->findnodes('(//*)[2]'); |
| 40 |
# TEST |
| 41 |
|
| 42 |
ok($xc->findnodes('foo:copy(//*)[2]')->pop->isSameNode($foo), ' TODO : Add test name'); |
| 43 |
|
| 44 |
# too many arguments |
| 45 |
eval { $xc->findvalue('foo:copy(1,xyz)') }; |
| 46 |
# TEST |
| 47 |
|
| 48 |
ok ($@, ' TODO : Add test name'); |
| 49 |
|
| 50 |
# without a namespace |
| 51 |
$xc->registerFunction('dummy', sub { 'DUMMY' }); |
| 52 |
# TEST |
| 53 |
|
| 54 |
ok($xc->findvalue('dummy()') eq 'DUMMY', ' TODO : Add test name'); |
| 55 |
|
| 56 |
# unregister it |
| 57 |
$xc->unregisterFunction('dummy'); |
| 58 |
eval { $xc->findvalue('dummy()') }; |
| 59 |
# TEST |
| 60 |
|
| 61 |
ok ($@, ' TODO : Add test name'); |
| 62 |
|
| 63 |
# retister by name |
| 64 |
sub dummy2 { 'DUMMY2' }; |
| 65 |
$xc->registerFunction('dummy2', 'dummy2'); |
| 66 |
# TEST |
| 67 |
|
| 68 |
ok($xc->findvalue('dummy2()') eq 'DUMMY2', ' TODO : Add test name'); |
| 69 |
|
| 70 |
# unregister |
| 71 |
$xc->unregisterFunction('dummy2'); |
| 72 |
eval { $xc->findvalue('dummy2()') }; |
| 73 |
# TEST |
| 74 |
|
| 75 |
ok ($@, ' TODO : Add test name'); |
| 76 |
|
| 77 |
|
| 78 |
# a mix of different arguments types |
| 79 |
$xc->registerFunction('join', |
| 80 |
sub { join shift, |
| 81 |
map { (ref($_)&&$_->isa('XML::LibXML::Node')) ? $_->nodeName : $_ } |
| 82 |
map { (ref($_)&&$_->isa('XML::LibXML::NodeList')) ? @$_ : $_ } |
| 83 |
@_ |
| 84 |
}); |
| 85 |
|
| 86 |
# TEST |
| 87 |
|
| 88 |
ok($xc->findvalue('join("","a","b","c")') eq 'abc', ' TODO : Add test name'); |
| 89 |
# TEST |
| 90 |
|
| 91 |
ok($xc->findvalue('join("-","a",/foo,//*)') eq 'a-foo-foo-bar-bar', ' TODO : Add test name'); |
| 92 |
# TEST |
| 93 |
|
| 94 |
ok($xc->findvalue('join("-",foo:copy(//*))') eq 'foo-bar-bar', ' TODO : Add test name'); |
| 95 |
|
| 96 |
# unregister foo:copy |
| 97 |
$xc->unregisterFunctionNS('copy','urn:foo'); |
| 98 |
eval { $xc->findvalue('foo:copy("bar")') }; |
| 99 |
# TEST |
| 100 |
|
| 101 |
ok ($@, ' TODO : Add test name'); |
| 102 |
|
| 103 |
# test context reentrance |
| 104 |
$xc->registerFunction('test-lock1', sub { $xc->find('string(//node())') }); |
| 105 |
$xc->registerFunction('test-lock2', sub { $xc->findnodes('//bar') }); |
| 106 |
# TEST |
| 107 |
|
| 108 |
ok($xc->find('test-lock1()') eq $xc->find('string(//node())'), ' TODO : Add test name'); |
| 109 |
# TEST |
| 110 |
|
| 111 |
ok($xc->find('count(//bar)=2'), ' TODO : Add test name'); |
| 112 |
# TEST |
| 113 |
|
| 114 |
ok($xc->find('count(test-lock2())=count(//bar)'), ' TODO : Add test name'); |
| 115 |
# TEST |
| 116 |
|
| 117 |
ok($xc->find('count(test-lock2()|//bar)=count(//bar)'), ' TODO : Add test name'); |
| 118 |
# TEST |
| 119 |
|
| 120 |
ok($xc->findnodes('test-lock2()[2]')->pop()->isSameNode($xc->findnodes('//bar[2]')), ' TODO : Add test name'); |
| 121 |
|
| 122 |
$xc->registerFunction('test-lock3', sub { $xc->findnodes('test-lock2(//bar)') }); |
| 123 |
# TEST |
| 124 |
|
| 125 |
ok($xc->find('count(test-lock2())=count(test-lock3())'), ' TODO : Add test name'); |
| 126 |
# TEST |
| 127 |
|
| 128 |
ok($xc->find('count(test-lock3())=count(//bar)'), ' TODO : Add test name'); |
| 129 |
# TEST |
| 130 |
|
| 131 |
ok($xc->find('count(test-lock3()|//bar)=count(//bar)'), ' TODO : Add test name'); |
| 132 |
|
| 133 |
# function creating new nodes |
| 134 |
$xc->registerFunction('new-foo', |
| 135 |
sub { |
| 136 |
return $doc->createElement('foo'); |
| 137 |
}); |
| 138 |
# TEST |
| 139 |
|
| 140 |
ok($xc->findnodes('new-foo()')->pop()->nodeName eq 'foo', ' TODO : Add test name'); |
| 141 |
my ($test_node) = $xc->findnodes('new-foo()'); |
| 142 |
|
| 143 |
$xc->registerFunction('new-chunk', |
| 144 |
sub { |
| 145 |
XML::LibXML->new->parse_string('<x><y><a/><a/></y><y><a/></y></x>')->find('//a') |
| 146 |
}); |
| 147 |
# TEST |
| 148 |
|
| 149 |
ok($xc->findnodes('new-chunk()')->size() == 3, ' TODO : Add test name'); |
| 150 |
my ($x)=$xc->findnodes('new-chunk()/parent::*'); |
| 151 |
# TEST |
| 152 |
|
| 153 |
ok($x->nodeName() eq 'y', ' TODO : Add test name'); |
| 154 |
# TEST |
| 155 |
|
| 156 |
ok($xc->findvalue('name(new-chunk()/parent::*)') eq 'y', ' TODO : Add test name'); |
| 157 |
# TEST |
| 158 |
|
| 159 |
ok($xc->findvalue('count(new-chunk()/parent::*)=2'), ' TODO : Add test name'); |
| 160 |
|
| 161 |
my $largedoc=XML::LibXML->new->parse_string('<a>'.('<b/>' x 3000).'</a>'); |
| 162 |
$xc->setContextNode($largedoc); |
| 163 |
$xc->registerFunction('pass1', |
| 164 |
sub { |
| 165 |
[$largedoc->findnodes('(//*)')] |
| 166 |
}); |
| 167 |
$xc->registerFunction('pass2',sub { $_[0] } ); |
| 168 |
$xc->registerVarLookupFunc( sub { [$largedoc->findnodes('(//*)')] }, undef); |
| 169 |
$largedoc->toString(); |
| 170 |
|
| 171 |
# TEST |
| 172 |
|
| 173 |
ok($xc->find('$a[name()="b"]')->size()==3000, ' TODO : Add test name'); |
| 174 |
my @pass1=$xc->findnodes('pass1()'); |
| 175 |
# TEST |
| 176 |
|
| 177 |
ok(@pass1==3001, ' TODO : Add test name'); |
| 178 |
# TEST |
| 179 |
|
| 180 |
ok($xc->find('pass2(//*)')->size()==3001, ' TODO : Add test name'); |