-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
main_test.go
146 lines (125 loc) · 3.17 KB
/
main_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
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
package main_test
import (
"fmt"
"reflect"
"testing"
main "github.com/urfave/cli/v2/cmd/urfave-cli-genflags"
)
func TestTypeName(t *testing.T) {
for _, tc := range []struct {
gt string
fc *main.FlagTypeConfig
expected string
}{
{gt: "int", fc: nil, expected: "IntFlag"},
{gt: "int", fc: &main.FlagTypeConfig{}, expected: "IntFlag"},
{gt: "int", fc: &main.FlagTypeConfig{TypeName: "VeryIntyFlag"}, expected: "VeryIntyFlag"},
{gt: "[]bool", fc: nil, expected: "BoolSliceFlag"},
{gt: "[]bool", fc: &main.FlagTypeConfig{}, expected: "BoolSliceFlag"},
{gt: "[]bool", fc: &main.FlagTypeConfig{TypeName: "ManyTruthsFlag"}, expected: "ManyTruthsFlag"},
{gt: "time.Rumination", fc: nil, expected: "RuminationFlag"},
{gt: "time.Rumination", fc: &main.FlagTypeConfig{}, expected: "RuminationFlag"},
{gt: "time.Rumination", fc: &main.FlagTypeConfig{TypeName: "PonderFlag"}, expected: "PonderFlag"},
} {
t.Run(
fmt.Sprintf("type=%s,cfg=%v", tc.gt, func() string {
if tc.fc != nil {
return tc.fc.TypeName
}
return "nil"
}()),
func(ct *testing.T) {
actual := main.TypeName(tc.gt, tc.fc)
if tc.expected != actual {
ct.Errorf("expected %q, got %q", tc.expected, actual)
}
},
)
}
}
func TestSpec_SortedFlagTypes(t *testing.T) {
spec := &main.Spec{
FlagTypes: map[string]*main.FlagTypeConfig{
"nerf": &main.FlagTypeConfig{},
"gerf": nil,
},
}
actual := spec.SortedFlagTypes()
expected := []*main.FlagType{
{
GoType: "gerf",
Config: nil,
},
{
GoType: "nerf",
Config: &main.FlagTypeConfig{},
},
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("expected %#v, got %#v", expected, actual)
}
}
func genFlagType() *main.FlagType {
return &main.FlagType{
GoType: "blerf",
Config: &main.FlagTypeConfig{
SkipInterfaces: []string{"fmt.Stringer"},
StructFields: []*main.FlagStructField{
{
Name: "Foibles",
Type: "int",
},
{
Name: "Hoopled",
Type: "bool",
},
},
TypeName: "YeOldeBlerfFlag",
ValuePointer: true,
},
}
}
func TestFlagType_StructFields(t *testing.T) {
ft := genFlagType()
sf := ft.StructFields()
if 2 != len(sf) {
t.Errorf("expected 2 struct fields, got %v", len(sf))
return
}
if "Foibles" != sf[0].Name {
t.Errorf("expected struct field order to be retained")
}
}
func TestFlagType_ValuePointer(t *testing.T) {
ft := genFlagType()
if !ft.ValuePointer() {
t.Errorf("expected ValuePointer to be true")
return
}
ft.Config = nil
if ft.ValuePointer() {
t.Errorf("expected ValuePointer to be false")
}
}
func TestFlagType_GenerateFmtStringerInterface(t *testing.T) {
ft := genFlagType()
if ft.GenerateFmtStringerInterface() {
t.Errorf("expected GenerateFmtStringerInterface to be false")
return
}
ft.Config = nil
if !ft.GenerateFmtStringerInterface() {
t.Errorf("expected GenerateFmtStringerInterface to be true")
}
}
func TestFlagType_GenerateFlagInterface(t *testing.T) {
ft := genFlagType()
if !ft.GenerateFlagInterface() {
t.Errorf("expected GenerateFlagInterface to be true")
return
}
ft.Config = nil
if !ft.GenerateFlagInterface() {
t.Errorf("expected GenerateFlagInterface to be true")
}
}