-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathattr_test.go
295 lines (253 loc) · 7.54 KB
/
attr_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package attr
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type User struct {
Username string `json:"username" db:"uname"`
Age int `json:"age" meta:"important"`
password string
}
var user User = User{"srathi", 30, "my_secret_123"}
func TestGetValue(t *testing.T) {
want := user.Username
got, err := GetValue(user, "Username")
require.Nil(t, err)
require.Equal(t, want, got, "Username mismatch")
wantErr := ErrUnexportedField
_, gotErr := GetValue(user, "password")
require.Equal(t, wantErr, gotErr, "Able to get an unexported field value")
}
func ExampleGetValue() {
testUser := User{Username: "srathi", password: "secret", Age: 30}
value, err := GetValue(testUser, "Age")
if err != nil {
// Handle error.
}
fmt.Printf("Value of Age: %v", value)
// Output: Value of Age: 30
}
func TestHas(t *testing.T) {
for _, test := range []struct {
attrName string
want bool
errMsg string
}{
{"Age", true, "Age not found"},
{"NonExistant", false, "NonExistant field found"},
} {
got, err := Has(&user, test.attrName)
require.Nil(t, err)
require.Equal(t, test.want, got, test.errMsg)
}
}
func ExampleHas() {
testUser := User{Username: "srathi", password: "secret", Age: 30}
ok, err := Has(&testUser, "Age")
if err != nil {
// Handle error.
}
fmt.Printf("Age found: %v\n", ok)
ok, err = Has(&testUser, "ABC")
if err != nil {
// Handle error.
}
fmt.Printf("ABC found: %v\n", ok)
// Output:
// Age found: true
// ABC found: false
}
func TestSetValue(t *testing.T) {
for _, test := range []struct {
attrName string
newValue interface{}
byPtr bool
wantErr error
errMsg string
}{
{"Username", "new-srathi", false, ErrNotPtr, "Able to set fields on a struct by value"},
{"password", "new-password", true, ErrUnexportedField, "Able to set a private field of a struct"},
{"Age", 40.5, true, ErrMismatchValue, "Able to set float value to an int field"},
{"Age", 40, true, nil, "New value not set in a struct"},
{"Age", 30, true, nil, "New value not set in a struct"},
} {
var gotErr error
if test.byPtr {
gotErr = SetValue(&user, test.attrName, test.newValue)
} else {
gotErr = SetValue(user, test.attrName, test.newValue)
}
require.Equal(t, test.wantErr, gotErr, test.errMsg)
}
}
func ExampleSetValue() {
testUser := User{Username: "srathi", password: "secret", Age: 30}
err := SetValue(&testUser, "password", "new-secret")
fmt.Printf("Error while setting a private field: %v\n", err)
err = SetValue(testUser, "Username", "new-username")
fmt.Printf("Error while passing struct by value: %v\n", err)
err = SetValue(&testUser, "Username", 100)
fmt.Printf("Error while setting 100 in username: %v\n", err)
err = SetValue(&testUser, "Username", "new-username")
if err != nil {
// Handle error.
}
fmt.Printf("New username: %s\n", testUser.Username)
// Output:
// Error while setting a private field: Specified field is not an exported or public field
// Error while passing struct by value: Specified struct is not passed by pointer
// Error while setting 100 in username: Specified value to set is of a different type
// New username: new-username
}
func TestNames(t *testing.T) {
// Only public fields are returned.
want := []string{"Username", "Age"}
got, err := Names(&user)
require.Nil(t, err)
require.Equal(t, want, got, "Struct field list is not correct")
}
func ExampleNames() {
testUser := User{Username: "srathi", password: "secret", Age: 30}
fields, err := Names(&testUser)
if err != nil {
// Handle error.
}
fmt.Printf("Field names: %v", fields)
// Output: Field names: [Username Age]
}
func TestValues(t *testing.T) {
// Only the value of the public fields are returned.
want := map[string]interface{}{"Username": "srathi", "Age": 30}
got, err := Values(&user)
require.Nil(t, err)
require.Equal(t, want, got, "Struct field values are not correct")
}
func ExampleValues() {
testUser := User{Username: "srathi", password: "secret", Age: 30}
values, err := Values(&testUser)
if err != nil {
// Handle error.
}
fmt.Printf("Values: %v\n", values)
// Output: Values: map[Age:30 Username:srathi]
}
func TestGetTag(t *testing.T) {
for _, test := range []struct {
attrName string
tagName string
wantValue string
errMsg string
}{
{"Age", "meta", "important", "meta tag value for 'Age' is not correct"},
{"Username", "json", "username", "json tag value for 'Username' is not correct"},
{"Age", "db", "", "json tag value for 'Age' is not correct"},
} {
gotValue, err := GetTag(&user, test.attrName, test.tagName)
require.Nil(t, err)
require.Equal(t, test.wantValue, gotValue, test.errMsg)
}
wantErr := ErrUnexportedField
_, gotErr := GetTag(&user, "password", "json")
require.Equal(t, wantErr, gotErr, "Able to get tag value of a private field")
}
func ExampleGetTag() {
// type User struct {
// Username string `json:"username" db:"uname"`
// password string `json:"password" db:"pw"`
// Age int `json:"age" meta:"important"`
// }
testUser := User{Username: "srathi", password: "secret", Age: 30}
tag, err := GetTag(&testUser, "Username", "db")
if err != nil {
// Handle error.
}
fmt.Printf("db tag value: %s\n", tag)
// Output: db tag value: uname
}
func TestTags(t *testing.T) {
for _, test := range []struct {
tagName string
want map[string]string
errMsg string
}{
{"json", map[string]string{"Username": "username", "Age": "age"},
"json tag values are not correct"},
{"db", map[string]string{"Username": "uname", "Age": ""},
"db tag values are not correct"},
} {
got, err := Tags(&user, test.tagName)
require.Nil(t, err)
require.Equal(t, test.want, got, test.errMsg)
}
}
func ExampleTags() {
// type User struct {
// Username string `json:"username" db:"uname"`
// password string `json:"password" db:"pw"`
// Age int `json:"age" meta:"important"`
// }
testUser := User{Username: "srathi", password: "secret", Age: 30}
values, err := Tags(&testUser, "json")
if err != nil {
// Handle error.
}
fmt.Printf("json tag values: %v\n", values)
values, err = Tags(&testUser, "db")
if err != nil {
// Handle error.
}
fmt.Printf("db tag values: %v\n", values)
// Output:
// json tag values: map[Age:age Username:username]
// db tag values: map[Age: Username:uname]
}
func TestGetKind(t *testing.T) {
for _, test := range []struct {
attrName string
kindStr string
}{
{"Username", "string"},
{"Age", "int"},
} {
want := test.kindStr
got, err := GetKind(&user, test.attrName)
require.Nil(t, err)
require.Equal(t, want, got, "Field 'Kind' mismatch")
}
wantErr := ErrNoField
_, gotErr := GetKind(user, "ABC")
require.Equal(t, wantErr, gotErr, "Able to get a non-existent field 'Kind'")
}
func ExampleGetKind() {
testUser := User{Username: "srathi", password: "secret", Age: 30}
kind, err := GetKind(testUser, "Age")
if err != nil {
// Handle error.
}
fmt.Printf("Kind of Age: %s\n", kind)
kind, err = GetKind(testUser, "Username")
if err != nil {
// Handle error.
}
fmt.Printf("Kind of Username: %s\n", kind)
// Output:
// Kind of Age: int
// Kind of Username: string
}
func TestKinds(t *testing.T) {
// Only public fields are returned.
want := map[string]string{"Username": "string", "Age": "int"}
got, err := Kinds(&user)
require.Nil(t, err)
require.Equal(t, want, got, "Struct field 'kind' map is not correct")
}
func ExampleKinds() {
testUser := User{Username: "srathi", password: "secret", Age: 30}
kinds, err := Kinds(&testUser)
if err != nil {
// Handle error.
}
fmt.Printf("Field kinds: %v", kinds)
// Output: Field kinds: map[Age:int Username:string]
}