Skip to content

Commit 055083c

Browse files
author
Morgan Haskel
committed
Merge pull request #407 from adamcrews/ceiling_function
Add a ceiling function to complement the floor function.
2 parents 6d07a6a + 53b1802 commit 055083c

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

README.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ string. *Type*: rvalue
122122
* `capitalize`: Capitalizes the first letter of a string or array of strings.
123123
Requires either a single string or an array as an input. *Type*: rvalue
124124

125+
* `ceiling`: Returns the smallest integer greater than or equal to the argument.
126+
Takes a single numeric value as an argument. *Type*: rvalue
127+
125128
* `chomp`: Removes the record separator from the end of a string or an array of
126129
strings; for example, 'hello\n' becomes 'hello'. Requires a single string or array as an input. *Type*: rvalue
127130

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 :

spec/acceptance/ceiling_spec.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

spec/functions/ceiling_spec.rb

+39
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 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+

0 commit comments

Comments
 (0)