-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathpath_test.go
More file actions
106 lines (99 loc) · 1.88 KB
/
Copy pathpath_test.go
File metadata and controls
106 lines (99 loc) · 1.88 KB
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
package pathutil
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testObj struct {
A string
B nestedObj
C []nestedSliceObj
//lint:ignore U1000 This is actually used through reflect.
unexported string
}
type nestedObj struct {
Val string
}
type nestedPtrObj struct {
Val string
}
type nestedSliceObj struct {
Val string
Ptr *nestedPtrObj
}
func TestTraverse(t *testing.T) {
obj := testObj{
A: "A",
B: nestedObj{
Val: "B",
},
C: []nestedSliceObj{
{Val: "C0", Ptr: &nestedPtrObj{Val: "Ptr0"}},
{Val: "C1"},
},
}
for _, testCase := range []struct {
path *Path
expectedValue interface{}
expectedErr bool
}{
{
path: PathFromSteps(t, "NOTTHERR"),
expectedErr: true,
},
{
path: PathFromSteps(t, "unexported"),
expectedErr: true,
},
{
path: PathFromSteps(t, "A"),
expectedValue: "A",
},
{
path: PathFromSteps(t, "B", "Blah"),
expectedErr: true,
},
{
path: PathFromSteps(t, "B", "Val"),
expectedValue: "B",
},
{
path: PathFromSteps(t, "B", "Val"),
expectedValue: "B",
},
{
path: PathFromSteps(t, "C", 0, "Val"),
expectedValue: "C0",
},
{
path: PathFromSteps(t, "C", 0, "Ptr", "Val"),
expectedValue: "Ptr0",
},
{
path: PathFromSteps(t, "C", 1, "Val"),
expectedValue: "C1",
},
// Pointer is nil
{
path: PathFromSteps(t, "C", 1, "Ptr", "Val"),
expectedErr: true,
},
// Index out of bounds
{
path: PathFromSteps(t, "C", 2, "Val"),
expectedErr: true,
},
} {
c := testCase
t.Run(fmt.Sprintf("%+v", c), func(t *testing.T) {
got, err := RetrieveValueAtPath(obj, c.path)
if c.expectedErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, c.expectedValue, got)
})
}
}