loadjson_spec.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. require 'spec_helper_acceptance'
  2. tmpdir = default.tmpdir('stdlib')
  3. describe 'loadjson function' do
  4. describe 'success' do
  5. shell("echo '{\"aaa\":1,\"bbb\":2,\"ccc\":3,\"ddd\":4}' > #{tmpdir}/test1json.json")
  6. pp1 = <<-DOC
  7. $o = loadjson('#{tmpdir}/test1json.json')
  8. notice(inline_template('loadjson[aaa] is <%= @o["aaa"].inspect %>'))
  9. notice(inline_template('loadjson[bbb] is <%= @o["bbb"].inspect %>'))
  10. notice(inline_template('loadjson[ccc] is <%= @o["ccc"].inspect %>'))
  11. notice(inline_template('loadjson[ddd] is <%= @o["ddd"].inspect %>'))
  12. DOC
  13. regex_array = [%r{loadjson\[aaa\] is 1}, %r{loadjson\[bbb\] is 2}, %r{loadjson\[ccc\] is 3}, %r{loadjson\[ddd\] is 4}]
  14. it 'loadjsons array of values' do
  15. apply_manifest(pp1, :catch_failures => true) do |r|
  16. regex_array.each do |i|
  17. expect(r.stdout).to match(i)
  18. end
  19. end
  20. end
  21. pp2 = <<-DOC
  22. $o = loadjson('#{tmpdir}/no-file.json', {'default' => 'value'})
  23. notice(inline_template('loadjson[default] is <%= @o["default"].inspect %>'))
  24. DOC
  25. it 'returns the default value if there is no file to load' do
  26. apply_manifest(pp2, :catch_failures => true) do |r|
  27. expect(r.stdout).to match(%r{loadjson\[default\] is "value"})
  28. end
  29. end
  30. shell("echo '!' > #{tmpdir}/test2json.json")
  31. pp3 = <<-DOC
  32. $o = loadjson('#{tmpdir}/test2json.json', {'default' => 'value'})
  33. notice(inline_template('loadjson[default] is <%= @o["default"].inspect %>'))
  34. DOC
  35. it 'returns the default value if the file was parsed with an error' do
  36. apply_manifest(pp3, :catch_failures => true) do |r|
  37. expect(r.stdout).to match(%r{loadjson\[default\] is "value"})
  38. end
  39. end
  40. end
  41. describe 'failure' do
  42. it 'fails with no arguments'
  43. end
  44. end