1 #! /usr/bin/env ruby -S rspec
5 describe Puppet::Parser::Functions.function(:validate_integer) do
6 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
8 describe 'when calling validate_integer from puppet without any argument or to many' do
9 it "should not compile when no argument is passed" do
10 Puppet[:code] = "validate_integer()"
11 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
13 it "should not compile when more than three arguments are passed" do
14 Puppet[:code] = "validate_integer(1, 1, 1, 1)"
15 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
19 describe 'when calling validate_integer from puppet only with input' do
20 %w{ 1 -1 }.each do |the_number|
21 it "should compile when #{the_number} is an encapsulated integer" do
22 Puppet[:code] = "validate_integer('#{the_number}')"
23 scope.compiler.compile
25 it "should compile when #{the_number} is an bare integer" do
26 Puppet[:code] = "validate_integer(#{the_number})"
27 scope.compiler.compile
31 %w{ [1,2,3,4,5] ['1','2','3','4','5'] }.each do |the_number|
32 it "should compile when multiple Integer arguments are passed in an Array" do
33 Puppet[:code] = "validate_integer(#{the_number})"
34 scope.compiler.compile
38 %w{ true false iAmAString 1test 7.0 -7.0 }.each do |the_number|
39 it "should not compile when #{the_number} is in a string" do
40 Puppet[:code] = "validate_integer('#{the_number}')"
41 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
44 it "should not compile when #{the_number} is a bare word" do
45 Puppet[:code] = "validate_integer(#{the_number})"
46 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
50 it "should not compile when an Integer is part of a larger String" do
51 Puppet[:code] = "validate_integer('1 test')"
52 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
55 it "should not compile when an Array with a non-Integer value is passed" do
56 Puppet[:code] = "validate_integer([1, '-7.0'])"
57 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /at array position 1 to be an Integer/)
60 it "should not compile when a Hash is passed" do
61 Puppet[:code] = "validate_integer({ 1 => 2 })"
62 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer or Array/)
65 it "should not compile when an explicitly undef variable is passed" do
66 Puppet[:code] = <<-'ENDofPUPPETcode'
68 validate_integer($foo)
70 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
73 it "should not compile when an undefined variable is passed" do
74 Puppet[:code] = <<-'ENDofPUPPETcode'
75 validate_integer($foobarbazishouldnotexist)
77 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
81 describe 'when calling validate_integer from puppet with input and a maximum' do
83 %w{ 1 -1 }.each do |the_number|
84 it "should compile when #{the_number} is lower than a maximum of #{max}" do
85 Puppet[:code] = "validate_integer(#{the_number},#{max})"
86 scope.compiler.compile
90 it "should compile when an Integer is equal the maximum" do
91 Puppet[:code] = "validate_integer(#{max},#{max})"
92 scope.compiler.compile
95 it "should not compile when #{max+1} is greater than a maximum of #{max}" do
96 Puppet[:code] = "validate_integer(#{max+1},#{max})"
97 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
100 %w{ [-10,1,2,3,4,5,10] ['-10','1','2','3','4','5','10'] }.each do |the_number|
101 it "should compile when each element of #{the_number} is lower than a maximum of #{max}" do
102 Puppet[:code] = "validate_integer(#{the_number},#{max})"
103 scope.compiler.compile
107 it "should not compile when an element of an Array [-10,1,2,3,4,5,#{max+1}] is greater than a maximum of #{max}" do
108 Puppet[:code] = "validate_integer([-10,1,2,3,4,5,#{max+1}],#{max})"
109 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
112 %w{ true false iAmAString 1test 7.0 -7.0 }.each do |the_max|
113 it "should not compile when a non-Integer maximum #{the_max}, encapsulated in a String, is passed" do
114 Puppet[:code] = "validate_integer(1,'#{the_max}')"
115 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
118 it "should not compile when a non-Integer maximum #{the_max} bare word is passed" do
119 Puppet[:code] = "validate_integer(1,#{the_max})"
120 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
124 it "should not compile when an explicitly undefined variable is passed as maximum and no minimum is passed" do
125 Puppet[:code] = <<-'ENDofPUPPETcode'
127 validate_integer(10, $foo)
129 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
131 it "should not compile when an explicitly undef is passed as maximum and no minimum is passed" do
132 Puppet[:code] = "validate_integer(10, undef)"
133 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
135 it "should not compile when an empty string is passed as maximum and no minimum is passed" do
136 Puppet[:code] = "validate_integer(10, '')"
137 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
139 it "should not compile when an undefined variable for a maximum is passed" do
140 Puppet[:code] = "validate_integer(10, $foobarbazishouldnotexist)"
141 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
145 describe 'when calling validate_integer from puppet with input, a maximum and a minimum' do
146 it "should not compile when a minimum larger than maximum is passed" do
147 Puppet[:code] = "validate_integer(1,1,2)"
148 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /second argument to be larger than third argument/)
153 %w{ 1 -1 }.each do |the_number|
154 it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
155 Puppet[:code] = "validate_integer(#{the_number},#{max},#{min})"
156 scope.compiler.compile
160 it "should compile when an Integer is equal the minimum" do
161 Puppet[:code] = "validate_integer(#{min},#{max},#{min})"
162 scope.compiler.compile
165 it "should compile when an Integer is equal the minimum and maximum" do
166 Puppet[:code] = "validate_integer(#{max},#{max},#{max})"
167 scope.compiler.compile
170 it "should compile when an empty maximum is passed and the Integer is greater than the minimum" do
171 Puppet[:code] = "validate_integer(#{max},'',#{min})"
172 scope.compiler.compile
174 it "should compile when an explicitly undefined maximum is passed and the Integer is greater than the minimum" do
175 Puppet[:code] = "validate_integer(#{max},undef,#{min})"
176 scope.compiler.compile
178 it "should compile when an explicitly undefined variable is passed for maximum and the Integer is greater than the minimum" do
179 Puppet[:code] = <<-"ENDofPUPPETcode"
181 validate_integer(#{max}, $foo, #{min})
183 scope.compiler.compile
185 it "should not compile when no maximum value is given and the Integer is greater than the minimum" do
186 Puppet[:code] = "validate_integer(#{max},,#{min})"
187 expect { scope.compiler.compile }.to raise_error(Puppet::Error, /Syntax error at ','/)
190 it "should not compile when #{min-1} is lower than a minimum of #{min}" do
191 Puppet[:code] = "validate_integer(#{min-1},#{max},#{min})"
192 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
195 %w{ [-10,1,2,3,4,5,10] ['-10','1','2','3','4','5','10'] }.each do |the_number|
196 it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
197 Puppet[:code] = "validate_integer(#{the_number},#{max},#{min})"
198 scope.compiler.compile
202 it "should not compile when an element of an Array [#{min-1},1,2,3,4,5,10] is lower than a minimum of #{min}" do
203 Puppet[:code] = "validate_integer([#{min-1},1,2,3,4,5,10],#{max},#{min})"
204 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
207 %w{ true false iAmAString 1test 7.0 -7.0 }.each do |the_min|
208 it "should not compile when a non-Integer minimum #{the_min}, encapsulated in a String, is passed" do
209 Puppet[:code] = "validate_integer(1,#{max},'#{the_min}')"
210 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
213 it "should not compile when a non-Integer minimum #{the_min} bare word is passed" do
214 Puppet[:code] = "validate_integer(1,#{max},#{the_min})"
215 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)