Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Sep 6, 2024
1 parent 5eb6dea commit f7927b7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,22 @@ 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) {
assertUint8OK(t, []caseUint8[int16]{
{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) {
Expand Down Expand Up @@ -983,40 +992,72 @@ 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) {
assertUint64OK(t, []caseUint64[int8]{
{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) {
assertUint64OK(t, []caseUint64[int16]{
{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) {
assertUint64OK(t, []caseUint64[int32]{
{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) {
assertUint64OK(t, []caseUint64[int64]{
{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) {
Expand Down

0 comments on commit f7927b7

Please sign in to comment.