-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseutil_test.go
143 lines (136 loc) · 4.15 KB
/
parseutil_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var parseFieldsData = []struct {
pkg string
typ string
format string
tag string
tagRegex string
tagFormat string
tagStrict bool
embedded bool
excluded []string
expectedFields []field
}{
{
pkg: "./internal/testdata",
typ: "User",
format: formatAsIs,
tag: "db",
tagFormat: formatAsIs,
embedded: true,
excluded: []string{"FullName"},
expectedFields: []field{
{Name: "CreatedAt", Alias: "created_at", Depth: 2},
{Name: "UpdatedAt", Alias: "updated_at", Depth: 2},
{Name: "FirstUpdatedAt", Alias: "FirstUpdatedAt", Depth: 2},
{Name: "DeletedAt", Alias: "deleted_at", Depth: 3},
{Name: "ID", Alias: "id", Depth: 1},
{Name: "FirstName", Alias: "name", Depth: 1},
{Name: "LastName", Alias: "surname", Depth: 1},
{Name: "Email", Alias: "email", Depth: 1},
{Name: "Password", Alias: "password", Depth: 1},
},
},
{
pkg: "./internal/testdata",
typ: "User",
format: formatAsIs,
tag: "db",
tagRegex: `(\w+),?.*`,
tagFormat: formatAsIs,
embedded: true,
excluded: []string{"FullName"},
expectedFields: []field{
{Name: "CreatedAt", Alias: "created_at", Depth: 2},
{Name: "UpdatedAt", Alias: "updated_at", Depth: 2},
{Name: "FirstUpdatedAt", Alias: "FirstUpdatedAt", Depth: 2},
{Name: "DeletedAt", Alias: "deleted_at", Depth: 3},
{Name: "ID", Alias: "id", Depth: 1},
{Name: "FirstName", Alias: "name", Depth: 1},
{Name: "LastName", Alias: "surname", Depth: 1},
{Name: "Email", Alias: "email", Depth: 1},
{Name: "Password", Alias: "password", Depth: 1},
},
},
{
pkg: "./internal/testdata",
typ: "User",
format: formatCamelCase,
tag: "",
embedded: true,
excluded: []string{},
expectedFields: []field{
{Name: "CreatedAt", Alias: "createdAt", Depth: 2},
{Name: "UpdatedAt", Alias: "updatedAt", Depth: 2},
{Name: "FirstUpdatedAt", Alias: "firstUpdatedAt", Depth: 2},
{Name: "DeletedAt", Alias: "deletedAt", Depth: 3},
{Name: "ID", Alias: "id", Depth: 1},
{Name: "FirstName", Alias: "firstName", Depth: 1},
{Name: "LastName", Alias: "lastName", Depth: 1},
{Name: "Email", Alias: "email", Depth: 1},
{Name: "Password", Alias: "password", Depth: 1},
{Name: "FullName", Alias: "fullName", Depth: 1},
},
},
{
pkg: "./internal/testdata",
typ: "User",
format: formatPascalCase,
tag: "",
embedded: true,
excluded: []string{},
expectedFields: []field{
{Name: "CreatedAt", Alias: "CreatedAt", Depth: 2},
{Name: "UpdatedAt", Alias: "UpdatedAt", Depth: 2},
{Name: "FirstUpdatedAt", Alias: "FirstUpdatedAt", Depth: 2},
{Name: "DeletedAt", Alias: "DeletedAt", Depth: 3},
{Name: "ID", Alias: "Id", Depth: 1},
{Name: "FirstName", Alias: "FirstName", Depth: 1},
{Name: "LastName", Alias: "LastName", Depth: 1},
{Name: "Email", Alias: "Email", Depth: 1},
{Name: "Password", Alias: "Password", Depth: 1},
{Name: "FullName", Alias: "FullName", Depth: 1},
},
},
{
pkg: "./internal/testdata",
typ: "User",
format: formatAsIs,
tag: "",
embedded: false,
excluded: []string{},
expectedFields: []field{
{Name: "ID", Alias: "ID", Depth: 1},
{Name: "FirstName", Alias: "FirstName", Depth: 1},
{Name: "LastName", Alias: "LastName", Depth: 1},
{Name: "Email", Alias: "Email", Depth: 1},
{Name: "Password", Alias: "Password", Depth: 1},
{Name: "FullName", Alias: "FullName", Depth: 1},
},
},
}
func TestParseFields(t *testing.T) {
for _, test := range parseFieldsData {
t.Run(test.typ, func(t *testing.T) {
_, typ, err := parseType(test.pkg, test.typ)
require.NoError(t, err)
parseParams := parseFieldsParams{
format: test.format,
tag: test.tag,
tagRegex: test.tagRegex,
tagFormat: test.tagFormat,
tagStrict: test.tagStrict,
embedded: test.embedded,
excluded: test.excluded,
}
actualFields, err := parseFields(typ, parseParams, 0)
require.NoError(t, err)
assert.Equal(t, test.expectedFields, actualFields)
})
}
}