-
Notifications
You must be signed in to change notification settings - Fork 2
/
ref_test.go
110 lines (106 loc) · 2.24 KB
/
ref_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package jsonlogic
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDottedRef(t *testing.T) {
tests := []struct {
name string
data interface{}
ref interface{}
expect interface{}
}{
{
name: "nil",
data: nil,
ref: "blah",
expect: nil,
},
{
name: "non-map",
data: 2.0,
ref: "blah",
expect: nil,
},
{
name: "bad-key",
data: []interface{}{2.0},
ref: false,
expect: nil,
},
{
name: "single-found",
data: map[string]interface{}{"one": 2.0},
ref: "one",
expect: 2.0,
},
{
name: "single-failed",
data: map[string]interface{}{"one": 2.0},
ref: "two",
expect: nil,
},
{
name: "deep-found",
data: map[string]interface{}{"one": map[string]interface{}{"two": 2.0}},
ref: "one.two",
expect: 2.0,
},
{
name: "deep-miss",
data: map[string]interface{}{"one": map[string]interface{}{"two": 2.0}},
ref: "one.three",
expect: nil,
},
{
name: "deep-non-trivial",
data: map[string]interface{}{"one": map[string]interface{}{"two": []interface{}{"hello", 2.0}}},
ref: "one.two",
expect: []interface{}{"hello", 2.0},
},
{
name: "deep-non-trivial",
data: map[string]interface{}{"one": map[string]interface{}{"two": []interface{}{"hello", 2.0}}},
ref: "one.two.0",
expect: "hello",
},
{
name: "deep-array",
data: map[string]interface{}{"one": map[string]interface{}{"two": []interface{}{"hello", 2.0}}},
ref: "one.two.3",
expect: nil,
},
{
name: "deep-non-trivial",
data: map[string]interface{}{"one": map[string]interface{}{"two": []interface{}{[]interface{}{"hello", 2.0}}}},
ref: "one.two.0.1",
expect: 2.0,
},
{
name: "array-float",
data: []interface{}{"hello", 2.0},
ref: 1.0,
expect: 2.0,
},
{
name: "array-string",
data: []interface{}{"hello", 2.0},
ref: "1",
expect: 2.0,
},
{
name: "array-non-int",
data: []interface{}{"hello", 2.0},
ref: 1.5,
expect: nil,
},
}
for _, st := range tests {
t.Run(st.name, func(t *testing.T) {
assert.NotPanics(t, func() {
v := DottedRef(st.data, st.ref)
assert.Equal(t, st.expect, v)
})
})
}
}