enbas/internal/flag/boolptrvalue_test.go
Dan Anglin 0ad02e0af4
All checks were successful
Tests / test (pull_request) Successful in 18s
REUSE Compliance Check / check (push) Successful in 5s
feat(BREAKING): new parser for the TDV
This commit adds a new parser for the internal time duration flag value
(TimeDurationValue). Previously this used the parser from the time
package from the standard library but this was limited to parsing units
of time up to hours.

The new parser allows users to specify duration in days, hours, minutes,
seconds and a combination of the above. It is quite flexible in the way
users format their string input.

Additonal changes:

- Added unit tests for the command-line parsing of the
  TimeDurationValue type.
- Updated the unit tests for the BoolPtrValue type.
- Updated documentation.

PR: #55
2024-08-20 03:32:54 +01:00

100 lines
2 KiB
Go

package flag_test
import (
"flag"
"slices"
"testing"
internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
)
func TestBoolPtrValue(t *testing.T) {
tests := []struct {
input string
want string
}{
{
input: "True",
want: "true",
},
{
input: "true",
want: "true",
},
{
input: "1",
want: "true",
},
{
input: "False",
want: "false",
},
{
input: "false",
want: "false",
},
{
input: "0",
want: "false",
},
}
for _, test := range slices.All(tests) {
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 := boolVal.String()
if got != want {
t.Errorf(
"Unexpected boolean value found after parsing BoolPtrValue: want %s, got %s",
want,
got,
)
} else {
t.Logf(
"Expected boolean value found after parsing BoolPtrValue: got %s",
got,
)
}
}
}
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 := boolVal.String()
if got != want {
t.Errorf("Unexpected string returned from the nil value; want %s, got %s", want, got)
} else {
t.Logf("Expected string returned from the nil value; got %s", got)
}
}