Skip to content

Commit 2d942e7

Browse files
committed
🐛 fix: fix unit tests error on upgrade goutil to 0.6.15
1 parent 292d31e commit 2d942e7

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

converters.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ var (
3333
// Int convert string to int
3434
func Int(in any) (int, error) { return ToInt(in) }
3535

36-
// MustInt convert string to int
36+
// MustInt convert string to int, alias of the mathutil.SafeInt
3737
func MustInt(in any) int {
38-
return mathutil.MustInt(in)
38+
return mathutil.SafeInt(in)
3939
}
4040

4141
// ToInt convert string to int
@@ -47,9 +47,9 @@ func Int64(in any) (int64, error) { return ToInt64(in) }
4747
// ToInt64 convert value to int64
4848
func ToInt64(val any) (int64, error) { return mathutil.ToInt64(val) }
4949

50-
// MustInt64 convert value to int64
50+
// MustInt64 convert value to int64, alias of the mathutil.SafeInt64
5151
func MustInt64(in any) int64 {
52-
return mathutil.MustInt64(in)
52+
return mathutil.SafeInt64(in)
5353
}
5454

5555
// Uint convert string to uint
@@ -58,9 +58,9 @@ func Uint(in any) (uint, error) { return ToUint(in) }
5858
// ToUint convert string to uint
5959
func ToUint(in any) (uint, error) { return mathutil.ToUint(in) }
6060

61-
// MustUint convert string to uint
61+
// MustUint convert string to uint, will ignore error
6262
func MustUint(in any) uint {
63-
return mathutil.MustUint(in)
63+
return mathutil.SafeUint(in)
6464
}
6565

6666
// Uint64 convert string to uint64
@@ -69,9 +69,9 @@ func Uint64(in any) (uint64, error) { return ToUint64(in) }
6969
// ToUint64 convert string to uint64
7070
func ToUint64(in any) (uint64, error) { return mathutil.ToUint64(in) }
7171

72-
// MustUint64 convert string to uint64
72+
// MustUint64 convert string to uint64, alias of the mathutil.SafeUint64
7373
func MustUint64(in any) uint64 {
74-
return mathutil.MustUint64(in)
74+
return mathutil.SafeUint64(in)
7575
}
7676

7777
// Float convert string to float
@@ -82,7 +82,7 @@ func ToFloat(s string) (float64, error) {
8282
return strconv.ParseFloat(Trim(s), 0)
8383
}
8484

85-
// MustFloat convert string to float
85+
// MustFloat convert string to float, will ignore error
8686
func MustFloat(s string) float64 {
8787
val, _ := strconv.ParseFloat(Trim(s), 0)
8888
return val
@@ -94,7 +94,7 @@ func ToBool(s string) (bool, error) { return Bool(s) }
9494
// Bool parse string to bool
9595
func Bool(s string) (bool, error) { return strutil.ToBool(s) }
9696

97-
// MustBool convert.
97+
// MustBool convert, will ignore error.
9898
func MustBool(s string) bool {
9999
val, _ := Bool(Trim(s))
100100
return val
@@ -103,10 +103,9 @@ func MustBool(s string) bool {
103103
// String convert val to string
104104
func String(val any) (string, error) { return ToString(val) }
105105

106-
// MustString convert value to string
106+
// MustString convert value to string, will ignore error
107107
func MustString(in any) string {
108-
val, _ := ToString(in)
109-
return val
108+
return strutil.SafeString(in)
110109
}
111110

112111
// ToString convert value to string

converters_test.go

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,51 @@ func TestValToInt(t *testing.T) {
3535
is.Eq(2, MustInt(in))
3636
}
3737

38+
// To int64
39+
i64Val, err := ToInt64("2")
40+
is.Nil(err)
41+
is.Eq(int64(2), i64Val)
42+
43+
i64Val, err = Int64("-2")
44+
is.Nil(err)
45+
is.Eq(int64(-2), i64Val)
46+
47+
is.Eq(int64(0), MustInt64("2a"))
48+
is.Eq(int64(0), MustInt64(nil))
49+
for _, in := range tests {
50+
is.Eq(int64(2), MustInt64(in))
51+
}
52+
3853
// To uint
3954
uintVal, err := Uint("2")
4055
is.Nil(err)
41-
is.Eq(uint64(2), uintVal)
56+
is.Eq(uint(2), uintVal)
4257

4358
_, err = ToUint("-2")
4459
is.Err(err)
4560

46-
is.Eq(uint64(0), MustUint("-2"))
47-
is.Eq(uint64(0), MustUint("2a"))
48-
is.Eq(uint64(0), MustUint(nil))
61+
is.Eq(uint(0), MustUint("-2"))
62+
is.Eq(uint(0), MustUint("2a"))
63+
is.Eq(uint(0), MustUint(nil))
4964
for _, in := range tests {
50-
is.Eq(uint64(2), MustUint(in))
65+
is.Eq(uint(2), MustUint(in))
5166
}
5267

53-
// To int64
54-
i64Val, err := ToInt64("2")
68+
// To uint64
69+
u64, err := Uint64("2")
5570
is.Nil(err)
56-
is.Eq(int64(2), i64Val)
71+
is.Eq(uint64(2), u64)
5772

58-
i64Val, err = Int64("-2")
59-
is.Nil(err)
60-
is.Eq(int64(-2), i64Val)
73+
_, err = ToUint64("-2")
74+
is.Err(err)
6175

62-
is.Eq(int64(0), MustInt64("2a"))
63-
is.Eq(int64(0), MustInt64(nil))
76+
is.Eq(uint64(0), MustUint64("-2"))
77+
is.Eq(uint64(0), MustUint64("2a"))
78+
is.Eq(uint64(0), MustUint64(nil))
6479
for _, in := range tests {
65-
is.Eq(int64(2), MustInt64(in))
80+
is.Eq(uint64(2), MustUint64(in))
6681
}
82+
6783
}
6884

6985
func TestValToStr(t *testing.T) {
@@ -277,16 +293,16 @@ func TestEscape(t *testing.T) {
277293
func TestStrToTime(t *testing.T) {
278294
is := assert.New(t)
279295
tests := map[string]string{
280-
"20180927": "2018-09-27 00:00:00 +0000 UTC",
281-
"2018-09-27": "2018-09-27 00:00:00 +0000 UTC",
282-
"2018-09-27 12": "2018-09-27 12:00:00 +0000 UTC",
283-
"2018-09-27T12": "2018-09-27 12:00:00 +0000 UTC",
284-
"2018-09-27 12:34": "2018-09-27 12:34:00 +0000 UTC",
285-
"2018-09-27T12:34": "2018-09-27 12:34:00 +0000 UTC",
286-
"2018-09-27 12:34:45": "2018-09-27 12:34:45 +0000 UTC",
287-
"2018-09-27T12:34:45": "2018-09-27 12:34:45 +0000 UTC",
288-
"2018/09/27 12:34:45": "2018-09-27 12:34:45 +0000 UTC",
289-
"2018/09/27T12:34:45Z": "2018-09-27 12:34:45 +0000 UTC",
296+
"20180927": "2018-09-27 00:00:00 +0800 CST",
297+
"2018-09-27": "2018-09-27 00:00:00 +0800 CST",
298+
"2018-09-27 12": "2018-09-27 12:00:00 +0800 CST",
299+
"2018-09-27T12": "2018-09-27 12:00:00 +0800 CST",
300+
"2018-09-27 12:34": "2018-09-27 12:34:00 +0800 CST",
301+
"2018-09-27T12:34": "2018-09-27 12:34:00 +0800 CST",
302+
"2018-09-27 12:34:45": "2018-09-27 12:34:45 +0800 CST",
303+
"2018-09-27T12:34:45": "2018-09-27 12:34:45 +0800 CST",
304+
"2018/09/27 12:34:45": "2018-09-27 12:34:45 +0800 CST",
305+
"2018/09/27T12:34:45Z": "2018-09-27 12:34:45 +0800 CST",
290306
}
291307

292308
for sample, want := range tests {

filtration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func TestFiltration_Filtering(t *testing.T) {
242242

243243
sTime, ok := f.Safe("sDate")
244244
is.True(ok)
245-
is.Eq("2018-10-16 12:34:00 +0000 UTC", fmt.Sprintf("%v", sTime))
245+
is.Eq("2018-10-16 12:34:00 +0800 CST", fmt.Sprintf("%v", sTime))
246246

247247
data["url"] = "a.com?p=1"
248248
f = New(data)

0 commit comments

Comments
 (0)