From ec08b41737ef7e086288acc3f1ea4bb8bf09875f Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 22 Jul 2014 21:04:09 +0200 Subject: [PATCH] Specs for various XPath::Evaluator helper methods. --- lib/oga/xml/node.rb | 5 +- spec/oga/xml/node_spec.rb | 4 + spec/oga/xpath/evaluator/general_spec.rb | 96 ++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 spec/oga/xpath/evaluator/general_spec.rb diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index 1647d23..dbc7deb 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -49,12 +49,13 @@ module Oga end ## - # Returns the parent node of the current node. + # Returns the parent node of the current node. If there is no parent node + # `nil` is returned instead. # # @return [Oga::XML::Node] # def parent - return node_set.owner + return node_set ? node_set.owner : nil end ## diff --git a/spec/oga/xml/node_spec.rb b/spec/oga/xml/node_spec.rb index fee205e..f4a87fe 100644 --- a/spec/oga/xml/node_spec.rb +++ b/spec/oga/xml/node_spec.rb @@ -57,6 +57,10 @@ describe Oga::XML::Node do node.parent.should == owner end + + example 'return nil if there is no parent node' do + described_class.new.parent.nil?.should == true + end end context '#previous' do diff --git a/spec/oga/xpath/evaluator/general_spec.rb b/spec/oga/xpath/evaluator/general_spec.rb new file mode 100644 index 0000000..a6222f5 --- /dev/null +++ b/spec/oga/xpath/evaluator/general_spec.rb @@ -0,0 +1,96 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + before do + @document = parse('Hello') + @evaluator = described_class.new(@document) + end + + context '#child_nodes' do + before do + @children = Oga::XML::NodeSet.new([ + Oga::XML::Element.new(:name => 'b'), + Oga::XML::Element.new(:name => 'b') + ]) + + @parent = Oga::XML::Element.new(:name => 'a', :children => @children) + end + + example "return a node's child nodes" do + nodes = @evaluator.child_nodes([@parent]) + + nodes[0].should == @children[0] + nodes[1].should == @children[1] + end + end + + context '#node_matches?' do + before do + @name_node = Oga::XML::Element.new(:name => 'a') + @name_ns_node = Oga::XML::Element.new(:name => 'b', :namespace => 'x') + end + + example 'return true if a node is matched by its name' do + @evaluator.node_matches?(@name_node, s(:test, nil, 'a')).should == true + end + + example 'return true if a node is matched by a wildcard name' do + @evaluator.node_matches?(@name_node, s(:test, nil, '*')).should == true + end + + example 'return false if a node is not matched by its name' do + @evaluator.node_matches?(@name_node, s(:test, nil, 'foo')).should == false + end + + example 'return true if a node is matched by its name and namespace' do + @evaluator.node_matches?(@name_ns_node, s(:test, 'x', 'b')).should == true + end + + example 'return false if a node is not matched by its namespace' do + @evaluator.node_matches?(@name_ns_node, s(:test, 'y', 'b')).should == false + end + + example 'return true if a node is matched by a wildcard namespace' do + @evaluator.node_matches?(@name_ns_node, s(:test, '*', 'b')).should == true + end + + example 'return true if a node is matched by a full wildcard search' do + @evaluator.node_matches?(@name_ns_node, s(:test, '*', '*')).should == true + end + + example 'return true if a node is matched without having a namespace' do + @evaluator.node_matches?(@name_node, s(:test, '*', 'a')).should == true + end + end + + context '#can_match_node?' do + example 'return true for an XML::Element instance' do + @evaluator.can_match_node?(Oga::XML::Element.new).should == true + end + + example 'return true for an XML::Attribute instance' do + @evaluator.can_match_node?(Oga::XML::Attribute.new).should == true + end + + example 'return false for an XML::Text instance' do + @evaluator.can_match_node?(Oga::XML::Text.new).should == false + end + end + + context '#has_parent?' do + before do + @parent = Oga::XML::Element.new + @child = Oga::XML::Element.new + + set = Oga::XML::NodeSet.new([@child], @parent) + end + + example 'return true if a node has a parent node' do + @evaluator.has_parent?(@child).should == true + end + + example 'return false if a node has no parent node' do + @evaluator.has_parent?(@parent).should == false + end + end +end