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.
This commit is contained in:
parent
9899a419b7
commit
4fb7e2f6ce
|
@ -1,38 +1,71 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Oga::XPath::Compiler do
|
describe Oga::XPath::Compiler do
|
||||||
describe 'ancestor-or-self axis' do
|
before do
|
||||||
before do
|
@document = parse('<a foo="bar"><b><c></c></b></a>')
|
||||||
@document = parse('<a><b><c></c></b></a>')
|
|
||||||
|
|
||||||
@a1 = @document.children[0]
|
@a1 = @document.children[0]
|
||||||
@b1 = @a1.children[0]
|
@b1 = @a1.children[0]
|
||||||
@c1 = @b1.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
|
end
|
||||||
|
|
||||||
it 'returns a node set containing the direct ancestor' do
|
describe 'ancestor-or-self::b' do
|
||||||
evaluate_xpath(@c1, 'ancestor-or-self::b').should == node_set(@b1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@b1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a node set containing a higher ancestor' do
|
describe 'ancestor-or-self::c' do
|
||||||
evaluate_xpath(@c1, 'ancestor-or-self::a').should == node_set(@a1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@c1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a node set containing the context node itself' do
|
describe 'ancestor-or-self::*[1]' do
|
||||||
evaluate_xpath(@c1, 'ancestor-or-self::c').should == node_set(@c1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@c1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a node set containing the first ancestor' do
|
describe 'ancestor-or-self::*' do
|
||||||
evaluate_xpath(@c1, 'ancestor-or-self::*[1]').should == node_set(@c1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@c1, @b1, @a1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a node set containing all ancestors' do
|
describe 'ancestor-or-self::foo' do
|
||||||
evaluate_xpath(@c1, 'ancestor-or-self::*')
|
it 'returns an empty NodeSet' do
|
||||||
.should == node_set(@c1, @b1, @a1)
|
evaluate_xpath(@c1).should == node_set
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns an empty node set for non existing ancestors' do
|
describe 'ancestor-or-self::*/ancestor-or-self::a' do
|
||||||
evaluate_xpath(@c1, 'ancestor-or-self::foo').should == node_set
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@a1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,33 +1,65 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Oga::XPath::Compiler do
|
describe Oga::XPath::Compiler do
|
||||||
describe 'ancestor axis' do
|
before do
|
||||||
before do
|
@document = parse('<a foo="bar"><b><c></c></b></a>')
|
||||||
@document = parse('<a><b><c></c></b></a>')
|
|
||||||
|
|
||||||
@a1 = @document.children[0]
|
@a1 = @document.children[0]
|
||||||
@b1 = @a1.children[0]
|
@b1 = @a1.children[0]
|
||||||
@c1 = @b1.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
|
end
|
||||||
|
|
||||||
it 'returns a node set containing a direct ancestor' do
|
describe 'ancestor::b' do
|
||||||
evaluate_xpath(@c1, 'ancestor::b').should == node_set(@b1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@b1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a node set containing a higher ancestor' do
|
describe 'ancestor::*[1]' do
|
||||||
evaluate_xpath(@c1, 'ancestor::a').should == node_set(@a1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@b1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a node set containing the first ancestor' do
|
describe 'ancestor::*' do
|
||||||
evaluate_xpath(@c1, 'ancestor::*[1]').should == node_set(@b1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@b1, @a1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a node set containing all ancestors' do
|
describe 'ancestor::c' do
|
||||||
evaluate_xpath(@c1, 'ancestor::*').should == node_set(@b1, @a1)
|
it 'returns an empty NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns an empty node set for non existing ancestors' do
|
describe 'ancestor::*/ancestor::a' do
|
||||||
evaluate_xpath(@c1, 'ancestor::c').should == node_set
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@a1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Oga
|
||||||
# @param [String] xpath
|
# @param [String] xpath
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def evaluate_xpath(document, xpath)
|
def evaluate_xpath(document, xpath = self.class.description)
|
||||||
ast = parse_xpath(xpath)
|
ast = parse_xpath(xpath)
|
||||||
compiler = Oga::XPath::Compiler.new
|
compiler = Oga::XPath::Compiler.new
|
||||||
block = compiler.compile(ast)
|
block = compiler.compile(ast)
|
||||||
|
@ -20,7 +20,7 @@ module Oga
|
||||||
# @param [String] css
|
# @param [String] css
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def evaluate_css(document, css)
|
def evaluate_css(document, css = self.class.description)
|
||||||
ast = parse_css(css)
|
ast = parse_css(css)
|
||||||
compiler = Oga::XPath::Compiler.new
|
compiler = Oga::XPath::Compiler.new
|
||||||
block = compiler.compile(ast)
|
block = compiler.compile(ast)
|
||||||
|
|
Loading…
Reference in New Issue