Rewrote all XPath evaluator call specs.

This commit is contained in:
Yorick Peterse 2014-11-09 23:23:52 +01:00
parent 58209cbad0
commit 28a1f1b8a9
27 changed files with 300 additions and 416 deletions

View File

@ -4,55 +4,54 @@ describe Oga::XPath::Evaluator do
context 'boolean() function' do context 'boolean() function' do
before do before do
@document = parse('<root><a>foo</a></root>') @document = parse('<root><a>foo</a></root>')
@evaluator = described_class.new(@document)
end end
example 'return true for a non empty string literal' do example 'return true for a non empty string literal' do
@evaluator.evaluate('boolean("foo")').should == true evaluate_xpath(@document, 'boolean("foo")').should == true
end end
example 'return false for an empty string' do example 'return false for an empty string' do
@evaluator.evaluate('boolean("")').should == false evaluate_xpath(@document, 'boolean("")').should == false
end end
example 'return true for a positive integer' do example 'return true for a positive integer' do
@evaluator.evaluate('boolean(10)').should == true evaluate_xpath(@document, 'boolean(10)').should == true
end end
example 'return true for a boolean true' do example 'return true for a boolean true' do
@evaluator.evaluate('boolean(true())').should == true evaluate_xpath(@document, 'boolean(true())').should == true
end end
example 'return false for a boolean false' do example 'return false for a boolean false' do
@evaluator.evaluate('boolean(false())').should == false evaluate_xpath(@document, 'boolean(false())').should == false
end end
example 'return true for a positive float' do example 'return true for a positive float' do
@evaluator.evaluate('boolean(10.5)').should == true evaluate_xpath(@document, 'boolean(10.5)').should == true
end end
example 'return true for a negative integer' do example 'return true for a negative integer' do
@evaluator.evaluate('boolean(-5)').should == true evaluate_xpath(@document, 'boolean(-5)').should == true
end end
example 'return true for a negative float' do example 'return true for a negative float' do
@evaluator.evaluate('boolean(-5.2)').should == true evaluate_xpath(@document, 'boolean(-5.2)').should == true
end end
example 'return false for a zero integer' do example 'return false for a zero integer' do
@evaluator.evaluate('boolean(0)').should == false evaluate_xpath(@document, 'boolean(0)').should == false
end end
example 'return false for a zero float' do example 'return false for a zero float' do
@evaluator.evaluate('boolean(0.0)').should == false evaluate_xpath(@document, 'boolean(0.0)').should == false
end end
example 'return true for a non empty node set' do example 'return true for a non empty node set' do
@evaluator.evaluate('boolean(root/a)').should == true evaluate_xpath(@document, 'boolean(root/a)').should == true
end end
example 'return false for an empty node set' do example 'return false for an empty node set' do
@evaluator.evaluate('boolean(root/b)').should == false evaluate_xpath(@document, 'boolean(root/b)').should == false
end end
end end
end end

View File

@ -4,27 +4,26 @@ describe Oga::XPath::Evaluator do
context 'ceiling() function' do context 'ceiling() function' do
before do before do
@document = parse('<root>10.123</root>') @document = parse('<root>10.123</root>')
@evaluator = described_class.new(@document)
end end
example 'return the ceiling of a literal number' do example 'return the ceiling of a literal number' do
@evaluator.evaluate('ceiling(10.123)').should == 11.0 evaluate_xpath(@document, 'ceiling(10.123)').should == 11.0
end end
example 'return the ceiling of a literal string' do example 'return the ceiling of a literal string' do
@evaluator.evaluate('ceiling("10.123")').should == 11.0 evaluate_xpath(@document, 'ceiling("10.123")').should == 11.0
end end
example 'return the ceiling of a node set' do example 'return the ceiling of a node set' do
@evaluator.evaluate('ceiling(root)').should == 11.0 evaluate_xpath(@document, 'ceiling(root)').should == 11.0
end end
example 'return NaN for empty node sets' do example 'return NaN for empty node sets' do
@evaluator.evaluate('ceiling(foo)').should be_nan evaluate_xpath(@document, 'ceiling(foo)').should be_nan
end end
example 'return NaN for an empty literal string' do example 'return NaN for an empty literal string' do
@evaluator.evaluate('ceiling("")').should be_nan evaluate_xpath(@document, 'ceiling("")').should be_nan
end end
end end
end end

View File

@ -4,27 +4,26 @@ describe Oga::XPath::Evaluator do
context 'concat() function' do context 'concat() function' do
before do before do
@document = parse('<root><a>foo</a><b>bar</b></root>') @document = parse('<root><a>foo</a><b>bar</b></root>')
@evaluator = described_class.new(@document)
end end
example 'concatenate two strings' do example 'concatenate two strings' do
@evaluator.evaluate('concat("foo", "bar")').should == 'foobar' evaluate_xpath(@document, 'concat("foo", "bar")').should == 'foobar'
end end
example 'concatenate two integers' do example 'concatenate two integers' do
@evaluator.evaluate('concat(10, 20)').should == '1020' evaluate_xpath(@document, 'concat(10, 20)').should == '1020'
end end
example 'concatenate two floats' do example 'concatenate two floats' do
@evaluator.evaluate('concat(10.5, 20.5)').should == '10.520.5' evaluate_xpath(@document, 'concat(10.5, 20.5)').should == '10.520.5'
end end
example 'concatenate two node sets' do example 'concatenate two node sets' do
@evaluator.evaluate('concat(root/a, root/b)').should == 'foobar' evaluate_xpath(@document, 'concat(root/a, root/b)').should == 'foobar'
end end
example 'concatenate a node set and a string' do example 'concatenate a node set and a string' do
@evaluator.evaluate('concat(root/a, "baz")').should == 'foobaz' evaluate_xpath(@document, 'concat(root/a, "baz")').should == 'foobaz'
end end
end end
end end

