forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_test.go
127 lines (114 loc) · 2.61 KB
/
read_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
package cliutil_test
import (
"fmt"
"os"
"testing"
"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/testutil/fakeobj"
)
func TestRead_cases(t *testing.T) {
buf := fakeobj.NewReader()
cliutil.Input = buf
defer func() {
cliutil.Input = os.Stdin
}()
t.Run("ReadInput", func(t *testing.T) {
buf.WriteString("inhere")
ans, err := cliutil.ReadInput("hi, your name? ")
fmt.Println("ans:", ans)
assert.NoError(t, err)
assert.Equal(t, "inhere", ans)
// error
buf.SetErrOnRead()
_, err = cliutil.ReadInput("hi, your name? ")
assert.Error(t, err)
fmt.Println(err)
buf.ErrOnRead = false
})
// test ReadLine
t.Run("ReadLine", func(t *testing.T) {
buf.WriteString("inhere")
ans, err := cliutil.ReadLine("hi, your name? ")
fmt.Println("ans:", ans)
assert.NoError(t, err)
assert.Equal(t, "inhere", ans)
})
// test ReadFirst
t.Run("ReadFirst", func(t *testing.T) {
buf.WriteString("Y")
ans, err := cliutil.ReadFirst("continue?[y/n] ")
fmt.Println("ans:", ans)
assert.NoError(t, err)
assert.Equal(t, "Y", ans)
})
// test ReadFirstRune
t.Run("ReadFirstRune", func(t *testing.T) {
buf.WriteString("Y")
ans, err := cliutil.ReadFirstRune("continue?[y/n] ")
fmt.Println("ans:", string(ans))
assert.NoError(t, err)
assert.Equal(t, 'Y', ans)
})
// test ReadAsBool
t.Run("ReadAsBool", func(t *testing.T) {
buf.WriteString("Y")
ans := cliutil.ReadAsBool("continue?[y/n] ", false)
fmt.Println("ans:", ans)
assert.True(t, ans)
})
// test Confirm
t.Run("Confirm", func(t *testing.T) {
buf.WriteString("Y")
ans := cliutil.Confirm("continue?", false)
fmt.Println(ans)
assert.True(t, ans)
ans = cliutil.Confirm("continue?", true)
fmt.Println(ans)
assert.True(t, ans)
})
}
func TestReadFirst(t *testing.T) {
// testutil.RewriteStdout()
// _, err := os.Stdout.WriteString("haha")
// ans, err1 := cliutil.ReadFirst("hi?")
// testutil.RestoreStdout()
// assert.NoError(t, err)
// assert.NoError(t, err1)
// assert.Equal(t, "haha", ans)
}
func TestInputIsYes(t *testing.T) {
tests := []struct {
in string
wnt bool
}{
{"y", true},
{"yes", true},
{"yES", true},
{"Y", true},
{"Yes", true},
{"YES", true},
{"h", false},
{"n", false},
{"no", false},
{"NO", false},
}
for _, test := range tests {
assert.Eq(t, test.wnt, cliutil.InputIsYes(test.in))
}
}
func TestByteIsYes(t *testing.T) {
tests := []struct {
in byte
wnt bool
}{
{'y', true},
{'Y', true},
{'h', false},
{'n', false},
{'N', false},
}
for _, test := range tests {
assert.Eq(t, test.wnt, cliutil.ByteIsYes(test.in))
}
}