Specs for various XPath evaluator helper methods.

This commit is contained in:
Yorick Peterse 2014-08-27 23:37:12 +02:00
parent 022d8e0ada
commit fcb28d5ae8
2 changed files with 51 additions and 0 deletions

View File

@ -1219,6 +1219,17 @@ module Oga
return node return node
end end
##
# Returns the text of the first node in the node set, or an empty string
# if the node set is empty.
#
# @param [Oga::XML::NodeSet] set
# @return [String]
#
def first_node_text(set)
return set[0].respond_to?(:text) ? set[0].text : ''
end
## ##
# Returns a node set containing all the child nodes of the given set of # Returns a node set containing all the child nodes of the given set of
# nodes. # nodes.

View File

@ -6,6 +6,46 @@ describe Oga::XPath::Evaluator do
@evaluator = described_class.new(@document) @evaluator = described_class.new(@document)
end end
context '#function_node' do
before do
@context_set = Oga::XML::NodeSet.new([@document])
end
example 'return the first node in the expression' do
exp = s(:axis, 'child', s(:test, nil, 'a'))
node = @evaluator.function_node(@context_set, exp)
node.should == @document.children[0]
end
example 'raise a TypeError if the expression did not return a node set' do
exp = s(:call, 'false')
block = lambda { @evaluator.function_node(@context_set, exp) }
block.should raise_error(TypeError)
end
example 'use the current context node if the expression is empty' do
a_node = @document.children[0]
@evaluator.stub(:current_node).and_return(a_node)
@evaluator.function_node(@context_set).should == a_node
end
end
context '#first_node_text' do
example 'return the text of the first node' do
@evaluator.first_node_text(@document.children).should == 'Hello'
end
example 'return an empty string if the node set is empty' do
set = Oga::XML::NodeSet.new
@evaluator.first_node_text(set).should == ''
end
end
context '#child_nodes' do context '#child_nodes' do
before do before do
@children = Oga::XML::NodeSet.new([ @children = Oga::XML::NodeSet.new([