Skip to content

Commit

Permalink
🐛 fix: fix unit tests error on upgrade goutil to 0.6.15
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 5, 2024
1 parent 292d31e commit 2d942e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
25 changes: 12 additions & 13 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var (
// Int convert string to int
func Int(in any) (int, error) { return ToInt(in) }

// MustInt convert string to int
// MustInt convert string to int, alias of the mathutil.SafeInt
func MustInt(in any) int {
return mathutil.MustInt(in)
return mathutil.SafeInt(in)
}

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

// MustInt64 convert value to int64
// MustInt64 convert value to int64, alias of the mathutil.SafeInt64
func MustInt64(in any) int64 {
return mathutil.MustInt64(in)
return mathutil.SafeInt64(in)
}

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

// MustUint convert string to uint
// MustUint convert string to uint, will ignore error
func MustUint(in any) uint {
return mathutil.MustUint(in)
return mathutil.SafeUint(in)
}

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

// MustUint64 convert string to uint64
// MustUint64 convert string to uint64, alias of the mathutil.SafeUint64
func MustUint64(in any) uint64 {
return mathutil.MustUint64(in)
return mathutil.SafeUint64(in)
}

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

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

// MustBool convert.
// MustBool convert, will ignore error.
func MustBool(s string) bool {
val, _ := Bool(Trim(s))
return val
Expand All @@ -103,10 +103,9 @@ func MustBool(s string) bool {
// String convert val to string
func String(val any) (string, error) { return ToString(val) }

// MustString convert value to string
// MustString convert value to string, will ignore error
func MustString(in any) string {
val, _ := ToString(in)
return val
return strutil.SafeString(in)
}

// ToString convert value to string
Expand Down
64 changes: 40 additions & 24 deletions converters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,51 @@ func TestValToInt(t *testing.T) {
is.Eq(2, MustInt(in))
}

// To int64
i64Val, err := ToInt64("2")
is.Nil(err)
is.Eq(int64(2), i64Val)

i64Val, err = Int64("-2")
is.Nil(err)
is.Eq(int64(-2), i64Val)

is.Eq(int64(0), MustInt64("2a"))
is.Eq(int64(0), MustInt64(nil))
for _, in := range tests {
is.Eq(int64(2), MustInt64(in))
}

// To uint
uintVal, err := Uint("2")
is.Nil(err)
is.Eq(uint64(2), uintVal)
is.Eq(uint(2), uintVal)

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

is.Eq(uint64(0), MustUint("-2"))
is.Eq(uint64(0), MustUint("2a"))
is.Eq(uint64(0), MustUint(nil))
is.Eq(uint(0), MustUint("-2"))
is.Eq(uint(0), MustUint("2a"))
is.Eq(uint(0), MustUint(nil))
for _, in := range tests {
is.Eq(uint64(2), MustUint(in))
is.Eq(uint(2), MustUint(in))
}

// To int64
i64Val, err := ToInt64("2")
// To uint64
u64, err := Uint64("2")
is.Nil(err)
is.Eq(int64(2), i64Val)
is.Eq(uint64(2), u64)

i64Val, err = Int64("-2")
is.Nil(err)
is.Eq(int64(-2), i64Val)
_, err = ToUint64("-2")
is.Err(err)

is.Eq(int64(0), MustInt64("2a"))
is.Eq(int64(0), MustInt64(nil))
is.Eq(uint64(0), MustUint64("-2"))
is.Eq(uint64(0), MustUint64("2a"))
is.Eq(uint64(0), MustUint64(nil))
for _, in := range tests {
is.Eq(int64(2), MustInt64(in))
is.Eq(uint64(2), MustUint64(in))
}

}

func TestValToStr(t *testing.T) {
Expand Down Expand Up @@ -277,16 +293,16 @@ func TestEscape(t *testing.T) {
func TestStrToTime(t *testing.T) {
is := assert.New(t)
tests := map[string]string{
"20180927": "2018-09-27 00:00:00 +0000 UTC",
"2018-09-27": "2018-09-27 00:00:00 +0000 UTC",
"2018-09-27 12": "2018-09-27 12:00:00 +0000 UTC",
"2018-09-27T12": "2018-09-27 12:00:00 +0000 UTC",
"2018-09-27 12:34": "2018-09-27 12:34:00 +0000 UTC",
"2018-09-27T12:34": "2018-09-27 12:34:00 +0000 UTC",
"2018-09-27 12:34:45": "2018-09-27 12:34:45 +0000 UTC",
"2018-09-27T12:34:45": "2018-09-27 12:34:45 +0000 UTC",
"2018/09/27 12:34:45": "2018-09-27 12:34:45 +0000 UTC",
"2018/09/27T12:34:45Z": "2018-09-27 12:34:45 +0000 UTC",
"20180927": "2018-09-27 00:00:00 +0800 CST",
"2018-09-27": "2018-09-27 00:00:00 +0800 CST",
"2018-09-27 12": "2018-09-27 12:00:00 +0800 CST",
"2018-09-27T12": "2018-09-27 12:00:00 +0800 CST",
"2018-09-27 12:34": "2018-09-27 12:34:00 +0800 CST",
"2018-09-27T12:34": "2018-09-27 12:34:00 +0800 CST",
"2018-09-27 12:34:45": "2018-09-27 12:34:45 +0800 CST",
"2018-09-27T12:34:45": "2018-09-27 12:34:45 +0800 CST",
"2018/09/27 12:34:45": "2018-09-27 12:34:45 +0800 CST",
"2018/09/27T12:34:45Z": "2018-09-27 12:34:45 +0800 CST",
}

for sample, want := range tests {
Expand Down
2 changes: 1 addition & 1 deletion filtration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestFiltration_Filtering(t *testing.T) {

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

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

0 comments on commit 2d942e7

Please sign in to comment.