-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags_test.go
More file actions
91 lines (69 loc) · 2.03 KB
/
flags_test.go
File metadata and controls
91 lines (69 loc) · 2.03 KB
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
package cli_test
import (
"context"
"testing"
"github.com/bjaus/cli"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type flaggedCmd struct {
Port int `flag:"port" short:"p" default:"8080" help:"Port to listen on" env:"PORT"`
Host string `flag:"host" default:"localhost" help:"Host to bind to"`
Verbose bool `flag:"verbose" short:"v" help:"Enable verbose logging"`
}
func (c *flaggedCmd) Run(_ context.Context) error { return nil }
type requiredFlagCmd struct {
Name string `flag:"name" required:"true" help:"Your name"`
}
func (c *requiredFlagCmd) Run(_ context.Context) error { return nil }
func TestScanFlags(t *testing.T) {
t.Parallel()
cmd := &flaggedCmd{}
defs := cli.ScanFlags(cmd)
require.Len(t, defs, 3)
byName := make(map[string]cli.FlagDef)
for _, d := range defs {
byName[d.Name] = d
}
port := byName["port"]
assert.Equal(t, "p", port.Short)
assert.Equal(t, "8080", port.Default)
assert.Equal(t, "Port to listen on", port.Help)
assert.Equal(t, "PORT", port.Env)
assert.Equal(t, "int", port.TypeName)
assert.False(t, port.IsBool)
verbose := byName["verbose"]
assert.Equal(t, "v", verbose.Short)
assert.True(t, verbose.IsBool)
}
func TestScanFlags_NoFlags(t *testing.T) {
t.Parallel()
cmd := &bareCmd{}
defs := cli.ScanFlags(cmd)
assert.Empty(t, defs)
}
func TestScanFlags_RequiredField(t *testing.T) {
t.Parallel()
cmd := &requiredFlagCmd{}
defs := cli.ScanFlags(cmd)
require.Len(t, defs, 1)
assert.True(t, defs[0].Required)
assert.Equal(t, "name", defs[0].Name)
}
type uintFlagScanCmd struct {
Port uint `flag:"port"`
MaxConn uint64 `flag:"max-conn"`
}
func (c *uintFlagScanCmd) Run(_ context.Context) error { return nil }
func TestScanFlags_UintTypeName(t *testing.T) {
t.Parallel()
cmd := &uintFlagScanCmd{}
defs := cli.ScanFlags(cmd)
require.Len(t, defs, 2)
byName := make(map[string]cli.FlagDef)
for _, d := range defs {
byName[d.Name] = d
}
assert.Equal(t, "uint", byName["port"].TypeName)
assert.Equal(t, "uint", byName["max-conn"].TypeName)
}