Support for the XPath sub/- operator.
This commit is contained in:
parent
89686b6cff
commit
a70645fb89
|
@ -690,6 +690,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
|
||||
# subtracts the right number of the left number.
|
||||
#
|
||||
# @param [Oga::XPath::Node] ast_node
|
||||
# @param [Oga::XML::NodeSet] context
|
||||
# @return [Float]
|
||||
#
|
||||
def on_sub(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.
|
||||
#
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
context 'subtraction operator' do
|
||||
before do
|
||||
@document = parse('<root><a>2</a><b>3</b></root>')
|
||||
@evaluator = described_class.new(@document)
|
||||
end
|
||||
|
||||
example 'subtract two numbers' do
|
||||
@evaluator.evaluate('2 - 3').should == -1.0
|
||||
end
|
||||
|
||||
example 'subtract a number and a string' do
|
||||
@evaluator.evaluate('2 - "3"').should == -1.0
|
||||
end
|
||||
|
||||
example 'subtract two strings' do
|
||||
@evaluator.evaluate('"2" - "3"').should == -1.0
|
||||
end
|
||||
|
||||
example 'subtract a node set and a number' do
|
||||
@evaluator.evaluate('root/a - 3').should == -1.0
|
||||
end
|
||||
|
||||
example 'subtract two node sets' do
|
||||
@evaluator.evaluate('root/a - root/b').should == -1.0
|
||||
end
|
||||
|
||||
example 'return NaN when trying to subtract invalid values' do
|
||||
@evaluator.evaluate('"" - 1').should be_nan
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue