File-Raw-XML
view release on metacpan or search on metacpan
t/40-xpath.t view on Meta::CPAN
[ '2 > 1 > 0', 'true', 'and so the second comparison is 1 > 0' ],
);
is(str($_->[0]), $_->[1], "3.4: $_->[0] is $_->[1] ($_->[2])") for @CMP;
# ---- 3.5: the arithmetic operators -------------------------------------
is(str('1 + 2 * 3'), '7', 'multiplicative binds tighter than additive');
is(str('(1 + 2) * 3'), '9', 'and parentheses win');
is(str('- 3 + 4'), '1', 'unary minus');
is(str('5 mod 2'), '1', 'mod truncates towards zero');
is(str('5 mod -2'), '1', 'so the sign follows the left operand');
is(str('-5 mod 2'), '-1', 'as here');
is(str('-5 mod -2'), '-1', 'and here');
# ---- 4.1: the node-set functions ---------------------------------------
is(str('count(//a)'), '3', 'count');
is(str('count(//@*)'), '4', 'count over attributes');
is(str('count(//zz)'), '0', 'count of nothing');
is(str('sum(//a)'), '6', 'sum');
is(str('string(//a)'), '1', 'string of a node-set is its first node in document order');
is(str('string(//b)'), '2pe', 'and a node string-value is its descendant text');
is(str('local-name(//p:e)'), 'e', 'local-name');
is(str('namespace-uri(//p:e)'), 'urn:p', 'namespace-uri');
is(str('name(//p:e)'), 'p:e', 'name is the qualified name as written');
is(str('local-name(//@id)'), 'id', 'local-name of an attribute');
is(str('name(/r/@x:lang)'), 'xml:lang', 'name of a prefixed attribute');
is(str('local-name(//zz)'), '', 'local-name of an empty node-set is the empty string');
is(str('name(/r/namespace::p)'), 'p', 'a namespace node is named by its prefix');
is(str('string(/r/namespace::p)'), 'urn:p', 'and its string-value is the URI');
is(str('id("e1")'), '1', 'id() over the index the document was parsed with');
is(str('count(id("e1 e2"))'), '2', 'id() splits on whitespace');
is(str('count(id("nope"))'), '0', 'and finds nothing for an unknown one');
# ---- 4.2: the string functions -----------------------------------------
is(str(q{concat('a', 'b', 'c')}), 'abc', 'concat is variadic');
is(str(q{starts-with('abcd', 'ab')}), 'true', 'starts-with');
is(str(q{starts-with('abcd', 'bc')}), 'false', 'and only at the start');
is(str(q{contains('abcd', 'bc')}), 'true', 'contains');
is(str(q{contains('abcd', 'zz')}), 'false', 'and not');
is(str(q{contains('abcd', '')}), 'true', 'the empty string is contained');
is(str(q{substring-before('1999/04/01', '/')}), '1999', 'substring-before');
is(str(q{substring-after('1999/04/01', '/')}), '04/01', 'substring-after');
is(str(q{substring-before('abc', 'z')}), '', 'and the empty string when there is no match');
# an empty node-set reaches these as the empty string, and the empty
# string is a prefix and a substring of itself
is(str('contains(//zz, "")'), 'true', 'contains over an empty node-set and an empty needle');
is(str('starts-with(//zz, "")'), 'true', 'and starts-with');
is(str('contains(//zz, "x")'), 'false', 'and a needle that is not there');
is(str('substring-before(//zz, "")'), '', 'substring-before of nothing');
is(str('substring-after(//zz, "")'), '', 'substring-after of nothing');
is(str(q{substring-after('abc', '')}), 'abc', 'and after the empty prefix of a string');
# 4.2's own examples for substring, infinities included
is(str(q{substring('12345', 2, 3)}), '234', 'substring');
is(str(q{substring('12345', 2)}), '2345', 'substring to the end');
is(str(q{substring('12345', 1.5, 2.6)}), '234', 'the arguments are rounded');
is(str(q{substring('12345', 0, 3)}), '12', 'a start before the string');
is(str(q{substring('12345', 0 div 0, 3)}), '', 'a NaN start selects nothing');
is(str(q{substring('12345', 1, 0 div 0)}), '', 'nor does a NaN length');
is(str(q{substring('12345', -42, 1 div 0)}), '12345', 'an infinite length reaches the end');
is(str(q{substring('12345', -1 div 0, 1 div 0)}), '', 'and -Infinity + Infinity is NaN, so nothing');
is(str(q{string-length('hello')}), '5', 'string-length');
is(str(q{normalize-space(' a b c ')}), 'a b c', 'normalize-space strips and collapses');
is(str(q{translate('bar', 'abc', 'ABC')}), 'BAr', 'translate');
is(str(q{translate('--aaa--', 'abc-', 'ABC')}), 'AAA', 'a character with no replacement is removed');
is(str(q{translate('abcdef', 'abc', 'x')}), 'xdef', 'and so is one past the end of the replacement');
# by character, not by byte. The expression crosses the seam as its UTF-8
# bytes when it is flagged as characters and as its raw bytes when it is
# not, which is what every other input to this dist does, so a Latin-1
# byte string is not an expression and is refused.
{
my $e = q{substring('} . "\x{e9}\x{ea}\x{eb}" . q{', 2, 1)};
utf8::upgrade($e);
is(scalar $doc->xpath("string($e)"), "\x{ea}", 'substring is by character');
my $len = q{string-length('h} . "\x{e9}" . q{llo')};
utf8::upgrade($len);
is(scalar $doc->xpath("string($len)"), '5', 'string-length counts characters');
my $tr = q{translate('} . "\x{e9}" . q{x', '} . "\x{e9}" . q{', 'e')};
utf8::upgrade($tr);
is(scalar $doc->xpath("string($tr)"), 'ex', 'and so is translate');
my $latin1 = $e;
utf8::downgrade($latin1);
ok(!eval { $doc->xpath("string($latin1)"); 1 }, 'a Latin-1 byte expression is not UTF-8 and is refused');
like($@, qr/the expression is not UTF-8/, ' saying so');
}
# ---- 4.3: the boolean functions ----------------------------------------
is(str('boolean(1)'), 'true', 'boolean of a non-zero number');
is(str('boolean(0)'), 'false', 'of zero');
is(str('boolean(0 div 0)'), 'false', 'of NaN');
is(str(q{boolean('')}), 'false', 'of the empty string');
is(str(q{boolean('x')}), 'true', 'of a non-empty one');
is(str('boolean(//a)'), 'true', 'of a non-empty node-set');
is(str('boolean(//zz)'), 'false', 'of an empty one');
is(str('not(1)'), 'false', 'not');
is(str('true()'), 'true', 'true');
is(str('false()'), 'false', 'false');
is(str('local-name(//*[lang("en")])'), 'r', 'lang() matches a language subtag of xml:lang');
is(str('count(//*[lang("en")])'), '7', 'and xml:lang is inherited by every descendant');
is(str('count(//*[lang("fr")])'), '0', 'and does not match another language');
is(str('count(//*[lang("EN-gb")])'), '7', 'the comparison is case-insensitive');
is(str('count(//*[lang("en-GB-x")])'), '0', 'and a longer tag does not match a shorter value');
# ---- 4.4: the number functions -----------------------------------------
is(str(q{number(' 12 ')}), '12', 'number strips surrounding whitespace');
is(str(q{number('12.5')}), '12.5', 'a fraction');
is(str(q{number('.5')}), '0.5', 'a leading point');
is(str(q{number('-3')}), '-3', 'a minus');
is(str(q{number('12x')}), 'NaN', 'anything else is NaN');
is(str(q{number('')}), 'NaN', 'including the empty string');
is(str(q{number('1e5')}), 'NaN', 'and an exponent, which is not a Number');
is(str(q{number('+1')}), 'NaN', 'and a plus sign');
is(str('number(true())'), '1', 'a boolean numbers to 1');
is(str('number(false())'), '0', 'or 0');
is(str('floor(1.5)'), '1', 'floor');
is(str('floor(-1.5)'), '-2', 'floor of a negative');
is(str('ceiling(1.5)'), '2', 'ceiling');
is(str('ceiling(-1.5)'), '-1', 'ceiling of a negative');
is(str('round(1.5)'), '2', 'round');
is(str('round(2.5)'), '3', 'a tie goes towards positive infinity');
is(str('round(-1.5)'), '-1', 'so -1.5 rounds to -1');
is(str('round(0.5)'), '1', 'and 0.5 to 1');
is(str('round(-0.5)'), '0', 'and -0.5 to negative zero, which prints as 0');
is(str('round(0 div 0)'), 'NaN', 'round of NaN');
is(str('round(1 div 0)'), 'Infinity', 'round of an infinity');
# ---- 4.2's number to string: no exponent, shortest round trip ----------
is(str('string(0)'), '0', 'zero');
is(str('string(-0)'), '0', 'negative zero prints as 0');
is(str('string(1000000)'), '1000000', 'an integer has no fraction');
is(str('string(1 div 0)'), 'Infinity', 'positive infinity');
is(str('string(-1 div 0)'), '-Infinity', 'negative infinity');
is(str('string(0 div 0)'), 'NaN', 'NaN');
is(str('string(-1 div 0 + 1 div 0)'), 'NaN', 'and an infinity less an infinity');
is(str('string(1 div 3)'), '0.3333333333333333',
'as many digits as distinguish the value: fifteen threes is a different double');
is(str('string(0.1 + 0.2)'), '0.30000000000000004',
'and 0.1 + 0.2 is not 0.3');
is(str('string(0.000001)'), '0.000001', 'no exponent, however small');
is(str('string(0.0000001)'), '0.0000001', 'still no exponent');
is(str('string(123456789012345678901234567890)'), '123456789012345680000000000000',
'no exponent, however large, and the digits are the double the literal names');
# ---- 3.3: union --------------------------------------------------------
is(nodes('//a | //b'), 'E:{}a|E:{}b|E:{}a|E:{}a', 'a union is in document order');
is(nodes('//a | //a'), 'E:{}a|E:{}a|E:{}a', 'and has no duplicates');
is(str('count(//a | //@id)'), '6', 'attributes and elements sort together');
# ---- the Perl surface --------------------------------------------------
{
my @all = $doc->xpath('//a', ns => \%NS);
is(scalar @all, 3, 'list context gives every node');
my $one = $doc->xpath('//a', ns => \%NS);
isa_ok($one, 'File::Raw::XML::Node', 'scalar context gives one node, which');
is($one->text, '1', 'and it is the first in document order');
is(scalar($doc->xpath('//zz')), undef, 'an empty node-set in scalar context is undef');
is_deeply([$doc->xpath('//zz')], [], 'and an empty list in list context');
}
{
my ($attr) = $doc->xpath('/r/@id');
isa_ok($attr, 'File::Raw::XML::Attr');
is($attr->name, 'id', 'an attribute knows its name');
is($attr->local, 'id', 'its local part');
is($attr->ns, '', 'its namespace, empty when it has none');
is($attr->prefix, '', 'and its prefix');
is($attr->value, 'r0', 'and its value');
is($attr->owner->local, 'r', 'and the element it is on');
isa_ok($attr->doc, 'File::Raw::XML::Document', 'and its document, which');
my ($lang) = $doc->xpath('/r/@x:lang', ns => \%NS);
is($lang->name, 'xml:lang', 'a prefixed attribute keeps the name as written');
is($lang->prefix, 'xml', 'and its prefix');
is($lang->ns, 'http://www.w3.org/XML/1998/namespace', 'and its namespace');
}
{
my ($ns) = $doc->xpath('/r/namespace::p', ns => \%NS);
isa_ok($ns, 'File::Raw::XML::Namespace');
is($ns->name, 'p', 'a namespace node is named by its prefix');
is($ns->prefix, 'p', 'which is also its prefix');
is($ns->uri, 'urn:p', 'and its URI');
is($ns->value, 'urn:p', 'which is its value too');
is($ns->owner->local, 'r', 'and the element it is in scope on');
}
# a node from a result keeps its document alive, as a Node does
{
my ($node, $attr, $nsn, $weak);
{
my $d = file_xml_decode('<a xmlns:z="urn:z" k="v"><b/></a>');
$weak = $d;
( run in 1.802 second using v1.01-cache-2.11-cpan-60884a8df76 )