]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/validate_integer_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_integer_spec.rb
1 #! /usr/bin/env ruby -S rspec
2
3 require 'spec_helper'
4
5 describe Puppet::Parser::Functions.function(:validate_integer) do
6   let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7
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/)
12     end
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/)
16     end
17   end
18
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
24       end
25       it "should compile when #{the_number} is an bare integer" do
26         Puppet[:code] = "validate_integer(#{the_number})"
27         scope.compiler.compile
28       end
29     end
30
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
35       end
36     end
37
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/)
42       end
43
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/)
47       end
48     end
49
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/)
53     end
54
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/)
58     end
59
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/)
63     end
64
65     it "should not compile when an explicitly undef variable is passed" do
66       Puppet[:code] = <<-'ENDofPUPPETcode'
67         $foo = undef
68         validate_integer($foo)
69       ENDofPUPPETcode
70       expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
71     end
72
73     it "should not compile when an undefined variable is passed" do
74       Puppet[:code] = <<-'ENDofPUPPETcode'
75         validate_integer($foobarbazishouldnotexist)
76       ENDofPUPPETcode
77       expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
78     end
79   end
80
81   describe 'when calling validate_integer from puppet with input and a maximum' do
82     max = 10
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
87       end
88     end
89
90     it "should compile when an Integer is equal the maximum" do
91       Puppet[:code] = "validate_integer(#{max},#{max})"
92       scope.compiler.compile
93     end
94
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/)
98     end
99
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
104       end
105     end
106
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/)
110     end
111
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/)
116       end
117  
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/)
121       end
122     end
123
124     it "should not compile when an explicitly undefined variable is passed as maximum and no minimum is passed" do
125       Puppet[:code] = <<-'ENDofPUPPETcode'
126         $foo = undef
127         validate_integer(10, $foo)
128       ENDofPUPPETcode
129       expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
130     end
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/)
134     end
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/)
138     end
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/)
142     end
143   end
144
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/)
149     end
150
151     max = 10
152     min = -10
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
157       end
158     end
159
160     it "should compile when an Integer is equal the minimum" do
161       Puppet[:code] = "validate_integer(#{min},#{max},#{min})"
162       scope.compiler.compile
163     end
164
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
168     end
169
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
173     end
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
177     end
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"
180         $foo = undef
181         validate_integer(#{max}, $foo, #{min})
182       ENDofPUPPETcode
183       scope.compiler.compile
184     end
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 ','/)
188     end
189
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/)
193     end
194
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
199       end
200     end
201
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/)
205     end
206
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/)
211       end
212  
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/)
216       end
217     end
218   end
219 end