-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmdtree_test.go
97 lines (77 loc) · 2.1 KB
/
cmdtree_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
package cmdtree
import (
"fmt"
"testing"
)
func TestRoot(t *testing.T) {
helpRan := false
root := Root(" ")
root.SubCmd("help", func(args string) error {
helpRan = true
return nil
})
if err := root.Execute("help"); err != nil {
t.Errorf("Received unexpected error: %v", err)
} else if !helpRan {
t.Error(`"help" command did not run.`)
}
}
func TestExecuteCommand(t *testing.T) {
rootRun := false
subRun := false
help := NewCmd(" ", "help", func(args string) error {
rootRun = true
return nil
})
help.SubCmd("cmdtree", func(args string) error {
subRun = true
return nil
})
// Test executing the root
if err := help.Execute("help"); err != nil {
t.Errorf("Unexpected error received: %v", err)
} else if !rootRun {
t.Error("Root was not run.")
}
// Test executing sub commands.
if err := help.Execute("help cmdtree"); err != nil {
t.Errorf("Unexpected error received: %v", err)
} else if !subRun {
t.Error("Command was not run.")
}
}
func TestExecuteEmptySubCmd(t *testing.T) {
help := NewCmd(" ", "help", nil)
help.SubCmd("cmdtree", nil)
if err := help.Execute("help"); err == nil {
t.Error("Expected a usage error.")
} else if expected := fmt.Sprintf("Command usage:\n%s", help); err.Error() != expected {
t.Logf("Got: %v", err)
t.Errorf("Expected: %s", expected)
}
}
func TestExecuteCommandWhenArgumentsNotMatch(t *testing.T) {
var helpArgsPassed string
help := NewCmd(" ", "help", func(passed string) error {
helpArgsPassed = passed
return nil
})
help.SubCmd("cmdtree", nil)
if err := help.Execute("help 1 2 3"); err != nil {
t.Error("Unexpected error.")
} else if helpArgsPassed == "" {
t.Error("help was not run")
} else if helpArgsPassed != "1 2 3" {
t.Errorf("help not passed correct arguments: %s", helpArgsPassed)
}
}
func TestCommandUsage(t *testing.T) {
help := NewCmd(" ", "help", nil)
help.SubCmd("cmdtree", nil).SubCmd("usage", nil)
help.SubCmd("golang", nil)
const expected = "help\n\tcmdtree\n\t\tusage\n\tgolang"
if got := help.String(); got != expected {
t.Logf("Got: %s", got)
t.Errorf("Expected output: %s", expected)
}
}