File tree 4 files changed +106
-0
lines changed
lib/puppet/parser/functions
4 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,9 @@ string. *Type*: rvalue
124
124
* ` capitalize ` : Capitalizes the first letter of a string or array of strings.
125
125
Requires either a single string or an array as an input. * Type* : rvalue
126
126
127
+ * ` ceiling ` : Returns the smallest integer greater than or equal to the argument.
128
+ Takes a single numeric value as an argument. * Type* : rvalue
129
+
127
130
* ` chomp ` : Removes the record separator from the end of a string or an array of
128
131
strings; for example, 'hello\n' becomes 'hello'. Requires a single string or array as an input. * Type* : rvalue
129
132
Original file line number Diff line number Diff line change
1
+ module Puppet ::Parser ::Functions
2
+ newfunction ( :ceiling , :type => :rvalue , :doc => <<-EOS
3
+ Returns the smallest integer greater or equal to the argument.
4
+ Takes a single numeric value as an argument.
5
+ EOS
6
+ ) do |arguments |
7
+
8
+ raise ( Puppet ::ParseError , "ceiling(): Wrong number of arguments " +
9
+ "given (#{ arguments . size } for 1)" ) if arguments . size != 1
10
+
11
+ begin
12
+ arg = Float ( arguments [ 0 ] )
13
+ rescue TypeError , ArgumentError => e
14
+ raise ( Puppet ::ParseError , "ceiling(): Wrong argument type " +
15
+ "given (#{ arguments [ 0 ] } for Numeric)" )
16
+ end
17
+
18
+ raise ( Puppet ::ParseError , "ceiling(): Wrong argument type " +
19
+ "given (#{ arg . class } for Numeric)" ) if arg . is_a? ( Numeric ) == false
20
+
21
+ arg . ceil
22
+ end
23
+ end
24
+
25
+ # 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
+ require 'spec_helper_acceptance'
3
+
4
+ describe 'ceiling function' , :unless => UNSUPPORTED_PLATFORMS . include? ( fact ( 'operatingsystem' ) ) do
5
+ describe 'success' do
6
+ it 'ceilings floats' do
7
+ pp = <<-EOS
8
+ $a = 12.8
9
+ $b = 13
10
+ $o = ceiling($a)
11
+ if $o == $b {
12
+ notify { 'output correct': }
13
+ }
14
+ EOS
15
+
16
+ apply_manifest ( pp , :catch_failures => true ) do |r |
17
+ expect ( r . stdout ) . to match ( /Notice: output correct/ )
18
+ end
19
+ end
20
+ it 'ceilings integers' do
21
+ pp = <<-EOS
22
+ $a = 7
23
+ $b = 7
24
+ $o = ceiling($a)
25
+ if $o == $b {
26
+ notify { 'output correct': }
27
+ }
28
+ EOS
29
+
30
+ apply_manifest ( pp , :catch_failures => true ) do |r |
31
+ expect ( r . stdout ) . to match ( /Notice: output correct/ )
32
+ end
33
+ end
34
+ end
35
+ describe 'failure' do
36
+ it 'handles improper argument counts'
37
+ it 'handles non-numbers'
38
+ end
39
+ end
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 ceiling function" do
6
+ let ( :scope ) { PuppetlabsSpec ::PuppetInternals . scope }
7
+
8
+ it "should exist" do
9
+ expect ( Puppet ::Parser ::Functions . function ( "ceiling" ) ) . to eq ( "function_ceiling" )
10
+ end
11
+
12
+ it "should raise a ParseError if there is less than 1 argument" do
13
+ expect { scope . function_ceiling ( [ ] ) } . to ( 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
+ expect { scope . function_ceiling ( [ "foo" ] ) } . to ( 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
+ expect { scope . function_ceiling ( [ true ] ) } . to ( 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_ceiling ( [ 12.4 ] )
26
+ expect ( result . is_a? ( Integer ) ) . to ( eq ( true ) )
27
+ end
28
+
29
+ it "should return the input when an integer is passed" do
30
+ result = scope . function_ceiling ( [ 7 ] )
31
+ expect ( result ) . to ( eq ( 7 ) )
32
+ end
33
+
34
+ it "should return the smallest integer greater than or equal to the input" do
35
+ result = scope . function_ceiling ( [ 3.8 ] )
36
+ expect ( result ) . to ( eq ( 4 ) )
37
+ end
38
+ end
39
+
You can’t perform that action at this time.
0 commit comments