diff options
author | Bryan Jen <bryan.jen@gmail.com> | 2015-09-14 10:40:35 -0700 |
---|---|---|
committer | Bryan Jen <bryan.jen@gmail.com> | 2015-09-14 10:40:35 -0700 |
commit | c0df8192ee5c2f606ae99c2a3115002962901dea (patch) | |
tree | e3f076d9e4174c5ef08cb22fbd8c57a26a8c23fe /spec/functions/parseyaml_spec.rb | |
parent | 2a7a93ffb49b2ae63d4fd09982fd8c382c1f4b5a (diff) | |
parent | eb948c4a0dc36790c5444fc236b0154c3d716c58 (diff) |
Merge pull request #511 from dmitryilyin/parseyaml_default
[MODULES-2462] Improve parseyaml function
Diffstat (limited to 'spec/functions/parseyaml_spec.rb')
-rwxr-xr-x | spec/functions/parseyaml_spec.rb | 74 |
1 files changed, 66 insertions, 8 deletions
diff --git a/spec/functions/parseyaml_spec.rb b/spec/functions/parseyaml_spec.rb index fb635f8..492a1c9 100755 --- a/spec/functions/parseyaml_spec.rb +++ b/spec/functions/parseyaml_spec.rb @@ -1,14 +1,72 @@ require 'spec_helper' describe 'parseyaml' do - it { is_expected.not_to eq(nil) } - it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } - it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } - it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) } - context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do - it { is_expected.to run.with_params('["one"').and_raise_error(Psych::SyntaxError) } + it 'should exist' do + is_expected.not_to eq(nil) end - context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do - it { is_expected.to run.with_params('["one"').and_raise_error(ArgumentError) } + + it 'should raise an error if called without any arguments' do + is_expected.to run.with_params(). + and_raise_error(/wrong number of arguments/i) + end + + context 'with correct YAML data' do + it 'should be able to parse a YAML data with a String' do + is_expected.to run.with_params('--- just a string'). + and_return('just a string') + is_expected.to run.with_params('just a string'). + and_return('just a string') + end + + it 'should be able to parse a YAML data with a Hash' do + is_expected.to run.with_params("---\na: '1'\nb: '2'\n"). + and_return({'a' => '1', 'b' => '2'}) + end + + it 'should be able to parse a YAML data with an Array' do + is_expected.to run.with_params("---\n- a\n- b\n- c\n"). + and_return(['a', 'b', 'c']) + end + + it 'should be able to parse a YAML data with a mixed structure' do + is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n"). + and_return({'a' => '1', 'b' => 2, 'c' => {'d' => [:a, true, false]}}) + end + + it 'should not return the default value if the data was parsed correctly' do + is_expected.to run.with_params("---\na: '1'\n", 'default_value'). + and_return({'a' => '1'}) + end + end + + context 'with incorrect YAML data' do + it 'should return "nil" if a default value should be returned but is not provided' do + is_expected.to run.with_params(''). + and_return(nil) + end + + it 'should support a structure for a default value' do + is_expected.to run.with_params('', {'a' => '1'}). + and_return({'a' => '1'}) + end + + [1, 1.2, nil, true, false, [], {}, :yaml].each do |value| + it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do + is_expected.to run.with_params(value, 'default_value'). + and_return('default_value') + end + end + + context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do + ['---', '...', '*8', ''].each do |value| + it "should return the default value for an incorrect #{value.inspect} string parameter" do + is_expected.to run.with_params(value, 'default_value'). + and_return('default_value') + end + end + end + + end + end |