From b09c6d56832eb1718c07d74abf3bc6ae3fe4e030 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:36:04 +0200 Subject: Adding upstream version 1.1.0. Signed-off-by: Daniel Baumann --- .../jessevdk/go-flags@v1.5.0/marshal_test.go | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 dependencies/pkg/mod/github.com/jessevdk/go-flags@v1.5.0/marshal_test.go (limited to 'dependencies/pkg/mod/github.com/jessevdk/go-flags@v1.5.0/marshal_test.go') diff --git a/dependencies/pkg/mod/github.com/jessevdk/go-flags@v1.5.0/marshal_test.go b/dependencies/pkg/mod/github.com/jessevdk/go-flags@v1.5.0/marshal_test.go new file mode 100644 index 0000000..4cfe865 --- /dev/null +++ b/dependencies/pkg/mod/github.com/jessevdk/go-flags@v1.5.0/marshal_test.go @@ -0,0 +1,119 @@ +package flags + +import ( + "fmt" + "testing" +) + +type marshalled string + +func (m *marshalled) UnmarshalFlag(value string) error { + if value == "yes" { + *m = "true" + } else if value == "no" { + *m = "false" + } else { + return fmt.Errorf("`%s' is not a valid value, please specify `yes' or `no'", value) + } + + return nil +} + +func (m marshalled) MarshalFlag() (string, error) { + if m == "true" { + return "yes", nil + } + + return "no", nil +} + +type marshalledError bool + +func (m marshalledError) MarshalFlag() (string, error) { + return "", newErrorf(ErrMarshal, "Failed to marshal") +} + +func TestUnmarshal(t *testing.T) { + var opts = struct { + Value marshalled `short:"v"` + }{} + + ret := assertParseSuccess(t, &opts, "-v=yes") + + assertStringArray(t, ret, []string{}) + + if opts.Value != "true" { + t.Errorf("Expected Value to be \"true\"") + } +} + +func TestUnmarshalDefault(t *testing.T) { + var opts = struct { + Value marshalled `short:"v" default:"yes"` + }{} + + ret := assertParseSuccess(t, &opts) + + assertStringArray(t, ret, []string{}) + + if opts.Value != "true" { + t.Errorf("Expected Value to be \"true\"") + } +} + +func TestUnmarshalOptional(t *testing.T) { + var opts = struct { + Value marshalled `short:"v" optional:"yes" optional-value:"yes"` + }{} + + ret := assertParseSuccess(t, &opts, "-v") + + assertStringArray(t, ret, []string{}) + + if opts.Value != "true" { + t.Errorf("Expected Value to be \"true\"") + } +} + +func TestUnmarshalError(t *testing.T) { + var opts = struct { + Value marshalled `short:"v"` + }{} + + assertParseFail(t, ErrMarshal, fmt.Sprintf("invalid argument for flag `%cv' (expected flags.marshalled): `invalid' is not a valid value, please specify `yes' or `no'", defaultShortOptDelimiter), &opts, "-vinvalid") +} + +func TestUnmarshalPositionalError(t *testing.T) { + var opts = struct { + Args struct { + Value marshalled + } `positional-args:"yes"` + }{} + + parser := NewParser(&opts, Default&^PrintErrors) + _, err := parser.ParseArgs([]string{"invalid"}) + + msg := "`invalid' is not a valid value, please specify `yes' or `no'" + + if err == nil { + assertFatalf(t, "Expected error: %s", msg) + return + } + + if err.Error() != msg { + assertErrorf(t, "Expected error message %#v, but got %#v", msg, err.Error()) + } +} + +func TestMarshalError(t *testing.T) { + var opts = struct { + Value marshalledError `short:"v"` + }{} + + p := NewParser(&opts, Default) + o := p.Command.Groups()[0].Options()[0] + + _, err := convertToString(o.value, o.tag) + + assertError(t, err, ErrMarshal, "Failed to marshal") +} -- cgit v1.2.3