|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'parsepson' do |
| 6 | + it 'exists' do |
| 7 | + is_expected.not_to eq(nil) |
| 8 | + end |
| 9 | + |
| 10 | + it 'raises an error if called without any arguments' do |
| 11 | + is_expected.to run.with_params |
| 12 | + .and_raise_error(%r{wrong number of arguments}i) |
| 13 | + end |
| 14 | + |
| 15 | + context 'with correct PSON data' do |
| 16 | + it 'is able to parse PSON data with a Hash' do |
| 17 | + is_expected.to run.with_params('{"a":"1","b":"2"}') |
| 18 | + .and_return('a' => '1', 'b' => '2') |
| 19 | + end |
| 20 | + |
| 21 | + it 'is able to parse PSON data with an Array' do |
| 22 | + is_expected.to run.with_params('["a","b","c"]') |
| 23 | + .and_return(['a', 'b', 'c']) |
| 24 | + end |
| 25 | + |
| 26 | + it 'is able to parse empty PSON values' do |
| 27 | + actual_array = ['[]', '{}'] |
| 28 | + expected = [[], {}] |
| 29 | + actual_array.each_with_index do |actual, index| |
| 30 | + is_expected.to run.with_params(actual).and_return(expected[index]) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + it 'is able to parse PSON data with a mixed structure' do |
| 35 | + is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') |
| 36 | + .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) |
| 37 | + end |
| 38 | + |
| 39 | + it 'is able to parse PSON data with a UTF8 and double byte characters' do |
| 40 | + is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') |
| 41 | + .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] }) |
| 42 | + end |
| 43 | + |
| 44 | + it 'does not return the default value if the data was parsed correctly' do |
| 45 | + is_expected.to run.with_params('{"a":"1"}', 'default_value') |
| 46 | + .and_return('a' => '1') |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context 'with incorrect PSON data' do |
| 51 | + it 'raises an error with invalid PSON and no default' do |
| 52 | + is_expected.to run.with_params('') |
| 53 | + .and_raise_error(PSON::ParserError) |
| 54 | + end |
| 55 | + |
| 56 | + it 'supports a structure for a default value' do |
| 57 | + is_expected.to run.with_params('', 'a' => '1') |
| 58 | + .and_return('a' => '1') |
| 59 | + end |
| 60 | + |
| 61 | + ['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value| |
| 62 | + it "returns the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do |
| 63 | + is_expected.to run.with_params(value, 'default_value') |
| 64 | + .and_return('default_value') |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | +end |
0 commit comments