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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package parser

import (
	"reflect"
	"testing"

	"avt-go/types"
)

// parse feeds the string through a fresh parser and returns all emitted Functions.
func parse(s string) []Function {
	p := New()
	var out []Function
	for _, ch := range s {
		if f := p.Feed(ch); f != nil {
			out = append(out, f)
		}
	}
	return out
}

func assertFuncs(t *testing.T, got []Function, want ...Function) {
	t.Helper()
	if len(got) != len(want) {
		t.Fatalf("len mismatch: got %d %v, want %d %v", len(got), got, len(want), want)
	}
	for i := range want {
		if !reflect.DeepEqual(got[i], want[i]) {
			t.Errorf("[%d] got %#v, want %#v", i, got[i], want[i])
		}
	}
}

func decModes(modes ...DecMode) []DecMode    { return modes }
func ansiModes(modes ...AnsiMode) []AnsiMode { return modes }
func sgrOps(ops ...SgrOp) []SgrOp            { return ops }

func TestParseC0(t *testing.T) {
	assertFuncs(t, parse("\x08"), FuncBs{})
	assertFuncs(t, parse("\x0a"), FuncLf{})
	assertFuncs(t, parse("\x0d"), FuncCr{})
	assertFuncs(t, parse("\x0e"), FuncSo{})
	assertFuncs(t, parse("\x0f"), FuncSi{})
}

func TestParseC1(t *testing.T) {
	assertFuncs(t, parse("\u0084"), FuncLf{})
	assertFuncs(t, parse("\u0085"), FuncNel{})
	assertFuncs(t, parse("\u0088"), FuncHts{})
	assertFuncs(t, parse("\u008d"), FuncRi{})
}

func TestParseEscSeq(t *testing.T) {
	assertFuncs(t, parse("\x1b7"), FuncDecsc{})
	assertFuncs(t, parse("\x1b8"), FuncDecrc{})
	assertFuncs(t, parse("\x1bc"), FuncRis{})
	assertFuncs(t, parse("\x1bM"), FuncRi{})
	assertFuncs(t, parse("\x1b#8"), FuncDecaln{})
	assertFuncs(t, parse("\x1b(0"), FuncGzd4{Charset: types.CharsetDrawing})
	assertFuncs(t, parse("\x1b(B"), FuncGzd4{Charset: types.CharsetAscii})
	assertFuncs(t, parse("\x1b)0"), FuncG1d4{Charset: types.CharsetDrawing})
	assertFuncs(t, parse("\x1b)B"), FuncG1d4{Charset: types.CharsetAscii})
}

func TestParseCsiSeq(t *testing.T) {
	// Cursor movement and positioning.
	assertFuncs(t, parse("\x1b[A"), FuncCuu{N: 0})
	assertFuncs(t, parse("\x1b[B"), FuncCud{N: 0})
	assertFuncs(t, parse("\x1b[2B"), FuncCud{N: 2})
	assertFuncs(t, parse("\x1b[C"), FuncCuf{N: 0})
	assertFuncs(t, parse("\x1b[3C"), FuncCuf{N: 3})
	assertFuncs(t, parse("\x1b[D"), FuncCub{N: 0})
	assertFuncs(t, parse("\x1b[4D"), FuncCub{N: 4})
	assertFuncs(t, parse("\x1b[5E"), FuncCnl{N: 5})
	assertFuncs(t, parse("\x1b[6F"), FuncCpl{N: 6})
	assertFuncs(t, parse("\x1b[G"), FuncCha{N: 0})
	assertFuncs(t, parse("\x1b[7G"), FuncCha{N: 7})
	assertFuncs(t, parse("\x1b[H"), FuncCup{Row: 0, Col: 0})
	assertFuncs(t, parse("\x1b[3;4H"), FuncCup{Row: 3, Col: 4})
	assertFuncs(t, parse("\u009b3;4H"), FuncCup{Row: 3, Col: 4})
	assertFuncs(t, parse("\x1b[8I"), FuncCht{N: 8})
	assertFuncs(t, parse("\x1b[2Z"), FuncCbt{N: 2})
	assertFuncs(t, parse("\x1b[`"), FuncCha{N: 0})
	assertFuncs(t, parse("\x1b[9`"), FuncCha{N: 9})
	assertFuncs(t, parse("\x1b[10a"), FuncCuf{N: 10})
	assertFuncs(t, parse("\x1b[11b"), FuncRep{N: 11})
	assertFuncs(t, parse("\x1b[12d"), FuncVpa{N: 12})
	assertFuncs(t, parse("\x1b[13e"), FuncVpr{N: 13})
	assertFuncs(t, parse("\x1b[f"), FuncCup{Row: 0, Col: 0})
	assertFuncs(t, parse("\x1b[14;15f"), FuncCup{Row: 14, Col: 15})

	// Erase, insert/delete, scrolling, tab control.
	assertFuncs(t, parse("\x1b[@"), FuncIch{N: 0})
	assertFuncs(t, parse("\x1b[J"), FuncEd{Scope: EdScopeBelow})
	assertFuncs(t, parse("\x1b[0J"), FuncEd{Scope: EdScopeBelow})
	assertFuncs(t, parse("\x1b[1J"), FuncEd{Scope: EdScopeAbove})
	assertFuncs(t, parse("\x1b[2J"), FuncEd{Scope: EdScopeAll})
	assertFuncs(t, parse("\u009b2J"), FuncEd{Scope: EdScopeAll})
	assertFuncs(t, parse("\x1b[3J"), FuncEd{Scope: EdScopeSavedLines})
	assertFuncs(t, parse("\x1b[K"), FuncEl{Scope: ElScopeToRight})
	assertFuncs(t, parse("\x1b[0K"), FuncEl{Scope: ElScopeToRight})
	assertFuncs(t, parse("\x1b[1K"), FuncEl{Scope: ElScopeToLeft})
	assertFuncs(t, parse("\x1b[2K"), FuncEl{Scope: ElScopeAll})
	assertFuncs(t, parse("\x1b[16L"), FuncIl{N: 16})
	assertFuncs(t, parse("\x1b[17M"), FuncDl{N: 17})
	assertFuncs(t, parse("\x1b[18P"), FuncDch{N: 18})
	assertFuncs(t, parse("\x1b[19S"), FuncSu{N: 19})
	assertFuncs(t, parse("\x1b[20T"), FuncSd{N: 20})
	assertFuncs(t, parse("\x1b[W"), FuncCtc{Op: CtcOpSet})
	assertFuncs(t, parse("\x1b[2W"), FuncCtc{Op: CtcOpClearCurrentColumn})
	assertFuncs(t, parse("\x1b[5W"), FuncCtc{Op: CtcOpClearAll})
	assertFuncs(t, parse("\x1b[21X"), FuncEch{N: 21})
	assertFuncs(t, parse("\x1b[g"), FuncTbc{Scope: TbcScopeCurrentColumn})
	assertFuncs(t, parse("\x1b[3g"), FuncTbc{Scope: TbcScopeAll})

	// ANSI modes and generic CSI.
	assertFuncs(t, parse("\x1b[4;20h"), FuncSm{Modes: ansiModes(AnsiModeInsert, AnsiModeNewLine)})
	assertFuncs(t, parse("\x1b[4;20l"), FuncRm{Modes: ansiModes(AnsiModeInsert, AnsiModeNewLine)})
	assertFuncs(t, parse("\x1b[m"), FuncSgr{Ops: sgrOps(SgrReset{})})
	assertFuncs(t, parse("\x1b[2;5r"), FuncDecstbm{Top: 2, Bottom: 5})
	assertFuncs(t, parse("\x1b[8;24;80t"), FuncXtwinops{Op: XtwinopsOp{Cols: 80, Rows: 24}})
	assertFuncs(t, parse("\x1b[s"), FuncScosc{})
	assertFuncs(t, parse("\x1b[u"), FuncScorc{})
	assertFuncs(t, parse("\x1b[!p"), FuncDecstr{})

	// DEC private modes.
	assertFuncs(t, parse("\x1b[?7h"), FuncDecset{Modes: decModes(DecModeAutoWrap)})
	assertFuncs(t, parse("\u009b?7h"), FuncDecset{Modes: decModes(DecModeAutoWrap)})
	assertFuncs(t, parse("\x1b[?6;1047h"), FuncDecset{Modes: decModes(DecModeOrigin, DecModeAltScreenBuffer)})
	assertFuncs(t, parse("\x1b[?47h"), FuncDecset{Modes: decModes(DecModeAltScreenBuffer)})
	assertFuncs(t, parse("\x1b[?1049h"), FuncDecset{Modes: decModes(DecModeSaveCursorAltScreen)})
	assertFuncs(t, parse("\u009b?1049h"), FuncDecset{Modes: decModes(DecModeSaveCursorAltScreen)})
	assertFuncs(t, parse("\x1b[?7l"), FuncDecrst{Modes: decModes(DecModeAutoWrap)})
	assertFuncs(t, parse("\u009b?7l"), FuncDecrst{Modes: decModes(DecModeAutoWrap)})
	assertFuncs(t, parse("\x1b[?47l"), FuncDecrst{Modes: decModes(DecModeAltScreenBuffer)})
	assertFuncs(t, parse("\x1b[?6;1049l"), FuncDecrst{Modes: decModes(DecModeOrigin, DecModeSaveCursorAltScreen)})
	assertFuncs(t, parse("\u009b?6;1049l"), FuncDecrst{Modes: decModes(DecModeOrigin, DecModeSaveCursorAltScreen)})
}

