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