-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapaccess_test.go
95 lines (85 loc) · 2.29 KB
/
mapaccess_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
package mapaccess
import (
"fmt"
"html/template"
"io/ioutil"
"reflect"
"testing"
)
type Data struct {
Array []string
Nested Nested
}
type Nested struct {
Array []string
}
var typed = Data{
Array: []string{"value"},
Nested: Nested{
Array: []string{"four"},
},
}
var data = map[string]interface{}{
"array": []interface{}{"value"},
"one": "two",
"nested": map[string]interface{}{
"key": "three",
"array": []interface{}{"four"},
},
}
func TestGet(t *testing.T) {
type args struct {
key string
data interface{}
}
tests := []struct {
name string
args args
want interface{}
wantErr bool
}{
{"root", args{"one", data}, "two", false},
{"root array", args{"array[0]", data}, "value", false},
{"nested", args{"nested.key", data}, "three", false},
{"nested array", args{"nested.array[0]", data}, "four", false},
{"spaces", args{" one.two[0]", data}, nil, true},
{"spaces", args{"o.test.", data}, nil, true},
{"spaces", args{"[0]", data}, nil, true},
{"nested array missing", args{"one.two[1]", data}, nil, true},
{"root missing", args{"two", data}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Get(tt.args.data, tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() = %v, want %v", got, tt.want)
}
})
}
}
func benchmarkMapaccess(key string, b *testing.B) {
for n := 0; n < b.N; n++ {
Get(data, key)
}
}
func benchmarkGoTemplate(key string, b *testing.B) {
for n := 0; n < b.N; n++ {
t, err := template.New("tmpl").Parse(key)
if err != nil {
fmt.Println(err)
}
t.Execute(ioutil.Discard, typed)
}
}
func BenchmarkMapaccessRootKey(b *testing.B) { benchmarkMapaccess("nested", b) }
func BenchmarkMapaccessNestedKey(b *testing.B) { benchmarkMapaccess("nested.array", b) }
func BenchmarkMapaccessNestedArray(b *testing.B) { benchmarkMapaccess("nested.array[0]", b) }
func BenchmarkGoTemplateRootKey(b *testing.B) { benchmarkGoTemplate("{{ .Nested }}", b) }
func BenchmarkGoTemplateNestedKey(b *testing.B) { benchmarkGoTemplate("{{ .Nested.Array }}", b) }
func BenchmarkGoTemplateNestedArray(b *testing.B) {
benchmarkGoTemplate("{{ index .Nested.Array 0 }}", b)
}