-
Notifications
You must be signed in to change notification settings - Fork 209
/
util_test.go
114 lines (107 loc) · 1.68 KB
/
util_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
111
112
113
114
package dbr
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestSnakeCase(t *testing.T) {
for _, test := range []struct {
in string
want string
}{
{
in: "",
want: "",
},
{
in: "IsDigit",
want: "is_digit",
},
{
in: "Is",
want: "is",
},
{
in: "IsID",
want: "is_id",
},
{
in: "IsSQL",
want: "is_sql",
},
{
in: "LongSQL",
want: "long_sql",
},
{
in: "Float64Val",
want: "float64_val",
},
{
in: "XMLName",
want: "xml_name",
},
} {
require.Equal(t, test.want, camelCaseToSnakeCase(test.in))
}
}
func BenchmarkCamelCaseToSnakeCase(b *testing.B) {
for i := 0; i < b.N; i++ {
camelCaseToSnakeCase("getHTTPResponseCode")
}
}
func TestFindValueByName(t *testing.T) {
for _, test := range []struct {
in interface{}
name []string
want []string
}{
{
in: struct {
CreatedAt time.Time
}{},
name: []string{"created_at"},
want: []string{"created_at"},
},
{
in: struct {
intVal int
}{},
name: []string{"int_val"},
},
{
in: struct {
IntVal int `db:"test"`
}{},
name: []string{"test"},
want: []string{"test"},
},
{
in: struct {
IntVal int `db:"-"`
}{},
name: []string{"int_val"},
},
{
in: struct {
Test1 struct {
Test2 int
}
}{},
name: []string{"test2"},
want: []string{"test2"},
},
} {
found := make([]interface{}, len(test.name))
s := newTagStore()
s.findValueByName(reflect.ValueOf(test.in), test.name, found, false)
var got []string
for i, v := range found {
if v != nil {
got = append(got, test.name[i])
}
}
require.Equal(t, test.want, got)
}
}