values_at_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. require 'spec_helper'
  2. describe 'values_at' do
  3. describe 'signature validation' do
  4. it { is_expected.not_to eq(nil) }
  5. it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  6. it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  7. it {
  8. pending('Current implementation ignores parameters after the first two.')
  9. is_expected.to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i)
  10. }
  11. it { is_expected.to run.with_params('', 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
  12. it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
  13. it { is_expected.to run.with_params(true, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
  14. it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) }
  15. it { is_expected.to run.with_params([0, 1, 2], 'two').and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
  16. it { is_expected.to run.with_params([0, 1, 2], []).and_raise_error(Puppet::ParseError, %r{provide at least one positive index}) }
  17. it { is_expected.to run.with_params([0, 1, 2], '-1-1').and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
  18. 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}) }
  19. end
  20. context 'when requesting a single item' do
  21. it { is_expected.to run.with_params([0, 1, 2], -1).and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
  22. it { is_expected.to run.with_params([0, 1, 2], 0).and_return([0]) }
  23. it { is_expected.to run.with_params([0, 1, 2], 1).and_return([1]) }
  24. it { is_expected.to run.with_params([0, 1, 2], [1]).and_return([1]) }
  25. it { is_expected.to run.with_params([0, 1, 2], '1').and_return([1]) }
  26. it { is_expected.to run.with_params([0, 1, 2], '1-1').and_return([1]) }
  27. it { is_expected.to run.with_params([0, 1, 2], 2).and_return([2]) }
  28. it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError, %r{index exceeds array size}) }
  29. end
  30. context 'when requesting a single item using UTF8 and double byte characters' do
  31. it { is_expected.to run.with_params(%w[ẩ β с ď], 0).and_return(['ẩ']) }
  32. it { is_expected.to run.with_params(%w[文 字 の 値], 2).and_return(['の']) }
  33. end
  34. context 'when requesting multiple items' do
  35. it { is_expected.to run.with_params([0, 1, 2], [1, -1]).and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) }
  36. it { is_expected.to run.with_params([0, 1, 2], [0, 2]).and_return([0, 2]) }
  37. it { is_expected.to run.with_params([0, 1, 2], ['0-2', 1, 2]).and_return([0, 1, 2, 1, 2]) }
  38. it { is_expected.to run.with_params([0, 1, 2], [3, 2]).and_raise_error(Puppet::ParseError, %r{index exceeds array size}) }
  39. describe 'different range syntaxes' do
  40. it { is_expected.to run.with_params([0, 1, 2], '0-2').and_return([0, 1, 2]) }
  41. it { is_expected.to run.with_params([0, 1, 2], '0..2').and_return([0, 1, 2]) }
  42. it { is_expected.to run.with_params([0, 1, 2], '0...2').and_return([0, 1]) }
  43. it {
  44. pending('fix this bounds check')
  45. is_expected.to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2])
  46. }
  47. end
  48. end
  49. end