-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_test.go
182 lines (167 loc) · 4.67 KB
/
cli_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cli_test
import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"
cli "github.com/multiverse-os/cli"
)
type parseTest struct {
Args []string
ExpectedAction string
}
func parseTests() []parseTest {
return []parseTest{
parseTest{
Args: []string{"test-cli", "h"},
ExpectedAction: "HelpCommand",
},
parseTest{
Args: []string{"test-cli", "help"},
ExpectedAction: "HelpCommand",
},
parseTest{
Args: []string{"test-cli", "-h"},
ExpectedAction: "RenderDefaultHelpTemplate",
},
parseTest{
Args: []string{"test-cli", "--help"},
ExpectedAction: "RenderDefaultHelpTemplate",
},
parseTest{
Args: []string{"test-cli", "v"},
ExpectedAction: "RenderDefaultVersionTemplate",
},
parseTest{
Args: []string{"test-cli", "version"},
ExpectedAction: "RenderDefaultVersionTemplate",
},
parseTest{
Args: []string{"test-cli", "-v"},
ExpectedAction: "RenderDefaultVersionTemplate",
},
parseTest{
Args: []string{"test-cli", "--version"},
ExpectedAction: "RenderDefaultVersionTemplate",
},
parseTest{
Args: []string{"test-cli", "list", "--help"},
ExpectedAction: "RenderDefaultHelpTemplate",
},
// TODO: This should be failing
parseTest{
Args: []string{"test-cli", "list", "help"},
ExpectedAction: "HelpCommand",
},
}
}
func initTestApp() cli.App {
return cli.App{
Name: "test-cli",
Description: "an example cli application for scripts and full-featured applications",
Version: cli.Version{Major: 0, Minor: 1, Patch: 1},
GlobalFlags: cli.Flags(
cli.Flag{
Name: "language",
Alias: "l",
Default: "en",
Description: "Locale used when executing the program",
},
cli.Flag{
Name: "daemon",
Alias: "d",
Description: "Daemonize the program when launching",
},
),
Commands: cli.Commands(
cli.Command{
Name: "list",
Alias: "l",
Description: "complete a task on the list",
Action: func(c *cli.Context) error {
c.CLI.Log("list!")
return nil
},
Flags: cli.Flags(
cli.Flag{
Name: "filter",
Alias: "f",
Default: "all",
Description: "filter all the things",
},
),
Subcommands: cli.Commands(
cli.Command{
Name: "add",
Description: "lists all of something",
Action: func(c *cli.Context) error {
c.CLI.Log("example action")
return nil
},
},
),
},
cli.Command{
Name: "show",
Alias: "sh",
Description: "show and item in the list",
Action: func(c *cli.Context) error {
c.CLI.Log("example action")
return nil
},
},
),
Actions: cli.Actions{
OnStart: func(c *cli.Context) error {
//c.CLI.Log("OnStart action")
return nil
},
Fallback: func(c *cli.Context) error {
//c.CLI.Log("Fallback action")
return nil
},
OnExit: func(c *cli.Context) error {
c.CLI.Log("OnExit action")
return nil
},
},
}
}
func Test_New(t *testing.T) {
// NOTE: Test Empty App
_, initErrors := cli.New()
if len(initErrors) != 0 {
t.Errorf("want (0) errors, got (%v)\n", len(initErrors))
}
}
// TODO: Will need to define different cli objects using New()
func Test_Parse(t *testing.T) {
for _, parseTest := range parseTests() {
// NOTE: Empty New() used, so it should only have 1 action
cmd, _ := cli.New(initTestApp())
cmd.Parse(parseTest.Args)
fmt.Printf("cli args (%v)\n", parseTest.Args)
fmt.Printf("expecting action name (%v)\n", parseTest.ExpectedAction)
var foundAction bool
for _, action := range cmd.Context.Actions {
fmt.Printf("found function name (%v)\n", runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name())
actionName := strings.TrimPrefix(
// TODO: see if you can check against The valueOf, the Pointer
// or anything but the name if at all possible
runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name(),
"github.com/multiverse-os/cli.",
)
if strings.Contains(actionName, "cli_test") {
continue
}
fmt.Println("action name:", actionName)
if actionName == parseTest.ExpectedAction {
foundAction = true
}
}
if !foundAction {
t.Errorf("want(true) got(%v)\n", foundAction)
}
}
}