From e555e0cb7d2af13c95fa85c095b7c4ed890c4034 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 20 Aug 2024 03:19:40 +0100 Subject: [PATCH] update flag unit tests --- internal/flag/boolptrvalue_test.go | 77 ++++++++++++++++++------- internal/flag/timedurationvalue_test.go | 44 ++++++++------ 2 files changed, 81 insertions(+), 40 deletions(-) diff --git a/internal/flag/boolptrvalue_test.go b/internal/flag/boolptrvalue_test.go index d136f55..30bef18 100644 --- a/internal/flag/boolptrvalue_test.go +++ b/internal/flag/boolptrvalue_test.go @@ -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) diff --git a/internal/flag/timedurationvalue_test.go b/internal/flag/timedurationvalue_test.go index 81fca3e..95103b8 100644 --- a/internal/flag/timedurationvalue_test.go +++ b/internal/flag/timedurationvalue_test.go @@ -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, ) }