[30960] | 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 a 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 a 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 a 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 not return the default value if the data was parsed correctly' do
|
---|
| 38 | is_expected.to run.with_params('{"a":"1"}', 'default_value').
|
---|
| 39 | and_return({'a' => '1'})
|
---|
| 40 | end
|
---|
| 41 |
|
---|
| 42 | end
|
---|
| 43 |
|
---|
| 44 | context 'with incorrect JSON data' do
|
---|
| 45 | it 'should raise an error with invalid JSON and no default' do
|
---|
| 46 | is_expected.to run.with_params('').
|
---|
| 47 | and_raise_error(PSON::ParserError)
|
---|
| 48 | end
|
---|
| 49 |
|
---|
| 50 | it 'should support a structure for a default value' do
|
---|
| 51 | is_expected.to run.with_params('', {'a' => '1'}).
|
---|
| 52 | and_return({'a' => '1'})
|
---|
| 53 | end
|
---|
| 54 |
|
---|
| 55 | ['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
|
---|
| 56 | it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
|
---|
| 57 | is_expected.to run.with_params(value, 'default_value').
|
---|
| 58 | and_return('default_value')
|
---|
| 59 | end
|
---|
| 60 | end
|
---|
| 61 |
|
---|
| 62 | end
|
---|
| 63 |
|
---|
| 64 | end
|
---|