From 4bec9af60fc2e0777c3d30df46ca5eed02a3a17a Mon Sep 17 00:00:00 2001 From: Luca Scalzotto Date: Mon, 19 Feb 2024 16:37:05 +0100 Subject: [PATCH] Set up testing, add execTime, exitCode, semver tests (#27) --- .github/workflows/build.yml | 1 + internal/execTime_test.go | 18 ++++++++++++++++++ internal/exitCode_test.go | 23 +++++++++++++++++++++++ internal/semver_test.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 internal/execTime_test.go create mode 100644 internal/exitCode_test.go create mode 100644 internal/semver_test.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2a289a0..b34782c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,4 +13,5 @@ jobs: with: go-version: '1.21' - run: test -z $(gofmt -l .) + - run: go test ./... - run: go build ./cmd/gokart.go diff --git a/internal/execTime_test.go b/internal/execTime_test.go new file mode 100644 index 0000000..a2ef9a6 --- /dev/null +++ b/internal/execTime_test.go @@ -0,0 +1,18 @@ +package internal + +import "testing" + +func TestFormatMinutesSeconds(t *testing.T) { + cases := map[float64]string{ + 3.14: "3.1s", + 60: "1m 0.0s", + 3712.5: "61m 52.5s", + } + + for in, expected := range cases { + actual := formatMinutesSeconds(in) + if actual != expected { + t.Errorf("formatMinutesSeconds(%f) = %s, expected %s", in, actual, expected) + } + } +} diff --git a/internal/exitCode_test.go b/internal/exitCode_test.go new file mode 100644 index 0000000..04bf698 --- /dev/null +++ b/internal/exitCode_test.go @@ -0,0 +1,23 @@ +package internal + +import ( + "gokart-prompt/internal/ansi" + "os" + "testing" +) + +func TestExitCode(t *testing.T) { + cases := map[string]string{ + "0": ansi.Color(ansi.Green, "➜ "), + "1": ansi.Color(ansi.Red, "1 ➜ "), + "130": ansi.Color(ansi.Red, "130 ➜ "), + } + + for exitCode, expected := range cases { + os.Setenv("EXIT_CODE", exitCode) + actual := ExitCode() + if actual != expected { + t.Errorf("EXIT_CODE=%s ExitCode() = %s, expected %s", exitCode, actual, expected) + } + } +} diff --git a/internal/semver_test.go b/internal/semver_test.go new file mode 100644 index 0000000..1cb5503 --- /dev/null +++ b/internal/semver_test.go @@ -0,0 +1,28 @@ +package internal + +import "testing" + +func TestSemVerMatches(t *testing.T) { + cases := [][]any{ + {"1.2.3", "1.2.3", true}, + {"1.2.3", "1.2", true}, + {"1.2.3", "1", true}, + {"invalid", "2.0.0", false}, + {"2.0.0", "invalid", false}, + {"", "3.0.0", false}, + {"4.0.0", "v4", true}, + {"v4.0.0", "4", true}, + {" 5.0.0 ", "5.0.0", true}, + {"5.0.0 ", " 5.0.0 ", true}, + } + + for _, c := range cases { + a := c[0].(string) + b := c[1].(string) + expected := c[2].(bool) + actual := SemVerMatches(a, b) + if actual != expected { + t.Errorf("SemVerMatches(%s, %s) = %t, expected %t", a, b, actual, expected) + } + } +}