From 6b2874c507335950349f3e4f928225799e13fc8a Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 28 Aug 2015 16:30:26 +0200 Subject: [PATCH] Revamped compiler "preceding-sibling" specs --- lib/oga/xpath/compiler.rb | 16 +++--- .../compiler/axes/preceding_sibling_spec.rb | 54 +++++++++++++------ 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index 09e1f97..038052c 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -387,14 +387,16 @@ module Oga .else { parent.assign(self.nil) } end .followed_by do - root.each_node.add_block(doc_node) do - doc_node.eq(input) - .if_true { self.break } - .followed_by do - doc_node.parent.eq(parent).if_true do - process(ast, doc_node).if_true { yield doc_node } + document_or_node(root).if_true do + root.each_node.add_block(doc_node) do + doc_node.eq(input) + .if_true { self.break } + .followed_by do + doc_node.parent.eq(parent).if_true do + process(ast, doc_node).if_true { yield doc_node } + end end - end + end end end end diff --git a/spec/oga/xpath/compiler/axes/preceding_sibling_spec.rb b/spec/oga/xpath/compiler/axes/preceding_sibling_spec.rb index 7bc8cc0..1cd0096 100644 --- a/spec/oga/xpath/compiler/axes/preceding_sibling_spec.rb +++ b/spec/oga/xpath/compiler/axes/preceding_sibling_spec.rb @@ -1,10 +1,9 @@ require 'spec_helper' describe Oga::XPath::Compiler do - describe 'preceding-sibling axis' do - before do - @document = parse(<<-EOF.strip.gsub(/\s+/m, '')) - + before do + @document = parse(<<-EOF.strip.gsub(/^\s+|\n/m, '')) + @@ -12,25 +11,48 @@ describe Oga::XPath::Compiler do - EOF + EOF - @foo1 = @document.children[0].children[0] - @foo2 = @document.children[0].children[1].children[0] - @bar1 = @document.children[0].children[1] + @foo1 = @document.children[0].children[0] + @foo2 = @document.children[0].children[1].children[0] + @bar1 = @document.children[0].children[1] + end + + describe 'relative to a document' do + describe 'preceding-sibling::*' do + it 'returns an empty NodeSet' do + evaluate_xpath(@document).should == node_set + end end - it 'returns a node set containing preceding siblings of root/bar' do - evaluate_xpath(@document, 'root/bar/preceding-sibling::foo') - .should == node_set(@foo1) + describe 'root/bar/preceding-sibling::foo' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@foo1) + end end - it 'returns a node set containing preceding siblings of root/bar/baz' do - evaluate_xpath(@document, 'root/bar/baz/preceding-sibling::foo') - .should == node_set(@foo2) + describe 'root/bar/baz/preceding-sibling::foo' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@foo2) + end end + end - it 'returns a node set containing preceding siblings relative to root/bar' do - evaluate_xpath(@bar1, 'preceding-sibling::foo').should == node_set(@foo1) + describe 'relative to an element' do + describe 'preceding-sibling::foo' do + it 'returns a NodeSet' do + evaluate_xpath(@bar1).should == node_set(@foo1) + end + end + end + + describe 'relative to an attribute' do + describe 'preceding-sibling::*' do + it 'returns an empty NodeSet' do + root = @document.children[0] + + evaluate_xpath(root.attribute('foo')).should == node_set + end end end end