update flag unit tests
All checks were successful
Tests / test (pull_request) Successful in 21s

This commit is contained in:
Dan Anglin 2024-08-20 03:19:40 +01:00
parent 22c654c9b1
commit e555e0cb7d
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 81 additions and 40 deletions

View file

@ -1,6 +1,7 @@
package flag_test
import (
"flag"
"slices"
"testing"
@ -10,52 +11,86 @@ import (
func TestBoolPtrValue(t *testing.T) {
tests := []struct {
input string
want bool
want string
}{
{
input: "True",
want: true,
want: "true",
},
{
input: "true",
want: "true",
},
{
input: "1",
want: "true",
},
{
input: "False",
want: "false",
},
{
input: "false",
want: false,
want: "false",
},
{
input: "0",
want: "false",
},
}
value := internalFlag.NewBoolPtrValue()
for _, test := range slices.All(tests) {
if err := value.Set(test.input); err != nil {
t.Fatalf(
"Unable to parse %s as a BoolPtrValue: %v",
test.input,
err,
)
args := []string{"--boolean-value=" + test.input}
t.Run("Flag parsing test: "+test.input, testBoolPtrValueParsing(args, test.want))
}
}
func testBoolPtrValueParsing(args []string, want string) func(t *testing.T) {
return func(t *testing.T) {
flagset := flag.NewFlagSet("test", flag.ExitOnError)
boolVal := internalFlag.NewBoolPtrValue()
flagset.Var(&boolVal, "boolean-value", "Boolean value")
if err := flagset.Parse(args); err != nil {
t.Fatalf("Received an error parsing the flag: %v", err)
}
got := *value.Value
got := boolVal.String()
if got != test.want {
if got != want {
t.Errorf(
"Unexpected bool parsed from %s: want %t, got %t",
test.input,
test.want,
"Unexpected boolean value found after parsing BoolPtrValue: want %s, got %s",
want,
got,
)
} else {
t.Logf(
"Expected bool parsed from %s: got %t",
test.input,
"Expected boolean value found after parsing BoolPtrValue: got %s",
got,
)
}
}
}
func TestNilBoolPtrValue(t *testing.T) {
value := internalFlag.NewBoolPtrValue()
func TestNotSetBoolPtrValue(t *testing.T) {
flagset := flag.NewFlagSet("test", flag.ExitOnError)
boolVal := internalFlag.NewBoolPtrValue()
var otherVal string
flagset.Var(&boolVal, "boolean-value", "Boolean value")
flagset.StringVar(&otherVal, "other-value", "", "Another value")
args := []string{"--other-value", "other-value"}
if err := flagset.Parse(args); err != nil {
t.Fatalf("Received an error parsing the flag: %v", err)
}
want := "NOT SET"
got := value.String()
got := boolVal.String()
if got != want {
t.Errorf("Unexpected string returned from the nil value; want %s, got %s", want, got)

View file

@ -1,6 +1,7 @@
package flag_test
import (
"flag"
"slices"
"testing"
@ -13,47 +14,52 @@ func TestTimeDurationValue(t *testing.T) {
want string
}{
{
input: "1 day",
input: `"1 day"`,
want: "24h0m0s",
},
{
input: "3 days, 5 hours, 39 minutes and 6 seconds",
input: `"3 days, 5 hours, 39 minutes and 6 seconds"`,
want: "77h39m6s",
},
{
input: "1 minute and 30 seconds",
input: `"1 minute and 30 seconds"`,
want: "1m30s",
},
{
input: "(7 seconds) (21 hours) (41 days)",
input: `"(7 seconds) (21 hours) (41 days)"`,
want: "1005h0m7s",
},
}
value := internalFlag.NewTimeDurationValue()
for _, test := range slices.All(parsingTests) {
if err := value.Set(test.input); err != nil {
t.Fatalf(
"Unable to parse %s into a TimeDurationValue: %v",
test.input,
err,
)
args := []string{"--duration", test.input}
t.Run("Flag parsing test: "+test.input, testTimeDurationValueParsing(args, test.want))
}
}
func testTimeDurationValueParsing(args []string, want string) func(t *testing.T) {
return func(t *testing.T) {
flagset := flag.NewFlagSet("test", flag.ExitOnError)
duration := internalFlag.NewTimeDurationValue()
flagset.Var(&duration, "duration", "Duration value")
if err := flagset.Parse(args); err != nil {
t.Fatalf("Received an error parsing the flag: %v", err)
}
got := value.String()
got := duration.String()
if got != test.want {
if got != want {
t.Errorf(
"Unexpected duration parsed from %s: want %s, got %s",
test.input,
test.want,
"Unexpected duration parsed from the flag: want %s, got %s",
want,
got,
)
} else {
t.Logf(
"Expected duration parsed from %s: got %s",
test.input,
"Expected duration parsed from the flag: got %s",
got,
)
}