From cdf48979d56d74e81b080e96a67ae4e8676c7cbb Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 24 Jul 2014 21:42:05 +0200 Subject: [PATCH] Re-organized XPath axis evaluation specs. --- .../evaluator/axes/ancestor_or_self_spec.rb | 47 ++++ .../oga/xpath/evaluator/axes/ancestor_spec.rb | 43 ++++ .../xpath/evaluator/axes/attribute_spec.rb | 50 ++++ spec/oga/xpath/evaluator/axes/child_spec.rb | 42 ++++ .../xpath/evaluator/axes/descendant_spec.rb | 61 +++++ spec/oga/xpath/evaluator/axes_spec.rb | 230 ------------------ 6 files changed, 243 insertions(+), 230 deletions(-) create mode 100644 spec/oga/xpath/evaluator/axes/ancestor_or_self_spec.rb create mode 100644 spec/oga/xpath/evaluator/axes/ancestor_spec.rb create mode 100644 spec/oga/xpath/evaluator/axes/attribute_spec.rb create mode 100644 spec/oga/xpath/evaluator/axes/child_spec.rb create mode 100644 spec/oga/xpath/evaluator/axes/descendant_spec.rb delete mode 100644 spec/oga/xpath/evaluator/axes_spec.rb diff --git a/spec/oga/xpath/evaluator/axes/ancestor_or_self_spec.rb b/spec/oga/xpath/evaluator/axes/ancestor_or_self_spec.rb new file mode 100644 index 0000000..55456a2 --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/ancestor_or_self_spec.rb @@ -0,0 +1,47 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'ancestor-or-self axis' do + before do + @document = parse('') + @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 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 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 node' do + @set[0].name.should == 'c' + end + end + end +end diff --git a/spec/oga/xpath/evaluator/axes/ancestor_spec.rb b/spec/oga/xpath/evaluator/axes/ancestor_spec.rb new file mode 100644 index 0000000..9dfde95 --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/ancestor_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'ancestor axis' do + before do + @document = parse('') + @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 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 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 diff --git a/spec/oga/xpath/evaluator/axes/attribute_spec.rb b/spec/oga/xpath/evaluator/axes/attribute_spec.rb new file mode 100644 index 0000000..a709837 --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/attribute_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'attribute axis' do + before do + document = parse('') + @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 diff --git a/spec/oga/xpath/evaluator/axes/child_spec.rb b/spec/oga/xpath/evaluator/axes/child_spec.rb new file mode 100644 index 0000000..cac27ec --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/child_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'child axis' do + before do + @document = parse('') + @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 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 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 diff --git a/spec/oga/xpath/evaluator/axes/descendant_spec.rb b/spec/oga/xpath/evaluator/axes/descendant_spec.rb new file mode 100644 index 0000000..d483f5e --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/descendant_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'descendant axis' do + before do + @document = parse('') + @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 node' do + @set[0].should == @first_a + end + + example 'return the second 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 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 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 diff --git a/spec/oga/xpath/evaluator/axes_spec.rb b/spec/oga/xpath/evaluator/axes_spec.rb deleted file mode 100644 index 1611998..0000000 --- a/spec/oga/xpath/evaluator/axes_spec.rb +++ /dev/null @@ -1,230 +0,0 @@ -require 'spec_helper' - -describe Oga::XPath::Evaluator do - before do - @document = parse('') - @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 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 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 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 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 node' do - @set[0].name.should == 'c' - end - end - end - - context 'attribute axis' do - before do - document = parse('') - @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 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 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 node' do - @set[0].should == @first_a - end - - example 'return the second 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 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 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