~mna/webparts-flag

ref: e84af31ab0aac85bc4ec57598c0bbfb6f3c23827 webparts-flag/flag_test.go -rw-r--r-- 3.1 KiB
e84af31aMartin Angers initial commit 1 year, 7 months ago
                                                                                
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package flag

import (
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

type F struct {
	S     string        `flag:"s,string,long-string"`
	I     int           `flag:"i,int"`
	B     bool          `flag:"b"`
	H     bool          `flag:"h,help"`
	T     time.Duration `flag:"t"`
	N     int
	args  []string
	flags map[string]bool
}

func (f *F) SetArgs(args []string) {
	f.args = args
}

func (f *F) SetFlags(flags map[string]bool) {
	f.flags = flags
}

func TestParseFlags(t *testing.T) {
	cases := []struct {
		args []string
		want *F
		err  string
	}{
		{
			want: &F{},
		},
		{
			args: []string{"toto"},
			want: &F{
				args: []string{"toto"},
			},
		},
		{
			args: []string{"-h"},
			want: &F{
				H:     true,
				flags: map[string]bool{"h": true},
			},
		},
		{
			args: []string{"-i", "10", "--int", "20"},
			want: &F{
				I:     20,
				flags: map[string]bool{"i": true, "int": true},
			},
		},
		{
			args: []string{"-i", "10", "--int", "20"},
			want: &F{
				I:     20,
				flags: map[string]bool{"i": true, "int": true},
			},
		},
		{
			args: []string{"-s", "a", "--string", "b", "-long-string", "c"},
			want: &F{
				S:     "c",
				flags: map[string]bool{"s": true, "string": true, "long-string": true},
			},
		},
		{
			args: []string{"-b", "--b", "-b"},
			want: &F{
				B:     true,
				flags: map[string]bool{"b": true},
			},
		},
		{
			args: []string{"-b", "-int", "1", "-string", "a", "arg1", "arg2"},
			want: &F{
				B:     true,
				I:     1,
				S:     "a",
				args:  []string{"arg1", "arg2"},
				flags: map[string]bool{"b": true, "int": true, "string": true},
			},
		},
		{
			args: []string{"-n", "1"},
			want: &F{},
			err:  "not defined: -n",
		},
		{
			args: []string{"-t", "3s"},
			want: &F{
				T:     3 * time.Second,
				flags: map[string]bool{"t": true},
			},
		},
		{
			args: []string{"-t", "nope"},
			want: &F{},
			err:  "invalid value",
		},
	}

	for _, c := range cases {
		t.Run(strings.Join(c.args, " "), func(t *testing.T) {
			var f F
			err := Parse(c.args, &f)

			if c.err != "" {
				require.Error(t, err)
				require.Contains(t, err.Error(), c.err)
				return
			}

			require.NoError(t, err)
			require.Equal(t, c.want, &f)
		})
	}
}

func TestParseNoFlag(t *testing.T) {
	type F struct {
		V int
	}
	f := F{V: 4}
	err := Parse([]string{"x"}, &f)
	require.NoError(t, err)
	require.Equal(t, 4, f.V)
}

type noFlagSetArgs struct {
	args []string
}

func (n *noFlagSetArgs) SetArgs(args []string) {
	n.args = args
}

func TestParseNoFlagSetArgs(t *testing.T) {
	f := noFlagSetArgs{}
	err := Parse([]string{"x"}, &f)
	require.NoError(t, err)
	require.Equal(t, []string{"x"}, f.args)
}

func TestParseArgsError(t *testing.T) {
	type F struct {
		X bool `flag:"x"`
	}
	f := F{}
	err := Parse([]string{"-zz"}, &f)
	require.Error(t, err)
	require.Contains(t, err.Error(), "-zz")
}

func TestParseNotStructPointer(t *testing.T) {
	var i int
	require.Panics(t, func() {
		_ = Parse([]string{"-h"}, i)
	})
}

func TestParseUnsupportedFlagType(t *testing.T) {
	type F struct {
		C *bool `flag:"c"`
	}
	var f F
	require.Panics(t, func() {
		_ = Parse([]string{"-h"}, &f)
	})
}