Revamp compiler specs for following-sibling

This commit is contained in:
Yorick Peterse 2015-08-28 15:48:03 +02:00
parent aa3fbcf522
commit 824c897467
2 changed files with 54 additions and 34 deletions

View File

@ -274,20 +274,22 @@ module Oga
end end
.followed_by(check.assign(self.false)) .followed_by(check.assign(self.false))
.followed_by do .followed_by do
root.each_node.add_block(doc_node) do document_or_node(root).if_true do
doc_node.eq(input) root.each_node.add_block(doc_node) do
.if_true do doc_node.eq(input)
check.assign(self.true) .if_true do
.followed_by(throw_message(:skip_children)) check.assign(self.true)
end .followed_by(throw_message(:skip_children))
.followed_by do
check.not.or(parent != doc_node.parent).if_true do
send_message(:next)
end end
end .followed_by do
.followed_by do check.not.or(parent != doc_node.parent).if_true do
process(ast, doc_node).if_true { yield doc_node } send_message(:next)
end end
end
.followed_by do
process(ast, doc_node).if_true { yield doc_node }
end
end
end end
end end
end end

View File

@ -1,11 +1,10 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XPath::Compiler do describe Oga::XPath::Compiler do
describe 'following-sibling axis' do before do
before do # Strip whitespace so it's easier to retrieve/compare elements.
# Strip whitespace so it's easier to retrieve/compare elements. @document = parse(<<-EOF.strip.gsub(/^\s+|\n/m, ''))
@document = parse(<<-EOF.strip.gsub(/\s+/m, '')) <root foo="bar">
<root>
<foo> <foo>
<bar></bar> <bar></bar>
<baz> <baz>
@ -16,30 +15,49 @@ describe Oga::XPath::Compiler do
</root> </root>
EOF EOF
@bar1 = @document.children[0].children[0].children[0] @bar1 = @document.children[0].children[0].children[0]
@baz1 = @document.children[0].children[0].children[1] @baz1 = @document.children[0].children[0].children[1]
@baz2 = @baz1.children[0] @baz2 = @baz1.children[0]
@baz3 = @document.children[0].children[1] @baz3 = @document.children[0].children[1]
end
describe 'relative to a document' do
describe 'following-sibling::foo' do
# This should return an empty set since the document doesn't have any
# following nodes.
it 'returns an empty NodeSet' do
evaluate_xpath(@document).should == node_set
end
end end
# This should return an empty set since the document doesn't have any describe 'root/foo/following-sibling::baz' do
# following nodes. it 'returns a NodeSet' do
it 'returns an empty node set for the sibling of a document' do evaluate_xpath(@document).should == node_set(@baz3)
evaluate_xpath(@document, 'following-sibling::foo').should == node_set end
end end
it 'returns a node set containing the siblings of root/foo' do describe 'root/foo/bar/following-sibling::baz' do
evaluate_xpath(@document, 'root/foo/following-sibling::baz') it 'returns a NodeSet' do
.should == node_set(@baz3) evaluate_xpath(@document).should == node_set(@baz1)
end
end end
end
it 'returns a node set containing the siblings of root/foo/bar' do describe 'relative to an element' do
evaluate_xpath(@document, 'root/foo/bar/following-sibling::baz') describe 'following-sibling::baz' do
.should == node_set(@baz1) it 'returns a NodeSet' do
evaluate_xpath(@bar1).should == node_set(@baz1)
end
end end
end
it 'returns a node set containing the siblings relative to root/foo/bar' do describe 'relative to an attribute' do
evaluate_xpath(@bar1, 'following-sibling::baz').should == node_set(@baz1) describe 'following-sibling::foo' do
it 'returns an empty NodeSet' do
root = @document.children[0]
evaluate_xpath(root.attribute('foo')).should == node_set
end
end end
end end
end end