-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevels_test.go
144 lines (130 loc) · 3.24 KB
/
levels_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
package egcmd
import (
"testing"
)
func TestLevelExample(t *testing.T) {
level := Level{name: "root"}
testCases := []struct {
name string
arguments string
description string
want Example
}{
{
"NoArguments",
"",
"Default behaviour",
Example{Description: "Default behaviour"},
},
{
"WithArguments",
"--verbose",
"verbose output",
Example{Arguments: "--verbose", Description: "verbose output"},
},
}
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := level.Example(tc.arguments, tc.description)
count := i + 1
if got := len(level.examples); count != got {
t.Errorf("Expected level example count to be %d, got %d", count, got)
}
if got := *actual; tc.want != got {
t.Errorf("Expected returned example to be %v, got %v", tc.want, got)
}
if got := *level.examples[i]; tc.want != got {
t.Errorf("Expected level example %d to be %v, got %v", i, tc.want, got)
}
})
}
}
func TestLevelEnvs(t *testing.T) {
level := Level{name: "root"}
testCases := []struct {
name string
envVars string
arguments string
description string
want Example
}{
{
"NoEnvs",
"",
"",
"Default behaviour",
Example{Description: "Default behaviour"},
},
{
"WithEnvs",
"LOGGING=verbose",
"",
"verbose output",
Example{EnvVars: "LOGGING=verbose", Description: "verbose output"},
},
}
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := level.Envs(tc.envVars, tc.arguments, tc.description)
count := i + 1
if got := len(level.examples); count != got {
t.Errorf("Expected level example count to be %d, got %d", count, got)
}
if got := *actual; tc.want != got {
t.Errorf("Expected returned example to be %v, got %v", tc.want, got)
}
if got := *level.examples[i]; tc.want != got {
t.Errorf("Expected level example %d to be %v, got %v", i, tc.want, got)
}
})
}
}
func TestAppCommand(t *testing.T) {
app := App{Level: Level{name: "root"}, commands: make(map[string]*Command)}
testCases := []struct {
name string
command string
exampleToAppend *Example
duplicateCommand bool
}{
{
"FirstCommand",
"init",
&Example{Description: "first"},
false,
},
{
"DuplicateCommand",
"init",
&Example{Description: "second"},
true,
},
{
"NextCommand",
"LOGGING=verbose",
&Example{Description: "next"},
false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := app.Command(tc.command)
isDuplicate := len(actual.examples) > 0
appCommand, found := app.commands[tc.command]
actual.examples = append(actual.examples, tc.exampleToAppend)
if got := actual.name; tc.command != got {
t.Errorf("Expected returned command to be %q, got %q", tc.command, got)
}
if !found {
t.Errorf("Expected command in app to be present, but was not found")
return
}
if got := appCommand.name; tc.command != got {
t.Errorf("Expected command in app to have the name %q, got %q", tc.command, got)
}
if got := isDuplicate; tc.duplicateCommand != got {
t.Errorf("Expected command to have an example count of %t, got %t", tc.duplicateCommand, got)
}
})
}
}