3 describe 'parsejson' do
5 is_expected.not_to eq(nil)
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)
13 context 'with correct JSON data' do
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'})
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'])
25 it 'should be able to parse empty JSON values' do
26 is_expected.to run.with_params('[]').
28 is_expected.to run.with_params('{}').
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] } })
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({'×' =>'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] } })
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'})
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)
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'})
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')