File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed
lib/puppet/parser/functions
spec/unit/puppet/parser/functions Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -196,6 +196,20 @@ as a result.
196
196
Would return: [ 'a','b','c']
197
197
198
198
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
+
199
213
- * Type* : rvalue
200
214
201
215
fqdn_rotate
Original file line number Diff line number Diff line change
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 :
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments