From 580856dcf7fa5d601cca657cbddeddb016bbd2d6 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 15 Jul 2014 09:34:11 +0200 Subject: [PATCH] Cleaned up XPath specs using a shared example. --- spec/oga/xpath/evaluator/paths_spec.rb | 40 ++++------------------- spec/oga/xpath/evaluator/wildcard_spec.rb | 16 +++------ spec/spec_helper.rb | 1 + spec/support/shared_examples.rb | 9 +++++ 4 files changed, 20 insertions(+), 46 deletions(-) create mode 100644 spec/support/shared_examples.rb diff --git a/spec/oga/xpath/evaluator/paths_spec.rb b/spec/oga/xpath/evaluator/paths_spec.rb index b79024f..b5de5c0 100644 --- a/spec/oga/xpath/evaluator/paths_spec.rb +++ b/spec/oga/xpath/evaluator/paths_spec.rb @@ -11,13 +11,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('/a') end - example 'return a NodeSet instance' do - @set.is_a?(Oga::XML::NodeSet).should == true - end - - example 'return the right amount of nodes' do - @set.length.should == 1 - end + it_behaves_like :node_set, :length => 1 example 'return the correct nodes' do @set[0].should == @document.children[0] @@ -29,9 +23,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('/x/a') end - example 'return an empty NodeSet' do - @set.empty?.should == true - end + it_behaves_like :node_set, :length => 0 end context 'relative paths' do @@ -39,13 +31,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('a') end - example 'return a NodeSet instance' do - @set.is_a?(Oga::XML::NodeSet).should == true - end - - example 'return the right amount of nodes' do - @set.length.should == 1 - end + it_behaves_like :node_set, :length => 1 example 'return the correct nodes' do @set[0].should == @document.children[0] @@ -57,9 +43,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('x/a') end - example 'return an empty NodeSet' do - @set.empty?.should == true - end + it_behaves_like :node_set, :length => 0 end context 'nested paths' do @@ -67,13 +51,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('/a/b') end - example 'return a NodeSet instance' do - @set.is_a?(Oga::XML::NodeSet).should == true - end - - example 'return the right amount of rows' do - @set.length.should == 2 - end + it_behaves_like :node_set, :length => 2 example 'return the correct nodes' do a = @document.children[0] @@ -88,13 +66,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('a/ns1:c') end - example 'return a NodeSet instance' do - @set.is_a?(Oga::XML::NodeSet).should == true - end - - example 'return the right amount of rows' do - @set.length.should == 1 - end + it_behaves_like :node_set, :length => 1 example 'return the correct row' do a = @document.children[0] diff --git a/spec/oga/xpath/evaluator/wildcard_spec.rb b/spec/oga/xpath/evaluator/wildcard_spec.rb index 858b7e8..a30191f 100644 --- a/spec/oga/xpath/evaluator/wildcard_spec.rb +++ b/spec/oga/xpath/evaluator/wildcard_spec.rb @@ -11,9 +11,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('a/*') end - example 'return the right amount of rows' do - @set.length.should == 3 - end + it_behaves_like :node_set, :length => 3 example 'include the first node' do @set[0].name.should == 'b' @@ -34,9 +32,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('a/*:b') end - example 'return the right amount of rows' do - @set.length.should == 2 - end + it_behaves_like :node_set, :length => 2 example 'include the first node' do @set[0].name.should == 'b' @@ -56,9 +52,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('a/ns1:*') end - example 'return the right amount of rows' do - @set.length.should == 1 - end + it_behaves_like :node_set, :length => 1 example 'include the correct node' do @set[0].name.should == 'c' @@ -71,9 +65,7 @@ describe Oga::XPath::Evaluator do @set = @evaluator.evaluate('a/*:*') end - example 'return the right amount of rows' do - @set.length.should == 3 - end + it_behaves_like :node_set, :length => 3 example 'include the first node' do @set[0].name.should == 'b' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1449041..f54dabd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ end require_relative '../lib/oga' require_relative 'support/parsing' +require_relative 'support/shared_examples' RSpec.configure do |config| config.color = true diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb new file mode 100644 index 0000000..552dd3f --- /dev/null +++ b/spec/support/shared_examples.rb @@ -0,0 +1,9 @@ +shared_examples :node_set do |options| + example 'return a NodeSet instance' do + @set.is_a?(Oga::XML::NodeSet).should == true + end + + example 'return the right amount of rows' do + @set.length.should == options[:length] + end +end