-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
95 lines (80 loc) · 1.77 KB
/
cli_test.go
File metadata and controls
95 lines (80 loc) · 1.77 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
92
93
94
95
package cli
import (
"flag"
"fmt"
"reflect"
"testing"
)
func TestFormatArgument(t *testing.T) {
tests := []struct {
name string
optional bool
many bool
result string
}{
{"arg", false, false, "<arg>"},
{"arg", false, true, "<arg...>"},
{"arg", true, false, "[arg]"},
{"arg", true, true, "[arg...]"},
}
for i, test := range tests {
result := FormatArgument(test.name, test.optional, test.many)
if result != test.result {
t.Errorf("%v: FormatArgument() = %v WANT %v", i, result, test.result)
}
}
}
func TestNewFlagSet(t *testing.T) {
fs := &MockFlagSetter{}
f := NewFlagSet("name", fs)
if fs.calledWith != f {
t.Fatal()
}
}
func TestCountFlags(t *testing.T) {
fs := NewFlagSet("", IntFlagSetter(0))
if CountFlags(fs) != 0 {
t.Fatal()
}
fs = NewFlagSet("", IntFlagSetter(200))
if CountFlags(fs) != 200 {
t.Fatal()
}
}
func TestGetFlagSetDefaults(t *testing.T) {
defs := GetFlagSetDefaults(NewFlagSet("", IntFlagSetter(0)))
if defs != "" {
t.Fatal(defs)
}
defs = GetFlagSetDefaults(NewFlagSet("two", IntFlagSetter(2)))
want := ` -int1 int
int1_usage (default 1)
-int2 int
int2_usage (default 2)`
if defs != want {
t.Fatal(defs, want)
}
}
func TestGetJoinedNameSortedAliases(t *testing.T) {
aliases := []string{"c", "b", "a"}
result := GetJoinedNameSortedAliases("d", aliases)
if result != "d, a, b, c" {
t.Fatal(result)
}
if !reflect.DeepEqual(aliases, []string{"c", "b", "a"}) {
t.Fatal()
}
}
type IntFlagSetter int
func (fs IntFlagSetter) SetFlags(f *flag.FlagSet) {
for i := 1; i <= int(fs); i++ {
name := fmt.Sprintf("int%d", i)
f.Int(name, i, name+"_usage")
}
}
type MockFlagSetter struct {
calledWith *flag.FlagSet
}
func (fs *MockFlagSetter) SetFlags(f *flag.FlagSet) {
fs.calledWith = f
}