From 84a9315b2448dc8247f31253606f97b9ba18f6a1 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 28 Aug 2015 16:22:49 +0200 Subject: [PATCH] Revamped compiler specs for "parent" --- lib/oga/xpath/compiler.rb | 2 +- spec/oga/xpath/compiler/axes/parent_spec.rb | 51 +++++++++++++++++---- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index 93cd5fd..09e1f97 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -237,7 +237,7 @@ module Oga node = node_literal parent = unique_literal(:parent) - input.is_a?(XML::Node).if_true do + attribute_or_node(input).if_true do parent.assign(input.parent).followed_by do process(ast, parent).if_true { yield parent } end diff --git a/spec/oga/xpath/compiler/axes/parent_spec.rb b/spec/oga/xpath/compiler/axes/parent_spec.rb index 559a834..ff3b0cf 100644 --- a/spec/oga/xpath/compiler/axes/parent_spec.rb +++ b/spec/oga/xpath/compiler/axes/parent_spec.rb @@ -1,23 +1,54 @@ require 'spec_helper' describe Oga::XPath::Compiler do - describe 'parent axis' do - before do - @document = parse('') + before do + @document = parse('') - @a1 = @document.children[0] + @a1 = @document.children[0] + @b1 = @a1.children[0] + end + + describe 'relative to a document' do + describe 'parent::a' do + it 'returns an empty NodeSet' do + evaluate_xpath(@document).should == node_set + end end - it 'returns an empty node set for non existing parents' do - evaluate_xpath(@document, 'parent::a').should == node_set + describe 'a/b/parent::a' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@a1) + end end - it 'returns a node set containing parents of a node' do - evaluate_xpath(@document, 'a/b/parent::a').should == node_set(@a1) + describe 'a/b/..' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@a1) + end end + end - it 'returns a node set containing parents of a node using the short form' do - evaluate_xpath(@document, 'a/b/..').should == node_set(@a1) + describe 'relative to an element' do + describe 'parent::a' do + it 'returns a NodeSet' do + evaluate_xpath(@b1).should == node_set(@a1) + end + end + end + + describe 'relative to the root element' do + describe 'parent::*' do + it 'returns an empty NodeSet' do + evaluate_xpath(@a1).should == node_set + end + end + end + + describe 'relative to an attribute' do + describe 'parent::a' do + it 'returns a NodeSet' do + evaluate_xpath(@a1.attribute('foo')).should == node_set(@a1) + end end end end