Update stdlib
[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, /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, /wrong number of arguments/i)
11     }
12     it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, /Unable to compute range/i) }
13     it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /Unknown range format/i) }
14   end
15
16   describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do
17     it { pending "the puppet 4 implementation"; is_expected.to run.with_params().and_raise_error(ArgumentError) }
18     it { pending "the puppet 4 implementation"; is_expected.to run.with_params('').and_raise_error(ArgumentError) }
19     it { pending "the puppet 4 implementation"; is_expected.to run.with_params({}).and_raise_error(ArgumentError) }
20     it { pending "the puppet 4 implementation"; is_expected.to run.with_params([]).and_raise_error(ArgumentError) }
21     it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
22     it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
23     it {                                        is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) }
24     it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError) }
25     it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError) }
26     it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError) }
27     it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) }
28     it { pending "the puppet 4 implementation"; is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError) }
29   end
30
31   context 'with characters as bounds' do
32     it { is_expected.to run.with_params('d', 'a').and_return([]) }
33     it { is_expected.to run.with_params('a', 'a').and_return(['a']) }
34     it { is_expected.to run.with_params('a', 'b').and_return(['a', 'b']) }
35     it { is_expected.to run.with_params('a', 'd').and_return(['a', 'b', 'c', 'd']) }
36     it { is_expected.to run.with_params('a', 'd', 1).and_return(['a', 'b', 'c', 'd']) }
37     it { is_expected.to run.with_params('a', 'd', '1').and_return(['a', 'b', 'c', 'd']) }
38     it { is_expected.to run.with_params('a', 'd', 2).and_return(['a', 'c']) }
39     it { is_expected.to run.with_params('a', 'd', -2).and_return(['a', 'c']) }
40     it { is_expected.to run.with_params('a', 'd', 3).and_return(['a', 'd']) }
41     it { is_expected.to run.with_params('a', 'd', 4).and_return(['a']) }
42   end
43
44   context 'with strings as bounds' do
45     it { is_expected.to run.with_params('onea', 'oned').and_return(['onea', 'oneb', 'onec', 'oned']) }
46     it { is_expected.to run.with_params('two', 'one').and_return([]) }
47     it { is_expected.to run.with_params('true', 'false').and_return([]) }
48     it { is_expected.to run.with_params('false', 'true').and_return(['false']) }
49   end
50
51   context 'with integers as bounds' do
52     it { is_expected.to run.with_params(4, 1).and_return([]) }
53     it { is_expected.to run.with_params(1, 1).and_return([1]) }
54     it { is_expected.to run.with_params(1, 2).and_return([1, 2]) }
55     it { is_expected.to run.with_params(1, 4).and_return([1, 2, 3, 4]) }
56     it { is_expected.to run.with_params(1, 4, 1).and_return([1, 2, 3, 4]) }
57     it { is_expected.to run.with_params(1, 4, '1').and_return([1, 2, 3, 4]) }
58     it { is_expected.to run.with_params(1, 4, 2).and_return([1, 3]) }
59     it { is_expected.to run.with_params(1, 4, -2).and_return([1, 3]) }
60     it { is_expected.to run.with_params(1, 4, 3).and_return([1, 4]) }
61     it { is_expected.to run.with_params(1, 4, 4).and_return([1]) }
62   end
63
64   context 'with integers as strings as bounds' do
65     it { is_expected.to run.with_params('4', '1').and_return([]) }
66     it { is_expected.to run.with_params('1', '1').and_return([1]) }
67     it { is_expected.to run.with_params('1', '2').and_return([1, 2]) }
68     it { is_expected.to run.with_params('1', '4').and_return([1, 2, 3, 4]) }
69     it { is_expected.to run.with_params('1', '4', 1).and_return([1, 2, 3, 4]) }
70     it { is_expected.to run.with_params('1', '4', '1').and_return([1, 2, 3, 4]) }
71     it { is_expected.to run.with_params('1', '4', 2).and_return([1, 3]) }
72     it { is_expected.to run.with_params('1', '4', -2).and_return([1, 3]) }
73     it { is_expected.to run.with_params('1', '4', 3).and_return([1, 4]) }
74     it { is_expected.to run.with_params('1', '4', 4).and_return([1]) }
75   end
76
77   context 'with prefixed numbers as strings as bounds' do
78     it { is_expected.to run.with_params('host01', 'host04').and_return(['host01', 'host02', 'host03', 'host04']) }
79     it { is_expected.to run.with_params('01', '04').and_return([1, 2, 3, 4]) }
80   end
81
82   context 'with prefixed numbers as utf8 strings as bounds' do
83     it { is_expected.to run.with_params('ħөŝŧ01', 'ħөŝŧ04').and_return(['ħөŝŧ01', 'ħөŝŧ02', 'ħөŝŧ03', 'ħөŝŧ04']) }
84   end
85
86   context 'with prefixed numbers as double byte character strings as bounds' do
87     it { is_expected.to run.with_params('ホスト01', 'ホスト04').and_return(['ホスト01', 'ホスト02', 'ホスト03', 'ホスト04']) }
88   end
89
90   context 'with dash-range syntax' do
91     it { is_expected.to run.with_params('4-1').and_return([]) }
92     it { is_expected.to run.with_params('1-1').and_return([1]) }
93     it { is_expected.to run.with_params('1-2').and_return([1, 2]) }
94     it { is_expected.to run.with_params('1-4').and_return([1, 2, 3, 4]) }
95   end
96
97   context 'with two-dot-range syntax' do
98     it { is_expected.to run.with_params('4..1').and_return([]) }
99     it { is_expected.to run.with_params('1..1').and_return([1]) }
100     it { is_expected.to run.with_params('1..2').and_return([1, 2]) }
101     it { is_expected.to run.with_params('1..4').and_return([1, 2, 3, 4]) }
102   end
103
104   context 'with three-dot-range syntax' do
105     it { is_expected.to run.with_params('4...1').and_return([]) }
106     it { is_expected.to run.with_params('1...1').and_return([]) }
107     it { is_expected.to run.with_params('1...2').and_return([1]) }
108     it { is_expected.to run.with_params('1...3').and_return([1, 2]) }
109     it { is_expected.to run.with_params('1...5').and_return([1, 2, 3, 4]) }
110   end
111
112   describe 'when passing mixed arguments as bounds' do
113     it {
114       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')
115       is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
116     }
117     it {
118       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')
119       is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
120     }
121     it {
122       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')
123       is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
124     }
125   end
126 end