-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrun_cmd_test.go
52 lines (48 loc) · 1.2 KB
/
run_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
package main
import (
"bytes"
"errors"
"strings"
"testing"
)
func TestRunCmd(t *testing.T) {
tests := []struct {
c config
input string
output string
err error
}{
{
c: config{numTimes: 5},
input: "",
output: strings.Repeat("Your name please? Press the Enter key when done.\n", 1),
err: errors.New("You didn't enter your name"),
},
{
c: config{numTimes: 5},
input: "Bill Bryson",
output: "Your name please? Press the Enter key when done.\n" + strings.Repeat("Nice to meet you Bill Bryson\n", 5),
},
{
c: config{numTimes: 5, name: "Bill Bryson"},
input: "",
output: strings.Repeat("Nice to meet you Bill Bryson\n", 5),
},
}
byteBuf := new(bytes.Buffer)
for _, tc := range tests {
r := strings.NewReader(tc.input)
err := runCmd(r, byteBuf, tc.c)
if err != nil && tc.err == nil {
t.Fatalf("Expected nil error, got: %v\n", err)
}
if tc.err != nil && err.Error() != tc.err.Error() {
t.Fatalf("Expected error: %v, Got error: %v\n", tc.err.Error(), err.Error())
}
gotMsg := byteBuf.String()
if gotMsg != tc.output {
t.Errorf("Expected stdout message to be: %v, Got: %v\n", tc.output, gotMsg)
}
byteBuf.Reset()
}
}