3 describe 'try_get_value' do
26 context 'single values' do
28 is_expected.not_to eq(nil)
31 it 'should be able to return a single value' do
32 is_expected.to run.with_params('test').and_return('test')
35 it 'should use the default value if data is a single value and path is present' do
36 is_expected.to run.with_params('test', 'path', 'default').and_return('default')
39 it 'should return default if there is no data' do
40 is_expected.to run.with_params(nil, nil, 'default').and_return('default')
43 it 'should be able to use data structures as default values' do
44 is_expected.to run.with_params('test', 'path', data).and_return(data)
48 context 'structure values' do
49 it 'should be able to extracts a single hash value' do
50 is_expected.to run.with_params(data, 'd', 'default').and_return('1')
53 it 'should be able to extract a deeply nested hash value' do
54 is_expected.to run.with_params(data, 'a/g', 'default').and_return('2')
57 it 'should return the default value if the path is not found' do
58 is_expected.to run.with_params(data, 'missing', 'default').and_return('default')
61 it 'should return the default value if the path is too long' do
62 is_expected.to run.with_params(data, 'a/g/c/d', 'default').and_return('default')
65 it 'should support an array index in the path' do
66 is_expected.to run.with_params(data, 'a/e/1', 'default').and_return('f1')
69 it 'should return the default value if an array index is not a number' do
70 is_expected.to run.with_params(data, 'a/b/c', 'default').and_return('default')
73 it 'should return the default value if and index is out of array length' do
74 is_expected.to run.with_params(data, 'a/e/5', 'default').and_return('default')
77 it 'should be able to path though both arrays and hashes' do
78 is_expected.to run.with_params(data, 'a/e/2/x/y', 'default').and_return('z')
81 it 'should be able to return "true" value' do
82 is_expected.to run.with_params(data, 'b', 'default').and_return(true)
83 is_expected.to run.with_params(data, 'm', true).and_return(true)
86 it 'should be able to return "false" value' do
87 is_expected.to run.with_params(data, 'c', 'default').and_return(false)
88 is_expected.to run.with_params(data, 'm', false).and_return(false)
91 it 'should return "nil" if value is not found and no default value is provided' do
92 is_expected.to run.with_params(data, 'a/1').and_return(nil)
95 it 'should be able to use a custom path separator' do
96 is_expected.to run.with_params(data, 'a::g', 'default', '::').and_return('2')
97 is_expected.to run.with_params(data, 'a::c', 'default', '::').and_return('default')