From 4fb7e2f6ce588ad0e456cef8027ed2c97440db8d Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 26 Aug 2015 22:35:13 +0200 Subject: [PATCH] Revamped ancestor/ancestor-or-self axis specs This makes it easier to get more natural spec descriptions without having to write them entirely by hand. --- .../compiler/axes/ancestor_or_self_spec.rb | 71 ++++++++++++++----- spec/oga/xpath/compiler/axes/ancestor_spec.rb | 64 ++++++++++++----- spec/support/evaluation_helpers.rb | 4 +- 3 files changed, 102 insertions(+), 37 deletions(-) diff --git a/spec/oga/xpath/compiler/axes/ancestor_or_self_spec.rb b/spec/oga/xpath/compiler/axes/ancestor_or_self_spec.rb index f12672f..8c1b55a 100644 --- a/spec/oga/xpath/compiler/axes/ancestor_or_self_spec.rb +++ b/spec/oga/xpath/compiler/axes/ancestor_or_self_spec.rb @@ -1,38 +1,71 @@ require 'spec_helper' describe Oga::XPath::Compiler do - describe 'ancestor-or-self axis' do - before do - @document = parse('') + before do + @document = parse('') - @a1 = @document.children[0] - @b1 = @a1.children[0] - @c1 = @b1.children[0] + @a1 = @document.children[0] + @b1 = @a1.children[0] + @c1 = @b1.children[0] + end + + describe 'relative to a document' do + describe 'ancestor-or-self::a' do + it 'returns an empty NodeSet' do + evaluate_xpath(@document).should == node_set + end + end + end + + describe 'relative to an attribute' do + describe 'ancestor-or-self::a' do + it 'returns a NodeSet' do + evaluate_xpath(@a1.attribute('foo')).should == node_set(@a1) + end + end + end + + describe 'relative to an element' do + describe 'ancestor-or-self::a' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@a1) + end end - it 'returns a node set containing the direct ancestor' do - evaluate_xpath(@c1, 'ancestor-or-self::b').should == node_set(@b1) + describe 'ancestor-or-self::b' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@b1) + end end - it 'returns a node set containing a higher ancestor' do - evaluate_xpath(@c1, 'ancestor-or-self::a').should == node_set(@a1) + describe 'ancestor-or-self::c' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@c1) + end end - it 'returns a node set containing the context node itself' do - evaluate_xpath(@c1, 'ancestor-or-self::c').should == node_set(@c1) + describe 'ancestor-or-self::*[1]' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@c1) + end end - it 'returns a node set containing the first ancestor' do - evaluate_xpath(@c1, 'ancestor-or-self::*[1]').should == node_set(@c1) + describe 'ancestor-or-self::*' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@c1, @b1, @a1) + end end - it 'returns a node set containing all ancestors' do - evaluate_xpath(@c1, 'ancestor-or-self::*') - .should == node_set(@c1, @b1, @a1) + describe 'ancestor-or-self::foo' do + it 'returns an empty NodeSet' do + evaluate_xpath(@c1).should == node_set + end end - it 'returns an empty node set for non existing ancestors' do - evaluate_xpath(@c1, 'ancestor-or-self::foo').should == node_set + describe 'ancestor-or-self::*/ancestor-or-self::a' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@a1) + end end end end diff --git a/spec/oga/xpath/compiler/axes/ancestor_spec.rb b/spec/oga/xpath/compiler/axes/ancestor_spec.rb index 5772ba3..cd7d50b 100644 --- a/spec/oga/xpath/compiler/axes/ancestor_spec.rb +++ b/spec/oga/xpath/compiler/axes/ancestor_spec.rb @@ -1,33 +1,65 @@ require 'spec_helper' describe Oga::XPath::Compiler do - describe 'ancestor axis' do - before do - @document = parse('') + before do + @document = parse('') - @a1 = @document.children[0] - @b1 = @a1.children[0] - @c1 = @b1.children[0] + @a1 = @document.children[0] + @b1 = @a1.children[0] + @c1 = @b1.children[0] + end + + describe 'relative to a document' do + describe 'ancestor::a' do + it 'returns an empty NodeSet' do + evaluate_xpath(@document).should == node_set + end + end + end + + describe 'relative to an attribute' do + describe 'ancestor-or-self::a' do + it 'returns a NodeSet' do + evaluate_xpath(@a1.attribute('foo')).should == node_set(@a1) + end + end + end + + describe 'relative to an element' do + describe 'ancestor::a' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@a1) + end end - it 'returns a node set containing a direct ancestor' do - evaluate_xpath(@c1, 'ancestor::b').should == node_set(@b1) + describe 'ancestor::b' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@b1) + end end - it 'returns a node set containing a higher ancestor' do - evaluate_xpath(@c1, 'ancestor::a').should == node_set(@a1) + describe 'ancestor::*[1]' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@b1) + end end - it 'returns a node set containing the first ancestor' do - evaluate_xpath(@c1, 'ancestor::*[1]').should == node_set(@b1) + describe 'ancestor::*' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@b1, @a1) + end end - it 'returns a node set containing all ancestors' do - evaluate_xpath(@c1, 'ancestor::*').should == node_set(@b1, @a1) + describe 'ancestor::c' do + it 'returns an empty NodeSet' do + evaluate_xpath(@c1).should == node_set + end end - it 'returns an empty node set for non existing ancestors' do - evaluate_xpath(@c1, 'ancestor::c').should == node_set + describe 'ancestor::*/ancestor::a' do + it 'returns a NodeSet' do + evaluate_xpath(@c1).should == node_set(@a1) + end end end end diff --git a/spec/support/evaluation_helpers.rb b/spec/support/evaluation_helpers.rb index 6e7f7a8..5bcbb55 100644 --- a/spec/support/evaluation_helpers.rb +++ b/spec/support/evaluation_helpers.rb @@ -5,7 +5,7 @@ module Oga # @param [String] xpath # @return [Oga::XML::NodeSet] # - def evaluate_xpath(document, xpath) + def evaluate_xpath(document, xpath = self.class.description) ast = parse_xpath(xpath) compiler = Oga::XPath::Compiler.new block = compiler.compile(ast) @@ -20,7 +20,7 @@ module Oga # @param [String] css # @return [Oga::XML::NodeSet] # - def evaluate_css(document, css) + def evaluate_css(document, css = self.class.description) ast = parse_css(css) compiler = Oga::XPath::Compiler.new block = compiler.compile(ast)