View File

@ -4,35 +4,34 @@ describe Oga::XPath::Evaluator do
context 'contains() function' do context 'contains() function' do
before do before do
@document = parse('<root><a>foo</a><b>foobar</b></root>') @document = parse('<root><a>foo</a><b>foobar</b></root>')
@evaluator = described_class.new(@document)
end end
example 'return true if the 1st string contains the 2nd string' do example 'return true if the 1st string contains the 2nd string' do
@evaluator.evaluate('contains("foobar", "oo")').should == true evaluate_xpath(@document, 'contains("foobar", "oo")').should == true
end end
example "return false if the 1st string doesn't contain the 2nd string" do example "return false if the 1st string doesn't contain the 2nd string" do
@evaluator.evaluate('contains("foobar", "baz")').should == false evaluate_xpath(@document, 'contains("foobar", "baz")').should == false
end end
example 'return true if the 1st node set contains the 2nd string' do example 'return true if the 1st node set contains the 2nd string' do
@evaluator.evaluate('contains(root/a, "oo")').should == true evaluate_xpath(@document, 'contains(root/a, "oo")').should == true
end end
example 'return true if the 1st node set contains the 2nd node set' do example 'return true if the 1st node set contains the 2nd node set' do
@evaluator.evaluate('contains(root/b, root/a)').should == true evaluate_xpath(@document, 'contains(root/b, root/a)').should == true
end end
example "return false if the 1st node doesn't contain the 2nd node set" do example "return false if the 1st node doesn't contain the 2nd node set" do
@evaluator.evaluate('contains(root/a, root/b)').should == false evaluate_xpath(@document, 'contains(root/a, root/b)').should == false
end end
example 'return true if the 1st string contains the 2nd node set' do example 'return true if the 1st string contains the 2nd node set' do
@evaluator.evaluate('contains("foobar", root/a)').should == true evaluate_xpath(@document, 'contains("foobar", root/a)').should == true
end end
example 'return true when using two empty strings' do example 'return true when using two empty strings' do
@evaluator.evaluate('contains("", "")').should == true evaluate_xpath(@document, 'contains("", "")').should == true
end end
end end
end end

View File

@ -4,33 +4,32 @@ describe Oga::XPath::Evaluator do
context 'count() function' do context 'count() function' do
before do before do
@document = parse('<root><a><b></b></a><a></a></root>') @document = parse('<root><a><b></b></a><a></a></root>')
@evaluator = described_class.new(@document)
end end
example 'return the amount of nodes as a Float' do example 'return the amount of nodes as a Float' do
@evaluator.evaluate('count(root)').is_a?(Float).should == true evaluate_xpath(@document, 'count(root)').is_a?(Float).should == true
end end
example 'count the amount of <root> nodes' do example 'count the amount of <root> nodes' do
@evaluator.evaluate('count(root)').should == 1 evaluate_xpath(@document, 'count(root)').should == 1
end end
example 'count the amount of <a> nodes' do example 'count the amount of <a> nodes' do
@evaluator.evaluate('count(root/a)').should == 2 evaluate_xpath(@document, 'count(root/a)').should == 2
end end
example 'count the amount of <b> nodes' do example 'count the amount of <b> nodes' do
@evaluator.evaluate('count(root/a/b)').should == 1 evaluate_xpath(@document, 'count(root/a/b)').should == 1
end end
example 'raise ArgumentError if no arguments are given' do example 'raise ArgumentError if no arguments are given' do
block = lambda { @evaluator.evaluate('count()') } block = -> { evaluate_xpath(@document, 'count()') }
block.should raise_error(ArgumentError) block.should raise_error(ArgumentError)
end end
example 'raise TypeError if the argument is not a NodeSet' do example 'raise TypeError if the argument is not a NodeSet' do
block = lambda { @evaluator.evaluate('count(1)') } block = -> { evaluate_xpath(@document, 'count(1)') }
block.should raise_error(TypeError) block.should raise_error(TypeError)
end end

View File

@ -3,11 +3,11 @@ require 'spec_helper'
describe Oga::XPath::Evaluator do describe Oga::XPath::Evaluator do
context 'false() function' do context 'false() function' do
before do before do
@evaluator = described_class.new(parse('')) @document = parse('')
end end
example 'return false' do example 'return false' do
@evaluator.evaluate('false()').should == false evaluate_xpath(@document, 'false()').should == false
end end
end end
end end

View File

@ -4,27 +4,26 @@ describe Oga::XPath::Evaluator do
context 'floor() function' do context 'floor() function' do
before do before do
@document = parse('<root>10.123</root>') @document = parse('<root>10.123</root>')
@evaluator = described_class.new(@document)
end end
example 'return the floor of a literal number' do example 'return the floor of a literal number' do
@evaluator.evaluate('floor(10.123)').should == 10.0 evaluate_xpath(@document, 'floor(10.123)').should == 10.0
end end
example 'return the floor of a literal string' do example 'return the floor of a literal string' do
@evaluator.evaluate('floor("10.123")').should == 10.0 evaluate_xpath(@document, 'floor("10.123")').should == 10.0
end end
example 'return the floor of a node set' do example 'return the floor of a node set' do
@evaluator.evaluate('floor(root)').should == 10.0 evaluate_xpath(@document, 'floor(root)').should == 10.0
end end
example 'return NaN for empty node sets' do example 'return NaN for empty node sets' do
@evaluator.evaluate('floor(foo)').should be_nan evaluate_xpath(@document, 'floor(foo)').should be_nan
end end
example 'return NaN for an empty literal string' do example 'return NaN for an empty literal string' do
@evaluator.evaluate('floor("")').should be_nan evaluate_xpath(@document, 'floor("")').should be_nan
end end
end end
end end

