summaryrefslogtreecommitdiff
path: root/spec/functions/parsejson_spec.rb
diff options
context:
space:
mode:
authorvarac <varacanero@zeromail.org>2017-01-13 12:41:58 +0100
committervarac <varacanero@zeromail.org>2017-01-13 12:41:58 +0100
commit066c08f8362d53f0f30897cb8710d11260c726ea (patch)
treea6369eecd88bb731fe413d0bbc8af73d74d1f447 /spec/functions/parsejson_spec.rb
parent71123634744b9fe2ec7d6a3e38e9789fd84801e3 (diff)
parentb65dd1f45d10e10e45455358aeabb29167990e2c (diff)
Merge remote-tracking branch 'origin/master' into leap_master
Diffstat (limited to 'spec/functions/parsejson_spec.rb')
-rwxr-xr-xspec/functions/parsejson_spec.rb68
1 files changed, 55 insertions, 13 deletions
diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb
index 1dd41b9..a01f1f6 100755
--- a/spec/functions/parsejson_spec.rb
+++ b/spec/functions/parsejson_spec.rb
@@ -1,22 +1,64 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the parsejson function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'parsejson' do
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
- it "should exist" do
- expect(Puppet::Parser::Functions.function("parsejson")).to eq("function_parsejson")
+ 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
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_parsejson([]) }.to( raise_error(Puppet::ParseError))
+ 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
- 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']))
+ 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_raise_error(PSON::ParserError)
+ 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