-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmap_test.go
98 lines (93 loc) · 1.44 KB
/
gmap_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
package gmap
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_nextIndex(t *testing.T) {
split := func(path string) (ret []string) {
next := nextResult{Path: path}
for {
next = nextIndex(next.Path)
ret = append(ret, next.Index)
if !next.More {
return
}
}
}
require.Equal(t,
[]string{"", "foo", "ba\\r", "he.ll\\o", ""},
split(`.foo.ba\\r.he\.ll\\o.`),
)
}
func Test_get(t *testing.T) {
type args struct {
v interface{}
path string
ignoreCase bool
}
var zero Value
tests := []struct {
name string
args args
want Value
}{
{
"normal",
args{
map[string]interface{}{
"foo": []interface{}{
map[string]interface{}{
"ba.r": 3,
},
},
},
`foo.0.ba\.r`,
false,
},
Value{true, 3},
},
{
"ignore case",
args{
map[string]interface{}{
"Foo": map[string]interface{}{
"Bar": 3,
},
},
`fOO.bAR`,
true,
},
Value{true, 3},
},
{
"out of bounds",
args{
[]interface{}{
map[string]interface{}{
"foo": 3,
},
},
"1.foo",
false,
},
zero,
},
{
"no such key",
args{
map[string]interface{}{
"foo": 3,
},
"bar",
false,
},
zero,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, get(tt.args.v, tt.args.path, tt.args.ignoreCase))
})
}
}