Skip to content

Commit 893e610

Browse files
committed
Merge pull request #135 from willaerk/master
Add floor function implementation and unit tests
2 parents fc2352f + 0527341 commit 893e610

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.markdown

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ as a result.
196196
Would return: ['a','b','c']
197197

198198

199+
- *Type*: rvalue
200+
201+
floor
202+
-----
203+
Returns the largest integer less or equal to the argument.
204+
Takes a single numeric value as an argument.
205+
206+
*Examples:*
207+
208+
floor('3.8')
209+
210+
Would return: '3'
211+
212+
199213
- *Type*: rvalue
200214

201215
fqdn_rotate

lib/puppet/parser/functions/floor.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:floor, :type => :rvalue, :doc => <<-EOS
3+
Returns the largest integer less or equal to the argument.
4+
Takes a single numeric value as an argument.
5+
EOS
6+
) do |arguments|
7+
8+
raise(Puppet::ParseError, "floor(): Wrong number of arguments " +
9+
"given (#{arguments.size} for 1)") if arguments.size != 1
10+
11+
arg = arguments[0]
12+
13+
raise(Puppet::ParseError, "floor(): Wrong argument type " +
14+
"given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false
15+
16+
arg.floor
17+
end
18+
end
19+
20+
# vim: set ts=2 sw=2 et :
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#! /usr/bin/env ruby -S rspec
2+
3+
require 'spec_helper'
4+
5+
describe "the floor function" do
6+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7+
8+
it "should exist" do
9+
Puppet::Parser::Functions.function("floor").should == "function_floor"
10+
end
11+
12+
it "should raise a ParseError if there is less than 1 argument" do
13+
lambda { scope.function_floor([]) }.should( raise_error(Puppet::ParseError, /Wrong number of arguments/))
14+
end
15+
16+
it "should should raise a ParseError if input isn't numeric (eg. String)" do
17+
lambda { scope.function_floor(["foo"]) }.should( raise_error(Puppet::ParseError, /Wrong argument type/))
18+
end
19+
20+
it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do
21+
lambda { scope.function_floor([true]) }.should( raise_error(Puppet::ParseError, /Wrong argument type/))
22+
end
23+
24+
it "should return an integer when a numeric type is passed" do
25+
result = scope.function_floor([12.4])
26+
result.is_a?(Integer).should(eq(true))
27+
end
28+
29+
it "should return the input when an integer is passed" do
30+
result = scope.function_floor([7])
31+
result.should(eq(7))
32+
end
33+
34+
it "should return the largest integer less than or equal to the input" do
35+
result = scope.function_floor([3.8])
36+
result.should(eq(3))
37+
end
38+
end
39+

0 commit comments

Comments
 (0)