-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_test.go
More file actions
384 lines (374 loc) · 9.27 KB
/
parser_test.go
File metadata and controls
384 lines (374 loc) · 9.27 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package ansi_test
import (
"testing"
"github.com/aoldershaw/ansi"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
)
func TestParser_Actions(t *testing.T) {
format.UseStringerRepresentation = true
for _, tt := range []struct {
description string
input []byte
actions []ansi.Action
}{
{
description: "no escape sequences",
input: []byte("just some text here!"),
actions: []ansi.Action{
ansi.Print("just some text here!"),
},
},
{
description: "linebreak",
input: []byte("hello\nworld"),
actions: []ansi.Action{
ansi.Print("hello"),
ansi.Linebreak{},
ansi.Print("world"),
},
},
{
description: "carriage return",
input: []byte("hello\rworld"),
actions: []ansi.Action{
ansi.Print("hello"),
ansi.CarriageReturn{},
ansi.Print("world"),
},
},
{
description: "colours",
input: []byte("normal\x1b[31mred fg\x1b[42mgreen bg\x1b[91mbright red fg\x1b[102mbright green bg"),
actions: []ansi.Action{
ansi.Print("normal"),
ansi.SetForeground(ansi.Red),
ansi.Print("red fg"),
ansi.SetBackground(ansi.Green),
ansi.Print("green bg"),
ansi.SetForeground(ansi.BrightRed),
ansi.Print("bright red fg"),
ansi.SetBackground(ansi.BrightGreen),
ansi.Print("bright green bg"),
},
},
{
description: "resetting",
input: []byte("some text\x1b[0mreset\x1b[mreset again\x1b[;31mreset to red"),
actions: []ansi.Action{
ansi.Print("some text"),
ansi.Reset{},
ansi.Print("reset"),
ansi.Reset{},
ansi.Print("reset again"),
ansi.Reset{},
ansi.SetForeground(ansi.Red),
ansi.Print("reset to red"),
},
},
{
description: "text styling",
input: []byte("normal\x1b[1mbold\x1b[2mfaint\x1b[3mitalic\x1b[4munderline\x1b[5mblink\x1b[7minverted\x1b[20mfraktur"),
actions: []ansi.Action{
ansi.Print("normal"),
ansi.SetBold(true),
ansi.Print("bold"),
ansi.SetFaint(true),
ansi.Print("faint"),
ansi.SetItalic(true),
ansi.Print("italic"),
ansi.SetUnderline(true),
ansi.Print("underline"),
ansi.SetBlink(true),
ansi.Print("blink"),
ansi.SetInverted(true),
ansi.Print("inverted"),
ansi.SetFraktur(true),
ansi.Print("fraktur"),
},
},
{
description: "multiple arguments to formatting",
input: []byte("\x1b[1;31;20mhello\x1b[;46m"),
actions: []ansi.Action{
ansi.SetBold(true),
ansi.SetForeground(ansi.Red),
ansi.SetFraktur(true),
ansi.Print("hello"),
ansi.Reset{},
ansi.SetBackground(ansi.Cyan),
},
},
{
description: "multiple arguments to formatting, some invalid, OK",
input: []byte("\x1b[1;69mhello"),
actions: []ansi.Action{
ansi.SetBold(true),
ansi.Print("hello"),
},
},
{
description: "multiple arguments to formatting, all invalid, not OK",
input: []byte("\x1b[68;69mhello"),
actions: []ansi.Action{
ansi.Print("hello"),
},
},
{
description: "multiple arguments to formatting, last is empty, does not reset",
input: []byte("\x1b[1;mhello"),
actions: []ansi.Action{
ansi.SetBold(true),
ansi.Print("hello"),
},
},
{
description: "cursor movement",
input: []byte("\x1b[5A\x1b[50A\x1b[A\x1b[5B\x1b[50B\x1b[B\x1b[5C\x1b[50C\x1b[C\x1b[5D\x1b[50D\x1b[D\x1b[;50H\x1b[50;H\x1b[H\x1b[;H\x1b[50;50H\x1b[;50f\x1b[50;f\x1b[f\x1b[;f\x1b[50;50f"),
actions: []ansi.Action{
ansi.CursorUp(5),
ansi.CursorUp(50),
ansi.CursorUp(1),
ansi.CursorDown(5),
ansi.CursorDown(50),
ansi.CursorDown(1),
ansi.CursorForward(5),
ansi.CursorForward(50),
ansi.CursorForward(1),
ansi.CursorBack(5),
ansi.CursorBack(50),
ansi.CursorBack(1),
ansi.CursorPosition(ansi.Pos{1, 50}),
ansi.CursorPosition(ansi.Pos{50, 1}),
ansi.CursorPosition(ansi.Pos{1, 1}),
ansi.CursorPosition(ansi.Pos{1, 1}),
ansi.CursorPosition(ansi.Pos{50, 50}),
ansi.CursorPosition(ansi.Pos{1, 50}),
ansi.CursorPosition(ansi.Pos{50, 1}),
ansi.CursorPosition(ansi.Pos{1, 1}),
ansi.CursorPosition(ansi.Pos{1, 1}),
ansi.CursorPosition(ansi.Pos{50, 50}),
},
},
{
description: "cursor movement (not ANSI.SYS)",
input: []byte("\x1b[E\x1b[5E\x1b[50E\x1b[F\x1b[5F\x1b[50F\x1b[G\x1b[0G\x1b[1G\x1b[5G\x1b[50G"),
actions: []ansi.Action{
ansi.CursorDown(1),
ansi.CursorColumn(0),
ansi.CursorDown(5),
ansi.CursorColumn(0),
ansi.CursorDown(50),
ansi.CursorColumn(0),
ansi.CursorUp(1),
ansi.CursorColumn(0),
ansi.CursorUp(5),
ansi.CursorColumn(0),
ansi.CursorUp(50),
ansi.CursorColumn(0),
ansi.CursorColumn(0),
ansi.CursorColumn(0),
ansi.CursorColumn(1),
ansi.CursorColumn(5),
ansi.CursorColumn(50),
},
},
{
description: "save/restore cursor",
input: []byte("\x1b[s\x1b[u"),
actions: []ansi.Action{
ansi.SaveCursorPosition{},
ansi.RestoreCursorPosition{},
},
},
{
description: "erasure",
input: []byte("\x1b[J\x1b[0J\x1b[1J\x1b[2J\x1b[K\x1b[0K\x1b[1K\x1b[2K"),
actions: []ansi.Action{
ansi.EraseDisplay(ansi.EraseToEnd),
ansi.EraseDisplay(ansi.EraseToEnd),
ansi.EraseDisplay(ansi.EraseToBeginning),
ansi.EraseDisplay(ansi.EraseAll),
ansi.EraseLine(ansi.EraseToEnd),
ansi.EraseLine(ansi.EraseToEnd),
ansi.EraseLine(ansi.EraseToBeginning),
ansi.EraseLine(ansi.EraseAll),
},
},
{
description: "incomplete escape sequence (no bracket)",
input: []byte("hello\x1bworld"),
actions: []ansi.Action{
ansi.Print("hello"),
ansi.Print("world"),
},
},
{
description: "incomplete escape sequence (double bracket)",
input: []byte("hello\x1b[[world"),
actions: []ansi.Action{
ansi.Print("hello"),
ansi.Print("world"),
},
},
{
description: "incomplete escape sequence at end",
input: []byte("hello\x1b"),
actions: []ansi.Action{
ansi.Print("hello"),
},
},
{
description: "unknown escape sequence",
input: []byte("hello\x1b[1Zworld"),
actions: []ansi.Action{
ansi.Print("hello"),
ansi.Print("world"),
},
},
{
description: "something",
input: []byte("hello\x1b\n"),
actions: []ansi.Action{
ansi.Print("hello"),
ansi.Linebreak{},
},
},
} {
t.Run(tt.description, func(t *testing.T) {
g := NewGomegaWithT(t)
p := ansi.NewParser()
actions := p.ParseAll(tt.input)
g.Expect(actions).To(Equal(tt.actions))
})
}
}
func TestParser_Carryover(t *testing.T) {
format.UseStringerRepresentation = true
for _, tt := range []struct {
description string
inputs [][]byte
actions []ansi.Action
}{
{
description: "no partial escapes",
inputs: [][]byte{
[]byte("\x1b[31mred text\x1b[m"),
[]byte("\x1b[32mnow it's green"),
},
actions: []ansi.Action{
ansi.SetForeground(ansi.Red),
ansi.Print("red text"),
ansi.Reset{},
ansi.SetForeground(ansi.Green),
ansi.Print("now it's green"),
},
},
{
description: "partial escape sequence",
inputs: [][]byte{
[]byte("hello\x1b"),
[]byte("[32mgreen"),
},
actions: []ansi.Action{
ansi.Print("hello"),
ansi.SetForeground(ansi.Green),
ansi.Print("green"),
},
},
{
description: "partial escape sequence with bracket",
inputs: [][]byte{
[]byte("hello\x1b["),
[]byte("32mgreen"),
},
actions: []ansi.Action{
ansi.Print("hello"),
ansi.SetForeground(ansi.Green),
ansi.Print("green"),
},
},
{
description: "partial escape sequence with bracket and code",
inputs: [][]byte{
[]byte("hello\x1b[32"),
[]byte("mgreen"),
},
actions: []ansi.Action{
ansi.Print("hello"),
ansi.SetForeground(ansi.Green),
ansi.Print("green"),
},
},
{
description: "partial escape sequence with bracket and codes",
inputs: [][]byte{
[]byte("hello\x1b[32;1"),
[]byte("mgreen and bold"),
},
actions: []ansi.Action{
ansi.Print("hello"),
ansi.SetForeground(ansi.Green),
ansi.SetBold(true),
ansi.Print("green and bold"),
},
},
{
description: "partial escape sequence with bracket codes split up",
inputs: [][]byte{
[]byte("hello\x1b[32;"),
[]byte("1mgreen and bold"),
},
actions: []ansi.Action{
ansi.Print("hello"),
ansi.SetForeground(ansi.Green),
ansi.SetBold(true),
ansi.Print("green and bold"),
},
},
{
description: "partial escape sequence with code split up",
inputs: [][]byte{
[]byte("hello\x1b[3"),
[]byte("2;1mgreen and bold"),
},
actions: []ansi.Action{
ansi.Print("hello"),
ansi.SetForeground(ansi.Green),
ansi.SetBold(true),
ansi.Print("green and bold"),
},
},
{
description: "incomplete rune",
inputs: [][]byte{
[]byte("hello \xe3\x81"),
},
actions: []ansi.Action{
ansi.Print("hello "),
},
},
{
description: "incomplete rune over multiple events",
inputs: [][]byte{
[]byte("hello \xe3"),
[]byte("\x81"),
[]byte("\x93"),
},
actions: []ansi.Action{
ansi.Print("hello "),
ansi.Print("こ"),
},
},
} {
t.Run(tt.description, func(t *testing.T) {
g := NewGomegaWithT(t)
p := ansi.NewParser()
var actions []ansi.Action
for _, input := range tt.inputs {
actions = append(actions, p.ParseAll(input)...)
}
g.Expect(actions).To(Equal(tt.actions))
})
}
}