Re-organized XPath axis evaluation specs.

This commit is contained in:
Yorick Peterse 2014-07-24 21:42:05 +02:00
parent dd37b028a0
commit cdf48979d5
6 changed files with 243 additions and 230 deletions

View File

@ -0,0 +1,47 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'ancestor-or-self axis' do
before do
@document = parse('<a><b><c></c></b><d></d><a></a></a>')
@c_node = @document.children[0].children[0].children[0]
@evaluator = described_class.new(@c_node)
end
context 'direct ancestors' do
before do
@set = @evaluator.evaluate('ancestor-or-self::b')
end
it_behaves_like :node_set, :length => 1
example 'return the <b> ancestor' do
@set[0].name.should == 'b'
end
end
context 'higher ancestors' do
before do
@set = @evaluator.evaluate('ancestor-or-self::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> ancestor' do
@set[0].name.should == 'a'
end
end
context 'missing ancestors' do
before do
@set = @evaluator.evaluate('ancestor-or-self::c')
end
it_behaves_like :node_set, :length => 1
example 'return the <c> node' do
@set[0].name.should == 'c'
end
end
end
end

View File

@ -0,0 +1,43 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'ancestor axis' do
before do
@document = parse('<a><b><c></c></b><d></d><a></a></a>')
@c_node = @document.children[0].children[0].children[0]
@evaluator = described_class.new(@c_node)
end
context 'direct ancestors' do
before do
@set = @evaluator.evaluate('ancestor::b')
end
it_behaves_like :node_set, :length => 1
example 'return the <b> ancestor' do
@set[0].name.should == 'b'
end
end
context 'higher ancestors' do
before do
@set = @evaluator.evaluate('ancestor::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> ancestor' do
@set[0].name.should == 'a'
end
end
context 'missing ancestors' do
before do
@set = @evaluator.evaluate('ancestor::foobar')
end
it_behaves_like :empty_node_set
end
end
end

View File

@ -0,0 +1,50 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'attribute axis' do
before do
document = parse('<a foo="bar"><b x="y"></b></a>')
@evaluator = described_class.new(document)
end
context 'top-level attributes' do
before do
@set = @evaluator.evaluate('attribute::foo')
end
it_behaves_like :node_set, :length => 1
example 'return an Attribute instance' do
@set[0].is_a?(Oga::XML::Attribute).should == true
end
example 'return the correct attribute' do
@set[0].name.should == 'foo'
end
end
context 'nested attributes' do
before do
@set = @evaluator.evaluate('/a/attribute::x')
end
it_behaves_like :node_set, :length => 1
example 'return an Attribute instance' do
@set[0].is_a?(Oga::XML::Attribute).should == true
end
example 'return the correct attribute' do
@set[0].name.should == 'x'
end
end
context 'missing attributes' do
before do
@set = @evaluator.evaluate('attribute::bar')
end
it_behaves_like :empty_node_set
end
end
end

View File

@ -0,0 +1,42 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'child axis' do
before do
@document = parse('<a><b><c></c></b><d></d><a></a></a>')
@evaluator = described_class.new(@document)
end
context 'direct children' do
before do
@set = @evaluator.evaluate('child::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].name.should == 'a'
end
end
context 'nested children' do
before do
@set = @evaluator.evaluate('child::a/child::b')
end
it_behaves_like :node_set, :length => 1
example 'return the <b> node' do
@set[0].name.should == 'b'
end
end
context 'invalid children' do
before do
@set = @evaluator.evaluate('child::foobar')
end
it_behaves_like :empty_node_set
end
end
end

View File

@ -0,0 +1,61 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'descendant axis' do
before do
@document = parse('<a><b><c></c></b><d></d><a></a></a>')
@evaluator = described_class.new(@document)
@first_a = @document.children[0]
@second_a = @first_a.children[-1]
end
context 'direct descendants' do
before do
@set = @evaluator.evaluate('descendant::a')
end
it_behaves_like :node_set, :length => 2
example 'return the first <a> node' do
@set[0].should == @first_a
end
example 'return the second <a> node' do
@set[1].should == @second_a
end
end
context 'nested descendants' do
before do
@set = @evaluator.evaluate('descendant::c')
end
it_behaves_like :node_set, :length => 1
example 'return the <c> node' do
@set[0].name.should == 'c'
end
end
context 'descendants of a specific node' do
before do
@set = @evaluator.evaluate('a/descendant::a')
end
it_behaves_like :node_set, :length => 1
example 'return the second <a> node' do
@set[0].should == @second_a
end
end
context 'invalid descendants' do
before do
@set = @evaluator.evaluate('descendant::foobar')
end
it_behaves_like :empty_node_set
end
end
end

View File

@ -1,230 +0,0 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
before do
@document = parse('<a><b><c></c></b><d></d><a></a></a>')
@c_node = @document.children[0].children[0].children[0]
end
context 'ancestor axis' do
before do
@evaluator = described_class.new(@c_node)
end
context 'direct ancestors' do
before do
@set = @evaluator.evaluate('ancestor::b')
end
it_behaves_like :node_set, :length => 1
example 'return the <b> ancestor' do
@set[0].name.should == 'b'
end
end
context 'higher ancestors' do
before do
@set = @evaluator.evaluate('ancestor::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> ancestor' do
@set[0].name.should == 'a'
end
end
context 'missing ancestors' do
before do
@set = @evaluator.evaluate('ancestor::foobar')
end
it_behaves_like :empty_node_set
end
end
context 'ancestor-or-self axis' do
before do
@evaluator = described_class.new(@c_node)
end
context 'direct ancestors' do
before do
@set = @evaluator.evaluate('ancestor-or-self::b')
end
it_behaves_like :node_set, :length => 1
example 'return the <b> ancestor' do
@set[0].name.should == 'b'
end
end
context 'higher ancestors' do
before do
@set = @evaluator.evaluate('ancestor-or-self::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> ancestor' do
@set[0].name.should == 'a'
end
end
context 'missing ancestors' do
before do
@set = @evaluator.evaluate('ancestor-or-self::c')
end
it_behaves_like :node_set, :length => 1
example 'return the <c> node' do
@set[0].name.should == 'c'
end
end
end
context 'attribute axis' do
before do
document = parse('<a foo="bar"><b x="y"></b></a>')
@evaluator = described_class.new(document)
end
context 'top-level attributes' do
before do
@set = @evaluator.evaluate('attribute::foo')
end
it_behaves_like :node_set, :length => 1
example 'return an Attribute instance' do
@set[0].is_a?(Oga::XML::Attribute).should == true
end
example 'return the correct attribute' do
@set[0].name.should == 'foo'
end
end
context 'nested attributes' do
before do
@set = @evaluator.evaluate('/a/attribute::x')
end
it_behaves_like :node_set, :length => 1
example 'return an Attribute instance' do
@set[0].is_a?(Oga::XML::Attribute).should == true
end
example 'return the correct attribute' do
@set[0].name.should == 'x'
end
end
context 'missing attributes' do
before do
@set = @evaluator.evaluate('attribute::bar')
end
it_behaves_like :empty_node_set
end
end
context 'child axis' do
before do
@evaluator = described_class.new(@document)
end
context 'direct children' do
before do
@set = @evaluator.evaluate('child::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].name.should == 'a'
end
end
context 'nested children' do
before do
@set = @evaluator.evaluate('child::a/child::b')
end
it_behaves_like :node_set, :length => 1
example 'return the <b> node' do
@set[0].name.should == 'b'
end
end
context 'invalid children' do
before do
@set = @evaluator.evaluate('child::foobar')
end
it_behaves_like :empty_node_set
end
end
context 'descendant axis' do
before do
@evaluator = described_class.new(@document)
@first_a = @document.children[0]
@second_a = @first_a.children[-1]
end
context 'direct descendants' do
before do
@set = @evaluator.evaluate('descendant::a')
end
it_behaves_like :node_set, :length => 2
example 'return the first <a> node' do
@set[0].should == @first_a
end
example 'return the second <a> node' do
@set[1].should == @second_a
end
end
context 'nested descendants' do
before do
@set = @evaluator.evaluate('descendant::c')
end
it_behaves_like :node_set, :length => 1
example 'return the <c> node' do
@set[0].name.should == 'c'
end
end
context 'descendants of a specific node' do
before do
@set = @evaluator.evaluate('a/descendant::a')
end
it_behaves_like :node_set, :length => 1
example 'return the second <a> node' do
@set[0].should == @second_a
end
end
context 'invalid descendants' do
before do
@set = @evaluator.evaluate('descendant::foobar')
end
it_behaves_like :empty_node_set
end
end
end