View File

@ -4,49 +4,21 @@ describe Oga::XPath::Evaluator do
context 'id() function' do context 'id() function' do
before do before do
@document = parse('<root id="r1"><a id="a1"></a><a id="a2">a1</a></root>') @document = parse('<root id="r1"><a id="a1"></a><a id="a2">a1</a></root>')
@first_a = @document.children[0].children[0]
@second_a = @document.children[0].children[1] @a1 = @document.children[0].children[0]
@evaluator = described_class.new(@document) @a2 = @document.children[0].children[1]
end end
context 'using a single string ID' do example 'return a node set containing the nodes with ID "a1"' do
before do evaluate_xpath(@document, 'id("a1")').should == node_set(@a1)
@set = @evaluator.evaluate('id("a1")')
end end
it_behaves_like :node_set, :length => 1 example 'return a node set containing the nodes with ID "a1" or "a2"' do
evaluate_xpath(@document, 'id("a1 a2")').should == node_set(@a1, @a2)
example 'return the first <a> node' do
@set[0].should == @first_a
end
end end
context 'using multiple string IDs' do example 'return a node set containing the nodes with an ID based on a path' do
before do evaluate_xpath(@document, 'id(root/a[2])').should == node_set(@a1)
@set = @evaluator.evaluate('id("a1 a2")')
end
it_behaves_like :node_set, :length => 2
example 'return the first <a> node' do
@set[0].should == @first_a
end
example 'return the second <a> node' do
@set[1].should == @second_a
end
end
context 'using a node set' do
before do
@set = @evaluator.evaluate('id(root/a[2])')
end
it_behaves_like :node_set, :length => 1
example 'return the first <a> node' do
@set[0].should == @first_a
end
end end
end end
end end

View File

@ -4,43 +4,22 @@ describe Oga::XPath::Evaluator do
context 'lang() function' do context 'lang() function' do
before do before do
@document = parse('<root xml:lang="en"><a></a><a xml:lang="nl"></a></root>') @document = parse('<root xml:lang="en"><a></a><a xml:lang="nl"></a></root>')
@evaluator = described_class.new(@document)
@root = @document.children[0]
@a1 = @root.children[0]
@a2 = @root.children[1]
end end
context 'selecting nodes with lang="en"' do example 'return a node set containing nodes with language "en"' do
before do evaluate_xpath(@document, 'root[lang("en")]').should == node_set(@root)
@set = @evaluator.evaluate('root[lang("en")]')
end end
it_behaves_like :node_set, :length => 1 example 'return a node set containing the nodes with language "nl"' do
evaluate_xpath(@document, 'root/a[lang("nl")]').should == node_set(@a2)
example 'return the <root> node' do
@set[0].should == @document.children[0]
end
end end
context 'selecting nodes with lang="nl"' do example 'return a node set containing the nodes with an inherited language' do
before do evaluate_xpath(@document, 'root/a[lang("en")]').should == node_set(@a1)
@set = @evaluator.evaluate('root/a[lang("nl")]')
end
it_behaves_like :node_set, :length => 1
example 'return the second <a> node' do
@set[0].should == @document.children[0].children[1]
end
end
context 'inheriting the language from ancestor nodes' do
before do
@set = @evaluator.evaluate('root/a[lang("en")]')
end
it_behaves_like :node_set, :length => 1
example 'return the first <a> node' do
@set[0].should == @document.children[0].children[0]
end
end end
end end
end end

View File

@ -4,14 +4,12 @@ describe Oga::XPath::Evaluator do
context 'last() function' do context 'last() function' do
before do before do
@document = parse('<root><a>foo</a><a>bar</a></root>') @document = parse('<root><a>foo</a><a>bar</a></root>')
@second_a = @document.children[0].children[1]
@set = described_class.new(@document).evaluate('root/a[last()]') @a2 = @document.children[0].children[1]
end end
it_behaves_like :node_set, :length => 1 example 'return a node set containing the last node' do
evaluate_xpath(@document, 'root/a[last()]').should == node_set(@a2)
example 'return the second <a> node' do
@set[0].should == @second_a
end end
end end
end end

View File

@ -4,47 +4,37 @@ describe Oga::XPath::Evaluator do
context 'local-name() function' do context 'local-name() function' do
before do before do
@document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>') @document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>')
@evaluator = described_class.new(@document)
end end
context 'outside predicates' do
example 'return the local name of the <x:a> node' do example 'return the local name of the <x:a> node' do
@evaluator.evaluate('local-name(root/x:a)').should == 'a' evaluate_xpath(@document, 'local-name(root/x:a)').should == 'a'
end end
example 'return the local name of the <b> node' do example 'return the local name of the <b> node' do
@evaluator.evaluate('local-name(root/b)').should == 'b' evaluate_xpath(@document, 'local-name(root/b)').should == 'b'
end end
example 'return the local name for the "num" attribute' do example 'return the local name for the "num" attribute' do
@evaluator.evaluate('local-name(root/b/@x:num)').should == 'num' evaluate_xpath(@document, 'local-name(root/b/@x:num)').should == 'num'
end end
example 'return only the name of the first node in the set' do example 'return only the name of the first node in the set' do
@evaluator.evaluate('local-name(root/*)').should == 'a' evaluate_xpath(@document, 'local-name(root/*)').should == 'a'
end end
example 'return an empty string by default' do example 'return an empty string by default' do
@evaluator.evaluate('local-name(foo)').should == '' evaluate_xpath(@document, 'local-name(foo)').should == ''
end end
example 'raise a TypeError for invalid argument types' do example 'raise a TypeError for invalid argument types' do
block = lambda { @evaluator.evaluate('local-name("foo")') } block = -> { evaluate_xpath(@document, 'local-name("foo")') }
block.should raise_error(TypeError) block.should raise_error(TypeError)
end end
end
context 'inside predicates' do example 'return a node set containing nodes with a local name' do
before do evaluate_xpath(@document, 'root/b[local-name()]')
@set = @evaluator.evaluate('root/b[local-name()]') .should == node_set(@document.children[0].children[1])
end
it_behaves_like :node_set, :length => 1
example 'return the <b> node' do
@set[0].should == @document.children[0].children[1]
end
end end
end end
end end

