Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_numeric_spec.rb
1 require 'spec_helper'
2
3 describe 'validate_numeric' 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', {}, { '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 a Numeric/) }
22       it { is_expected.to run.with_params(invalid, 10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
23       it { is_expected.to run.with_params(invalid, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
24     end
25
26     context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
27       it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
28     end
29
30     context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
31       it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
32       it { is_expected.to run.with_params([0, 1, 2, {0=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
33     end
34
35     it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
36     it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
37     it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, /second argument to be larger than third argument/) }
38   end
39
40   context 'with no range constraints' do
41     it { is_expected.to run.with_params(1) }
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, 2, 3, 4]) }
46     it { is_expected.to run.with_params([1, '2', '3', 4]) }
47   end
48
49   context "with a maximum limit of 10.0" do
50     describe 'rejects numbers greater than the limit' do
51       it { is_expected.to run.with_params(11, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
52       it { is_expected.to run.with_params(100, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
53       it { is_expected.to run.with_params(2**65, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
54       it { is_expected.to run.with_params([1,2,10.0,100], 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
55     end
56
57     describe 'accepts numbers less or equal to the limit' do
58       it { is_expected.to run.with_params(10.0, 10.0) }
59       it { is_expected.to run.with_params(1, 10.0) }
60       it { is_expected.to run.with_params(-1, 10.0) }
61       it { is_expected.to run.with_params('1', 10.0) }
62       it { is_expected.to run.with_params('-1', 10.0) }
63       it { is_expected.to run.with_params([1, 2, 3, 4], 10.0) }
64       it { is_expected.to run.with_params([1, '2', '3', 4], 10.0) }
65     end
66
67     context "with a minimum limit of -10.0" do
68       describe 'rejects numbers greater than the upper limit' do
69         it { is_expected.to run.with_params(11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
70         it { is_expected.to run.with_params(100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
71         it { is_expected.to run.with_params(2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
72         it { is_expected.to run.with_params([1,2,10.0,100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
73       end
74
75       describe 'rejects numbers smaller than the lower limit' do
76         it { is_expected.to run.with_params(-11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
77         it { is_expected.to run.with_params(-100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
78         it { is_expected.to run.with_params(-2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
79         it { is_expected.to run.with_params([-10.0, 1,2,10.0,-100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
80       end
81
82       describe 'accepts numbers between and including the limits' do
83         it { is_expected.to run.with_params(10.0, 10.0, -10.0) }
84         it { is_expected.to run.with_params(-10.0, 10.0, -10.0) }
85         it { is_expected.to run.with_params(1, 10.0, -10.0) }
86         it { is_expected.to run.with_params(-1, 10.0, -10.0) }
87         it { is_expected.to run.with_params('1', 10.0, -10.0) }
88         it { is_expected.to run.with_params('-1', 10.0, -10.0) }
89         it { is_expected.to run.with_params([1, 2, 3, 4], 10.0, -10.0) }
90         it { is_expected.to run.with_params([1, '2', '3', 4], 10.0, -10.0) }
91       end
92     end
93   end
94
95   it { is_expected.to run.with_params(10.0, 10.0, 10.0) }
96
97   describe 'empty upper limit is interpreted as infinity' do
98     it { is_expected.to run.with_params(11, '', 10.0) }
99   end
100 end