dig44_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'spec_helper'
  2. describe 'dig44' do
  3. let(:data) do
  4. {
  5. 'a' => {
  6. 'g' => '2',
  7. 'e' => [
  8. 'f0',
  9. 'f1',
  10. {
  11. 'x' => {
  12. 'y' => 'z',
  13. },
  14. },
  15. 'f3',
  16. ],
  17. },
  18. 'b' => true,
  19. 'c' => false,
  20. 'd' => '1',
  21. 'e' => :undef,
  22. 'f' => nil,
  23. }
  24. end
  25. let(:utf8_data) do
  26. {
  27. 'ẵ' => {
  28. 'в' => [
  29. '©',
  30. 'ĝ',
  31. 'に',
  32. ],
  33. },
  34. }
  35. end
  36. context 'with single values' do
  37. it 'exists' do
  38. is_expected.not_to be_nil
  39. end
  40. it 'requires two arguments' do
  41. is_expected.to run.with_params.and_raise_error(ArgumentError)
  42. end
  43. it 'fails if the data is not a structure' do
  44. is_expected.to run.with_params('test', []).and_raise_error(Puppet::Error)
  45. end
  46. it 'fails if the path is not an array' do
  47. is_expected.to run.with_params({}, '').and_raise_error(Puppet::Error)
  48. end
  49. it 'returns the value if the value is string' do
  50. is_expected.to run.with_params(data, ['d'], 'default').and_return('1')
  51. end
  52. it 'returns true if the value is true' do
  53. is_expected.to run.with_params(data, ['b'], 'default').and_return(true)
  54. end
  55. it 'returns false if the value is false' do
  56. is_expected.to run.with_params(data, ['c'], 'default').and_return(false)
  57. end
  58. it 'returns the default if the value is nil' do
  59. is_expected.to run.with_params(data, ['f'], 'default').and_return('default')
  60. end
  61. it 'returns the default if the value is :undef (same as nil)' do
  62. is_expected.to run.with_params(data, ['e'], 'default').and_return('default')
  63. end
  64. it 'returns the default if the path is not found' do
  65. is_expected.to run.with_params(data, ['missing'], 'default').and_return('default')
  66. end
  67. end
  68. context 'with structured values' do
  69. it 'is able to extract a deeply nested hash value' do
  70. is_expected.to run.with_params(data, %w[a g], 'default').and_return('2')
  71. end
  72. it 'returns the default value if the path is too long' do
  73. is_expected.to run.with_params(data, %w[a g c d], 'default').and_return('default')
  74. end
  75. it 'supports an array index (number) in the path' do
  76. is_expected.to run.with_params(data, ['a', 'e', 1], 'default').and_return('f1')
  77. end
  78. it 'supports an array index (string) in the path' do
  79. is_expected.to run.with_params(data, %w[a e 1], 'default').and_return('f1')
  80. end
  81. it 'returns the default value if an array index is not a number' do
  82. is_expected.to run.with_params(data, %w[a b c], 'default').and_return('default')
  83. end
  84. it 'returns the default value if and index is out of array length' do
  85. is_expected.to run.with_params(data, %w[a e 5], 'default').and_return('default')
  86. end
  87. it 'is able to path though both arrays and hashes' do
  88. is_expected.to run.with_params(data, %w[a e 2 x y], 'default').and_return('z')
  89. end
  90. it 'returns "nil" if value is not found and no default value is provided' do
  91. is_expected.to run.with_params(data, %w[a 1]).and_return(nil)
  92. end
  93. end
  94. context 'with internationalization (i18N) values' do
  95. it 'is able to return a unicode character' do
  96. is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 0]).and_return('©')
  97. end
  98. it 'is able to return a utf8 character' do
  99. is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 1]).and_return('ĝ')
  100. end
  101. it 'is able to return a double byte character' do
  102. is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 2]).and_return('に')
  103. end
  104. end
  105. end