summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorDmitry Ilyin <dilyin@mirantis.com>2015-08-24 22:00:18 +0300
committerDmitry Ilyin <dilyin@mirantis.com>2015-08-25 21:41:03 +0300
commiteb948c4a0dc36790c5444fc236b0154c3d716c58 (patch)
tree02357ac0aaebe45825bf6fffeb8f0cf9e883cab4 /spec
parentf820bb156038f638d8e488286d0c2b92c5636925 (diff)
downloadpuppet-stdlib-eb948c4a0dc36790c5444fc236b0154c3d716c58.tar.gz
puppet-stdlib-eb948c4a0dc36790c5444fc236b0154c3d716c58.tar.bz2
[MODULES-2462] Improve parseyaml function
* Add default value support Second argument will be returned if yaml cannot be parsed instead of false value * Update tests
Diffstat (limited to 'spec')
-rwxr-xr-xspec/acceptance/parsejson_spec.rb17
-rwxr-xr-xspec/acceptance/parseyaml_spec.rb19
-rwxr-xr-xspec/functions/parsejson_spec.rb65
-rwxr-xr-xspec/functions/parseyaml_spec.rb74
4 files changed, 153 insertions, 22 deletions
diff --git a/spec/acceptance/parsejson_spec.rb b/spec/acceptance/parsejson_spec.rb
index 5097810..d0feabd 100755
--- a/spec/acceptance/parsejson_spec.rb
+++ b/spec/acceptance/parsejson_spec.rb
@@ -16,19 +16,28 @@ describe 'parsejson function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('o
end
end
end
+
describe 'failure' do
it 'raises error on incorrect json' do
pp = <<-EOS
$a = '{"hunter": "washere", "tests": "passing",}'
- $ao = parsejson($a)
+ $ao = parsejson($a, {'tests' => 'using the default value'})
notice(inline_template('a is <%= @ao.inspect %>'))
EOS
- apply_manifest(pp, :expect_failures => true) do |r|
- expect(r.stderr).to match(/expected next name/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/tests are "using the default value"/)
end
end
- it 'raises error on incorrect number of arguments'
+ it 'raises error on incorrect number of arguments' do
+ pp = <<-EOS
+ $o = parsejson()
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stderr).to match(/wrong number of arguments/i)
+ end
+ end
end
end
diff --git a/spec/acceptance/parseyaml_spec.rb b/spec/acceptance/parseyaml_spec.rb
index 5819837..7946de0 100755
--- a/spec/acceptance/parseyaml_spec.rb
+++ b/spec/acceptance/parseyaml_spec.rb
@@ -16,20 +16,29 @@ describe 'parseyaml function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('o
end
end
end
+
describe 'failure' do
- it 'raises error on incorrect yaml' do
+ it 'returns the default value on incorrect yaml' do
pp = <<-EOS
$a = "---\nhunter: washere\ntests: passing\n:"
- $o = parseyaml($a)
+ $o = parseyaml($a, {'tests' => 'using the default value'})
$tests = $o['tests']
notice(inline_template('tests are <%= @tests.inspect %>'))
EOS
- apply_manifest(pp, :expect_failures => true) do |r|
- expect(r.stderr).to match(/(syntax error|did not find expected key)/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/tests are "using the default value"/)
end
end
- it 'raises error on incorrect number of arguments'
+ it 'raises error on incorrect number of arguments' do
+ pp = <<-EOS
+ $o = parseyaml()
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stderr).to match(/wrong number of arguments/i)
+ end
+ end
end
end
diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb
index 436566e..5bea8af 100755
--- a/spec/functions/parsejson_spec.rb
+++ b/spec/functions/parsejson_spec.rb
@@ -1,9 +1,64 @@
require 'spec_helper'
describe 'parsejson' 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"').and_raise_error(PSON::ParserError) }
- it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) }
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
+
+ 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 JSON data' do
+
+ it 'should be able to parse a JSON data with a Hash' do
+ is_expected.to run.with_params('{"a":"1","b":"2"}').
+ and_return({'a'=>'1', 'b'=>'2'})
+ end
+
+ it 'should be able to parse a JSON data with an Array' do
+ is_expected.to run.with_params('["a","b","c"]').
+ and_return(['a', 'b', 'c'])
+ end
+
+ it 'should be able to parse empty JSON values' do
+ is_expected.to run.with_params('[]').
+ and_return([])
+ is_expected.to run.with_params('{}').
+ and_return({})
+ end
+
+ it 'should be able to parse a JSON data with a mixed structure' do
+ is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}').
+ and_return({'a' =>'1', 'b' => 2, 'c' => { 'd' => [true, false] } })
+ end
+
+ it 'should not return the default value if the data was parsed correctly' do
+ is_expected.to run.with_params('{"a":"1"}', '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
+
+ end
+
end
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