Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_slength_spec.rb
1 require 'spec_helper'
2
3 describe 'validate_slength' do
4   after(:each) do
5     ENV.delete('STDLIB_LOG_DEPRECATIONS')
6   end
7
8   # Checking for deprecation warning
9   it 'displays a single deprecation' do
10     ENV['STDLIB_LOG_DEPRECATIONS'] = 'true'
11     expect(scope).to receive(:warning).with(include('This method is deprecated'))
12     is_expected.to run.with_params('1234567890', 10)
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, %r{wrong number of arguments}i) }
18     it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
19     it { is_expected.to run.with_params('', 2, 3, 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
20     it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{second argument to be a positive Numeric}) }
21     it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, %r{second argument to be a positive Numeric}) }
22     it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, %r{third argument to be unset or a positive Numeric}) }
23     it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, %r{third argument to be unset or a positive Numeric}) }
24     it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, %r{argument to be equal to or larger than third argument}) }
25   end
26
27   context 'with a maximum length of 10' do
28     describe 'rejects strings longer than the limit' do
29       it { is_expected.to run.with_params('1234567890a', 10).and_raise_error(Puppet::ParseError, %r{Expected length}) }
30       it { is_expected.to run.with_params('1234567890abcdef', 10).and_raise_error(Puppet::ParseError, %r{Expected length}) }
31       it { is_expected.to run.with_params(['one', '1234567890abcdef'], 10).and_raise_error(Puppet::ParseError, %r{Expected length}) }
32     end
33
34     describe 'accepts strings shorter or equal to the limit' do
35       it { is_expected.to run.with_params('1234567890', 10) }
36       it { is_expected.to run.with_params('12345', 10) }
37       it { is_expected.to run.with_params(['one', 'two'], 10) }
38     end
39   end
40
41   context 'with a minimum length of 5' do
42     describe 'rejects strings longer than the upper limit' do
43       it { is_expected.to run.with_params('1234567890a', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
44       it { is_expected.to run.with_params('1234567890abcdef', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
45     end
46
47     describe 'rejects numbers shorter than the lower limit' do
48       it { is_expected.to run.with_params('one', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
49       it { is_expected.to run.with_params(['12345678', 'two'], 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
50     end
51
52     describe 'accepts strings of length between and including the limits' do
53       it { is_expected.to run.with_params('12345', 10, 5) }
54       it { is_expected.to run.with_params('123456', 10, 5) }
55       it { is_expected.to run.with_params('1234567', 10, 5) }
56       it { is_expected.to run.with_params('12345678', 10, 5) }
57       it { is_expected.to run.with_params('123456789', 10, 5) }
58       it { is_expected.to run.with_params('1234567890', 10, 5) }
59       it { is_expected.to run.with_params(['1233456', '12345678'], 10, 5) }
60     end
61   end
62
63   describe 'corner cases' do
64     it {
65       pending('this should work')
66       is_expected.to run.with_params('', 0, 0)
67     }
68     it { is_expected.to run.with_params('1234567890', 10, 10) }
69   end
70
71   describe 'empty upper limit is interpreted as infinity' do
72     it {
73       pending('not implemented')
74       is_expected.to run.with_params('1234567890ab', '', 10)
75     }
76     it {
77       pending('not implemented')
78       is_expected.to run.with_params('12345678', '', 10).and_raise_error(Puppet::ParseError, %r{Expected length})
79     }
80   end
81 end