Skip to content

Commit c0c36d5

Browse files
author
Kristof Willaert
committed
Merge pull request #11 from bkkrw/master
Add floor function
2 parents 35bad77 + a00fdc2 commit c0c36d5

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

lib/puppet/parser/functions/floor.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#! /usr/bin/env ruby -S rspec
2+
3+
require 'spec_helper'
4+
5+
describe "the to_bytes function" do
6+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7+
8+
it "should exist" do
9+
Puppet::Parser::Functions.function("to_bytes").should == "function_to_bytes"
10+
end
11+
12+
it "should raise a ParseError if there is less than 1 arguments" do
13+
lambda { scope.function_to_bytes([]) }.should( raise_error(Puppet::ParseError))
14+
end
15+
16+
it "should convert kB to B" do
17+
result = scope.function_to_bytes(["4 kB"])
18+
result.should(eq(4096))
19+
end
20+
21+
it "should work without B in unit" do
22+
result = scope.function_to_bytes(["4 k"])
23+
result.should(eq(4096))
24+
end
25+
26+
it "should work without a space before unit" do
27+
result = scope.function_to_bytes(["4k"])
28+
result.should(eq(4096))
29+
end
30+
31+
it "should work without a unit" do
32+
result = scope.function_to_bytes(["5678"])
33+
result.should(eq(5678))
34+
end
35+
36+
it "should convert fractions" do
37+
result = scope.function_to_bytes(["1.5 kB"])
38+
result.should(eq(1536))
39+
end
40+
41+
it "should convert scientific notation" do
42+
result = scope.function_to_bytes(["1.5e2 B"])
43+
result.should(eq(150))
44+
end
45+
46+
it "should do nothing with a positive number" do
47+
result = scope.function_to_bytes([5678])
48+
result.should(eq(5678))
49+
end
50+
51+
it "should should raise a ParseError if input isn't a number" do
52+
lambda { scope.function_to_bytes(["foo"]) }.should( raise_error(Puppet::ParseError))
53+
end
54+
55+
it "should should raise a ParseError if prefix is unknown" do
56+
lambda { scope.function_to_bytes(["5 uB"]) }.should( raise_error(Puppet::ParseError))
57+
end
58+
end

0 commit comments

Comments
 (0)