-
Notifications
You must be signed in to change notification settings - Fork 13
/
cli_test.go
77 lines (73 loc) · 1.88 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
package main
import (
"bytes"
"strings"
"testing"
)
func TestRun_global(t *testing.T) {
tests := []struct {
desc string
arg string
expectedStatus int
expectedSubOut string
expectedSubErr string
}{
{
desc: "undefined flag",
arg: "lstf --undefined",
expectedStatus: exitCodeErr,
expectedSubErr: "",
},
{
desc: "help",
arg: "lstf --help",
expectedStatus: exitCodeErr,
expectedSubErr: "Usage: lstf",
},
{
desc: "version",
arg: "lstf --version",
expectedStatus: exitCodeOK,
expectedSubErr: "lstf version",
},
{
desc: "credits",
arg: "lstf --credits",
expectedStatus: exitCodeOK,
expectedSubOut: "Go (the standard library)",
},
{
desc: "normal",
arg: "lstf",
expectedStatus: exitCodeOK,
expectedSubOut: "Local Address:Port",
},
{
desc: "--numeric",
arg: "lstf -n",
expectedStatus: exitCodeOK,
expectedSubOut: "Local Address:Port",
},
{
desc: "--json",
arg: "lstf --json",
expectedStatus: exitCodeOK,
expectedSubOut: "{\"direction\":",
},
}
for _, tc := range tests {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := &CLI{outStream: outStream, errStream: errStream}
args := strings.Split(tc.arg, " ")
status := cli.Run(args)
if status != tc.expectedStatus {
t.Errorf("desc: %q, status should be %v, not %v", tc.desc, tc.expectedStatus, status)
}
if !strings.Contains(outStream.String(), tc.expectedSubOut) {
t.Errorf("desc: %q, subout should contain %q, got %q", tc.desc, tc.expectedSubOut, outStream.String())
}
if !strings.Contains(errStream.String(), tc.expectedSubErr) {
t.Errorf("desc: %q, subout should contain %q, got %q", tc.desc, tc.expectedSubErr, errStream.String())
}
}
}