Support for the XPath floor() function.

This commit is contained in:
Yorick Peterse 2014-08-27 23:52:23 +02:00
parent 27d2b6c2c3
commit c8fb1ad202
2 changed files with 45 additions and 0 deletions

View File

@ -1185,6 +1185,21 @@ module Oga
return sum return sum
end end
##
# Processes the `floor()` function call.
#
# This function floors a numeric value and returns the result as a float.
#
# @param [Oga::XML::NodeSet] context
# @param [Oga::XPath::Node] expression
# @return [Float]
#
def on_call_floor(context, expression)
number = on_call_number(context, expression)
return number.nan? ? number : number.floor.to_f
end
## ##
# Processes an `(int)` node. # Processes an `(int)` node.
# #

View File

@ -0,0 +1,30 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'floor() function' do
before do
@document = parse('<root>10.123</root>')
@evaluator = described_class.new(@document)
end
example 'return the floor of a literal number' do
@evaluator.evaluate('floor(10.123)').should == 10.0
end
example 'return the floor of a literal string' do
@evaluator.evaluate('floor("10.123")').should == 10.0
end
example 'return the floor of a node set' do
@evaluator.evaluate('floor(root)').should == 10.0
end
example 'return NaN for empty node sets' do
@evaluator.evaluate('floor(foo)').should be_nan
end
example 'return NaN for an empty literal string' do
@evaluator.evaluate('floor("")').should be_nan
end
end
end