-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathterminal_test.go
46 lines (41 loc) · 1 KB
/
terminal_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
package utils
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestInputYes(t *testing.T) {
type args struct {
question string
input string
}
tests := []struct {
name string
args args
ok bool
wantErr bool
}{
{"0", args{"test", "y\n"}, true, false},
{"1", args{"test", "Y\n"}, true, false},
{"2", args{"test", "n\n"}, false, false},
{"3", args{"test", "N\n"}, false, false},
{"4", args{"test", "N"}, false, false},
{"5", args{"test", "\n"}, true, false},
{"6", args{"test", ""}, true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// reset os.Stdin to mock user input
fp, err := NewTmpFile(bytes.NewReader([]byte(tt.args.input)))
require.NoError(t, err)
defer fp.Close()
os.Stdin = fp
if ok, err := InputYes(tt.args.question); (err != nil) != tt.wantErr {
t.Errorf("[%s]InputYes() error = %v, wantErr %v", tt.name, err, tt.wantErr)
} else {
require.True(t, ok == tt.ok)
}
})
}
}