Skip to content

Commit

Permalink
Set up testing, add execTime, exitCode, semver tests (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Feb 19, 2024
1 parent bfec874 commit 4bec9af
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ jobs:
with:
go-version: '1.21'
- run: test -z $(gofmt -l .)
- run: go test ./...
- run: go build ./cmd/gokart.go
18 changes: 18 additions & 0 deletions internal/execTime_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
23 changes: 23 additions & 0 deletions internal/exitCode_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
28 changes: 28 additions & 0 deletions internal/semver_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 4bec9af

Please sign in to comment.