forked from sbreitf1/go-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_test.go
83 lines (72 loc) · 1.66 KB
/
console_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
package console
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNilToList(t *testing.T) {
assert.Nil(t, toList(nil))
}
func TestSliceToList(t *testing.T) {
v := []int{1, 2, 3, 4}
list := toList(v)
assert.Equal(t, []string{"1", "2", "3", "4"}, list)
}
func TestArrayToList(t *testing.T) {
v := [4]int{1, 2, 3, 4}
list := toList(v)
assert.Equal(t, []string{"1", "2", "3", "4"}, list)
}
func TestMapToList(t *testing.T) {
v := make(map[string]int)
v["foo"] = 4
v["bar"] = 2
list := toList(v)
assert.Len(t, list, 2)
assert.Contains(t, list, "4")
assert.Contains(t, list, "2")
}
func TestReadKey(t *testing.T) {
oldInput := DefaultInput
defer func() {
DefaultInput = oldInput
}()
DefaultInput = &keyFakeInput{Key: KeyEnter, Rune: '\n', Error: nil, isReading: false}
BeginReadKey()
defer EndReadKey()
key, r, err := ReadKey()
assert.Equal(t, KeyEnter, key)
assert.Equal(t, '\n', r)
assert.NoError(t, err)
}
type keyFakeInput struct {
Error error
Rune rune
Key Key
isReading bool
}
func (i *keyFakeInput) ReadLine() (string, error) {
panic("ReadLine not implemented on keyFakeInput")
}
func (i *keyFakeInput) ReadPassword() (string, error) {
panic("ReadLine not implemented on keyFakeInput")
}
func (i *keyFakeInput) BeginReadKey() error {
if i.isReading {
panic("BeginReadKey after BeginReadKey")
}
i.isReading = true
return nil
}
func (i *keyFakeInput) ReadKey() (Key, rune, error) {
if !i.isReading {
panic("ReadKey before BeginReadKey")
}
return i.Key, i.Rune, i.Error
}
func (i *keyFakeInput) EndReadKey() error {
if !i.isReading {
panic("EndReadKey before BeginReadKey")
}
i.isReading = false
return nil
}