Skip to content

Commit

Permalink
🐛 fix: conv - fix some to unit convert tests error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 8, 2023
1 parent e06088b commit 58c5080
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 13 additions & 3 deletions conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,21 @@ func TestToInt(t *testing.T) {
is.Nil(err)
is.Eq(int64(-2), i64Val)

// To uint
uVal, err := goutil.ToUint("2")
is.Nil(err)
is.Eq(uint(2), uVal)

uVal = goutil.Uint("2")
is.Nil(err)
is.Eq(uint(2), uVal)

// To uint64
u64Val, err := goutil.ToUint("2")
u64Val, err := goutil.ToUint64("2")
is.Nil(err)
is.Eq(uint64(2), u64Val)

u64Val = goutil.Uint("2")
u64Val = goutil.Uint64("2")
is.Nil(err)
is.Eq(uint64(2), u64Val)
}
Expand Down Expand Up @@ -104,8 +113,9 @@ func TestConvTo(t *testing.T) {
{"23", reflect.Float32, float32(23), true},
{"23", reflect.String, "23", true},
{"true", reflect.Bool, true, true},
{nil, reflect.Int, 0, true},
{nil, reflect.Uint, uint(0), true},
// failed
{nil, reflect.Int, nil, false},
{"23", reflect.Bool, nil, false},
{"abc", reflect.Float64, nil, false},
{"abc", reflect.Float32, nil, false},
Expand Down
8 changes: 4 additions & 4 deletions mathutil/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestToInt(t *testing.T) {
is.Eq(uint(2), uintVal)

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

Expand Down Expand Up @@ -209,7 +209,7 @@ func TestToInt(t *testing.T) {
is.Eq(uint64(2), uintVal)

_, err = mathutil.ToUint64(nil)
is.Err(err)
is.NoErr(err)
_, err = mathutil.ToUint64("-2")
is.Err(err)

Expand Down Expand Up @@ -299,11 +299,11 @@ func TestToString(t *testing.T) {
is.Eq("2", val)

_, err = mathutil.ToString(nil)
is.Err(err)
is.NoErr(err)

is.Eq("", mathutil.SafeString(nil))
is.Eq("[1]", mathutil.QuietString([]int{1}))
is.Eq("23", mathutil.StringOrDefault(nil, "23"))
is.Eq("23", mathutil.StringOrDefault([]int{1}, "23"))
is.Eq("23", mathutil.StringOr("23", "2"))

is.Panics(func() {
Expand Down

0 comments on commit 58c5080

Please sign in to comment.