View File

@ -4,47 +4,37 @@ describe Oga::XPath::Evaluator do
context 'name() function' do context 'name() function' do
before do before do
@document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>') @document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>')
@evaluator = described_class.new(@document)
end end
context 'outside predicates' do
example 'return the name of the <x:a> node' do example 'return the name of the <x:a> node' do
@evaluator.evaluate('name(root/x:a)').should == 'x:a' evaluate_xpath(@document, 'name(root/x:a)').should == 'x:a'
end end
example 'return the name of the <b> node' do example 'return the name of the <b> node' do
@evaluator.evaluate('name(root/b)').should == 'b' evaluate_xpath(@document, 'name(root/b)').should == 'b'
end end
example 'return the local name for the "num" attribute' do example 'return the local name for the "num" attribute' do
@evaluator.evaluate('name(root/b/@x:num)').should == 'x:num' evaluate_xpath(@document, 'name(root/b/@x:num)').should == 'x:num'
end end
example 'return only the name of the first node in the set' do example 'return only the name of the first node in the set' do
@evaluator.evaluate('name(root/*)').should == 'x:a' evaluate_xpath(@document, 'name(root/*)').should == 'x:a'
end end
example 'return an empty string by default' do example 'return an empty string by default' do
@evaluator.evaluate('name(foo)').should == '' evaluate_xpath(@document, 'name(foo)').should == ''
end end
example 'raise a TypeError for invalid argument types' do example 'raise a TypeError for invalid argument types' do
block = lambda { @evaluator.evaluate('name("foo")') } block = -> { evaluate_xpath(@document, 'name("foo")') }
block.should raise_error(TypeError) block.should raise_error(TypeError)
end end
end
context 'inside predicates' do example 'return a node set containing nodes with a name' do
before do evaluate_xpath(@document, 'root/b[name()]')
@set = @evaluator.evaluate('root/b[name()]') .should == node_set(@document.children[0].children[1])
end
it_behaves_like :node_set, :length => 1
example 'return the <b> node' do
@set[0].should == @document.children[0].children[1]
end
end end
end end
end end

View File

@ -4,39 +4,29 @@ describe Oga::XPath::Evaluator do
context 'namespace-uri() function' do context 'namespace-uri() function' do
before do before do
@document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>') @document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>')
@evaluator = described_class.new(@document)
end end
context 'outside predicates' do
example 'return the namespace URI of the <x:a> node' do example 'return the namespace URI of the <x:a> node' do
@evaluator.evaluate('namespace-uri(root/x:a)').should == 'y' evaluate_xpath(@document, 'namespace-uri(root/x:a)').should == 'y'
end end
example 'return the namespace URI of the "num" attribute' do example 'return the namespace URI of the "num" attribute' do
@evaluator.evaluate('namespace-uri(root/b/@x:num)').should == 'y' evaluate_xpath(@document, 'namespace-uri(root/b/@x:num)').should == 'y'
end end
example 'return an empty string when there is no namespace URI' do example 'return an empty string when there is no namespace URI' do
@evaluator.evaluate('namespace-uri(root/b)').should == '' evaluate_xpath(@document, 'namespace-uri(root/b)').should == ''
end end
example 'raise TypeError for invalid argument types' do example 'raise TypeError for invalid argument types' do
block = lambda { @evaluator.evaluate('namespace-uri("foo")') } block = -> { evaluate_xpath(@document, 'namespace-uri("foo")') }
block.should raise_error(TypeError) block.should raise_error(TypeError)
end end
end
context 'inside predicates' do example 'return a node set containing nodes with a namespace URI' do
before do evaluate_xpath(@document, 'root/*[namespace-uri()]')
@set = @evaluator.evaluate('root/*[namespace-uri()]') .should == node_set(@document.children[0].children[0])
end
it_behaves_like :node_set, :length => 1
example 'return the <x:a> node' do
@set[0].should == @document.children[0].children[0]
end
end end
end end
end end

View File

@ -4,37 +4,27 @@ describe Oga::XPath::Evaluator do
context 'normalize-space() function' do context 'normalize-space() function' do
before do before do
@document = parse('<root><a> fo o </a></root>') @document = parse('<root><a> fo o </a></root>')
@evaluator = described_class.new(@document)
end end
context 'outside predicates' do
example 'normalize a literal string' do example 'normalize a literal string' do
@evaluator.evaluate('normalize-space(" fo o ")').should == 'fo o' evaluate_xpath(@document, 'normalize-space(" fo o ")').should == 'fo o'
end end
example 'normalize a string in a node set' do example 'normalize a string in a node set' do
@evaluator.evaluate('normalize-space(root/a)').should == 'fo o' evaluate_xpath(@document, 'normalize-space(root/a)').should == 'fo o'
end end
example 'normalize an integer' do example 'normalize an integer' do
@evaluator.evaluate('normalize-space(10)').should == '10' evaluate_xpath(@document, 'normalize-space(10)').should == '10'
end end
example 'normalize a float' do example 'normalize a float' do
@evaluator.evaluate('normalize-space(10.5)').should == '10.5' evaluate_xpath(@document, 'normalize-space(10.5)').should == '10.5'
end
end end
context 'inside predicates' do example 'return a node set containing nodes with normalized spaces' do
before do evaluate_xpath(@document, 'root/a[normalize-space()]')
@set = @evaluator.evaluate('root/a[normalize-space()]') .should == node_set(@document.children[0].children[0])
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].should == @document.children[0].children[0]
end
end end
end end
end end

View File

@ -4,23 +4,22 @@ describe Oga::XPath::Evaluator do
context 'not() function' do context 'not() function' do
before do before do
@document = parse('<root>foo</root>') @document = parse('<root>foo</root>')
@evaluator = described_class.new(@document)
end end
example 'return false when the argument is a non-zero integer' do example 'return false when the argument is a non-zero integer' do
@evaluator.evaluate('not(10)').should == false evaluate_xpath(@document, 'not(10)').should == false
end end
example 'return true when the argument is a zero integer' do example 'return true when the argument is a zero integer' do
@evaluator.evaluate('not(0)').should == true evaluate_xpath(@document, 'not(0)').should == true
end end
example 'return false when the argument is a non-empty node set' do example 'return false when the argument is a non-empty node set' do
@evaluator.evaluate('not(root)').should == false evaluate_xpath(@document, 'not(root)').should == false
end end
example 'return itrue when the argument is an empty node set' do example 'return itrue when the argument is an empty node set' do
@evaluator.evaluate('not(foo)').should == true evaluate_xpath(@document, 'not(foo)').should == true
end end
end end
end end

View File

@ -4,47 +4,46 @@ describe Oga::XPath::Evaluator do
context 'number() function' do context 'number() function' do
before do before do
@document = parse('<root><a>10</a><b>10.5</b><!--10--></root>') @document = parse('<root><a>10</a><b>10.5</b><!--10--></root>')
@evaluator = described_class.new(@document)
end end
example 'convert a literal string to a number' do example 'convert a literal string to a number' do
@evaluator.evaluate('number("10")').should == 10.0 evaluate_xpath(@document, 'number("10")').should == 10.0
end end
example 'convert a literal string with deciamsl to a number' do example 'convert a literal string with deciamsl to a number' do
@evaluator.evaluate('number("10.5")').should == 10.5 evaluate_xpath(@document, 'number("10.5")').should == 10.5
end end
example 'convert boolean true to a number' do example 'convert boolean true to a number' do
@evaluator.evaluate('number(true())').should == 1.0 evaluate_xpath(@document, 'number(true())').should == 1.0
end end
example 'convert boolean false to a number' do example 'convert boolean false to a number' do
@evaluator.evaluate('number(false())').should be_zero evaluate_xpath(@document, 'number(false())').should be_zero
end end
example 'convert a node set to a number' do example 'convert a node set to a number' do
@evaluator.evaluate('number(root/a)').should == 10.0 evaluate_xpath(@document, 'number(root/a)').should == 10.0
end end
example 'convert a node set with decimals to a number' do example 'convert a node set with decimals to a number' do
@evaluator.evaluate('number(root/b)').should == 10.5 evaluate_xpath(@document, 'number(root/b)').should == 10.5
end end
example 'convert a comment to a number' do example 'convert a comment to a number' do
@evaluator.evaluate('number(root/comment())').should == 10.0 evaluate_xpath(@document, 'number(root/comment())').should == 10.0
end end
example 'return NaN for values that can not be converted to floats' do example 'return NaN for values that can not be converted to floats' do
@evaluator.evaluate('number("a")').should be_nan evaluate_xpath(@document, 'number("a")').should be_nan
end end
example 'return NaN for empty node sets' do example 'return NaN for empty node sets' do
@evaluator.evaluate('number(foo)').should be_nan evaluate_xpath(@document, 'number(foo)').should be_nan
end end
example 'return NaN for empty strings' do example 'return NaN for empty strings' do
@evaluator.evaluate('number("")').should be_nan evaluate_xpath(@document, 'number("")').should be_nan
end end
end end
end end

View File

@ -4,17 +4,19 @@ describe Oga::XPath::Evaluator do
context 'position() function' do context 'position() function' do
before do before do
@document = parse('<root><a>foo</a><a>bar</a></root>') @document = parse('<root><a>foo</a><a>bar</a></root>')
@set = described_class.new(@document).evaluate('root/a[position()]')
@a1 = @document.children[0].children[0]
@a2 = @document.children[0].children[1]
end end
it_behaves_like :node_set, :length => 2 example 'return a node set containing the first node' do
evaluate_xpath(@document, 'root/a[position() = 1]')
example 'return the first <a> node' do .should == node_set(@a1)
@set[0].should == @document.children[0].children[0]
end end
example 'return the second <a> node' do example 'return a node set containing the second node' do
@set[1].should == @document.children[0].children[1] evaluate_xpath(@document, 'root/a[position() = 2]')
.should == node_set(@a2)
end end
end end
end end

View File

