forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge_test.go
96 lines (83 loc) · 2.51 KB
/
bridge_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
package common
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type bridgeTestFieldsType struct {
Exported string
ExportedTag string `js:"renamed"`
ExportedHidden string `js:"-"`
unexported string //nolint:structcheck,unused // actually checked in the test
unexportedTag string `js:"unexported"` //nolint:structcheck,unused // actually checked in the test
}
type bridgeTestMethodsType struct{}
func (bridgeTestMethodsType) ExportedFn() {}
//nolint:unused // needed for the actual test to check that it won't be seen
func (bridgeTestMethodsType) unexportedFn() {}
func (*bridgeTestMethodsType) ExportedPtrFn() {}
//nolint:unused // needed for the actual test to check that it won't be seen
func (*bridgeTestMethodsType) unexportedPtrFn() {}
type bridgeTestOddFieldsType struct {
TwoWords string
URL string
}
type bridgeTestConstructorType struct{}
type bridgeTestConstructorSpawnedType struct{}
func (bridgeTestConstructorType) XConstructor() bridgeTestConstructorSpawnedType {
return bridgeTestConstructorSpawnedType{}
}
func TestFieldNameMapper(t *testing.T) {
t.Parallel()
testdata := []struct {
Typ reflect.Type
Fields map[string]string
Methods map[string]string
}{
{reflect.TypeOf(bridgeTestFieldsType{}), map[string]string{
"Exported": "exported",
"ExportedTag": "renamed",
"ExportedHidden": "",
"unexported": "",
"unexportedTag": "",
}, nil},
{reflect.TypeOf(bridgeTestMethodsType{}), nil, map[string]string{
"ExportedFn": "exportedFn",
"unexportedFn": "",
}},
{reflect.TypeOf(bridgeTestOddFieldsType{}), map[string]string{
"TwoWords": "two_words",
"URL": "url",
}, nil},
{reflect.TypeOf(bridgeTestConstructorType{}), nil, map[string]string{
"XConstructor": "Constructor",
}},
}
for _, data := range testdata {
data := data
for field, name := range data.Fields {
field, name := field, name
t.Run(field, func(t *testing.T) {
t.Parallel()
f, ok := data.Typ.FieldByName(field)
if assert.True(t, ok, "no such field") {
assert.Equal(t, name, (FieldNameMapper{}).FieldName(data.Typ, f))
}
})
}
for meth, name := range data.Methods {
meth, name := meth, name
t.Run(meth, func(t *testing.T) {
t.Parallel()
m, ok := data.Typ.MethodByName(meth)
if name != "" {
if assert.True(t, ok, "no such method") {
assert.Equal(t, name, (FieldNameMapper{}).MethodName(data.Typ, m))
}
} else {
assert.False(t, ok, "exported by accident")
}
})
}
}
}