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

View File

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

View File

@ -7,19 +7,23 @@ describe Oga::XPath::Compiler do
@compiler = described_class.new @compiler = described_class.new
end end
it 'returns the value of a variable' do describe 'when a variable is defined' do
ast = parse_xpath('$number') it 'returns the value of a variable' do
block = @compiler.compile(ast) 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 end
it 'raises RuntimeError when evaluating an unbound variable' do describe 'when a variable is undefined' do
ast = parse_xpath('$number') it 'raises RuntimeError' do
block = @compiler.compile(ast) ast = parse_xpath('$number')
block = @compiler.compile(ast)
proc { block.call(@document) }.should proc { block.call(@document) }.should
raise_error 'Undefined XPath variable: number' raise_error 'Undefined XPath variable: number'
end
end end
end end
end end

View File

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