-
Notifications
You must be signed in to change notification settings - Fork 4
/
number.go
187 lines (162 loc) · 4.83 KB
/
number.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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package assert
import (
"fmt"
"reflect"
)
// Length 断言长度是否为指定的值
//
// v 可以是以下类型:
// - map
// - string
// - slice
// - array
func (a *Assertion) Length(v interface{}, l int, msg ...interface{}) *Assertion {
a.TB().Helper()
rl, err := getLen(v)
if err != "" {
a.Assert(false, NewFailure("Length", msg, map[string]interface{}{"err": err}))
}
return a.Assert(rl == l, NewFailure("Length", msg, map[string]interface{}{"l1": rl, "l2": l}))
}
// NotLength 断言长度不是指定的值
//
// v 可以是以下类型:
// - map
// - string
// - slice
// - array
func (a *Assertion) NotLength(v interface{}, l int, msg ...interface{}) *Assertion {
a.TB().Helper()
rl, err := getLen(v)
if err != "" {
a.Assert(false, NewFailure("NotLength", msg, map[string]interface{}{"err": err}))
}
return a.Assert(rl != l, NewFailure("NotLength", msg, map[string]interface{}{"l": rl}))
}
func (a *Assertion) Greater(v interface{}, val float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("Greater", msg, nil))
}
return a.Assert(vv > val, NewFailure("Greater", msg, nil))
}
func (a *Assertion) Less(v interface{}, val float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("Less", msg, nil))
}
return a.Assert(vv < val, NewFailure("Less", msg, nil))
}
func (a *Assertion) GreaterEqual(v interface{}, val float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("GreaterEqual", msg, nil))
}
return a.Assert(vv >= val, NewFailure("GreaterEqual", msg, nil))
}
func (a *Assertion) LessEqual(v interface{}, val float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("LessEqual", msg, nil))
}
return a.Assert(vv <= val, NewFailure("LessEqual", msg, nil))
}
// Positive 断言 v 为正数
//
// NOTE: 不包含 0
func (a *Assertion) Positive(v interface{}, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("Positive", msg, nil))
}
return a.Assert(vv > 0, NewFailure("Positive", msg, nil))
}
// Negative 断言 v 为负数
//
// NOTE: 不包含 0
func (a *Assertion) Negative(v interface{}, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("Negative", msg, nil))
}
return a.Assert(vv < 0, NewFailure("Negative", msg, nil))
}
// Between 断言 v 是否存在于 (min,max) 之间
func (a *Assertion) Between(v interface{}, min, max float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("Between", msg, nil))
}
return a.Assert(vv > min && vv < max, NewFailure("Between", msg, nil))
}
// BetweenEqual 断言 v 是否存在于 [min,max] 之间
func (a *Assertion) BetweenEqual(v interface{}, min, max float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("BetweenEqual", msg, nil))
}
return a.Assert(vv >= min && vv <= max, NewFailure("BetweenEqual", msg, nil))
}
// BetweenEqualMin 断言 v 是否存在于 [min,max) 之间
func (a *Assertion) BetweenEqualMin(v interface{}, min, max float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("BetweenEqualMin", msg, nil))
}
return a.Assert(vv >= min && vv < max, NewFailure("BetweenEqualMin", msg, nil))
}
// BetweenEqualMax 断言 v 是否存在于 (min,max] 之间
func (a *Assertion) BetweenEqualMax(v interface{}, min, max float64, msg ...interface{}) *Assertion {
vv, ok := getNumber(v)
if !ok {
return a.Assert(false, NewFailure("BetweenEqualMax", msg, nil))
}
return a.Assert(vv > min && vv <= max, NewFailure("BetweenEqualMax", msg, nil))
}
// bool 表示是否成功转换
func getNumber(v interface{}) (float64, bool) {
switch val := v.(type) {
case int:
return float64(val), true
case int8:
return float64(val), true
case int16:
return float64(val), true
case int32:
return float64(val), true
case int64:
return float64(val), true
case uint:
return float64(val), true
case uint8:
return float64(val), true
case uint16:
return float64(val), true
case uint32:
return float64(val), true
case uint64:
return float64(val), true
case float32:
return float64(val), true
case float64:
return float64(val), true
}
return 0, false
}
func getLen(v interface{}) (l int, msg string) {
r := reflect.ValueOf(v)
for r.Kind() == reflect.Ptr {
r = r.Elem()
}
if v == nil {
return 0, ""
}
switch r.Kind() {
case reflect.Array, reflect.String, reflect.Slice, reflect.Map, reflect.Chan:
return r.Len(), ""
}
return 0, fmt.Sprintf("无法获取 %s 类型的长度信息", r.Kind())
}