Support for the XPath mul/* operator.

This commit is contained in:
Yorick Peterse 2014-08-29 09:36:40 +02:00
parent 034b360d13
commit 89686b6cff
2 changed files with 50 additions and 0 deletions

View File

@ -674,6 +674,22 @@ module Oga
return on_call_number(context, left) % on_call_number(context, right)
end
##
# Processes the `*` operator.
#
# This operator converts the left and right expressions to numbers and
# multiplies the left number with the right number.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [Float]
#
def on_mul(ast_node, context)
left, right = *ast_node
return on_call_number(context, left) * on_call_number(context, right)
end
##
# Delegates function calls to specific handlers.
#

View File

@ -0,0 +1,34 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'multiplication operator' do
before do
@document = parse('<root><a>2</a><b>3</b></root>')
@evaluator = described_class.new(@document)
end
example 'multiply two numbers' do
@evaluator.evaluate('2 * 3').should == 6.0
end
example 'multiply a number and a string' do
@evaluator.evaluate('2 * "3"').should == 6.0
end
example 'multiply two strings' do
@evaluator.evaluate('"2" * "3"').should == 6.0
end
example 'multiply a node set and a number' do
@evaluator.evaluate('root/a * 3').should == 6.0
end
example 'multiply two node sets' do
@evaluator.evaluate('root/a * root/b').should == 6.0
end
example 'return NaN when trying to multiply invalid values' do
@evaluator.evaluate('"" * 1').should be_nan
end
end
end