@ -4,27 +4,26 @@ describe Oga::XPath::Evaluator do
context 'round() function' do context 'round() function' do
before do before do
@document = parse('<root>10.123</root>') @document = parse('<root>10.123</root>')
@evaluator = described_class.new(@document)
end end
example 'return the rounded value of a literal number' do example 'return the rounded value of a literal number' do
@evaluator.evaluate('round(10.123)').should == 10.0 evaluate_xpath(@document, 'round(10.123)').should == 10.0
end end
example 'return the rounded value of a literal string' do example 'return the rounded value of a literal string' do
@evaluator.evaluate('round("10.123")').should == 10.0 evaluate_xpath(@document, 'round("10.123")').should == 10.0
end end
example 'return the rounded value of a node set' do example 'return the rounded value of a node set' do
@evaluator.evaluate('round(root)').should == 10.0 evaluate_xpath(@document, 'round(root)').should == 10.0
end end
example 'return NaN for empty node sets' do example 'return NaN for empty node sets' do
@evaluator.evaluate('round(foo)').should be_nan evaluate_xpath(@document, 'round(foo)').should be_nan
end end
example 'return NaN for an empty literal string' do example 'return NaN for an empty literal string' do
@evaluator.evaluate('round("")').should be_nan evaluate_xpath(@document, 'round("")').should be_nan
end end
end end
end end

View File

@ -4,35 +4,34 @@ describe Oga::XPath::Evaluator do
context 'starts-with() function' do context 'starts-with() function' do
before do before do
@document = parse('<root><a>foo</a><b>foobar</b></root>') @document = parse('<root><a>foo</a><b>foobar</b></root>')
@evaluator = described_class.new(@document)
end end
example 'return true if the 1st string starts with the 2nd string' do example 'return true if the 1st string starts with the 2nd string' do
@evaluator.evaluate('starts-with("foobar", "foo")').should == true evaluate_xpath(@document, 'starts-with("foobar", "foo")').should == true
end end
example "return false if the 1st string doesn't start with the 2nd string" do example "return false if the 1st string doesn't start with the 2nd string" do
@evaluator.evaluate('starts-with("foobar", "baz")').should == false evaluate_xpath(@document, 'starts-with("foobar", "baz")').should == false
end end
example 'return true if the 1st node set starts with the 2nd string' do example 'return true if the 1st node set starts with the 2nd string' do
@evaluator.evaluate('starts-with(root/a, "foo")').should == true evaluate_xpath(@document, 'starts-with(root/a, "foo")').should == true
end end
example 'return true if the 1st node set starts with the 2nd node set' do example 'return true if the 1st node set starts with the 2nd node set' do
@evaluator.evaluate('starts-with(root/b, root/a)').should == true evaluate_xpath(@document, 'starts-with(root/b, root/a)').should == true
end end
example "return false if the 1st node set doesn't start with the 2nd string" do example "return false if the 1st node set doesn't start with the 2nd string" do
@evaluator.evaluate('starts-with(root/a, "baz")').should == false evaluate_xpath(@document, 'starts-with(root/a, "baz")').should == false
end end
example 'return true if the 1st string starts with the 2nd node set' do example 'return true if the 1st string starts with the 2nd node set' do
@evaluator.evaluate('starts-with("foobar", root/a)').should == true evaluate_xpath(@document, 'starts-with("foobar", root/a)').should == true
end end
example 'return true when using two empty strings' do example 'return true when using two empty strings' do
@evaluator.evaluate('starts-with("", "")').should == true evaluate_xpath(@document, 'starts-with("", "")').should == true
end end
end end
end end

View File

@ -4,39 +4,28 @@ describe Oga::XPath::Evaluator do
context 'string-length() function' do context 'string-length() function' do
before do before do
@document = parse('<root><a>x</a></root>') @document = parse('<root><a>x</a></root>')
@evaluator = described_class.new(@document)
end end
context 'outside predicates' do
example 'return the length of a literal string' do example 'return the length of a literal string' do
@evaluator.evaluate('string-length("foo")').should == 3.0 evaluate_xpath(@document, 'string-length("foo")').should == 3.0
end end
example 'return the length of a literal integer' do example 'return the length of a literal integer' do
@evaluator.evaluate('string-length(10)').should == 2.0 evaluate_xpath(@document, 'string-length(10)').should == 2.0
end end
example 'return the length of a literal float' do example 'return the length of a literal float' do
# This includes the counting of the dot. That is, "10.5".length => 4 # This includes the counting of the dot. That is, "10.5".length => 4
@evaluator.evaluate('string-length(10.5)').should == 4.0 evaluate_xpath(@document, 'string-length(10.5)').should == 4.0
end end
example 'return the length of a string in a node set' do example 'return the length of a string in a node set' do
@evaluator.evaluate('string-length(root)').should == 1.0 evaluate_xpath(@document, 'string-length(root)').should == 1.0
end
end end
context 'inside predicates' do example 'return a node set containing nodes with a specific text length' do
before do evaluate_xpath(@document, 'root/a[string-length() = 1]')
# Since the length of <a> is 1 this should just return the <a> node. .should == node_set(@document.children[0].children[0])
@set = @evaluator.evaluate('root/a[string-length()]')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].should == @document.children[0].children[0]
end
end end
end end
end end

View File

