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:
parent
ac06670c24
commit
7c68f2a49b
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue