Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / range_spec.rb
1 require 'spec_helper'
2
3 describe 'range' do
4   it { is_expected.not_to eq(nil) }
5
6   describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do
7     it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
8     it {
9       pending('Current implementation ignores parameters after the third.')
10       is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i)
11     }
12     it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, %r{Unable to compute range}i) }
13     it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{Unknown range format}i) }
14   end
15
16   describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do
17     it {
18       pending 'the puppet 4 implementation'
19       is_expected.to run.with_params.and_raise_error(ArgumentError)
20     }
21     it {
22       pending 'the puppet 4 implementation'
23       is_expected.to run.with_params('').and_raise_error(ArgumentError)
24     }
25     it {
26       pending 'the puppet 4 implementation'
27       is_expected.to run.with_params({}).and_raise_error(ArgumentError)
28     }
29     it {
30       pending 'the puppet 4 implementation'
31       is_expected.to run.with_params([]).and_raise_error(ArgumentError)
32     }
33     it {
34       pending 'the puppet 4 implementation'
35       is_expected.to run.with_params(true).and_raise_error(ArgumentError)
36     }
37     it {
38       is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError)
39     }
40     it {
41       pending 'the puppet 4 implementation'
42       is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError)
43     }
44     it {
45       pending 'the puppet 4 implementation'
46       is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError)
47     }
48     it {
49       pending 'the puppet 4 implementation'
50       is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError)
51     }
52     it {
53       pending 'the puppet 4 implementation'
54       is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError)
55     }
56     it {
57       pending 'the puppet 4 implementation'
58       is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError)
59     }
60   end
61
62   context 'with characters as bounds' do
63     it { is_expected.to run.with_params('d', 'a').and_return([]) }
64     it { is_expected.to run.with_params('a', 'a').and_return(['a']) }
65     it { is_expected.to run.with_params('a', 'b').and_return(['a', 'b']) }
66     it { is_expected.to run.with_params('a', 'd').and_return(['a', 'b', 'c', 'd']) }
67     it { is_expected.to run.with_params('a', 'd', 1).and_return(['a', 'b', 'c', 'd']) }
68     it { is_expected.to run.with_params('a', 'd', '1').and_return(['a', 'b', 'c', 'd']) }
69     it { is_expected.to run.with_params('a', 'd', 2).and_return(['a', 'c']) }
70     it { is_expected.to run.with_params('a', 'd', -2).and_return(['a', 'c']) }
71     it { is_expected.to run.with_params('a', 'd', 3).and_return(['a', 'd']) }
72     it { is_expected.to run.with_params('a', 'd', 4).and_return(['a']) }
73   end
74
75   context 'with strings as bounds' do
76     it { is_expected.to run.with_params('onea', 'oned').and_return(['onea', 'oneb', 'onec', 'oned']) }
77     it { is_expected.to run.with_params('two', 'one').and_return([]) }
78     it { is_expected.to run.with_params('true', 'false').and_return([]) }
79     it { is_expected.to run.with_params('false', 'true').and_return(['false']) }
80   end
81
82   context 'with integers as bounds' do
83     it { is_expected.to run.with_params(4, 1).and_return([]) }
84     it { is_expected.to run.with_params(1, 1).and_return([1]) }
85     it { is_expected.to run.with_params(1, 2).and_return([1, 2]) }
86     it { is_expected.to run.with_params(1, 4).and_return([1, 2, 3, 4]) }
87     it { is_expected.to run.with_params(1, 4, 1).and_return([1, 2, 3, 4]) }
88     it { is_expected.to run.with_params(1, 4, '1').and_return([1, 2, 3, 4]) }
89     it { is_expected.to run.with_params(1, 4, 2).and_return([1, 3]) }
90     it { is_expected.to run.with_params(1, 4, -2).and_return([1, 3]) }
91     it { is_expected.to run.with_params(1, 4, 3).and_return([1, 4]) }
92     it { is_expected.to run.with_params(1, 4, 4).and_return([1]) }
93   end
94
95   context 'with integers as strings as bounds' do
96     it { is_expected.to run.with_params('4', '1').and_return([]) }
97     it { is_expected.to run.with_params('1', '1').and_return([1]) }
98     it { is_expected.to run.with_params('1', '2').and_return([1, 2]) }
99     it { is_expected.to run.with_params('1', '4').and_return([1, 2, 3, 4]) }
100     it { is_expected.to run.with_params('1', '4', 1).and_return([1, 2, 3, 4]) }
101     it { is_expected.to run.with_params('1', '4', '1').and_return([1, 2, 3, 4]) }
102     it { is_expected.to run.with_params('1', '4', 2).and_return([1, 3]) }
103     it { is_expected.to run.with_params('1', '4', -2).and_return([1, 3]) }
104     it { is_expected.to run.with_params('1', '4', 3).and_return([1, 4]) }
105     it { is_expected.to run.with_params('1', '4', 4).and_return([1]) }
106   end
107
108   context 'with prefixed numbers as strings as bounds' do
109     it { is_expected.to run.with_params('host01', 'host04').and_return(['host01', 'host02', 'host03', 'host04']) }
110     it { is_expected.to run.with_params('01', '04').and_return([1, 2, 3, 4]) }
111   end
112
113   context 'with prefixed numbers as utf8 strings as bounds' do
114     it { is_expected.to run.with_params('ħөŝŧ01', 'ħөŝŧ04').and_return(['ħөŝŧ01', 'ħөŝŧ02', 'ħөŝŧ03', 'ħөŝŧ04']) }
115   end
116
117   context 'with prefixed numbers as double byte character strings as bounds' do
118     it { is_expected.to run.with_params('ホスト01', 'ホスト04').and_return(['ホスト01', 'ホスト02', 'ホスト03', 'ホスト04']) }
119   end
120
121   context 'with dash-range syntax' do
122     it { is_expected.to run.with_params('4-1').and_return([]) }
123     it { is_expected.to run.with_params('1-1').and_return([1]) }
124     it { is_expected.to run.with_params('1-2').and_return([1, 2]) }
125     it { is_expected.to run.with_params('1-4').and_return([1, 2, 3, 4]) }
126   end
127
128   context 'with two-dot-range syntax' do
129     it { is_expected.to run.with_params('4..1').and_return([]) }
130     it { is_expected.to run.with_params('1..1').and_return([1]) }
131     it { is_expected.to run.with_params('1..2').and_return([1, 2]) }
132     it { is_expected.to run.with_params('1..4').and_return([1, 2, 3, 4]) }
133   end
134
135   context 'with three-dot-range syntax' do
136     it { is_expected.to run.with_params('4...1').and_return([]) }
137     it { is_expected.to run.with_params('1...1').and_return([]) }
138     it { is_expected.to run.with_params('1...2').and_return([1]) }
139     it { is_expected.to run.with_params('1...3').and_return([1, 2]) }
140     it { is_expected.to run.with_params('1...5').and_return([1, 2, 3, 4]) }
141   end
142
143   describe 'when passing mixed arguments as bounds' do
144     it {
145       pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length
146       is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds})
147     }
148     it {
149       pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length
150       is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds})
151     }
152     it {
153       pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length
154       is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds})
155     }
156   end
157 end