From 3de937c7359e77250ee1045df43b219b3d7b2ee2 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Fri, 6 Sep 2024 23:07:50 +0200 Subject: [PATCH] test: add more tests --- conversion_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/conversion_test.go b/conversion_test.go index d6deb66..2930293 100644 --- a/conversion_test.go +++ b/conversion_test.go @@ -233,6 +233,10 @@ func TestToUint8(t *testing.T) { {name: "zero", input: 0, want: 0}, {name: "positive within range", input: 100, want: 100}, }) + + assertUint8Error(t, []caseUint8[int8]{ + {name: "negative value", input: -1}, + }) }) t.Run("from int16", func(t *testing.T) { @@ -240,6 +244,11 @@ func TestToUint8(t *testing.T) { {name: "zero", input: 0, want: 0}, {name: "positive within range", input: 100, want: 100}, }) + + assertUint8Error(t, []caseUint8[int16]{ + {name: "positive out of range", input: 10000}, + {name: "negative value", input: -1}, + }) }) t.Run("from int32", func(t *testing.T) { @@ -983,12 +992,28 @@ func assertUint64OK[in safecast.Number](t *testing.T, tests []caseUint64[in]) { } } +func assertUint64Error[in safecast.Number](t *testing.T, tests []caseUint64[in]) { + t.Helper() + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := safecast.ToUint64(tt.input) + requireError(t, err) + assertEqual(t, tt.want, got) + }) + } +} + func TestToUint64(t *testing.T) { t.Run("from int", func(t *testing.T) { assertUint64OK(t, []caseUint64[int]{ {name: "zero", input: 0, want: 0}, {name: "positive within range", input: 10000, want: 10000}, }) + + assertUint64Error(t, []caseUint64[int]{ + {name: "negative value", input: -1}, + }) }) t.Run("from int8", func(t *testing.T) { @@ -996,6 +1021,10 @@ func TestToUint64(t *testing.T) { {name: "zero", input: 0, want: 0}, {name: "positive within range", input: 100, want: 100}, }) + + assertUint64Error(t, []caseUint64[int]{ + {name: "negative value", input: -1}, + }) }) t.Run("from int16", func(t *testing.T) { @@ -1003,6 +1032,10 @@ func TestToUint64(t *testing.T) { {name: "zero", input: 0, want: 0}, {name: "positive within range", input: 10000, want: 10000}, }) + + assertUint64Error(t, []caseUint64[int]{ + {name: "negative value", input: -1}, + }) }) t.Run("from int32", func(t *testing.T) { @@ -1010,6 +1043,10 @@ func TestToUint64(t *testing.T) { {name: "zero", input: 0, want: 0}, {name: "positive within range", input: 10000, want: 10000}, }) + + assertUint64Error(t, []caseUint64[int]{ + {name: "negative value", input: -1}, + }) }) t.Run("from int64", func(t *testing.T) { @@ -1017,6 +1054,10 @@ func TestToUint64(t *testing.T) { {name: "zero", input: 0, want: 0}, {name: "positive within range", input: 10000, want: 10000}, }) + + assertUint64Error(t, []caseUint64[int]{ + {name: "negative value", input: -1}, + }) }) t.Run("from uint", func(t *testing.T) {