forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrenderer_test.go
141 lines (130 loc) · 3.02 KB
/
renderer_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//go:build !windows
// +build !windows
package prompt
import (
"reflect"
"syscall"
"testing"
istrings "github.com/elk-language/go-prompt/strings"
)
func TestFormatCompletion(t *testing.T) {
scenarioTable := []struct {
scenario string
completions []Suggest
prefix string
suffix string
expected []Suggest
maxWidth istrings.Width
expectedWidth istrings.Width
}{
{
scenario: "",
completions: []Suggest{
{Text: "select"},
{Text: "from"},
{Text: "insert"},
{Text: "where"},
},
prefix: " ",
suffix: " ",
expected: []Suggest{
{Text: " select "},
{Text: " from "},
{Text: " insert "},
{Text: " where "},
},
maxWidth: 20,
expectedWidth: 8,
},
{
scenario: "",
completions: []Suggest{
{Text: "select", Description: "select description"},
{Text: "from", Description: "from description"},
{Text: "insert", Description: "insert description"},
{Text: "where", Description: "where description"},
},
prefix: " ",
suffix: " ",
expected: []Suggest{
{Text: " select ", Description: " select description "},
{Text: " from ", Description: " from description "},
{Text: " insert ", Description: " insert description "},
{Text: " where ", Description: " where description "},
},
maxWidth: 40,
expectedWidth: 28,
},
}
for _, s := range scenarioTable {
ac, width := formatSuggestions(s.completions, s.maxWidth)
if !reflect.DeepEqual(ac, s.expected) {
t.Errorf("Should be %#v, but got %#v", s.expected, ac)
}
if width != s.expectedWidth {
t.Errorf("Should be %#v, but got %#v", s.expectedWidth, width)
}
}
}
func TestBreakLineCallback(t *testing.T) {
var i int
r := NewRenderer()
r.out = &PosixWriter{
fd: syscall.Stdin, // "write" to stdin just so we don't mess with the output of the tests
}
r.col = 1
b := NewBuffer()
r.BreakLine(b, nil)
if i != 0 {
t.Errorf("i should initially be 0, before applying a break line callback")
}
r.breakLineCallback = func(doc *Document) {
i++
}
r.BreakLine(b, nil)
r.BreakLine(b, nil)
r.BreakLine(b, nil)
if i != 3 {
t.Errorf("BreakLine callback not called, i should be 3")
}
}
func TestGetMultilinePrefix(t *testing.T) {
tests := map[string]struct {
prefix string
want string
}{
"single width chars": {
prefix: ">>",
want: "..",
},
"double width chars": {
prefix: "本日",
want: "....",
},
"trailing spaces and single width chars": {
prefix: ">!> ",
want: "... ",
},
"trailing spaces and double width chars": {
prefix: "本日: ",
want: "..... ",
},
"leading spaces and single width chars": {
prefix: " ah: ",
want: "..... ",
},
"leading spaces and double width chars": {
prefix: " 本日: ",
want: "....... ",
},
}
r := NewRenderer()
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := r.getMultilinePrefix(tc.prefix)
if tc.want != got {
t.Errorf("Expected %#v, but got %#v", tc.want, got)
}
})
}
}