This commit is contained in:
parent
22c654c9b1
commit
e555e0cb7d
2 changed files with 81 additions and 40 deletions
|
@ -1,6 +1,7 @@
|
||||||
package flag_test
|
package flag_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -10,52 +11,86 @@ import (
|
||||||
func TestBoolPtrValue(t *testing.T) {
|
func TestBoolPtrValue(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
want bool
|
want string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
input: "True",
|
input: "True",
|
||||||
want: true,
|
want: "true",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "true",
|
||||||
|
want: "true",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "1",
|
||||||
|
want: "true",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "False",
|
||||||
|
want: "false",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "false",
|
input: "false",
|
||||||
want: false,
|
want: "false",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "0",
|
||||||
|
want: "false",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
value := internalFlag.NewBoolPtrValue()
|
|
||||||
|
|
||||||
for _, test := range slices.All(tests) {
|
for _, test := range slices.All(tests) {
|
||||||
if err := value.Set(test.input); err != nil {
|
args := []string{"--boolean-value=" + test.input}
|
||||||
t.Fatalf(
|
|
||||||
"Unable to parse %s as a BoolPtrValue: %v",
|
t.Run("Flag parsing test: "+test.input, testBoolPtrValueParsing(args, test.want))
|
||||||
test.input,
|
}
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
got := *value.Value
|
func testBoolPtrValueParsing(args []string, want string) func(t *testing.T) {
|
||||||
|
return func(t *testing.T) {
|
||||||
|
flagset := flag.NewFlagSet("test", flag.ExitOnError)
|
||||||
|
boolVal := internalFlag.NewBoolPtrValue()
|
||||||
|
|
||||||
if got != test.want {
|
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(
|
t.Errorf(
|
||||||
"Unexpected bool parsed from %s: want %t, got %t",
|
"Unexpected boolean value found after parsing BoolPtrValue: want %s, got %s",
|
||||||
test.input,
|
want,
|
||||||
test.want,
|
|
||||||
got,
|
got,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
t.Logf(
|
t.Logf(
|
||||||
"Expected bool parsed from %s: got %t",
|
"Expected boolean value found after parsing BoolPtrValue: got %s",
|
||||||
test.input,
|
|
||||||
got,
|
got,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNilBoolPtrValue(t *testing.T) {
|
func TestNotSetBoolPtrValue(t *testing.T) {
|
||||||
value := internalFlag.NewBoolPtrValue()
|
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"
|
want := "NOT SET"
|
||||||
got := value.String()
|
got := boolVal.String()
|
||||||
|
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("Unexpected string returned from the nil value; want %s, got %s", want, got)
|
t.Errorf("Unexpected string returned from the nil value; want %s, got %s", want, got)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package flag_test
|
package flag_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -13,47 +14,52 @@ func TestTimeDurationValue(t *testing.T) {
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
input: "1 day",
|
input: `"1 day"`,
|
||||||
want: "24h0m0s",
|
want: "24h0m0s",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "3 days, 5 hours, 39 minutes and 6 seconds",
|
input: `"3 days, 5 hours, 39 minutes and 6 seconds"`,
|
||||||
want: "77h39m6s",
|
want: "77h39m6s",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "1 minute and 30 seconds",
|
input: `"1 minute and 30 seconds"`,
|
||||||
want: "1m30s",
|
want: "1m30s",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "(7 seconds) (21 hours) (41 days)",
|
input: `"(7 seconds) (21 hours) (41 days)"`,
|
||||||
want: "1005h0m7s",
|
want: "1005h0m7s",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
value := internalFlag.NewTimeDurationValue()
|
|
||||||
|
|
||||||
for _, test := range slices.All(parsingTests) {
|
for _, test := range slices.All(parsingTests) {
|
||||||
if err := value.Set(test.input); err != nil {
|
args := []string{"--duration", test.input}
|
||||||
t.Fatalf(
|
|
||||||
"Unable to parse %s into a TimeDurationValue: %v",
|
t.Run("Flag parsing test: "+test.input, testTimeDurationValueParsing(args, test.want))
|
||||||
test.input,
|
}
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
got := value.String()
|
func testTimeDurationValueParsing(args []string, want string) func(t *testing.T) {
|
||||||
|
return func(t *testing.T) {
|
||||||
|
flagset := flag.NewFlagSet("test", flag.ExitOnError)
|
||||||
|
duration := internalFlag.NewTimeDurationValue()
|
||||||
|
|
||||||
if got != test.want {
|
flagset.Var(&duration, "duration", "Duration value")
|
||||||
|
|
||||||
|
if err := flagset.Parse(args); err != nil {
|
||||||
|
t.Fatalf("Received an error parsing the flag: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := duration.String()
|
||||||
|
|
||||||
|
if got != want {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"Unexpected duration parsed from %s: want %s, got %s",
|
"Unexpected duration parsed from the flag: want %s, got %s",
|
||||||
test.input,
|
want,
|
||||||
test.want,
|
|
||||||
got,
|
got,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
t.Logf(
|
t.Logf(
|
||||||
"Expected duration parsed from %s: got %s",
|
"Expected duration parsed from the flag: got %s",
|
||||||
test.input,
|
|
||||||
got,
|
got,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue