File: values_at_spec.rb

package info (click to toggle)
puppet-module-puppetlabs-stdlib 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,804 kB
  • sloc: ruby: 16,417; sh: 46; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 3,508 bytes parent folder | download
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
require 'spec_helper'

describe 'values_at' do
  describe 'signature validation' do
    it { is_expected.not_to eq(nil) }
    it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
    it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
    it {
      pending('Current implementation ignores parameters after the first two.')
      is_expected.to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i)
    }
    it { is_expected.to run.with_params('', 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
    it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
    it { is_expected.to run.with_params(true, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
    it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
    it { is_expected.to run.with_params([0, 1, 2], 'two').and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
    it { is_expected.to run.with_params([0, 1, 2], []).and_raise_error(Puppet::ParseError, %r{provide at least one positive index}) }
    it { is_expected.to run.with_params([0, 1, 2], '-1-1').and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
    it { is_expected.to run.with_params([0, 1, 2], '2-1').and_raise_error(Puppet::ParseError, %r{Stop index in given indices range is smaller than the start index}) }
  end

  context 'when requesting a single item' do
    it { is_expected.to run.with_params([0, 1, 2], -1).and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
    it { is_expected.to run.with_params([0, 1, 2], 0).and_return([0]) }
    it { is_expected.to run.with_params([0, 1, 2], 1).and_return([1]) }
    it { is_expected.to run.with_params([0, 1, 2], [1]).and_return([1]) }
    it { is_expected.to run.with_params([0, 1, 2], '1').and_return([1]) }
    it { is_expected.to run.with_params([0, 1, 2], '1-1').and_return([1]) }
    it { is_expected.to run.with_params([0, 1, 2], 2).and_return([2]) }
    it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError, %r{index exceeds array size}) }
  end

  context 'when requesting a single item using UTF8 and double byte characters' do
    it { is_expected.to run.with_params(['ẩ', 'β', 'с', 'ď'], 0).and_return(['ẩ']) }
    it { is_expected.to run.with_params(['文', '字', 'の', '値'], 2).and_return(['の']) }
  end

  context 'when requesting multiple items' do
    it { is_expected.to run.with_params([0, 1, 2], [1, -1]).and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
    it { is_expected.to run.with_params([0, 1, 2], [0, 2]).and_return([0, 2]) }
    it { is_expected.to run.with_params([0, 1, 2], ['0-2', 1, 2]).and_return([0, 1, 2, 1, 2]) }
    it { is_expected.to run.with_params([0, 1, 2], [3, 2]).and_raise_error(Puppet::ParseError, %r{index exceeds array size}) }

    describe 'different range syntaxes' do
      it { is_expected.to run.with_params([0, 1, 2], '0-2').and_return([0, 1, 2]) }
      it { is_expected.to run.with_params([0, 1, 2], '0..2').and_return([0, 1, 2]) }
      it { is_expected.to run.with_params([0, 1, 2], '0...2').and_return([0, 1]) }
      it {
        pending('fix this bounds check')
        is_expected.to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2])
      }
    end
  end
end