diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 9194ce5..35300d0 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -1185,6 +1185,21 @@ module Oga return sum 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. # diff --git a/spec/oga/xpath/evaluator/calls/floor_spec.rb b/spec/oga/xpath/evaluator/calls/floor_spec.rb new file mode 100644 index 0000000..a8208f0 --- /dev/null +++ b/spec/oga/xpath/evaluator/calls/floor_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'floor() function' do + before do + @document = parse('10.123') + @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