-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_test.go
42 lines (38 loc) · 1.13 KB
/
cmd_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
package broccli
import (
"fmt"
"testing"
)
// TestCmdParams creates a dummy Cmd instance and tests attaching flags, args and environment variables.
func TestCmdParams(t *testing.T) {
c := &Cmd{}
c.AddFlag("flag1", "f1", "int", "Flag 1", TypeInt, IsRequired)
c.AddFlag("flag2", "f2", "path", "Flag 2", TypePathFile, IsRegularFile)
c.AddFlag("flag3", "f3", "", "Flag 3", TypeBool, 0, OnTrue(func(c *Cmd) {
c.flags["flag2"].flags = c.flags["flag2"].flags | IsExistent
}))
c.AddArg("arg1", "ARG1", "Arg 1", TypeInt, IsRequired)
c.AddArg("arg2", "ARG2", "Arg 2", TypeAlphanumeric, 0)
c.AddEnvVar("ENVVAR1", "Env var 1", TypeInt, 0)
sa := c.sortedArgs()
sf := c.sortedFlags()
se := c.sortedEnvVars()
if len(sa) != 2 || len(sf) != 3 || len(se) != 1 {
t.Errorf("Invalid args or flags or env vars added")
}
for i, a := range sa {
if a != fmt.Sprintf("arg%d", (i+1)) {
t.Errorf("Invalid arg was added")
}
}
for i, f := range sf {
if f != fmt.Sprintf("flag%d", (i+1)) {
t.Errorf("Invalid flag was added")
}
}
for i, e := range se {
if e != fmt.Sprintf("ENVVAR%d", (i+1)) {
t.Errorf("Invalid env var was added")
}
}
}