func TestParsePartialAndInterrupted(t *testing.T) {
	p := New()

	feed := func(s string) []Function {
		var out []Function
		for _, ch := range s {
			if f := p.Feed(ch); f != nil {
				out = append(out, f)
			}
		}
		return out
	}
	feedOne := func(ch rune) Function { return p.Feed(ch) }

	// Normal sequence: ESC [ 3 ; 4 H -> Cup(3,4)
	if f := feedOne('\x1b'); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('['); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('3'); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne(';'); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('4'); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('H'); !reflect.DeepEqual(f, FuncCup{Row: 3, Col: 4}) {
		t.Errorf("got %v", f)
	}

	// Interrupted by another ESC
	if f := feedOne('\x1b'); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('['); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('3'); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('\x1b'); f != nil {
		t.Error("expected nil")
	}
	if f := feedOne('M'); !reflect.DeepEqual(f, FuncRi{}) {
		t.Errorf("got %v", f)
	}

	// ESC cancelled by 0x18 -> ground -> next char prints
	feed("\x1b")
	if p.State != StateEscape {
		t.Errorf("expected Escape, got %v", p.State)
	}
	if f := feedOne('\u0018'); f != nil {
		t.Error("expected nil")
	}
	if p.State != StateGround {
		t.Errorf("expected Ground, got %v", p.State)
	}
	if f := feedOne('A'); !reflect.DeepEqual(f, FuncPrint{Char: 'A'}) {
		t.Errorf("got %v", f)
	}

	// CSI param cancelled by 0x1a
	feed("\x1b[12")
	if p.State != StateCsiParam {
		t.Errorf("expected CsiParam, got %v", p.State)
	}
	if f := feedOne('\u001a'); f != nil {
		t.Error("expected nil")
	}
	if p.State != StateGround {
		t.Errorf("expected Ground, got %v", p.State)
	}
	if f := feedOne('B'); !reflect.DeepEqual(f, FuncPrint{Char: 'B'}) {
		t.Errorf("got %v", f)
	}

	// OSC cancelled by 0x18
	feed("\x1b]title")
	if p.State != StateOscString {
		t.Errorf("expected OscString, got %v", p.State)
	}
	if f := feedOne('\u0018'); f != nil {
		t.Error("expected nil")
	}
	if p.State != StateGround {
		t.Errorf("expected Ground, got %v", p.State)
	}
	if f := feedOne('C'); !reflect.DeepEqual(f, FuncPrint{Char: 'C'}) {
		t.Errorf("got %v", f)
	}

	// DCS param cancelled by 0x1a
	feed("\x1bP1;2")
	if p.State != StateDcsParam {
		t.Errorf("expected DcsParam, got %v", p.State)
	}
	if f := feedOne('\u001a'); f != nil {
		t.Error("expected nil")
	}
	if p.State != StateGround {
		t.Errorf("expected Ground, got %v", p.State)
	}
	if f := feedOne('D'); !reflect.DeepEqual(f, FuncPrint{Char: 'D'}) {
		t.Errorf("got %v", f)
	}
}

