1 #! /usr/bin/env ruby -S rspec
5 describe Puppet::Parser::Functions.function(:validate_numeric) do
6 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
8 describe 'when calling validate_numeric from puppet without any argument or to many' do
9 it "should not compile when no argument is passed" do
10 Puppet[:code] = "validate_numeric()"
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_numeric(1, 1, 1, 1)"
15 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
19 describe 'when calling validate_numeric from puppet only with input' do
20 %w{ 1 -1 1.0 -1.0 }.each do |the_number|
21 it "should compile when #{the_number} is an encapsulated numeric" do
22 Puppet[:code] = "validate_numeric('#{the_number}')"
23 scope.compiler.compile
25 it "should compile when #{the_number} is a bare numeric" do
26 Puppet[:code] = "validate_numeric(#{the_number})"
27 scope.compiler.compile
31 %w{ [1,2,3,4,5] ['1','2','3','4','5'] [1.1,2.2,3.3,4.4,5.5] ['1.1','2.2','3.3','4.4','5.5'] }.each do |the_number|
32 it "should compile when multiple Numeric arguments are passed in an Array" do
33 Puppet[:code] = "validate_numeric(#{the_number})"
34 scope.compiler.compile
38 %w{ true false iAmAString 1test }.each do |the_number|
39 it "should not compile when #{the_number} is in a string" do
40 Puppet[:code] = "validate_numeric('#{the_number}')"
41 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
44 it "should not compile when #{the_number} is a bare word" do
45 Puppet[:code] = "validate_numeric(#{the_number})"
46 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
50 it "should not compile when a Numeric is part of a larger String" do
51 Puppet[:code] = "validate_numeric('1.0 test')"
52 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
55 it "should not compile when an Array with a non-Numeric value is passed" do
56 Puppet[:code] = "validate_numeric([1, 'test'])"
57 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /at array position 1 to be a Numeric/)
60 it "should not compile when a Hash is passed" do
61 Puppet[:code] = "validate_numeric({ 1 => 2 })"
62 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric or Array/)
65 it "should not compile when an explicitly undef variable is passed" do
66 Puppet[:code] = <<-'ENDofPUPPETcode'
68 validate_numeric($foo)
70 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
73 it "should not compile when an undefined variable is passed" do
74 Puppet[:code] = 'validate_numeric($foobarbazishouldnotexist)'
75 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
79 describe 'when calling validate_numeric from puppet with input and a maximum' do
81 %w{ 1 -1 1.0 -1.0 }.each do |the_number|
82 it "should compile when #{the_number} is lower than a maximum of #{max}" do
83 Puppet[:code] = "validate_numeric(#{the_number},#{max})"
84 scope.compiler.compile
88 it "should compile when a Numeric is equal the maximum" do
89 Puppet[:code] = "validate_numeric(#{max},#{max})"
90 scope.compiler.compile
93 it "should not compile when #{max+1} is greater than a maximum of #{max}" do
94 Puppet[:code] = "validate_numeric(#{max+1},#{max})"
95 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
98 %w{ [-10,1,2,3,4,5,10] ['-10','1','2','3','4','5','10'] }.each do |the_number|
99 it "should compile when each element of #{the_number} is lower than a maximum of #{max}" do
100 Puppet[:code] = "validate_numeric(#{the_number},#{max})"
101 scope.compiler.compile
105 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
106 Puppet[:code] = "validate_numeric([-10,1,2,3,4,5,#{max+1}],#{max})"
107 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
110 %w{ true false iAmAString 1test }.each do |the_max|
111 it "should not compile when a non-Numeric maximum #{the_max}, encapsulated in a String, is passed" do
112 Puppet[:code] = "validate_numeric(1,'#{the_max}')"
113 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
116 it "should not compile when a non-Numeric maximum #{the_max} bare word is passed" do
117 Puppet[:code] = "validate_numeric(1,#{the_max})"
118 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
122 it "should not compile when an explicitly undefined variable is passed as maximum and no minimum is passed" do
123 Puppet[:code] = <<-'ENDofPUPPETcode'
125 validate_numeric(10, $foo)
127 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
129 it "should not compile when an explicitly undef is passed as maximum and no minimum is passed" do
130 Puppet[:code] = "validate_numeric(10, undef)"
131 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
133 it "should not compile when an empty string is passed as maximum and no minimum is passed" do
134 Puppet[:code] = "validate_numeric(10, '')"
135 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
137 it "should not compile when an undefined variable for a maximum is passed" do
138 Puppet[:code] = "validate_numeric(10, $foobarbazishouldnotexist)"
139 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
143 describe 'when calling validate_numeric from puppet with input, a maximum and a minimum' do
144 it "should not compile when a minimum larger than maximum is passed" do
145 Puppet[:code] = "validate_numeric(1,1,2)"
146 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /second argument to be larger than third argument/)
151 %w{ 1 -1 }.each do |the_number|
152 it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
153 Puppet[:code] = "validate_numeric(#{the_number},#{max},#{min})"
154 scope.compiler.compile
158 it "should compile when a Numeric is equal the minimum" do
159 Puppet[:code] = "validate_numeric(#{min},#{max},#{min})"
160 scope.compiler.compile
163 it "should compile when a Numeric is equal the minimum and maximum" do
164 Puppet[:code] = "validate_numeric(#{max},#{max},#{max})"
165 scope.compiler.compile
168 it "should compile when an empty maximum is passed and the Numeric is greater than the minimum" do
169 Puppet[:code] = "validate_numeric(#{max}.1,'',#{min})"
170 scope.compiler.compile
172 it "should compile when an explicitly undefined maximum is passed and the Numeric is greater than the minimum" do
173 Puppet[:code] = "validate_numeric(#{max}.1,undef,#{min})"
174 scope.compiler.compile
176 it "should compile when an explicitly undefined variable is passed for maximum and the Numeric is greater than the minimum" do
177 Puppet[:code] = <<-"ENDofPUPPETcode"
179 validate_numeric(#{max}.1, $foo, #{min})
181 scope.compiler.compile
183 it "should not compile when no maximum value is given and the Numeric is greater than the minimum" do
184 Puppet[:code] = "validate_numeric(#{max}.1,,#{min})"
185 expect { scope.compiler.compile }.to raise_error(Puppet::Error, /Syntax error at ','/)
188 it "should not compile when #{min-1} is lower than a minimum of #{min}" do
189 Puppet[:code] = "validate_numeric(#{min-1.0},#{max},#{min})"
190 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
193 %w{ [-10,1,2,3,4,5,10] ['-10.0','1','2','3','4','5','10.0'] }.each do |the_number|
194 it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
195 Puppet[:code] = "validate_numeric(#{the_number},#{max},#{min})"
196 scope.compiler.compile
200 it "should not compile when an element of an Array [#{min-1.1},1,2,3,4,5,10.0] is lower than a minimum of #{min}" do
201 Puppet[:code] = "validate_numeric([#{min-1},1,2,3,4,5,10],#{max},#{min})"
202 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
205 %w{ true false iAmAString 1test }.each do |the_min|
206 it "should not compile when a non-Numeric minimum #{the_min}, encapsulated in a String, is passed" do
207 Puppet[:code] = "validate_numeric(1,#{max},'#{the_min}')"
208 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
211 it "should not compile when a non-Numeric minimum #{the_min} bare word is passed" do
212 Puppet[:code] = "validate_numeric(1,#{max},#{the_min})"
213 expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)