Skip to content

Commit

Permalink
refactor: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Aug 12, 2024
1 parent bdb805f commit 39e8597
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"strconv"
)

const (
InvalidType = "invalid type"
Negative = "negative value"
)

func Int(input interface{}) (int, error) {
switch input := input.(type) {
case float64:
Expand All @@ -16,7 +21,7 @@ func Int(input interface{}) (int, error) {
}
return int(mem), nil
}
return 0, nil
return 0, errors.New(InvalidType)
}

func Uint(input interface{}) (uint, error) {
Expand All @@ -25,7 +30,7 @@ func Uint(input interface{}) (uint, error) {
return 0, err
}
if tmpInt < 0 {
return 0, errors.New("negative value")
return 0, errors.New(Negative)
}
return uint(tmpInt), nil
}
6 changes: 6 additions & 0 deletions internal/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func Test_Int(t *testing.T) {
{name: `string positive`,
input: "1",
output: 1},
{name: `invalid type`,
input: interface{}(nil),
err: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -64,6 +67,9 @@ func Test_Uint(t *testing.T) {
{name: `string positive`,
input: "1",
output: 1},
{name: `invalid type`,
input: interface{}(nil),
err: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit 39e8597

Please sign in to comment.