forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunebuf_test.go
226 lines (203 loc) · 6.81 KB
/
runebuf_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package tui
import (
"image"
"reflect"
"testing"
)
func TestRuneBuffer_MoveForward(t *testing.T) {
for _, tt := range []struct {
text string
in, out int
}{
{"foo", 0, 1},
{"foo", 2, 3},
{"foo", 3, 3},
{"Lorem ipsum dolor \nsit amet.", 17, 18},
} {
t.Run("", func(t *testing.T) {
var buf RuneBuffer
buf.SetWithIdx(tt.in, []rune(tt.text))
buf.MoveForward()
if tt.out != buf.idx {
t.Fatalf("want = %v; got = %v", tt.out, buf.idx)
}
})
}
}
func TestRuneBuffer_MoveBackward(t *testing.T) {
for _, tt := range []struct {
text string
in, out int
}{
{"foo", 0, 0},
{"foo", 2, 1},
{"foo", 3, 2},
{"Lorem ipsum dolor \nsit amet.", 18, 17},
} {
t.Run("", func(t *testing.T) {
var buf RuneBuffer
buf.SetWithIdx(tt.in, []rune(tt.text))
buf.MoveBackward()
if tt.out != buf.idx {
t.Fatalf("want = %v; got = %v", tt.out, buf.idx)
}
})
}
}
func TestRuneBuffer_MoveToLineStart(t *testing.T) {
for _, tt := range []struct {
text string
in, out int
}{
{"foo", 3, 0},
{"foo", 0, 0},
{"Lorem ipsum dolor \nsit amet.", 21, 19},
{"Lorem ipsum dolor \n\nsit amet.", 21, 20},
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 40, 33},
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 90, 79},
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 79, 79},
// On a empty line.
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 78, 78},
// On newline character.
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 77, 33},
} {
t.Run("", func(t *testing.T) {
var buf RuneBuffer
buf.SetWithIdx(tt.in, []rune(tt.text))
buf.MoveToLineStart()
if tt.out != buf.idx {
t.Fatalf("want = %v; got = %v", tt.out, buf.idx)
}
})
}
}
func TestRuneBuffer_MoveToLineEnd(t *testing.T) {
for _, tt := range []struct {
text string
in, out int
}{
{"foo", 0, 3},
{"foo", 3, 3},
{"Lorem ipsum dolor \nsit amet.", 0, 18},
{"Lorem ipsum dolor \n\nsit amet.", 20, 29},
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 0, 31},
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 33, 77},
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 79, 117},
{"Sed maximus tempor condimentum.\n\nNam et risus est. Cras ornare iaculis orci, \n\nsit amet fringilla nisl pharetra quis.", 77, 77},
} {
t.Run("", func(t *testing.T) {
var buf RuneBuffer
buf.SetWithIdx(tt.in, []rune(tt.text))
buf.MoveToLineEnd()
if tt.out != buf.idx {
t.Fatalf("want = %v; got = %v", tt.out, buf.idx)
}
})
}
}
func TestRuneBuffer_Backspace(t *testing.T) {
for _, tt := range []struct {
curr RuneBuffer
want RuneBuffer
}{
{RuneBuffer{idx: 0, buf: []rune("foo bar")}, RuneBuffer{idx: 0, buf: []rune("foo bar")}},
{RuneBuffer{idx: 1, buf: []rune("foo bar")}, RuneBuffer{idx: 0, buf: []rune("oo bar")}},
{RuneBuffer{idx: 7, buf: []rune("foo bar")}, RuneBuffer{idx: 6, buf: []rune("foo ba")}},
{RuneBuffer{idx: 4, buf: []rune("foo bar")}, RuneBuffer{idx: 3, buf: []rune("foobar")}},
} {
t.Run("", func(t *testing.T) {
tt.curr.Backspace()
if tt.want.idx != tt.curr.idx {
t.Fatalf("want = %v; got = %v", tt.want.idx, tt.curr.idx)
}
if !reflect.DeepEqual(tt.want.buf, tt.curr.buf) {
t.Fatalf("want = %q; got = %q", string(tt.want.buf), string(tt.curr.buf))
}
})
}
}
func TestRuneBuffer_Kill(t *testing.T) {
for _, tt := range []struct {
curr RuneBuffer
want RuneBuffer
}{
{RuneBuffer{idx: 0, buf: []rune("foo bar")}, RuneBuffer{idx: 0, buf: []rune("")}},
{RuneBuffer{idx: 1, buf: []rune("foo bar")}, RuneBuffer{idx: 1, buf: []rune("f")}},
{RuneBuffer{idx: 7, buf: []rune("foo bar")}, RuneBuffer{idx: 7, buf: []rune("foo bar")}},
{RuneBuffer{idx: 4, buf: []rune("foo bar")}, RuneBuffer{idx: 4, buf: []rune("foo ")}},
{RuneBuffer{idx: 0, buf: []rune("foo \nbar")}, RuneBuffer{idx: 0, buf: []rune("bar")}},
{RuneBuffer{idx: 5, buf: []rune("foo \nbar")}, RuneBuffer{idx: 5, buf: []rune("foo \n")}},
{RuneBuffer{idx: 6, buf: []rune("foo \nbar")}, RuneBuffer{idx: 6, buf: []rune("foo \nb")}},
{RuneBuffer{idx: 0, buf: []rune("\n")}, RuneBuffer{idx: 0, buf: []rune("")}},
} {
t.Run("", func(t *testing.T) {
tt.curr.Kill()
if tt.want.idx != tt.curr.idx {
t.Fatalf("want = %v; got = %v", tt.want.idx, tt.curr.idx)
}
if !reflect.DeepEqual(tt.want.buf, tt.curr.buf) {
t.Fatalf("want = %q; got = %q", string(tt.want.buf), string(tt.curr.buf))
}
})
}
}
func TestRuneBuffer_SplitByLines(t *testing.T) {
for _, tt := range []struct {
text string
width int
wrap bool
want []string
}{
{"Lorem ipsum dolor sit amet.", 12, true, []string{"Lorem ipsum ", "dolor sit ", "amet."}},
{"Lorem ipsum dolor sit amet.", 27, true, []string{"Lorem ipsum dolor sit amet."}},
{"Lorem ipsum dolor sit amet.", 12, false, []string{"Lorem ipsum dolor sit amet."}},
} {
var buf RuneBuffer
buf.Set([]rune(tt.text))
buf.SetMaxWidth(tt.width)
buf.wordwrap = tt.wrap
got := buf.SplitByLine()
if !reflect.DeepEqual(tt.want, got) {
t.Fatalf("want = %#v; got = %#v", tt.want, got)
}
}
}
func TestRuneBuffer_CursorPos(t *testing.T) {
for _, tt := range []struct {
text string
screenWidth int
idx int
wrap bool
want image.Point
}{
// Lorem ipsum
// dolor sit amet.
{"Lorem ipsum dolor sit amet.", 12, 11, true, image.Pt(11, 0)},
{"Lorem ipsum dolor sit amet.", 12, 12, true, image.Pt(12, 0)},
{"Lorem ipsum dolor sit amet.", 12, 13, true, image.Pt(0, 1)},
// Lorem ipsum dolor
// sit amet.
{"Lorem ipsum dolor sit amet.", 19, 17, true, image.Pt(17, 0)},
{"Lorem ipsum dolor sit amet.", 19, 18, true, image.Pt(18, 0)},
{"Lorem ipsum dolor sit amet.", 19, 19, true, image.Pt(0, 1)},
{"Lorem ipsum dolor sit amet.", 19, 20, true, image.Pt(1, 1)},
{"Lorem ipsum dolor sit amet.", 19, 21, true, image.Pt(2, 1)},
// aa bb
//
// cc dd
{"aa bb\n\ncc dd", 10, 4, true, image.Pt(4, 0)},
{"aa bb\n\ncc dd", 10, 5, true, image.Pt(5, 0)},
{"aa bb\n\ncc dd", 10, 6, true, image.Pt(0, 1)},
{"aa bb\n\ncc dd", 10, 7, true, image.Pt(0, 2)},
} {
t.Run("", func(t *testing.T) {
var r RuneBuffer
r.wordwrap = tt.wrap
r.SetWithIdx(tt.idx, []rune(tt.text))
r.SetMaxWidth(tt.screenWidth)
if got := r.CursorPos(); tt.want != got {
t.Fatalf("want = %s; got = %s", tt.want, got)
}
})
}
}