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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
require 'shared_behaviours/iterative_functions'
describe 'the index function' do
include PuppetSpec::Compiler
context "should be callable on Array with" do
it 'a lambda to compute match' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$n = $a.index |$v| { $v == 2 }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "1")['ensure']).to eq('present')
end
it 'a lambda taking two arguments to compute match' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [6,6,6]
$n = $a.index |$i, $v| { $i == 2 }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "2")['ensure']).to eq('present')
end
it 'a given value' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$n = $a.index(3)
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "2")['ensure']).to eq('present')
end
end
context "should be callable on Hash with" do
it 'a lambda to compute match' do
catalog = compile_to_catalog(<<-MANIFEST)
$h = {1 => 10, 2 => 20, 3 => 30 }
$n = $h.index |$v| { $v == 20 }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "2")['ensure']).to eq('present')
end
it 'a lambda taking two arguments to compute match' do
catalog = compile_to_catalog(<<-MANIFEST)
$h = {1 => 10, 2 => 20, 3 => 30 }
$n = $h.index |$k, $v| { $k == 3 }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "3")['ensure']).to eq('present')
end
it 'a given value' do
catalog = compile_to_catalog(<<-MANIFEST)
$h = {1 => 10, 2 => 20, 3 => 30 }
$n = $h.index(20)
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "2")['ensure']).to eq('present')
end
end
context "should be callable on String with" do
it 'a lambda to compute match' do
catalog = compile_to_catalog(<<-MANIFEST)
$s = "foobarfuu"
$n = $s.index |$v| { $v == 'o' }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "1")['ensure']).to eq('present')
end
it 'a lambda taking two arguments to compute match' do
catalog = compile_to_catalog(<<-MANIFEST)
$s = "foobarfuu"
$n = $s.index |$i, $v| { $i == 2 }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "2")['ensure']).to eq('present')
end
it 'a given value returns index of first found substring given as a string' do
catalog = compile_to_catalog(<<-MANIFEST)
$s = "foobarfuu"
$n = $s.index('fu')
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "6")['ensure']).to eq('present')
end
it 'a given value returns index of first found substring given as a regexp' do
catalog = compile_to_catalog(<<-MANIFEST)
$s = "foobarfuub"
$n = $s.index(/f(oo|uu)b/)
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "0")['ensure']).to eq('present')
end
end
context "should be callable on an iterable" do
it 'for example a reverse_each' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$n = $a.reverse_each.index |$v| { $v == 1 }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "2")['ensure']).to eq('present')
end
end
context 'stops iteration when result is known' do
it 'true when boolean true is found' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$n = $a.index |$i, $v| { if $i == 0 { true } else { fail("unwanted") } }
file { "$n": ensure => present }
MANIFEST
expect(catalog.resource(:file, "0")['ensure']).to eq('present')
end
end
context 'returns undef when value is not found' do
it 'when using array and a lambda' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$n = $a.index |$v| { $v == 'blue' }
file { "test": ensure => if $n =~ Undef { present } else {absent} }
MANIFEST
expect(catalog.resource(:file, "test")['ensure']).to eq('present')
end
it 'when using array and a value' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$n = $a.index('blue')
file { "test": ensure => if $n =~ Undef { present } else {absent} }
MANIFEST
expect(catalog.resource(:file, "test")['ensure']).to eq('present')
end
it 'when using a hash and a lambda' do
catalog = compile_to_catalog(<<-MANIFEST)
$h = {1 => 10, 2 => 20, 3 => 30}
$n = $h.index |$v| { $v == 'blue' }
file { "test": ensure => if $n =~ Undef { present } else {absent} }
MANIFEST
expect(catalog.resource(:file, "test")['ensure']).to eq('present')
end
it 'when using a hash and a value' do
catalog = compile_to_catalog(<<-MANIFEST)
$h = {1 => 10, 2 => 20, 3 => 30}
$n = $h.index('blue')
file { "test": ensure => if $n =~ Undef { present } else {absent} }
MANIFEST
expect(catalog.resource(:file, "test")['ensure']).to eq('present')
end
it 'when using a String and a lambda' do
catalog = compile_to_catalog(<<-MANIFEST)
$s = "foobarfuub"
$n = $s.index() |$v| { false }
file { "test": ensure => if $n =~ Undef { present } else {absent} }
MANIFEST
expect(catalog.resource(:file, "test")['ensure']).to eq('present')
end
it 'when using a String and a value' do
catalog = compile_to_catalog(<<-MANIFEST)
$s = "foobarfuub"
$n = $s.index('banana')
file { "test": ensure => if $n =~ Undef { present } else {absent} }
MANIFEST
expect(catalog.resource(:file, "test")['ensure']).to eq('present')
end
end
end
|