Skip to content

Commit

Permalink
test: added tests for utils convertToInt
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijamt committed Feb 21, 2024
1 parent d2194bf commit 23566cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ func convertToInt(num any) (int, error) {
switch val := num.(type) {
case int:
return val, nil
case int8:
return int(val), nil
case int16:
return int(val), nil
case int32:
return int(val), nil
case int64:
return int(val), nil
case float32:
return int(val), nil
case float64:
return int(val), nil
}
Expand Down
33 changes: 33 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gitlab

import (
"testing"

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

func TestConvertToInt(t *testing.T) {
var tests = []struct {
in any
outVal int
outErr error
}{
{int(52), int(52), nil},
{int8(13), int(13), nil},
{int16(612), int(612), nil},
{int32(56236), int(56236), nil},
{int64(23462346), int(23462346), nil},
{float32(62346.62), int(62346), nil},
{float64(263467.26), int(263467), nil},
{"1", int(0), ErrInvalidValue},
}

for _, tst := range tests {
t.Logf("convertToInt(%T(%v))", tst.in, tst.in)
val, err := convertToInt(tst.in)
assert.Equal(t, tst.outVal, val)
if tst.outErr != nil {
assert.ErrorIs(t, err, tst.outErr)
}
}
}

0 comments on commit 23566cb

Please sign in to comment.