Revamp a few more XPath compiler specs

This commit is contained in:
Yorick Peterse 2015-08-31 09:39:33 +02:00
parent 604d0d9337
commit 8b2455679f
4 changed files with 63 additions and 49 deletions

View File

@ -1,14 +1,16 @@
require 'spec_helper'
describe Oga::XPath::Compiler do
describe 'querying HTML documents' do
before do
@document = parse_html('<html xmlns="foo"><body></body></html>')
@body = @document.children[0].children[0]
end
it 'returns a NodeSet when a custom default namespace is declared' do
evaluate_xpath(@document, 'html/body').should == node_set(@body)
describe 'relative to an HTML document' do
describe 'html/body' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@body)
end
end
end
end

View File

@ -1,7 +1,6 @@
require 'spec_helper'
describe Oga::XPath::Compiler do
describe 'predicates' do
before do
@document = parse('<root><a>10</a><a><b>20</a></a></root>')
@ -11,21 +10,22 @@ describe Oga::XPath::Compiler do
@a2 = root.children[1]
end
describe 'using an integer as an index' do
it 'returns a NodeSet containing the first <a> node' do
evaluate_xpath(@document, 'root/a[1]').should == node_set(@a1)
describe 'relative to a document' do
describe 'root/a[1]' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@a1)
end
end
describe 'using a float as an index' do
it 'returns a NodeSet containing the first <a> node' do
evaluate_xpath(@document, 'root/a[1.5]').should == node_set(@a1)
describe 'root/a[1.5]' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@a1)
end
end
describe 'using a node test' do
it 'returns a NodeSet containing all <a> nodes with <b> child nodes' do
evaluate_xpath(@document, 'root/a[b]').should == node_set(@a2)
describe 'root/a[b]' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@a2)
end
end
end

View File

@ -7,14 +7,17 @@ describe Oga::XPath::Compiler do
@compiler = described_class.new
end
describe 'when a variable is defined' do
it 'returns the value of a variable' do
ast = parse_xpath('$number')
block = @compiler.compile(ast)
block.call(@document, 'number' => 10.0).should == 10.0
end
end
it 'raises RuntimeError when evaluating an unbound variable' do
describe 'when a variable is undefined' do
it 'raises RuntimeError' do
ast = parse_xpath('$number')
block = @compiler.compile(ast)
@ -23,3 +26,4 @@ describe Oga::XPath::Compiler do
end
end
end
end

View File

@ -1,7 +1,6 @@
require 'spec_helper'
describe Oga::XPath::Compiler do
describe 'wildcard paths' do
before do
@document = parse('<a xmlns:ns1="x"><b></b><b></b><ns1:c></ns1:c></a>')
@ -11,20 +10,29 @@ describe Oga::XPath::Compiler do
@c1 = @a1.children[2]
end
it 'evaluates a wildcard path' do
evaluate_xpath(@document, 'a/*').should == @a1.children
describe 'relative to a document' do
describe 'a/*' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == @a1.children
end
end
it 'evaluates a path using a namespace wildcard' do
evaluate_xpath(@document, 'a/*:b').should == node_set(@b1, @b2)
describe 'a/*:b' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@b1, @b2)
end
end
it 'evaluates a path using a namespace and a name wildcard' do
evaluate_xpath(@document, 'a/ns1:*').should == node_set(@c1)
describe 'a/ns1:*' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@c1)
end
end
it 'evaluates a containing a namespace wildcard and a name wildcard' do
evaluate_xpath(@document, 'a/*:*').should == @a1.children
describe 'a/*:*' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == @a1.children
end
end
end
end