Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / parsejson_spec.rb
1 require 'spec_helper'
2
3 describe 'parsejson' do
4   it 'should exist' do
5     is_expected.not_to eq(nil)
6   end
7
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)
11   end
12
13   context 'with correct JSON data' do
14
15     it 'should be able to parse JSON data with a Hash' do
16       is_expected.to run.with_params('{"a":"1","b":"2"}').
17                          and_return({'a'=>'1', 'b'=>'2'})
18     end
19
20     it 'should be able to parse JSON data with an Array' do
21       is_expected.to run.with_params('["a","b","c"]').
22                          and_return(['a', 'b', 'c'])
23     end
24
25     it 'should be able to parse empty JSON values' do
26       is_expected.to run.with_params('[]').
27                          and_return([])
28       is_expected.to run.with_params('{}').
29                          and_return({})
30     end
31
32     it 'should be able to parse JSON data with a mixed structure' do
33       is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}').
34                          and_return({'a' =>'1', 'b' => 2, 'c' => { 'd' => [true, false] } })
35     end
36
37     it 'should be able to parse JSON data with a UTF8 and double byte characters' do
38       is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}').
39           and_return({'×' =>'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] } })
40     end
41
42     it 'should not return the default value if the data was parsed correctly' do
43       is_expected.to run.with_params('{"a":"1"}', 'default_value').
44                          and_return({'a' => '1'})
45     end
46
47   end
48
49   context 'with incorrect JSON data' do
50     it 'should raise an error with invalid JSON and no default' do
51       is_expected.to run.with_params('').
52                          and_raise_error(PSON::ParserError)
53     end
54
55     it 'should support a structure for a default value' do
56       is_expected.to run.with_params('', {'a' => '1'}).
57                          and_return({'a' => '1'})
58     end
59
60     ['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
61       it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
62         is_expected.to run.with_params(value, 'default_value').
63                            and_return('default_value')
64       end
65     end
66
67   end
68
69 end