-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_test.go
151 lines (132 loc) · 3.78 KB
/
tag_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
package rflutil
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ParseTag(t *testing.T) {
type SS struct {
I int `mytag:"i,optional,k1=v1,omitempty"`
S string `mytag:"s,optional,k1=v1,k2=v2,omitempty"`
U uint `mytag:"-,optional"`
}
s := SS{I: 1, S: "hello", U: 2}
v := valOf(s)
t.Run("#1: tag not exist", func(t *testing.T) {
field, ok := v.Type().FieldByName("I")
assert.True(t, ok)
_, err := ParseTag(&field, "abc", ",")
assert.ErrorIs(t, err, ErrNotFound)
})
t.Run("#2: inexact delimiter", func(t *testing.T) {
field, ok := v.Type().FieldByName("I")
assert.True(t, ok)
tag, err := ParseTag(&field, "mytag", " ")
assert.Nil(t, err)
assert.Equal(t, "i,optional,k1=v1,omitempty", tag.Name)
})
t.Run("#3: ignored tag", func(t *testing.T) {
field, ok := v.Type().FieldByName("U")
assert.True(t, ok)
tag, err := ParseTag(&field, "mytag", ",")
assert.Nil(t, err)
assert.Equal(t, "-", tag.Name)
assert.True(t, tag.Ignored)
})
t.Run("#4: success", func(t *testing.T) {
field, ok := v.Type().FieldByName("S")
assert.True(t, ok)
tag, err := ParseTag(&field, "mytag", ",")
assert.Nil(t, err)
assert.Equal(t, "s", tag.Name)
assert.False(t, tag.Ignored)
assert.Equal(t, map[string]string{
"optional": "",
"k1": "v1",
"k2": "v2",
"omitempty": "",
}, tag.Attrs)
val, _ := tag.GetAttr("k2")
assert.Equal(t, "v2", val)
assert.Equal(t, "", tag.GetAttrDefault("optional", "x"))
assert.Equal(t, "x", tag.GetAttrDefault("not-exist", "x"))
})
}
func Test_ParseTagOf(t *testing.T) {
type SS struct {
I int `mytag:"i,optional,k1=v1,omitempty"`
S string `mytag:"s,optional,k1=v1,k2=v2,omitempty"`
U uint `mytag:"-,optional"`
}
s := SS{I: 1, S: "hello", U: 2}
v := valOf(s)
t.Run("#1: invalid input type", func(t *testing.T) {
_, err := ParseTagOf(valOf("123"), "xField", "abc", ",")
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#2: field not exist", func(t *testing.T) {
_, err := ParseTagOf(v, "xField", "mytag", ",")
assert.ErrorIs(t, err, ErrNotFound)
})
t.Run("#3: ignored tag", func(t *testing.T) {
tag, err := ParseTagOf(v, "U", "mytag", ",")
assert.Nil(t, err)
assert.Equal(t, "-", tag.Name)
assert.True(t, tag.Ignored)
})
t.Run("#4: success", func(t *testing.T) {
tag, err := ParseTagOf(v, "S", "mytag", ",")
assert.Nil(t, err)
assert.Equal(t, "s", tag.Name)
assert.False(t, tag.Ignored)
assert.Equal(t, map[string]string{
"optional": "",
"k1": "v1",
"k2": "v2",
"omitempty": "",
}, tag.Attrs)
})
}
func Test_ParseTagsOf(t *testing.T) {
type SS struct {
I int `mytag:"i,optional,k1=v1,omitempty"`
S string `mytag:"s,optional,k1=v1,k2=v2,omitempty"`
U uint `mytag:"-,optional"`
B bool `tagx:"b"`
}
s := SS{I: 1, S: "hello", U: 2}
v := valOf(s)
t.Run("#1: invalid input type", func(t *testing.T) {
_, err := ParseTagsOf(valOf("123"), "abc", ",")
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#2: tag not exist", func(t *testing.T) {
tags, err := ParseTagsOf(v, "abc", ",")
assert.Nil(t, err)
assert.Equal(t, 0, len(tags))
})
t.Run("#3: success", func(t *testing.T) {
tags, err := ParseTagsOf(v, "mytag", ",")
assert.Nil(t, err)
assert.Equal(t, 3, len(tags))
assert.Equal(t, "i", tags[0].Name)
assert.False(t, tags[0].Ignored)
assert.Equal(t, map[string]string{
"optional": "",
"k1": "v1",
"omitempty": "",
}, tags[0].Attrs)
assert.Equal(t, "s", tags[1].Name)
assert.False(t, tags[1].Ignored)
assert.Equal(t, map[string]string{
"optional": "",
"k1": "v1",
"k2": "v2",
"omitempty": "",
}, tags[1].Attrs)
assert.Equal(t, "-", tags[2].Name)
assert.True(t, tags[2].Ignored)
assert.Equal(t, map[string]string{
"optional": "",
}, tags[2].Attrs)
})
}