forked from jdevoo/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_test.go
89 lines (85 loc) · 1.5 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
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
package main
import (
"bytes"
"flag"
"os"
"strings"
"testing"
)
func TestFlags(T *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
tests := []struct {
cmd string
stdin string
args []string
wantExit int
wantOutput string
}{
{
"gen",
"",
[]string{"-h"},
1,
"Usage:",
},
{
"gen",
"",
[]string{},
1,
"Usage:",
},
{
"gen",
"",
[]string{"-s", "ten names for money"},
1,
"Usage:",
},
{
"gen",
"",
[]string{"-c"},
1,
"Usage:",
},
{
"gen",
"",
[]string{"ten names for money"},
0,
"Cash",
},
{
"gen",
"you understand english but always reply in french",
[]string{"-s", "ten names for money"},
0,
"illets",
},
{
"gen",
"you understand english but always reply in french",
[]string{"ten names for money"},
0,
"oici",
},
}
for _, test := range tests {
flag.CommandLine = flag.NewFlagSet(test.cmd, flag.ExitOnError)
os.Args = append([]string{test.cmd}, test.args...)
var out bytes.Buffer
actualExit := emitGen(strings.NewReader(test.stdin), &out)
if test.wantExit != actualExit {
T.Errorf("Wrong exit code for args: %v %v, expected: %v, got: %v",
test.stdin, test.args, test.wantExit, actualExit)
continue
}
actualOutput := out.String()
if !strings.Contains(actualOutput, test.wantOutput) {
T.Errorf("Wrong output for args: %v %v, expected: %v, got: %v",
test.stdin, test.args, test.wantOutput, actualOutput)
}
}
}