-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
123 lines (112 loc) · 2.86 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
package main
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestProcessCommands(t *testing.T) {
c := processCommands(Command{
Name: "mycliapp",
Help: "does something cool",
Aliases: []string{"myapp", "app"},
Output: "Hello World!\n",
Flags: []Flag{
Flag{
Name: "debug",
Help: "enables debugging",
Short: "d",
Default: false,
Global: true,
Type: "bool",
},
},
Commands: []Command{
Command{
Name: "subcommand1",
Help: "a subcommand",
Flags: []Flag{
Flag{
Name: "upper",
Help: "converts output to uppercase",
Short: "u",
Type: "bool",
},
},
Output: "{{ if .Flags.upper.Bool -}}\nHELLO FROM SC1!\n{{ else -}}\nHello from SC1!\n{{ end -}}\n",
},
},
})
assert.Equal(t, c.Use, "mycliapp", "Use should be set to mycliapp")
assert.Equal(t, c.Short, "does something cool", "Short is not equal")
assert.Equal(t, c.Long, "does something cool", "Long is not equal")
assert.Equal(t, c.Aliases, []string{"myapp", "app"}, "Aliases is not equal")
assert.Equal(t, len(flags), 2, "Flags length is not equal")
commands := c.Commands()
assert.Equal(t, len(commands), 1, "Commands length is not equal")
assert.Equal(t, commands[0].Use, "subcommand1", "Not equal")
assert.Equal(t, commands[0].Short, "a subcommand", "Not equal")
assert.Equal(t, commands[0].Long, "a subcommand", "Not equal")
assert.Nil(t, c.Execute())
}
func TestFileExists(t *testing.T) {
if !fileExists("examples/fileutil.yml") {
t.Fatal("should be true")
}
if fileExists("examples/doesnotexist.yml") {
t.Fatal("should be false")
}
}
func TestLoadCLIYAML(t *testing.T) {
want := Command{
Name: "mycliapp",
Help: "does something cool",
Aliases: []string{"myapp", "app"},
Output: "Hello, World!\n",
Flags: []Flag{
Flag{
Name: "debug",
Help: "enables debugging",
Short: "d",
Default: false,
Global: true,
Type: "bool",
},
},
Commands: []Command{
Command{
Name: "subcommand1",
Help: "a subcommand",
Flags: []Flag{
Flag{
Name: "upper",
Help: "converts output to uppercase",
Short: "u",
Type: "bool",
},
},
Output: "{{ if .Flags.upper.Bool -}}\nHELLO FROM SC1!\n{{ else -}}\nHello from SC1!\n{{ end -}}\n",
},
Command{
Name: "subcommand2",
Help: "another subcommand with children",
Commands: []Command{
Command{
Name: "child1",
Help: "the first child command",
Output: "Hello from child1\n",
},
Command{
Name: "child2",
Help: "the second child command",
Output: "Hello from child2\n",
},
},
},
},
}
c, err := loadCLIYAML("examples/mycliapp.yaml")
assert.Nil(t, err)
if !reflect.DeepEqual(c, want) {
t.Errorf("Loaded data not equal, data returned %+v, want %+v", c, want)
}
}