func TestIgnoreUnsupportedSeq(t *testing.T) {
	if got := parse("\x1b[4q"); len(got) != 0 {
		t.Errorf("expected empty, got %v", got)
	}
	if got := parse("\x1b[9W"); len(got) != 0 {
		t.Errorf("expected empty, got %v", got)
	}
	// Unknown DEC mode -> Decset with empty modes
	assertFuncs(t, parse("\x1b[?9999h"), FuncDecset{Modes: decModes()})
	if got := parse("\x1b[:m"); len(got) != 0 {
		t.Errorf("expected empty, got %v", got)
	}
	if got := parse("\x1b[1?m"); len(got) != 0 {
		t.Errorf("expected empty, got %v", got)
	}
	if got := parse("\x1b[ 1H"); len(got) != 0 {
		t.Errorf("expected empty, got %v", got)
	}
	if got := parse("\x1b[ 1m"); len(got) != 0 {
		t.Errorf("expected empty, got %v", got)
	}
	if got := parse("\x1b[1 q"); len(got) != 0 {
		t.Errorf("expected empty, got %v", got)
	}
	// Incomplete 24-bit color -> Sgr with empty ops
	assertFuncs(t, parse("\x1b[38;2m"), FuncSgr{Ops: sgrOps()})
	assertFuncs(t, parse("\x1b[48;5m"), FuncSgr{Ops: sgrOps()})
}

