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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
# -*- cperl -*-
use strict;
use warnings;
use Test::More tests => 32;
use XML::LibXML;
use XML::LibXML::XPathContext;
my $doc = XML::LibXML->new->parse_string(<<'XML');
<foo><bar a="b">Bla</bar><bar/></foo>
XML
# TEST
ok($doc, ' TODO : Add test name');
my $xc = XML::LibXML::XPathContext->new($doc);
$xc->registerNs('foo','urn:foo');
$xc->registerFunctionNS('copy','urn:foo',
sub { @_==1 ? $_[0] : die "too many parameters"}
);
# copy string, real, integer, nodelist
# TEST
ok($xc->findvalue('foo:copy("bar")') eq 'bar', ' TODO : Add test name');
# TEST
ok($xc->findvalue('foo:copy(3.14)') < 3.141, ' TODO : Add test name'); # can't use == here because of
# TEST
ok($xc->findvalue('foo:copy(3.14)') > 3.139, ' TODO : Add test name'); # float math
# TEST
ok($xc->findvalue('foo:copy(7)') == 7, ' TODO : Add test name');
# TEST
ok($xc->find('foo:copy(//*)')->size() == 3, ' TODO : Add test name');
my ($foo)=$xc->findnodes('(//*)[2]');
# TEST
ok($xc->findnodes('foo:copy(//*)[2]')->pop->isSameNode($foo), ' TODO : Add test name');
# too many arguments
eval { $xc->findvalue('foo:copy(1,xyz)') };
# TEST
ok ($@, ' TODO : Add test name');
# without a namespace
$xc->registerFunction('dummy', sub { 'DUMMY' });
# TEST
ok($xc->findvalue('dummy()') eq 'DUMMY', ' TODO : Add test name');
# unregister it
$xc->unregisterFunction('dummy');
eval { $xc->findvalue('dummy()') };
# TEST
ok ($@, ' TODO : Add test name');
# retister by name
sub dummy2 { 'DUMMY2' };
$xc->registerFunction('dummy2', 'dummy2');
# TEST
ok($xc->findvalue('dummy2()') eq 'DUMMY2', ' TODO : Add test name');
# unregister
$xc->unregisterFunction('dummy2');
eval { $xc->findvalue('dummy2()') };
# TEST
ok ($@, ' TODO : Add test name');
# a mix of different arguments types
$xc->registerFunction('join',
sub { join shift,
map { (ref($_)&&$_->isa('XML::LibXML::Node')) ? $_->nodeName : $_ }
map { (ref($_)&&$_->isa('XML::LibXML::NodeList')) ? @$_ : $_ }
@_
});
# TEST
ok($xc->findvalue('join("","a","b","c")') eq 'abc', ' TODO : Add test name');
# TEST
ok($xc->findvalue('join("-","a",/foo,//*)') eq 'a-foo-foo-bar-bar', ' TODO : Add test name');
# TEST
ok($xc->findvalue('join("-",foo:copy(//*))') eq 'foo-bar-bar', ' TODO : Add test name');
# unregister foo:copy
$xc->unregisterFunctionNS('copy','urn:foo');
eval { $xc->findvalue('foo:copy("bar")') };
# TEST
ok ($@, ' TODO : Add test name');
# test context reentrance
$xc->registerFunction('test-lock1', sub { $xc->find('string(//node())') });
$xc->registerFunction('test-lock2', sub { $xc->findnodes('//bar') });
# TEST
ok($xc->find('test-lock1()') eq $xc->find('string(//node())'), ' TODO : Add test name');
# TEST
ok($xc->find('count(//bar)=2'), ' TODO : Add test name');
# TEST
ok($xc->find('count(test-lock2())=count(//bar)'), ' TODO : Add test name');
# TEST
ok($xc->find('count(test-lock2()|//bar)=count(//bar)'), ' TODO : Add test name');
# TEST
ok($xc->findnodes('test-lock2()[2]')->pop()->isSameNode($xc->findnodes('//bar[2]')), ' TODO : Add test name');
$xc->registerFunction('test-lock3', sub { $xc->findnodes('test-lock2(//bar)') });
# TEST
ok($xc->find('count(test-lock2())=count(test-lock3())'), ' TODO : Add test name');
# TEST
ok($xc->find('count(test-lock3())=count(//bar)'), ' TODO : Add test name');
# TEST
ok($xc->find('count(test-lock3()|//bar)=count(//bar)'), ' TODO : Add test name');
# function creating new nodes
$xc->registerFunction('new-foo',
sub {
return $doc->createElement('foo');
});
# TEST
ok($xc->findnodes('new-foo()')->pop()->nodeName eq 'foo', ' TODO : Add test name');
my ($test_node) = $xc->findnodes('new-foo()');
$xc->registerFunction('new-chunk',
sub {
XML::LibXML->new->parse_string('<x><y><a/><a/></y><y><a/></y></x>')->find('//a')
});
# TEST
ok($xc->findnodes('new-chunk()')->size() == 3, ' TODO : Add test name');
my ($x)=$xc->findnodes('new-chunk()/parent::*');
# TEST
ok($x->nodeName() eq 'y', ' TODO : Add test name');
# TEST
ok($xc->findvalue('name(new-chunk()/parent::*)') eq 'y', ' TODO : Add test name');
# TEST
ok($xc->findvalue('count(new-chunk()/parent::*)=2'), ' TODO : Add test name');
my $largedoc=XML::LibXML->new->parse_string('<a>'.('<b/>' x 3000).'</a>');
$xc->setContextNode($largedoc);
$xc->registerFunction('pass1',
sub {
[$largedoc->findnodes('(//*)')]
});
$xc->registerFunction('pass2',sub { $_[0] } );
$xc->registerVarLookupFunc( sub { [$largedoc->findnodes('(//*)')] }, undef);
$largedoc->toString();
# TEST
ok($xc->find('$a[name()="b"]')->size()==3000, ' TODO : Add test name');
my @pass1=$xc->findnodes('pass1()');
# TEST
ok(@pass1==3001, ' TODO : Add test name');
# TEST
ok($xc->find('pass2(//*)')->size()==3001, ' TODO : Add test name');
|