1 | require 'spec_helper'
|
---|
2 |
|
---|
3 | describe 'try_get_value' do
|
---|
4 |
|
---|
5 | let(:data) do
|
---|
6 | {
|
---|
7 | 'a' => {
|
---|
8 | 'g' => '2',
|
---|
9 | 'e' => [
|
---|
10 | 'f0',
|
---|
11 | 'f1',
|
---|
12 | {
|
---|
13 | 'x' => {
|
---|
14 | 'y' => 'z'
|
---|
15 | }
|
---|
16 | },
|
---|
17 | 'f3',
|
---|
18 | ]
|
---|
19 | },
|
---|
20 | 'b' => true,
|
---|
21 | 'c' => false,
|
---|
22 | 'd' => '1',
|
---|
23 | }
|
---|
24 | end
|
---|
25 |
|
---|
26 | context 'single values' do
|
---|
27 | it 'should exist' do
|
---|
28 | is_expected.not_to eq(nil)
|
---|
29 | end
|
---|
30 |
|
---|
31 | it 'should be able to return a single value' do
|
---|
32 | is_expected.to run.with_params('test').and_return('test')
|
---|
33 | end
|
---|
34 |
|
---|
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')
|
---|
37 | end
|
---|
38 |
|
---|
39 | it 'should return default if there is no data' do
|
---|
40 | is_expected.to run.with_params(nil, nil, 'default').and_return('default')
|
---|
41 | end
|
---|
42 |
|
---|
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)
|
---|
45 | end
|
---|
46 | end
|
---|
47 |
|
---|
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')
|
---|
51 | end
|
---|
52 |
|
---|
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')
|
---|
55 | end
|
---|
56 |
|
---|
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')
|
---|
59 | end
|
---|
60 |
|
---|
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')
|
---|
63 | end
|
---|
64 |
|
---|
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')
|
---|
67 | end
|
---|
68 |
|
---|
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')
|
---|
71 | end
|
---|
72 |
|
---|
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')
|
---|
75 | end
|
---|
76 |
|
---|
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')
|
---|
79 | end
|
---|
80 |
|
---|
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)
|
---|
84 | end
|
---|
85 |
|
---|
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)
|
---|
89 | end
|
---|
90 |
|
---|
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)
|
---|
93 | end
|
---|
94 |
|
---|
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')
|
---|
98 | end
|
---|
99 | end
|
---|
100 | end
|
---|