summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/github.com/jessevdk/go-flags@v1.5.0/marshal_test.go
blob: 4cfe86568e04cc993bfdb914bdf21bca27e46aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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")
}