1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
require 'shared_behaviours/iterative_functions'
describe 'the step method' do
include PuppetSpec::Compiler
it 'raises an error when given a type that cannot be iterated' do
expect do
compile_to_catalog(<<-MANIFEST)
3.14.step(1) |$v| { }
MANIFEST
end.to raise_error(Puppet::Error, /expects an Iterable value, got Float/)
end
it 'raises an error when called with more than two arguments and a block' do
expect do
compile_to_catalog(<<-MANIFEST)
[1].step(1,2) |$v| { }
MANIFEST
end.to raise_error(Puppet::Error, /expects 2 arguments, got 3/)
end
it 'raises an error when called with more than two arguments and without a block' do
expect do
compile_to_catalog(<<-MANIFEST)
[1].step(1,2)
MANIFEST
end.to raise_error(Puppet::Error, /expects 2 arguments, got 3/)
end
it 'raises an error when called with a block with too many required parameters' do
expect do
compile_to_catalog(<<-MANIFEST)
[1].step(1) |$v1, $v2| { }
MANIFEST
end.to raise_error(Puppet::Error, /block expects 1 argument, got 2/)
end
it 'raises an error when called with a block with too few parameters' do
expect do
compile_to_catalog(<<-MANIFEST)
[1].step(1) | | { }
MANIFEST
end.to raise_error(Puppet::Error, /block expects 1 argument, got none/)
end
it 'raises an error when called with step == 0' do
expect do
compile_to_catalog(<<-MANIFEST)
[1].step(0) |$x| { }
MANIFEST
end.to raise_error(Puppet::Error, /'step' expects an Integer\[1\] value, got Integer\[0, 0\]/)
end
it 'raises an error when step is not an integer' do
expect do
compile_to_catalog(<<-MANIFEST)
[1].step('three') |$x| { }
MANIFEST
end.to raise_error(Puppet::Error, /'step' expects an Integer value, got String/)
end
it 'does not raise an error when called with a block with too many but optional arguments' do
expect do
compile_to_catalog(<<-MANIFEST)
[1].step(1) |$v1, $v2=extra| { }
MANIFEST
end.to_not raise_error
end
it 'returns Undef when called with a block' do
expect do
compile_to_catalog(<<-MANIFEST)
assert_type(Undef, [1].step(2) |$x| { $x })
MANIFEST
end.not_to raise_error
end
it 'returns an Iterable when called without a block' do
expect do
compile_to_catalog(<<-MANIFEST)
assert_type(Iterable, [1].step(2))
MANIFEST
end.not_to raise_error
end
it 'should produce "times" interval of integer according to step' do
expect(eval_and_collect_notices('10.step(2) |$x| { notice($x) }')).to eq(['0', '2', '4', '6', '8'])
end
it 'should produce interval of Integer[5,20] according to step' do
expect(eval_and_collect_notices('Integer[5,20].step(4) |$x| { notice($x) }')).to eq(['5', '9', '13', '17'])
end
it 'should produce the elements of [a,b,c,d,e,f,g,h] according to step' do
expect(eval_and_collect_notices('[a,b,c,d,e,f,g,h].step(2) |$x| { notice($x) }')).to eq(%w(a c e g))
end
it 'should produce the elements {a=>1,b=>2,c=>3,d=>4,e=>5,f=>6,g=>7,h=>8} according to step' do
expect(eval_and_collect_notices('{a=>1,b=>2,c=>3,d=>4,e=>5,f=>6,g=>7,h=>8}.step(2) |$t| { notice($t[1]) }')).to eq(%w(1 3 5 7))
end
it 'should produce the choices of Enum[a,b,c,d,e,f,g,h] according to step' do
expect(eval_and_collect_notices('Enum[a,b,c,d,e,f,g,h].step(2) |$x| { notice($x) }')).to eq(%w(a c e g))
end
it 'should produce descending interval of Integer[5,20] when chained after a reverse_each' do
expect(eval_and_collect_notices('Integer[5,20].reverse_each.step(4) |$x| { notice($x) }')).to eq(['20', '16', '12', '8'])
end
end
|