Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more tests #13

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading