test: add more tests for the internal flag package
All checks were successful
REUSE Compliance Check / check (push) Successful in 6s

This commit is contained in:
Dan Anglin 2024-08-20 04:25:29 +01:00
parent 0ad02e0af4
commit 74f32aab53
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 113 additions and 0 deletions

View file

@ -0,0 +1,56 @@
package flag_test
import (
"flag"
"testing"
internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
)
func TestIntSliceValue(t *testing.T) {
flagset := flag.NewFlagSet("test", flag.ExitOnError)
intSliceVal := internalFlag.NewIntSliceValue()
if !intSliceVal.Empty() {
t.Fatalf("The initialised IntSliceValue is not empty")
}
flagset.Var(&intSliceVal, "int-value", "Integer value")
args := []string{
"--int-value", "0",
"--int-value", "1",
"--int-value", "2",
"--int-value", "3",
"--int-value", "4",
}
if err := flagset.Parse(args); err != nil {
t.Fatalf("Received an error parsing the flag: %v", err)
}
wantLength := 5
if !intSliceVal.ExpectedLength(wantLength) {
t.Fatalf(
"Error: intSliceVal.ExpectedLength(%d) == false: actual length is %d",
wantLength,
len(intSliceVal),
)
}
want := "0, 1, 2, 3, 4"
got := intSliceVal.String()
if got != want {
t.Errorf(
"Unexpected result after parsing IntSliceValue: want %s, got %s",
want,
got,
)
} else {
t.Logf(
"Expected result after parsing IntSliceValue: got %s",
got,
)
}
}

View file

@ -0,0 +1,57 @@
package flag_test
import (
"flag"
"testing"
internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
)
func TestStringSliceValue(t *testing.T) {
flagset := flag.NewFlagSet("test", flag.ExitOnError)
stringSliceVal := internalFlag.NewStringSliceValue()
if !stringSliceVal.Empty() {
t.Fatalf("The initialised StringSliceValue is not empty")
}
flagset.Var(&stringSliceVal, "colour", "String value")
args := []string{
"--colour", "orange",
"--colour", "blue",
"--colour", "magenta",
"--colour", "red",
"--colour", "green",
"--colour", "silver",
}
if err := flagset.Parse(args); err != nil {
t.Fatalf("Received an error parsing the flag: %v", err)
}
wantLength := 6
if !stringSliceVal.ExpectedLength(wantLength) {
t.Fatalf(
"Error: intSliceVal.ExpectedLength(%d) == false: actual length is %d",
wantLength,
len(stringSliceVal),
)
}
want := "orange, blue, magenta, red, green, silver"
got := stringSliceVal.String()
if got != want {
t.Errorf(
"Unexpected result after parsing StringSliceValue: want %s, got %s",
want,
got,
)
} else {
t.Logf(
"Expected result after parsing StringSliceValue: got %s",
got,
)
}
}