Raise for non node sets in the sum() function.

According to the XPath spec this function *can only* take node sets, nothing
else. Lets actually enforce that.
This commit is contained in:
Yorick Peterse 2014-08-27 23:47:19 +02:00
parent ac06670c24
commit 7c68f2a49b
2 changed files with 10 additions and 0 deletions

View File

@ -1174,6 +1174,10 @@ module Oga
nodes = process(expression, context) nodes = process(expression, context)
sum = 0.0 sum = 0.0
unless nodes.is_a?(XML::NodeSet)
raise TypeError, 'sum() can only operate on NodeSet instances'
end
nodes.each do |node| nodes.each do |node|
sum += node.text.to_f sum += node.text.to_f
end end

View File

@ -19,5 +19,11 @@ describe Oga::XPath::Evaluator do
example 'return zero by default' do example 'return zero by default' do
@evaluator.evaluate('sum(foo)').should be_zero @evaluator.evaluate('sum(foo)').should be_zero
end 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
end end