@ -11,66 +11,58 @@ describe Oga::XPath::Evaluator do
<d><![CDATA[foobar]]></d> <d><![CDATA[foobar]]></d>
</root> </root>
EOF EOF
@evaluator = described_class.new(@document)
end end
context 'outside predicates' do
example 'convert the <root> node to a string' do example 'convert the <root> node to a string' do
@evaluator.evaluate('string(root)') evaluate_xpath(@document, 'string(root)')
.should == "\n a1\n b1\n \n foobar\n" .should == "\n a1\n b1\n \n foobar\n"
end end
example 'convert the <a> node to a string' do example 'convert the <a> node to a string' do
@evaluator.evaluate('string(root/a)').should == 'a1' evaluate_xpath(@document, 'string(root/a)').should == 'a1'
end end
example 'convert the <b> node to a string' do example 'convert the <b> node to a string' do
@evaluator.evaluate('string(root/b)').should == 'b1' evaluate_xpath(@document, 'string(root/b)').should == 'b1'
end end
example 'convert the "num" attribute to a string' do example 'convert the "num" attribute to a string' do
@evaluator.evaluate('string(root/b/@num)').should == '10' evaluate_xpath(@document, 'string(root/b/@num)').should == '10'
end end
example 'convert the first node in a set to a string' do example 'convert the first node in a set to a string' do
@evaluator.evaluate('string(root/*)').should == 'a1' evaluate_xpath(@document, 'string(root/*)').should == 'a1'
end end
example 'convert an integer to a string' do example 'convert an integer to a string' do
@evaluator.evaluate('string(10)').should == '10' evaluate_xpath(@document, 'string(10)').should == '10'
end end
example 'convert a float to a string' do example 'convert a float to a string' do
@evaluator.evaluate('string(10.5)').should == '10.5' evaluate_xpath(@document, 'string(10.5)').should == '10.5'
end end
example 'convert a string to a string' do example 'convert a string to a string' do
@evaluator.evaluate('string("foo")').should == 'foo' evaluate_xpath(@document, 'string("foo")').should == 'foo'
end end
example 'convert a comment to a string' do example 'convert a comment to a string' do
@evaluator.evaluate('string(root/c/comment())').should == 'foo' evaluate_xpath(@document, 'string(root/c/comment())').should == 'foo'
end end
example 'convert a CDATA to a string' do example 'convert a CDATA to a string' do
@evaluator.evaluate('string(root/d/node())').should == 'foobar' evaluate_xpath(@document, 'string(root/d/node())').should == 'foobar'
end end
example 'return an empty string by default' do example 'return an empty string by default' do
@evaluator.evaluate('string(foobar)').should == '' evaluate_xpath(@document, 'string(foobar)').should == ''
end
end end
context 'inside predicates' do example 'return a node set containing nodes with certain text' do
before do b = @document.children[0].children[1].next_element
@set = @evaluator.evaluate('root/b[string()]')
end
it_behaves_like :node_set, :length => 1 evaluate_xpath(@document, 'root/b[string() = "b1"]')
.should == node_set(b)
example 'return the <b> node' do
@set[0].name.should == 'b'
end
end end
end end
end end

View File

@ -4,27 +4,27 @@ describe Oga::XPath::Evaluator do
context 'substring-after() function' do context 'substring-after() function' do
before do before do
@document = parse('<root><a>-</a><b>a-b-c</b></root>') @document = parse('<root><a>-</a><b>a-b-c</b></root>')
@evaluator = described_class.new(@document)
end end
example 'return the substring of the 1st string after the 2nd string' do example 'return the substring of the 1st string after the 2nd string' do
@evaluator.evaluate('substring-after("a-b-c", "-")').should == 'b-c' evaluate_xpath(@document, 'substring-after("a-b-c", "-")').should == 'b-c'
end end
example 'return an empty string if the 2nd string is not present' do example 'return an empty string if the 2nd string is not present' do
@evaluator.evaluate('substring-after("a-b-c", "x")').should == '' evaluate_xpath(@document, 'substring-after("a-b-c", "x")').should == ''
end end
example 'return the substring of the 1st node set after the 2nd string' do example 'return the substring of the 1st node set after the 2nd string' do
@evaluator.evaluate('substring-after(root/b, "-")').should == 'b-c' evaluate_xpath(@document, 'substring-after(root/b, "-")').should == 'b-c'
end end
example 'return the substring of the 1st node set after the 2nd node set' do example 'return the substring of the 1st node set after the 2nd node set' do
@evaluator.evaluate('substring-after(root/b, root/a)').should == 'b-c' evaluate_xpath(@document, 'substring-after(root/b, root/a)')
.should == 'b-c'
end end
example 'return an empty string when using two empty strings' do example 'return an empty string when using two empty strings' do
@evaluator.evaluate('substring-after("", "")').should == '' evaluate_xpath(@document, 'substring-after("", "")').should == ''
end end
end end
end end

View File

@ -4,27 +4,27 @@ describe Oga::XPath::Evaluator do
context 'substring-before() function' do context 'substring-before() function' do
before do before do
@document = parse('<root><a>-</a><b>a-b-c</b></root>') @document = parse('<root><a>-</a><b>a-b-c</b></root>')
@evaluator = described_class.new(@document)
end end
example 'return the substring of the 1st string before the 2nd string' do example 'return the substring of the 1st string before the 2nd string' do
@evaluator.evaluate('substring-before("a-b-c", "-")').should == 'a' evaluate_xpath(@document, 'substring-before("a-b-c", "-")').should == 'a'
end end
example 'return an empty string if the 2nd string is not present' do example 'return an empty string if the 2nd string is not present' do
@evaluator.evaluate('substring-before("a-b-c", "x")').should == '' evaluate_xpath(@document, 'substring-before("a-b-c", "x")').should == ''
end end
example 'return the substring of the 1st node set before the 2nd string' do example 'return the substring of the 1st node set before the 2nd string' do
@evaluator.evaluate('substring-before(root/b, "-")').should == 'a' evaluate_xpath(@document, 'substring-before(root/b, "-")').should == 'a'
end end
example 'return the substring of the 1st node set before the 2nd node set' do example 'return the substring of the 1st node set before the 2nd node set' do
@evaluator.evaluate('substring-before(root/b, root/a)').should == 'a' evaluate_xpath(@document, 'substring-before(root/b, root/a)')
.should == 'a'
end end
example 'return an empty string when using two empty strings' do example 'return an empty string when using two empty strings' do
@evaluator.evaluate('substring-before("", "")').should == '' evaluate_xpath(@document, 'substring-before("", "")').should == ''
end end
end end
end end

