-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
89 lines (74 loc) · 1.84 KB
/
main_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 box
import (
"context"
"io"
"strings"
"testing"
)
func TestPrint(t *testing.T) {
var buffer strings.Builder
Print(context.TODO(), []string{
"aaaa", "bbbb", "cccc", "fjdaksljflkdajfkljsalkfjdlkf",
"jfkldsjflkjdsalkfjlkdsajflkajds",
"fsdfsdf"}, &buffer)
actual := buffer.String()
expect := `aaaa fjdaksljflkdajfkljsalkfjdlkf
bbbb jfkldsjflkjdsalkfjlkdsajflkajds
cccc fsdfsdf
`
if actual != expect {
t.Fatalf("expect `%s` buf `%s`", expect, actual)
}
}
func TestCutReduntantColorChange(t *testing.T) {
source := "\x1B[32;1m....\x1B[32;1m hogehoge"
expect := "\x1B[32;1m.... hogehoge"
actual := cutReduntantColorChange(source)
if expect != actual {
t.Fatalf("expect `%s` but `%s`", expect, actual)
}
source = "\x1B[32;1m....\x1B[33;1m hogehoge"
expect = source // not change
actual = cutReduntantColorChange(source)
if expect != actual {
t.Fatalf("expect `%s` but `%s`", expect, actual)
}
source = "\x1B[32;1m....\x1B[32;1m....\x1B[32;1m hogehoge"
expect = "\x1B[32;1m........ hogehoge"
actual = cutReduntantColorChange(source)
if expect != actual {
t.Fatalf("expect `%s` but `%s`", expect, actual)
}
}
type TstTty struct {
history []string
}
func (t *TstTty) GetKey() (string, error) {
if len(t.history) <= 0 {
return "", io.EOF
}
result := t.history[0]
t.history = t.history[1:]
return result, nil
}
func (t *TstTty) Close() error {
return nil
}
func TestSelectIndex(t *testing.T) {
b := &Box{
width: 80,
height: 25,
tty: &TstTty{history: []string{"l", "l", "\n"}},
}
list := []string{"A", "B", "C", "D", "E"}
r, err := b.SelectIndex(list, false, io.Discard)
if err != nil {
t.Fatal(err.Error())
}
if len(r) != 1 {
t.Fatalf("too few result: %d", len(r))
}
if r[0] != 2 {
t.Fatalf("expect %v,but %v", 2, r[0])
}
}