From 4ad4b89860726691238e9fccfead5cbc9f4608a9 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 28 Aug 2015 16:57:24 +0200 Subject: [PATCH] Revamp compiler specs for "self" This also includes a fix for node() so that it matches attributes. --- lib/oga/xpath/compiler.rb | 4 +- spec/oga/xpath/compiler/axes/self_spec.rb | 72 ++++++++++++++++------- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index c5d3eb7..597d391 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -248,8 +248,6 @@ module Oga # @param [Oga::Ruby::Node] input # @return [Oga::Ruby::Node] def on_axis_self(ast, input) - node = node_literal - process(ast, input).if_true { yield input } end @@ -1312,7 +1310,7 @@ module Oga # @param [Oga::Ruby::Node] input # @return [Oga::Ruby::Node] def on_type_test_node(input) - input.is_a?(XML::Node).or(input.is_a?(XML::Document)) + document_or_node(input).or(input.is_a?(XML::Attribute)) end # @param [#to_s] value diff --git a/spec/oga/xpath/compiler/axes/self_spec.rb b/spec/oga/xpath/compiler/axes/self_spec.rb index 903c1a6..d18ac65 100644 --- a/spec/oga/xpath/compiler/axes/self_spec.rb +++ b/spec/oga/xpath/compiler/axes/self_spec.rb @@ -1,41 +1,73 @@ require 'spec_helper' describe Oga::XPath::Compiler do - describe 'self axis' do - before do - @document = parse('foobartest') + before do + @document = parse('foobartest') - @a1 = @document.children[0] - @b1 = @a1.children[0] - @b2 = @a1.children[1] + @a1 = @document.children[0] + @b1 = @a1.children[0] + @b2 = @a1.children[1] + end + + describe 'relative to a document' do + describe 'a/self::a' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@a1) + end end - it 'returns a node set containing the context node' do - evaluate_xpath(@document, 'a/self::a').should == node_set(@a1) + describe 'a/self::b' do + it 'returns an empty NodeSet' do + evaluate_xpath(@document).should == node_set + end end - it 'returns an empty node set for non existing nodes' do - evaluate_xpath(@document, 'a/self::b').should == node_set + describe 'a/.' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@a1) + end end - it 'returns a node set containing the context node using the short form' do - evaluate_xpath(@document, 'a/.').should == node_set(@a1) + describe 'a/b[. = "foo"]' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@b1) + end end - it 'returns a node set by matching the text of a node' do - evaluate_xpath(@document, 'a/b[. = "foo"]').should == node_set(@b1) + describe 'a/b[c/. = "test"]' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@b2) + end end - it 'returns a node set by matching the text of a path' do - evaluate_xpath(@document, 'a/b[c/. = "test"]').should == node_set(@b2) + describe 'a/b[c[. = "test"]]' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@b2) + end end - it 'returns a node set by matching the text of a nested predicate' do - evaluate_xpath(@document, 'a/b[c[. = "test"]]').should == node_set(@b2) + describe 'self::node()' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@document) + end end + end - it 'returns a node set containing the document itself' do - evaluate_xpath(@document, 'self::node()').should == node_set(@document) + describe 'relative to an element' do + describe 'self::node()' do + it 'returns a NodeSet' do + evaluate_xpath(@a1).should == node_set(@a1) + end + end + end + + describe 'relative to an attribute' do + describe 'self::node()' do + it 'returns a NodeSet' do + attr = @a1.attribute('foo') + + evaluate_xpath(attr).should == node_set(attr) + end end end end