From f3e79ddcd56a221c7799b35efde7e9803a5c7923 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Mon, 1 Jun 2015 12:21:59 +0100 Subject: Convert tests to use plain rspec-puppet Tests in the new style produces the following documentation output: abs should not eq nil should run abs() and raise an Puppet::ParseError should run abs(-34) and return 34 should run abs("-34") and return 34 should run abs(34) and return 34 should run abs("34") and return 34 --- spec/functions/parsejson_spec.rb | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'spec/functions/parsejson_spec.rb') diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb index 1dd41b9..436566e 100755 --- a/spec/functions/parsejson_spec.rb +++ b/spec/functions/parsejson_spec.rb @@ -1,22 +1,9 @@ -#! /usr/bin/env ruby -S rspec require 'spec_helper' -describe "the parsejson function" do - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } - - it "should exist" do - expect(Puppet::Parser::Functions.function("parsejson")).to eq("function_parsejson") - end - - it "should raise a ParseError if there is less than 1 arguments" do - expect { scope.function_parsejson([]) }.to( raise_error(Puppet::ParseError)) - end - - it "should convert JSON to a data structure" do - json = <<-EOS -["aaa","bbb","ccc"] -EOS - result = scope.function_parsejson([json]) - expect(result).to(eq(['aaa','bbb','ccc'])) - end +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']) } end -- cgit v1.2.3 From eb948c4a0dc36790c5444fc236b0154c3d716c58 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin Date: Mon, 24 Aug 2015 22:00:18 +0300 Subject: [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 --- spec/functions/parsejson_spec.rb | 65 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) (limited to 'spec/functions/parsejson_spec.rb') 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 -- cgit v1.2.3 From 799c38e14e1583e676e2b25a9c1782fd40e29fff Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Mon, 21 Sep 2015 10:56:08 -0700 Subject: Fix backwards compatibility from #511 Maintain the old behavior in the case where the optional second parameter isn't passed. Also, adding arity is backwards incompatible since stdlib still supports 2.7, so remove that. --- spec/functions/parsejson_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/functions/parsejson_spec.rb') diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb index 5bea8af..a01f1f6 100755 --- a/spec/functions/parsejson_spec.rb +++ b/spec/functions/parsejson_spec.rb @@ -41,10 +41,10 @@ describe 'parsejson' do end - context 'with incorrect YAML data' do - it 'should return "nil" if a default value should be returned but is not provided' do + context 'with incorrect JSON data' do + it 'should raise an error with invalid JSON and no default' do is_expected.to run.with_params(''). - and_return(nil) + and_raise_error(PSON::ParserError) end it 'should support a structure for a default value' do -- cgit v1.2.3