From a1e7d2d07f73b4ea651a82d72d3709232871e27d Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 28 Aug 2015 15:53:57 +0200 Subject: [PATCH] Revamp compiler "following" specs --- lib/oga/xpath/compiler.rb | 26 ++++---- .../compiler/axes/following_sibling_spec.rb | 2 +- .../oga/xpath/compiler/axes/following_spec.rb | 61 ++++++++++++------- 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index 76c0eba..93cd5fd 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -308,18 +308,20 @@ module Oga .else { root.assign(orig_input) } .followed_by(check.assign(self.false)) .followed_by do - root.each_node.add_block(doc_node) do - doc_node.eq(input) - .if_true do - check.assign(self.true) - .followed_by(throw_message(:skip_children)) - end - .followed_by do - check.if_false { send_message(:next) } - end - .followed_by do - process(ast, doc_node).if_true { yield doc_node } - end + document_or_node(root).if_true do + root.each_node.add_block(doc_node) do + doc_node.eq(input) + .if_true do + check.assign(self.true) + .followed_by(throw_message(:skip_children)) + end + .followed_by do + check.if_false { send_message(:next) } + end + .followed_by do + process(ast, doc_node).if_true { yield doc_node } + end + end end end end diff --git a/spec/oga/xpath/compiler/axes/following_sibling_spec.rb b/spec/oga/xpath/compiler/axes/following_sibling_spec.rb index 5b53b3c..671e9a1 100644 --- a/spec/oga/xpath/compiler/axes/following_sibling_spec.rb +++ b/spec/oga/xpath/compiler/axes/following_sibling_spec.rb @@ -13,7 +13,7 @@ describe Oga::XPath::Compiler do - EOF + EOF @bar1 = @document.children[0].children[0].children[0] @baz1 = @document.children[0].children[0].children[1] diff --git a/spec/oga/xpath/compiler/axes/following_spec.rb b/spec/oga/xpath/compiler/axes/following_spec.rb index a5616a6..68b6c7f 100644 --- a/spec/oga/xpath/compiler/axes/following_spec.rb +++ b/spec/oga/xpath/compiler/axes/following_spec.rb @@ -1,11 +1,10 @@ require 'spec_helper' describe Oga::XPath::Compiler do - describe 'following axis' do - before do - # Strip whitespace so it's easier to retrieve/compare elements. - @document = parse(<<-EOF.strip.gsub(/\s+/m, '')) - + before do + # Strip whitespace so it's easier to retrieve/compare elements. + @document = parse(<<-EOF.strip.gsub(/^\s+|\n/m, '')) + @@ -14,33 +13,51 @@ describe Oga::XPath::Compiler do - EOF + EOF - @bar1 = @document.children[0].children[0].children[0] - @baz1 = @document.children[0].children[0].children[1] - @baz2 = @baz1.children[0] - @baz3 = @document.children[0].children[1] - end + @bar1 = @document.children[0].children[0].children[0] + @baz1 = @document.children[0].children[0].children[1] + @baz2 = @baz1.children[0] + @baz3 = @document.children[0].children[1] + end + describe 'relative to a document' do # This should return an empty set since the document doesn't have any # following nodes. - it 'returns an empty node set for the sibling of a document' do - evaluate_xpath(@document, 'following::foo').should == node_set + describe 'following::foo' do + it 'returns an empty NodeSet' do + evaluate_xpath(@document).should == node_set + end end - it 'returns a node set containing the following nodes of root/foo' do - evaluate_xpath(@document, 'root/foo/following::baz') - .should == node_set(@baz3) + describe 'root/foo/following::baz' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@baz3) + end end - it 'returns a node set containing the following nodes of root/foo/bar' do - evaluate_xpath(@document, 'root/foo/bar/following::baz') - .should == node_set(@baz1, @baz2, @baz3) + describe 'root/foo/bar/following::baz' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@baz1, @baz2, @baz3) + end end + end - it 'returns a node set containing the siblings relative to root/foo/bar' do - evaluate_xpath(@bar1, 'following::baz') - .should == node_set(@baz1, @baz2, @baz3) + describe 'relative to an element' do + describe 'following::baz' do + it 'returns a NodeSet' do + evaluate_xpath(@bar1).should == node_set(@baz1, @baz2, @baz3) + end + end + end + + describe 'relative to an attribute' do + describe 'following::foo' do + it 'returns an empty NodeSet' do + root = @document.children[0] + + evaluate_xpath(root.attribute('foo')).should == node_set + end end end end