func TestParseSgrSeq(t *testing.T) {
	assertFuncs(t, parse("\x1b[;1;m"), FuncSgr{Ops: sgrOps(SgrReset{}, SgrSetBoldIntensity{}, SgrReset{})})
	assertFuncs(t, parse("\x1b[1m"), FuncSgr{Ops: sgrOps(SgrSetBoldIntensity{})})
	assertFuncs(t, parse("\x1b[2m"), FuncSgr{Ops: sgrOps(SgrSetFaintIntensity{})})
	assertFuncs(t, parse("\x1b[3m"), FuncSgr{Ops: sgrOps(SgrSetItalic{})})
	assertFuncs(t, parse("\x1b[4m"), FuncSgr{Ops: sgrOps(SgrSetUnderline{})})
	assertFuncs(t, parse("\x1b[5m"), FuncSgr{Ops: sgrOps(SgrSetBlink{})})
	assertFuncs(t, parse("\x1b[7m"), FuncSgr{Ops: sgrOps(SgrSetInverse{})})
	assertFuncs(t, parse("\x1b[9m"), FuncSgr{Ops: sgrOps(SgrSetStrikethrough{})})
	assertFuncs(t, parse("\x1b[21m"), FuncSgr{Ops: sgrOps(SgrResetIntensity{})})
	assertFuncs(t, parse("\x1b[22m"), FuncSgr{Ops: sgrOps(SgrResetIntensity{})})
	assertFuncs(t, parse("\x1b[23m"), FuncSgr{Ops: sgrOps(SgrResetItalic{})})
	assertFuncs(t, parse("\x1b[24m"), FuncSgr{Ops: sgrOps(SgrResetUnderline{})})
	assertFuncs(t, parse("\x1b[25m"), FuncSgr{Ops: sgrOps(SgrResetBlink{})})
	assertFuncs(t, parse("\x1b[27m"), FuncSgr{Ops: sgrOps(SgrResetInverse{})})
	assertFuncs(t, parse("\x1b[29m"), FuncSgr{Ops: sgrOps(SgrResetStrikethrough{})})

	assertFuncs(t, parse("\x1b[31m"), FuncSgr{Ops: sgrOps(SgrSetForegroundColor{Color: types.ColorIndexed{N: 1}})})

	// sub-param form: 38:2:r:g:b
	assertFuncs(t, parse("\x1b[38:2:1:2:3m"), FuncSgr{Ops: sgrOps(SgrSetForegroundColor{Color: types.ColorRGB{R: 1, G: 2, B: 3}})})
	// sub-param with extra colon: 38:2::r:g:b
	assertFuncs(t, parse("\x1b[38:2::1:2:3m"), FuncSgr{Ops: sgrOps(SgrSetForegroundColor{Color: types.ColorRGB{R: 1, G: 2, B: 3}})})

	assertFuncs(t, parse("\x1b[38:5:88m"), FuncSgr{Ops: sgrOps(SgrSetForegroundColor{Color: types.ColorIndexed{N: 88}})})
	assertFuncs(t, parse("\x1b[39m"), FuncSgr{Ops: sgrOps(SgrResetForegroundColor{})})

	assertFuncs(t, parse("\x1b[41m"), FuncSgr{Ops: sgrOps(SgrSetBackgroundColor{Color: types.ColorIndexed{N: 1}})})

	// bright colors: 91-97 -> indexed 9-15
	assertFuncs(t, parse("\x1b[91m"), FuncSgr{Ops: sgrOps(SgrSetForegroundColor{Color: types.ColorIndexed{N: 9}})})

	assertFuncs(t, parse("\x1b[48:2:1:2:3m"), FuncSgr{Ops: sgrOps(SgrSetBackgroundColor{Color: types.ColorRGB{R: 1, G: 2, B: 3}})})
	assertFuncs(t, parse("\x1b[48:2::1:2:3m"), FuncSgr{Ops: sgrOps(SgrSetBackgroundColor{Color: types.ColorRGB{R: 1, G: 2, B: 3}})})
	assertFuncs(t, parse("\x1b[48:5:99m"), FuncSgr{Ops: sgrOps(SgrSetBackgroundColor{Color: types.ColorIndexed{N: 99}})})
	assertFuncs(t, parse("\x1b[49m"), FuncSgr{Ops: sgrOps(SgrResetBackgroundColor{})})

	// bright bg: 100-107 -> indexed 8-15
	assertFuncs(t, parse("\x1b[104m"), FuncSgr{Ops: sgrOps(SgrSetBackgroundColor{Color: types.ColorIndexed{N: 12}})})

	// legacy multi-param 24-bit fg + bg
	assertFuncs(t, parse("\x1b[1;38;2;1;2;3;48;2;1;2;3;0m"), FuncSgr{Ops: sgrOps(
		SgrSetBoldIntensity{},
		SgrSetForegroundColor{Color: types.ColorRGB{R: 1, G: 2, B: 3}},
		SgrSetBackgroundColor{Color: types.ColorRGB{R: 1, G: 2, B: 3}},
		SgrReset{},
	)})

	// legacy multi-param 8-bit
	assertFuncs(t, parse("\x1b[1;38;5;88;48;5;99;0m"), FuncSgr{Ops: sgrOps(
		SgrSetBoldIntensity{},
		SgrSetForegroundColor{Color: types.ColorIndexed{N: 88}},
		SgrSetBackgroundColor{Color: types.ColorIndexed{N: 99}},
		SgrReset{},
	)})
}

