-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtag_test.go
184 lines (147 loc) · 6.58 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
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
package trygo
import (
"reflect"
"strings"
"testing"
)
func Test_ParseTag(t *testing.T) {
//"name,limit:20,scope:[1 2 3],default:1,require,pattern:regex"
var str string
var i64 int64
var f64 float64
taginfos := make(Taginfos)
taginfos.Parse(reflect.String, `field:"teststr,limit:10,scope:[One Two Three],default:Two,require"`)
strTaginfo := taginfos.Get("teststr") //parseOneTag(reflect.String, `field:"teststr,limit:10,scope:[One Two Three],default:Two,require"`)
if !strTaginfo.Default.Exist || strTaginfo.Default.Value.String() != "Two" {
t.Errorf("string tag: default value does not exist, must exist and value is Two")
}
if !strTaginfo.Limit.Exist || strTaginfo.Limit.Value != 10 {
t.Errorf("string tag: limit does not exist, must exist and value is 10")
}
if !strTaginfo.Require {
t.Errorf("string tag: require not is true ")
}
if !strTaginfo.Scope.Exist || !(strTaginfo.Scope.Items.check("One") && strTaginfo.Scope.Items.check("Two") && strTaginfo.Scope.Items.check("Three")) {
t.Errorf("string tag: scope does not exist, must exist and value is [One Two Three]")
}
str = "Four"
if err := strTaginfo.Check(str); err == nil {
t.Errorf("string tag: %v", err)
}
str = ""
if err := strTaginfo.Check(str); err == nil {
t.Errorf("string tag: value is empty, tag is require")
}
str = "Three"
var str2 string
if err := strTaginfo.Check(str, &str2); err != nil || str2 != str {
t.Errorf("string tag: %v, str2:%v, str:%v", err, str2, str)
}
taginfos.Parse(reflect.Int64, "testi64,limit:3,scope:[~-100 -2 -1 0 1 2 100~],default:1,require")
i64Taginfo := taginfos.Get("testi64")
if err := i64Taginfo.Check(""); err == nil || err.Error() != "tag require: value is empty, tag:testi64(int64)" {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check("5000"); err == nil || err.Error() != "tag limit: value is too long, limit:3, tag:testi64(int64)" {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check(int16(5000)); err == nil || err.Error() != "tag limit: value is too long, limit:3, tag:testi64(int64)" {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check("6"); err == nil || err.Error() != "tag scope: value is not in scope:[~-100 -2 -1 0 1 2 100~], tag:testi64(int64)" {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check(int8(6)); err == nil || err.Error() != "tag scope: value is not in scope:[~-100 -2 -1 0 1 2 100~], tag:testi64(int64)" {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check("1"); err != nil {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check(int32(1)); err != nil {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check(int64(999)); err != nil {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check("300", &i64); err != nil || i64 != 300 {
t.Errorf("int64 tag: %v", err)
}
if err := i64Taginfo.Check(int64(999), &i64); err != nil || i64 != 999 {
t.Errorf("int64 tag: %v", err)
}
var i16 int16
if err := i64Taginfo.Check(250, &i16); err != nil || i16 != 250 {
t.Errorf("int64 tag: %v", err)
}
taginfos.Parse(reflect.Float64, "testf64,limit:6,scope:[100.10 200.20~500.50],default:250.25")
f64Taginfo := taginfos.Get("testf64")
if err := f64Taginfo.Check("", &f64); err != nil || f64 != 250.25 {
t.Errorf("float64 tag: %v, f64:%v", err, f64)
}
if err := f64Taginfo.Check("443.45", &f64); err != nil || f64 != 443.45 {
t.Errorf("float64 tag: %v, f64:%v", err, f64)
}
if err := f64Taginfo.Check("A443.4", &f64); err == nil || err.Error() != "strconv.ParseFloat: parsing \"A443.4\": invalid syntax" {
t.Errorf("float64 tag: %v, f64:%v", err, f64)
}
if err := f64Taginfo.Check(float32(343.68), &f64); err != nil || f64 != 343.68 {
t.Errorf("float64 tag: %v, f64:%v", err, f64)
}
if err := f64Taginfo.Check(int(235), &f64); err != nil || f64 != 235 {
t.Errorf("float64 tag: %v, f64:%v", err, f64)
}
taginfos.Parse(reflect.String, "email,limit:30,require,pattern:\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")
emailTaginfo := taginfos.Get("email")
if err := emailTaginfo.Check(""); err == nil || err.Error() != "tag require: value is empty, tag:email(string)" {
t.Errorf("email tag: %v", err)
}
if err := emailTaginfo.Check("trywen@qq.com", &str); err != nil || str != "trywen@qq.com" {
t.Errorf("email tag: %v, str:%v", err, str)
}
}
func Test_ParseStruct(t *testing.T) {
taginfos := make(Taginfos)
type UserForm struct {
Account string `field:"account,limit:20,require"`
Pwd string `field:"pwd,limit:20,require"`
Name string `field:"name,limit:20"`
Sex int `field:"sex,scope:[1 2 3],default:1"`
Age int `field:"age,scope:[0~200],default:0"`
Email string `field:"email,limit:30,pattern:\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"`
Stature float32 `field:"stature,scope:[0.0~],default:0.0"`
}
type QueryUserForm struct {
Status []int `field:"status,scope:[1 2 3 4]"`
Orderby []string
Psize int `field:"psize,scope:[10 20 50 100]"`
Pno int `field:"pno,scope:[1~]"`
}
taginfos.ParseStruct("user", reflect.TypeOf(UserForm{}), true)
accountTaginfo := taginfos.Get("user.account")
if accountTaginfo == nil || accountTaginfo.String() != "name:user.account(string) (limit:{{true} 20}, require:true, scope:, default:{{false} <invalid Value>}, pattern:{{false} <nil>})" {
t.Errorf("ParseStruct error, formDomainModel=true, accountTaginfo:%v", accountTaginfo)
}
sexTaginfo := taginfos.Get("user.sex")
if sexTaginfo == nil || sexTaginfo.String() != "name:user.sex(int) (limit:{{false} 0}, require:false, scope:1 2 3, default:{{true} <int Value>}, pattern:{{false} <nil>})" {
t.Errorf("ParseStruct error, formDomainModel=true, sexTaginfo:%v", sexTaginfo)
}
emailTaginfo := taginfos.Get("user.email")
if emailTaginfo == nil {
t.Errorf("ParseStruct error, formDomainModel=true, emailTaginfo:%v", emailTaginfo)
}
if err := emailTaginfo.Check("trywen@qq.com"); err != nil {
t.Errorf("ParseStruct error, err:%v", err)
}
if err := emailTaginfo.Check("trywen@qq"); err == nil || !strings.HasPrefix(err.Error(), "tag pattern: pattern match fail!") {
t.Errorf("ParseStruct error, err:%v", err)
}
taginfos.ParseStruct("user", reflect.TypeOf(QueryUserForm{}), true)
statusTaginfo := taginfos.Get("user.status")
if statusTaginfo == nil || statusTaginfo.String() != "name:user.status(int) (limit:{{false} 0}, require:false, scope:1 2 3 4, default:{{false} <invalid Value>}, pattern:{{false} <nil>})" {
t.Errorf("ParseStruct error, formDomainModel=true, statusTaginfo:%v", statusTaginfo)
}
var status int
if err := statusTaginfo.Check("2", &status); err != nil || status != 2 {
t.Errorf("ParseStruct error, err:%v", err)
}
}