From 39e85978cb9fafe976fc964d37e8d9f98ea005e5 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Mon, 12 Aug 2024 07:22:57 +0200 Subject: [PATCH] refactor: improve test coverage --- internal/parse/parse.go | 9 +++++++-- internal/parse/parse_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/parse/parse.go b/internal/parse/parse.go index c6fbc049..7b0d4844 100644 --- a/internal/parse/parse.go +++ b/internal/parse/parse.go @@ -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: @@ -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) { @@ -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 } diff --git a/internal/parse/parse_test.go b/internal/parse/parse_test.go index f4b341b8..cbf0a1d7 100644 --- a/internal/parse/parse_test.go +++ b/internal/parse/parse_test.go @@ -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) { @@ -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) {