-
Notifications
You must be signed in to change notification settings - Fork 123
/
flag_test.go
56 lines (52 loc) · 1.21 KB
/
flag_test.go
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
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
var flagParseErrorTests = []struct {
in string
flag string
reason string
}{
{
"unknown flag: --nope",
"--nope",
"Flag %s is missing.",
},
{
"flag needs an argument: --delete",
"--delete",
"Flag %s needs an argument.",
},
{
"flag needs an argument: 'd' in -d",
"-d",
"Flag %s needs an argument.",
},
{
`invalid argument "20dd" for "--delete-older-than" flag: time: unknown unit "dd" in duration "20dd"`,
"--delete-older-than",
"Flag %s have an invalid argument.",
},
{
`invalid argument "sdfjasdl" for "--max-tokens" flag: strconv.ParseInt: parsing "sdfjasdl": invalid syntax`,
"--max-tokens",
"Flag %s have an invalid argument.",
},
{
`invalid argument "nope" for "-r, --raw" flag: strconv.ParseBool: parsing "nope": invalid syntax`,
"-r, --raw",
"Flag %s have an invalid argument.",
},
}
func TestFlagParseError(t *testing.T) {
for _, tf := range flagParseErrorTests {
t.Run(tf.in, func(t *testing.T) {
err := newFlagParseError(fmt.Errorf(tf.in))
require.Equal(t, tf.flag, err.Flag())
require.Equal(t, tf.reason, err.ReasonFormat())
require.Equal(t, tf.in, err.Error())
})
}
}