3 describe 'parsejson' do
5 is_expected.not_to eq(nil)
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)
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')
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'])
24 it 'is able to parse empty JSON values' do
25 actual_array = ['[]', '{}']
27 actual_array.each_with_index do |actual, index|
28 is_expected.to run.with_params(actual).and_return(expected[index])
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] })
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('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] })
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')
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)
54 it 'supports a structure for a default value' do
55 is_expected.to run.with_params('', 'a' => '1')
56 .and_return('a' => '1')
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')