func TestParseStringSeq(t *testing.T) {
	// OSC terminated by BEL
	assertFuncs(t, parse("\x1b]title\x07A"), FuncPrint{Char: 'A'})
	// OSC terminated by ST (ESC \)
	assertFuncs(t, parse("\x1b]title\x1b\\A"), FuncPrint{Char: 'A'})
	// C1 OSC + ST
	assertFuncs(t, parse("\u009dtitle\u009cA"), FuncPrint{Char: 'A'})
	// DCS
	assertFuncs(t, parse("\x1bPabc\u009cA"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\x1bPabc\x1b\\A"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\u0090abc\u009cA"), FuncPrint{Char: 'A'})
	// SOS PM APC
	assertFuncs(t, parse("\x1bXabc\u009cA"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\x1bXabc\x1b\\A"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\x1b^abc\u009cA"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\x1b^abc\x1b\\A"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\x1b_abc\u009cA"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\x1b_abc\x1b\\A"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\u0098abc\u009cA"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\u009eabc\u009cA"), FuncPrint{Char: 'A'})
	assertFuncs(t, parse("\u009fabc\u009cA"), FuncPrint{Char: 'A'})
}

func TestParseUnicode(t *testing.T) {
	assertFuncs(t, parse("日"), FuncPrint{Char: '日'})
	// Unicode in CSI params is ignored, only final byte matters
	assertFuncs(t, parse("\x1b[日A"), FuncPrint{Char: 'A'})
}

func TestDumpNonGroundStates(t *testing.T) {
	assertDump := func(input string, wantState State, wantDump string) {
		t.Helper()
		p := New()
		for _, ch := range input {
			p.Feed(ch)
		}
		if p.State != wantState {
			t.Errorf("input %q: state got %v, want %v", input, p.State, wantState)
		}
		if got := p.Dump(); got != wantDump {
			t.Errorf("input %q: dump got %q, want %q", input, got, wantDump)
		}
	}

	assertDump("\x1b", StateEscape, "\x1b")
	assertDump("\x1b(", StateEscapeIntermediate, "\x1b(")
	assertDump("\x1b[", StateCsiEntry, "\u009b")
	assertDump("\x1b[ ", StateCsiIntermediate, "\u009b ")
	assertDump("\x1b[:", StateCsiIgnore, "\u009b\u003a")
	assertDump("\x1bP", StateDcsEntry, "\u0090")
	assertDump("\x1bP ", StateDcsIntermediate, "\u0090 ")
	assertDump("\x1bP1;2", StateDcsParam, "\u00901;2")
	assertDump("\x1bPz", StateDcsPassthrough, "\u0090\u0040")
	assertDump("\x1bP:", StateDcsIgnore, "\u0090\u003a")
	assertDump("\x1b]", StateOscString, "\u009d")
	assertDump("\x1bX", StateSosPmApcString, "\u0098")
}

func TestDumpInProgress(t *testing.T) {
	p := New()
	for _, ch := range "\x1b[;1;;38:2:1:2:3;" {
		p.Feed(ch)
	}
	got := p.Dump()
	want := "\u009b0;1;0;38:2:1:2:3;0"
	if got != want {
		t.Errorf("got %q, want %q", got, want)
	}
}

func TestDumpFunctionsRoundtrip(t *testing.T) {
	functions := []Function{
		FuncBs{},
		FuncCbt{N: 2},
		FuncCha{N: 7},
		FuncCht{N: 8},
		FuncCnl{N: 5},
		FuncCpl{N: 6},
		FuncCr{},
		FuncCtc{Op: CtcOpSet},
		FuncCtc{Op: CtcOpClearCurrentColumn},
		FuncCtc{Op: CtcOpClearAll},
		FuncCub{N: 4},
		FuncCud{N: 2},
		FuncCuf{N: 3},
		FuncCup{Row: 3, Col: 4},
		FuncCuu{N: 1},
		FuncDch{N: 18},
		FuncDecaln{},
		FuncDecrc{},
		FuncDecrst{Modes: decModes()},
		FuncDecrst{Modes: decModes(DecModeCursorKeys, DecModeOrigin, DecModeAutoWrap, DecModeTextCursorEnable, DecModeAltScreenBuffer, DecModeSaveCursor, DecModeSaveCursorAltScreen)},
		FuncDecsc{},
		FuncDecset{Modes: decModes()},
		FuncDecset{Modes: decModes(DecModeCursorKeys, DecModeOrigin, DecModeAutoWrap, DecModeTextCursorEnable, DecModeAltScreenBuffer, DecModeSaveCursor, DecModeSaveCursorAltScreen)},
		FuncDecstbm{Top: 2, Bottom: 5},
		FuncDecstr{},
		FuncDl{N: 17},
		FuncEch{N: 21},
		FuncEd{Scope: EdScopeBelow},
		FuncEd{Scope: EdScopeAbove},
		FuncEd{Scope: EdScopeAll},
		FuncEd{Scope: EdScopeSavedLines},
		FuncEl{Scope: ElScopeToRight},
		FuncEl{Scope: ElScopeToLeft},
		FuncEl{Scope: ElScopeAll},
		FuncG1d4{Charset: types.CharsetDrawing},
		FuncG1d4{Charset: types.CharsetAscii},
		FuncGzd4{Charset: types.CharsetDrawing},
		FuncGzd4{Charset: types.CharsetAscii},
		FuncHt{},
		FuncHts{},
		FuncIch{N: 16},
		FuncIl{N: 16},
		FuncLf{},
		FuncNel{},
		FuncPrint{Char: 'A'},
		FuncPrint{Char: '日'},
		FuncRep{N: 11},
		FuncRi{},
		FuncRis{},
		FuncRm{Modes: ansiModes()},
		FuncRm{Modes: ansiModes(AnsiModeInsert, AnsiModeNewLine)},
		FuncScorc{},
		FuncScosc{},
		FuncSd{N: 20},
		FuncSgr{Ops: sgrOps()},
		FuncSgr{Ops: sgrOps(
			SgrReset{},
			SgrSetBoldIntensity{},
			SgrSetFaintIntensity{},
			SgrSetItalic{},
			SgrSetUnderline{},
			SgrSetBlink{},
			SgrSetInverse{},
			SgrSetStrikethrough{},
			SgrResetIntensity{},
			SgrResetItalic{},
			SgrResetUnderline{},
			SgrResetBlink{},
			SgrResetInverse{},
			SgrResetStrikethrough{},
			SgrSetForegroundColor{Color: types.ColorIndexed{N: 1}},
			SgrResetForegroundColor{},
			SgrSetBackgroundColor{Color: types.ColorRGB{R: 1, G: 2, B: 3}},
			SgrResetBackgroundColor{},
		)},
		FuncSi{},
		FuncSm{Modes: ansiModes()},
		FuncSm{Modes: ansiModes(AnsiModeInsert, AnsiModeNewLine)},
		FuncSo{},
		FuncSu{N: 19},
		FuncTbc{Scope: TbcScopeCurrentColumn},
		FuncTbc{Scope: TbcScopeAll},
		FuncVpa{N: 12},
		FuncVpr{N: 13},
		FuncXtwinops{Op: XtwinopsOp{Cols: 80, Rows: 24}},
	}

	seq := DumpFunctions(functions)
	got := parse(seq)
	if !reflect.DeepEqual(got, functions) {
		t.Errorf("roundtrip mismatch:\ngot:  %v\nwant: %v", got, functions)
	}
}