diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 8f561a7..9194ce5 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -1174,6 +1174,10 @@ module Oga nodes = process(expression, context) sum = 0.0 + unless nodes.is_a?(XML::NodeSet) + raise TypeError, 'sum() can only operate on NodeSet instances' + end + nodes.each do |node| sum += node.text.to_f end diff --git a/spec/oga/xpath/evaluator/calls/sum_spec.rb b/spec/oga/xpath/evaluator/calls/sum_spec.rb index 467a134..f6b7b64 100644 --- a/spec/oga/xpath/evaluator/calls/sum_spec.rb +++ b/spec/oga/xpath/evaluator/calls/sum_spec.rb @@ -19,5 +19,11 @@ describe Oga::XPath::Evaluator do example 'return zero by default' do @evaluator.evaluate('sum(foo)').should be_zero end + + example 'raise a TypeError for non node set arguments' do + block = lambda { @evaluator.evaluate('sum("foo")') } + + block.should raise_error(TypeError) + end end end