Note that exim contains tracker-specific configuration
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / parsejson_spec.rb
1 require 'spec_helper'
2
3 describe 'parsejson' do
4   it 'exists' do
5     is_expected.not_to eq(nil)
6   end
7
8   it 'raises an error if called without any arguments' do
9     is_expected.to run.with_params
10                       .and_raise_error(%r{wrong number of arguments}i)
11   end
12
13   context 'with correct JSON data' do
14     it 'is able to parse JSON data with a Hash' do
15       is_expected.to run.with_params('{"a":"1","b":"2"}')
16                         .and_return('a' => '1', 'b' => '2')
17     end
18
19     it 'is able to parse JSON data with an Array' do
20       is_expected.to run.with_params('["a","b","c"]')
21                         .and_return(['a', 'b', 'c'])
22     end
23
24     it 'is able to parse empty JSON values' do
25       actual_array = ['[]', '{}']
26       expected = [[], {}]
27       actual_array.each_with_index do |actual, index|
28         is_expected.to run.with_params(actual).and_return(expected[index])
29       end
30     end
31
32     it 'is 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 'is 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 'does 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   end
47
48   context 'with incorrect JSON data' do
49     it 'raises an error with invalid JSON and no default' do
50       is_expected.to run.with_params('')
51                         .and_raise_error(PSON::ParserError)
52     end
53
54     it 'supports a structure for a default value' do
55       is_expected.to run.with_params('', 'a' => '1')
56                         .and_return('a' => '1')
57     end
58
59     ['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
60       it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
61         is_expected.to run.with_params(value, 'default_value')
62                           .and_return('default_value')
63       end
64     end
65   end
66 end