3 describe 'parseyaml' do
5 is_expected.not_to eq(nil)
8 it 'should raise an error if called without any arguments' do
9 is_expected.to run.with_params().
10 and_raise_error(/wrong number of arguments/i)
13 context 'with correct YAML data' do
14 it 'should be able to parse a YAML data with a String' do
15 is_expected.to run.with_params('--- just a string').
16 and_return('just a string')
17 is_expected.to run.with_params('just a string').
18 and_return('just a string')
21 it 'should be able to parse YAML data with a Hash' do
22 is_expected.to run.with_params("---\na: '1'\nb: '2'\n").
23 and_return({'a' => '1', 'b' => '2'})
26 it 'should be able to parse YAML data with an Array' do
27 is_expected.to run.with_params("---\n- a\n- b\n- c\n").
28 and_return(['a', 'b', 'c'])
31 it 'should be able to parse YAML data with a mixed structure' do
32 is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n").
33 and_return({'a' => '1', 'b' => 2, 'c' => {'d' => [:a, true, false]}})
36 it 'should be able to parse YAML data with a UTF8 and double byte characters' do
37 is_expected.to run.with_params("---\na: ×\nこれ: 記号\nです:\n ©:\n - Á\n - ß\n").
38 and_return({"a"=>"×", "これ"=>"記号", "です"=>{"©"=>["Á", "ß"]} })
41 it 'should not return the default value if the data was parsed correctly' do
42 is_expected.to run.with_params("---\na: '1'\n", 'default_value').
43 and_return({'a' => '1'})
48 context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do
49 it 'should raise an error with invalid YAML and no default' do
50 is_expected.to run.with_params('["one"').
51 and_raise_error(Psych::SyntaxError)
55 context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
56 it 'should raise an error with invalid YAML and no default' do
57 is_expected.to run.with_params('["one"').
58 and_raise_error(ArgumentError)
62 context 'with incorrect YAML data' do
63 it 'should support a structure for a default value' do
64 is_expected.to run.with_params('', {'a' => '1'}).
65 and_return({'a' => '1'})
68 [1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
69 it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
70 is_expected.to run.with_params(value, 'default_value').
71 and_return('default_value')
75 context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
76 ['---', '...', '*8', ''].each do |value|
77 it "should return the default value for an incorrect #{value.inspect} string parameter" do
78 is_expected.to run.with_params(value, 'default_value').
79 and_return('default_value')