6558d00ffc94093e3165cf4ec8fa3ceee4e0a523
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_integer_spec.rb
1 require 'spec_helper'
2
3 describe 'validate_integer' do
4   after(:all) do
5     ENV.delete('STDLIB_LOG_DEPRECATIONS')
6   end
7
8   # Checking for deprecation warning
9   it 'should display a single deprecation' do
10     ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
11     scope.expects(:warning).with(includes('This method is deprecated'))
12     is_expected.to run.with_params(3)
13   end
14
15   describe 'signature validation' do
16     it { is_expected.not_to eq(nil) }
17     it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
18     it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
19
20     [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', 7.0, -7.0, {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x'].each do |invalid|
21       it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /to be an Integer/) }
22       it { is_expected.to run.with_params(invalid, 10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
23       it { is_expected.to run.with_params(invalid, 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
24       it { is_expected.to run.with_params([0, 1, 2, invalid, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
25     end
26
27     context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
28       it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
29     end
30
31     context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
32       it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
33       it { is_expected.to run.with_params([0, 1, 2, {0=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
34     end
35
36     it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, /to be unset or an Integer/) }
37     it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, /to be unset or an Integer/) }
38     it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, /second argument to be larger than third argument/) }
39   end
40
41   context 'with no range constraints' do
42     it { is_expected.to run.with_params(1) }
43     it { is_expected.to run.with_params(-1) }
44     it { is_expected.to run.with_params('1') }
45     it { is_expected.to run.with_params('-1') }
46     it { is_expected.to run.with_params([1, 2, 3, 4]) }
47     it { is_expected.to run.with_params([1, '2', '3', 4]) }
48   end
49
50   context "with a maximum limit of 10" do
51     describe 'rejects numbers greater than the limit' do
52       it { is_expected.to run.with_params(11, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
53       it { is_expected.to run.with_params(100, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
54       it { is_expected.to run.with_params(2**65, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
55       it { is_expected.to run.with_params([1,2,10,100], 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
56     end
57
58     describe 'accepts numbers less or equal to the limit' do
59       it { is_expected.to run.with_params(10, 10) }
60       it { is_expected.to run.with_params(1, 10) }
61       it { is_expected.to run.with_params(-1, 10) }
62       it { is_expected.to run.with_params('1', 10) }
63       it { is_expected.to run.with_params('-1', 10) }
64       it { is_expected.to run.with_params([1, 2, 3, 4], 10) }
65       it { is_expected.to run.with_params([1, '2', '3', 4], 10) }
66     end
67
68     context "with a minimum limit of -10" do
69       describe 'rejects numbers greater than the upper limit' do
70         it { is_expected.to run.with_params(11, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
71         it { is_expected.to run.with_params(100, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
72         it { is_expected.to run.with_params(2**65, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
73         it { is_expected.to run.with_params([1,2,10,100], 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
74       end
75
76       describe 'rejects numbers smaller than the lower limit' do
77         it { is_expected.to run.with_params(-11, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
78         it { is_expected.to run.with_params(-100, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
79         it { is_expected.to run.with_params(-2**65, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
80         it { is_expected.to run.with_params([-10, 1,2,10,-100], 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
81       end
82
83       describe 'accepts numbers between and including the limits' do
84         it { is_expected.to run.with_params(10, 10, -10) }
85         it { is_expected.to run.with_params(-10, 10, -10) }
86         it { is_expected.to run.with_params(1, 10, -10) }
87         it { is_expected.to run.with_params(-1, 10, -10) }
88         it { is_expected.to run.with_params('1', 10, -10) }
89         it { is_expected.to run.with_params('-1', 10, -10) }
90         it { is_expected.to run.with_params([1, 2, 3, 4], 10, -10) }
91         it { is_expected.to run.with_params([1, '2', '3', 4], 10, -10) }
92       end
93     end
94   end
95
96   it { is_expected.to run.with_params(10, 10, 10) }
97
98   describe 'empty upper limit is interpreted as infinity' do
99     it { is_expected.to run.with_params(11, '', 10) }
100   end
101 end