| 1 | package input |
| 2 | |
| 3 | import ( |
| 4 | "image/color" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/charmbracelet/x/ansi" |
| 9 | ) |
| 10 | |
| 11 | func TestParseSequence_Events(t *testing.T) { |
| 12 | 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") |
| 13 | want := []Event{ |
| 14 | KeyPressEvent{Code: KeyTab, Mod: ModShift | ModAlt}, |
| 15 | KeyPressEvent{Code: 't', Text: "t"}, |
| 16 | KeyPressEvent{Code: 'e', Text: "e"}, |
| 17 | KeyPressEvent{Code: 's', Text: "s"}, |
| 18 | KeyPressEvent{Code: 't', Text: "t"}, |
| 19 | KeyPressEvent{Code: KeySpace, Mod: ModCtrl}, |
| 20 | ForegroundColorEvent{color.RGBA{R: 0x12, G: 0x12, B: 0x12, A: 0xff}}, |
| 21 | KeyPressEvent{Code: KeyEscape, Mod: ModShift}, |
| 22 | ModeReportEvent{Mode: ansi.AltScreenSaveCursorMode, Value: ansi.ModeReset}, |
| 23 | ModeReportEvent{Mode: ansi.InsertReplaceMode, Value: ansi.ModeSet}, |
| 24 | } |
| 25 | |
| 26 | var p Parser |
| 27 | for i := 0; len(input) != 0; i++ { |
| 28 | if i >= len(want) { |
| 29 | t.Fatalf("reached end of want events") |
| 30 | } |
| 31 | n, got := p.parseSequence(input) |
| 32 | if !reflect.DeepEqual(got, want[i]) { |
| 33 | t.Errorf("got %#v (%T), want %#v (%T)", got, got, want[i], want[i]) |
| 34 | } |
| 35 | input = input[n:] |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func BenchmarkParseSequence(b *testing.B) { |
| 40 | var p Parser |
| 41 | input := []byte("\x1b\x1b[Ztest\x00\x1b]10;1234/1234/1234\x07\x1b[27;2;27~") |
| 42 | b.ReportAllocs() |
| 43 | b.ResetTimer() |
| 44 | for i := 0; i < b.N; i++ { |
| 45 | p.parseSequence(input) |
| 46 | } |
| 47 | } |