parseyaml_spec.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. require 'spec_helper'
  2. describe 'parseyaml' do
  3. it 'exists' do
  4. is_expected.not_to eq(nil)
  5. end
  6. it 'raises an error if called without any arguments' do
  7. is_expected.to run.with_params
  8. .and_raise_error(%r{wrong number of arguments}i)
  9. end
  10. context 'with correct YAML data' do
  11. it 'is able to parse a YAML data with a String' do
  12. actual_array = ['--- just a string', 'just a string']
  13. actual_array.each do |actual|
  14. is_expected.to run.with_params(actual).and_return('just a string')
  15. end
  16. end
  17. it 'is able to parse YAML data with a Hash' do
  18. is_expected.to run.with_params("---\na: '1'\nb: '2'\n")
  19. .and_return('a' => '1', 'b' => '2')
  20. end
  21. it 'is able to parse YAML data with an Array' do
  22. is_expected.to run.with_params("---\n- a\n- b\n- c\n")
  23. .and_return(%w[a b c])
  24. end
  25. it 'is able to parse YAML data with a mixed structure' do
  26. is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n")
  27. .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [:a, true, false] })
  28. end
  29. it 'is able to parse YAML data with a UTF8 and double byte characters' do
  30. is_expected.to run.with_params("---\na: ×\nこれ: 記号\nです:\n ©:\n - Á\n - ß\n")
  31. .and_return('a' => '×', 'これ' => '記号', 'です' => { '©' => %w[Á ß] })
  32. end
  33. it 'does not return the default value if the data was parsed correctly' do
  34. is_expected.to run.with_params("---\na: '1'\n", 'default_value')
  35. .and_return('a' => '1')
  36. end
  37. end
  38. context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do
  39. it 'raises an error with invalid YAML and no default' do
  40. is_expected.to run.with_params('["one"')
  41. .and_raise_error(Psych::SyntaxError)
  42. end
  43. end
  44. context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
  45. it 'raises an error with invalid YAML and no default' do
  46. is_expected.to run.with_params('["one"')
  47. .and_raise_error(ArgumentError)
  48. end
  49. end
  50. context 'with incorrect YAML data' do
  51. it 'supports a structure for a default value' do
  52. is_expected.to run.with_params('', 'a' => '1')
  53. .and_return('a' => '1')
  54. end
  55. [1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
  56. it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
  57. is_expected.to run.with_params(value, 'default_value')
  58. .and_return('default_value')
  59. end
  60. end
  61. context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
  62. ['---', '...', '*8', ''].each do |value|
  63. it "should return the default value for an incorrect #{value.inspect} string parameter" do
  64. is_expected.to run.with_params(value, 'default_value')
  65. .and_return('default_value')
  66. end
  67. end
  68. end
  69. end
  70. end