Skip to content

Commit

Permalink
propose change
Browse files Browse the repository at this point in the history
  • Loading branch information
juandspy authored Oct 4, 2024
1 parent 6092b48 commit 652501f
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions types/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,35 @@ package types

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUint64ToUint32(t *testing.T) {
tests := []struct {
name string
input uint64
want uint32
errType error
wantErr bool
}{
{"Zero", 0, 0, nil},
{"Max uint32", 4294967295, 4294967295, nil},
{"Overflow", 4294967296, 0, &OutOfRangeError{}},
{"Large overflow", 18446744073709551615, 0, &OutOfRangeError{}},
{"Mid-range value", 2147483648, 2147483648, nil},
{"Zero", 0, 0, false},
{"Max uint32", 4294967295, 4294967295, false},
{"Overflow", 4294967296, 0, true},
{"Large overflow", 18446744073709551615, 0, true},
{"Mid-range value", 2147483648, 2147483648, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Uint64ToUint32(tt.input)
if tt.errType != nil {
if tt.wantErr {
// Check if the error is of the expected type
if err == nil {
t.Errorf("Expected error type %T, got no error", tt.errType)
} else if _, ok := err.(*OutOfRangeError); !ok {
t.Errorf("Expected error type *OutOfRangeError, got %T", err)
}
require.Error(t, err)
assert.Equal(t, err, &OutOfRangeError{tt.input, "uint32"})
} else {
if err != nil {
t.Errorf("Uint64ToUint32() error = %v, want no error", err)
return
}
if got != tt.want {
t.Errorf("Uint64ToUint32() = %v, want %v", got, tt.want)
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
Expand Down

0 comments on commit 652501f

Please sign in to comment.