-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.go
82 lines (72 loc) · 2.12 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
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
package cli
import (
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)
func Test_NewFlag(t *testing.T) {
var dest int
nonNilValuer := &flagValuer[int]{value: &dest}
t.Run("ok", func(t *testing.T) {
assert.Check(t, NewFlag("long", "", nonNilValuer, "foo") != nil)
assert.Check(t, NewFlag("", "s", nonNilValuer, "foo") != nil)
assert.Check(t, NewFlag("long", "s", nonNilValuer, "foo") != nil)
assert.Check(t, NewFlag("long", "s", nonNilValuer, "") != nil)
})
t.Run("wrong setup", func(t *testing.T) {
t.Run("short and long names are unset", func(t *testing.T) {
assert.Check(t, func() (result cmp.Result) {
defer func() {
if reason := recover(); reason != nil {
result = cmp.Equal(reason, "longName and/or shortName must be non-empty")()
}
}()
NewFlag("", "", nonNilValuer, "")
return cmp.ResultFailure("did not panic")
})
})
t.Run("short is set with more than one character", func(t *testing.T) {
assert.Check(t, func() (result cmp.Result) {
defer func() {
if reason := recover(); reason != nil {
result = cmp.Equal(reason, "shortName must be one character long")()
}
}()
NewFlag("long", "notSoShort", nonNilValuer, "")
return cmp.ResultFailure("did not panic")
})
})
t.Run("nil valuer", func(t *testing.T) {
assert.Check(t, func() (result cmp.Result) {
defer func() {
if reason := recover(); reason != nil {
result = cmp.Equal(reason, "a non-nil valuer is required")()
}
}()
NewFlag("long", "", nil, "")
return cmp.ResultFailure("did not panic")
})
})
})
}
func Test_flagValue_LongName(t *testing.T) {
assert.Check(t, flagValue{
longName: "long",
shortName: "short",
description: "description",
}.LongName() == "long")
}
func Test_flagValue_ShortName(t *testing.T) {
assert.Check(t, flagValue{
longName: "long",
shortName: "short",
description: "description",
}.ShortName() == "short")
}
func Test_flagValue_Description(t *testing.T) {
assert.Check(t, flagValue{
longName: "long",
shortName: "short",
description: "description",
}.Description() == "description")
}