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'
|
||||
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'ancestor-or-self axis' do
|
||||
before do
|
||||
@document = parse('<a><b><c></c></b></a>')
|
||||
before do
|
||||
@document = parse('<a foo="bar"><b><c></c></b></a>')
|
||||
|
||||
@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
|
||||
|
|
|
@ -1,33 +1,65 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'ancestor axis' do
|
||||
before do
|
||||
@document = parse('<a><b><c></c></b></a>')
|
||||
before do
|
||||
@document = parse('<a foo="bar"><b><c></c></b></a>')
|
||||
|
||||
@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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue