File: parse_test.go

package info (click to toggle)
golang-github-charmbracelet-x 0.0~git20251028.0cf22f8%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,940 kB
  • sloc: sh: 124; makefile: 5
file content (47 lines) | stat: -rw-r--r-- 1,323 bytes parent folder | download
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
package input

import (
	"image/color"
	"reflect"
	"testing"

	"github.com/charmbracelet/x/ansi"
)

func TestParseSequence_Events(t *testing.T) {
	input := []byte("\x1b\x1b[Ztest\x00\x1b]10;rgb:1234/1234/1234\x07\x1b[27;2;27~\x1b[?1049;2$y\x1b[4;1$y")
	want := []Event{
		KeyPressEvent{Code: KeyTab, Mod: ModShift | ModAlt},
		KeyPressEvent{Code: 't', Text: "t"},
		KeyPressEvent{Code: 'e', Text: "e"},
		KeyPressEvent{Code: 's', Text: "s"},
		KeyPressEvent{Code: 't', Text: "t"},
		KeyPressEvent{Code: KeySpace, Mod: ModCtrl},
		ForegroundColorEvent{color.RGBA{R: 0x12, G: 0x12, B: 0x12, A: 0xff}},
		KeyPressEvent{Code: KeyEscape, Mod: ModShift},
		ModeReportEvent{Mode: ansi.AltScreenSaveCursorMode, Value: ansi.ModeReset},
		ModeReportEvent{Mode: ansi.InsertReplaceMode, Value: ansi.ModeSet},
	}

	var p Parser
	for i := 0; len(input) != 0; i++ {
		if i >= len(want) {
			t.Fatalf("reached end of want events")
		}
		n, got := p.parseSequence(input)
		if !reflect.DeepEqual(got, want[i]) {
			t.Errorf("got %#v (%T), want %#v (%T)", got, got, want[i], want[i])
		}
		input = input[n:]
	}
}

func BenchmarkParseSequence(b *testing.B) {
	var p Parser
	input := []byte("\x1b\x1b[Ztest\x00\x1b]10;1234/1234/1234\x07\x1b[27;2;27~")
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		p.parseSequence(input)
	}
}