]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/validate_numeric_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_numeric_spec.rb
1 #! /usr/bin/env ruby -S rspec
2
3 require 'spec_helper'
4
5 describe Puppet::Parser::Functions.function(:validate_numeric) do
6   let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7
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/)
12     end
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/)
16     end
17   end
18
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
24       end
25       it "should compile when #{the_number} is a bare numeric" do
26         Puppet[:code] = "validate_numeric(#{the_number})"
27         scope.compiler.compile
28       end
29     end
30
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
35       end
36     end
37
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/)
42       end
43
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/)
47       end
48     end
49
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/)
53     end
54
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/)
58     end
59
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/)
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_numeric($foo)
69       ENDofPUPPETcode
70       expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
71     end
72
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/)
76     end
77   end
78
79   describe 'when calling validate_numeric from puppet with input and a maximum' do
80     max = 10
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
85       end
86     end
87
88     it "should compile when a Numeric is equal the maximum" do
89       Puppet[:code] = "validate_numeric(#{max},#{max})"
90       scope.compiler.compile
91     end
92
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/)
96     end
97
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
102       end
103     end
104
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/)
108     end
109
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/)
114       end
115  
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/)
119       end
120     end
121
122     it "should not compile when an explicitly undefined variable is passed as maximum and no minimum is passed" do
123       Puppet[:code] = <<-'ENDofPUPPETcode'
124         $foo = undef
125         validate_numeric(10, $foo)
126       ENDofPUPPETcode
127       expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
128     end
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/)
132     end
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/)
136     end
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/)
140     end
141   end
142
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/)
147     end
148
149     max = 10
150     min = -10
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
155       end
156     end
157
158     it "should compile when a Numeric is equal the minimum" do
159       Puppet[:code] = "validate_numeric(#{min},#{max},#{min})"
160       scope.compiler.compile
161     end
162
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
166     end
167
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
171     end
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
175     end
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"
178         $foo = undef
179         validate_numeric(#{max}.1, $foo, #{min})
180       ENDofPUPPETcode
181       scope.compiler.compile
182     end
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 ','/)
186     end
187
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/)
191     end
192
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
197       end
198     end
199
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/)
203     end
204
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/)
209       end
210  
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/)
214       end
215     end
216   end
217 end