Revamp descendant compiler specs

This commit is contained in:
Yorick Peterse 2015-08-28 15:29:09 +02:00
parent 70bea2071c
commit aa3fbcf522
2 changed files with 47 additions and 18 deletions

View File

@ -223,8 +223,10 @@ module Oga
def on_axis_descendant(ast, input)
node = node_literal
input.each_node.add_block(node) do
process(ast, node).if_true { yield node }
document_or_node(input).if_true do
input.each_node.add_block(node) do
process(ast, node).if_true { yield node }
end
end
end

View File

@ -1,33 +1,60 @@
require 'spec_helper'
describe Oga::XPath::Compiler do
describe 'descendant axis' do
before do
@document = parse('<a><b><c></c></b><a></a></a>')
before do
@document = parse('<a foo="bar"><b><c></c></b><a></a></a>')
@a1 = @document.children[0]
@a2 = @a1.children[-1]
@c1 = @a1.children[0].children[0]
@a1 = @document.children[0]
@a2 = @a1.children[-1]
@b1 = @a1.children[0]
@c1 = @b1.children[0]
end
describe 'relative to a document' do
describe 'descendant::a' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@a1, @a2)
end
end
it 'returns a node set containing a direct descendant' do
evaluate_xpath(@document, 'descendant::a').should == node_set(@a1, @a2)
describe 'descendant::c' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@c1)
end
end
it 'returns a node set containing a nested descendant' do
evaluate_xpath(@document, 'descendant::c').should == node_set(@c1)
describe 'a/descendant::a' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@a2)
end
end
it 'returns a node set containing a descendant relative to a node' do
evaluate_xpath(@document, 'a/descendant::a').should == node_set(@a2)
describe 'descendant::a[1]' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@a1)
end
end
it 'returns a node set containing the first descendant' do
evaluate_xpath(@document, 'descendant::a[1]').should == node_set(@a1)
describe 'a/b/c/descendant::c' do
it 'returns an empty NodeSet' do
evaluate_xpath(@document).should == node_set
end
end
end
it 'returns an empty node set for non existing descendants' do
evaluate_xpath(@document, 'a/b/c/descendant::c').should == node_set
describe 'relative to an element' do
describe 'descendant::b' do
it 'returns a NodeSet' do
evaluate_xpath(@a1).should == node_set(@b1)
end
end
end
describe 'relative to an attribute' do
describe 'descendant::b' do
it 'returns an empty NodeSet' do
evaluate_xpath(@a1.attribute('foo')).should == node_set
end
end
end
end