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
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,31 +1,31 @@
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>')
before do
@document = parse('<root><a>10</a><a><b>20</a></a></root>')
root = @document.children[0]
root = @document.children[0]
@a1 = root.children[0]
@a2 = root.children[1]
end
@a1 = root.children[0]
@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,19 +7,23 @@ describe Oga::XPath::Compiler do
@compiler = described_class.new
end
it 'returns the value of a variable' do
ast = parse_xpath('$number')
block = @compiler.compile(ast)
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
block.call(@document, 'number' => 10.0).should == 10.0
end
end
it 'raises RuntimeError when evaluating an unbound variable' do
ast = parse_xpath('$number')
block = @compiler.compile(ast)
describe 'when a variable is undefined' do
it 'raises RuntimeError' do
ast = parse_xpath('$number')
block = @compiler.compile(ast)
proc { block.call(@document) }.should
raise_error 'Undefined XPath variable: number'
proc { block.call(@document) }.should
raise_error 'Undefined XPath variable: number'
end
end
end
end

View File

@ -1,30 +1,38 @@
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>')
before do
@document = parse('<a xmlns:ns1="x"><b></b><b></b><ns1:c></ns1:c></a>')
@a1 = @document.children[0]
@b1 = @a1.children[0]
@b2 = @a1.children[1]
@c1 = @a1.children[2]
@a1 = @document.children[0]
@b1 = @a1.children[0]
@b2 = @a1.children[1]
@c1 = @a1.children[2]
end
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 wildcard path' do
evaluate_xpath(@document, 'a/*').should == @a1.children
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 wildcard' do
evaluate_xpath(@document, 'a/*:b').should == node_set(@b1, @b2)
describe 'a/ns1:*' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@c1)
end
end
it 'evaluates a path using a namespace and a name wildcard' do
evaluate_xpath(@document, 'a/ns1:*').should == node_set(@c1)
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