View File

@ -4,27 +4,26 @@ describe Oga::XPath::Evaluator do
context 'substring() function' do context 'substring() function' do
before do before do
@document = parse('<root><a>foobar</a><b>3</b></root>') @document = parse('<root><a>foobar</a><b>3</b></root>')
@evaluator = described_class.new(@document)
end end
example 'return the substring of a string' do example 'return the substring of a string' do
@evaluator.evaluate('substring("foo", 2)').should == 'oo' evaluate_xpath(@document, 'substring("foo", 2)').should == 'oo'
end end
example 'return the substring of a string using a custom length' do example 'return the substring of a string using a custom length' do
@evaluator.evaluate('substring("foo", 2, 1)').should == 'o' evaluate_xpath(@document, 'substring("foo", 2, 1)').should == 'o'
end end
example 'return the substring of a node set' do example 'return the substring of a node set' do
@evaluator.evaluate('substring(root/a, 2)').should == 'oobar' evaluate_xpath(@document, 'substring(root/a, 2)').should == 'oobar'
end end
example 'return the substring of a node set with a node set as the length' do example 'return the substring of a node set with a node set as the length' do
@evaluator.evaluate('substring(root/a, 1, root/b)').should == 'foo' evaluate_xpath(@document, 'substring(root/a, 1, root/b)').should == 'foo'
end end
example 'return an empty string when the source string is empty' do example 'return an empty string when the source string is empty' do
@evaluator.evaluate('substring("", 1, 3)').should == '' evaluate_xpath(@document, 'substring("", 1, 3)').should == ''
end end
end end
end end

View File

@ -4,24 +4,23 @@ describe Oga::XPath::Evaluator do
context 'sum() spec' do context 'sum() spec' do
before do before do
@document = parse('<root><a>1</a><b>2</b></root>') @document = parse('<root><a>1</a><b>2</b></root>')
@evaluator = described_class.new(@document)
end end
example 'return the sum of the <root> node' do example 'return the sum of the <root> node' do
# The test of <root> is "12", which is then converted to a number. # The test of <root> is "12", which is then converted to a number.
@evaluator.evaluate('sum(root)').should == 12.0 evaluate_xpath(@document, 'sum(root)').should == 12.0
end end
example 'return the sum of the child nodes of the <root> node' do example 'return the sum of the child nodes of the <root> node' do
@evaluator.evaluate('sum(root/*)').should == 3.0 evaluate_xpath(@document, 'sum(root/*)').should == 3.0
end end
example 'return zero by default' do example 'return zero by default' do
@evaluator.evaluate('sum(foo)').should be_zero evaluate_xpath(@document, 'sum(foo)').should be_zero
end end
example 'raise a TypeError for non node set arguments' do example 'raise a TypeError for non node set arguments' do
block = lambda { @evaluator.evaluate('sum("foo")') } block = -> { evaluate_xpath(@document, 'sum("foo")') }
block.should raise_error(TypeError) block.should raise_error(TypeError)
end end

View File

@ -4,31 +4,35 @@ describe Oga::XPath::Evaluator do
context 'translate() function' do context 'translate() function' do
before do before do
@document = parse('<root><a>bar</a><b>abc</b><c>ABC</c></root>') @document = parse('<root><a>bar</a><b>abc</b><c>ABC</c></root>')
@evaluator = described_class.new(@document)
end end
example 'translate a string using all string literals' do example 'translate a string using all string literals' do
@evaluator.evaluate('translate("bar", "abc", "ABC")').should == 'BAr' evaluate_xpath(@document, 'translate("bar", "abc", "ABC")')
.should == 'BAr'
end end
example "remove characters that don't occur in the replacement string" do example "remove characters that don't occur in the replacement string" do
@evaluator.evaluate('translate("-aaa-", "abc-", "ABC")').should == 'AAA' evaluate_xpath(@document, 'translate("-aaa-", "abc-", "ABC")')
.should == 'AAA'
end end
example 'use the first character occurence in the search string' do example 'use the first character occurence in the search string' do
@evaluator.evaluate('translate("ab", "aba", "123")').should == '12' evaluate_xpath(@document, 'translate("ab", "aba", "123")').should == '12'
end end
example 'ignore excess characters in the replacement string' do example 'ignore excess characters in the replacement string' do
@evaluator.evaluate('translate("abc", "abc", "123456")').should == '123' evaluate_xpath(@document, 'translate("abc", "abc", "123456")')
.should == '123'
end end
example 'translate a node set string using string literals' do example 'translate a node set string using string literals' do
@evaluator.evaluate('translate(root/a, "abc", "ABC")').should == 'BAr' evaluate_xpath(@document, 'translate(root/a, "abc", "ABC")')
.should == 'BAr'
end end
example 'translate a node set string using other node set strings' do example 'translate a node set string using other node set strings' do
@evaluator.evaluate('translate(root/a, root/b, root/c)').should == 'BAr' evaluate_xpath(@document, 'translate(root/a, root/b, root/c)')
.should == 'BAr'
end end
end end
end end

View File

@ -3,11 +3,11 @@ require 'spec_helper'
describe Oga::XPath::Evaluator do describe Oga::XPath::Evaluator do
context 'true() function' do context 'true() function' do
before do before do
@evaluator = described_class.new(parse('')) @document = parse('')
end end
example 'return true' do example 'return true' do
@evaluator.evaluate('true()').should == true evaluate_xpath(@document, 'true()').should == true
end end
end end
end end