Debian Codegraph @

ansi/parser_osc_test.go

raw
1 package ansi
2
3 import (
4 "fmt"
5 "strings"
6 "testing"
7 )
8
9 func TestOscSequence(t *testing.T) {
10 const maxBufferSize = 1024
11 cases := []testCase{
12 {
13 name: "parse",
14 input: "\x1b]2;charmbracelet: ~/Source/bubbletea\x07",
15 expected: []any{
16 []byte("2;charmbracelet: ~/Source/bubbletea"),
17 },
18 },
19 {
20 name: "empty",
21 input: "\x1b]\x07",
22 expected: []any{
23 []byte{},
24 },
25 },
26 {
27 name: "max_params",
28 input: fmt.Sprintf("\x1b]%s\x1b\\", strings.Repeat(";", 17)),
29 expected: []any{
30 []byte(strings.Repeat(";", 17)),
31 Cmd('\\'),
32 },
33 },
34 {
35 name: "bell_terminated",
36 input: "\x1b]11;ff/00/ff\x07",
37 expected: []any{
38 []byte("11;ff/00/ff"),
39 },
40 },
41 {
42 name: "esc_st_terminated",
43 input: "\x1b]11;ff/00/ff\x1b\\",
44 expected: []any{
45 []byte("11;ff/00/ff"),
46 Cmd('\\'),
47 },
48 },
49 {
50 name: "utf8",
51 input: string([]byte{
52 0x1b, 0x5d, 0x32, 0x3b, 0x65, 0x63, 0x68, 0x6f, 0x20, 0x27,
53 0xc2, 0xaf, 0x5c, 0x5f, 0x28, 0xe3, 0x83, 0x84, 0x29, 0x5f,
54 0x2f, 0xc2, 0xaf, 0x27, 0x20, 0x26, 0x26, 0x20, 0x73, 0x6c,
55 0x65, 0x65, 0x70, 0x20, 0x31, 0x9c,
56 }),
57 expected: []any{
58 []byte("2;echo '¯\\_(ツ)_/¯' && sleep 1"),
59 },
60 },
61 {
62 name: "string_terminator",
63 input: "\x1b]2;\xe6\x9c\xab\x1b\\",
64 expected: []any{
65 []byte("2;\xe6"),
66 Cmd('\\'),
67 },
68 },
69 {
70 name: "exceed_max_buffer_size",
71 input: fmt.Sprintf("\x1b]52;s%s\x07", strings.Repeat("a", maxBufferSize)),
72 expected: []any{
73 fmt.Appendf(nil, "52;s%s", strings.Repeat("a", maxBufferSize-4)), // 4 is the len of "52;s"
74 },
75 },
76 {
77 name: "title_empty_params_esc",
78 input: "\x1b]0;abc\x1b\\\x1b];;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\x07",
79 expected: []any{
80 []byte("0;abc"),
81 Cmd('\\'),
82 []byte(strings.Repeat(";", 45)),
83 },
84 },
85 {
86 name: "just command",
87 input: "\x1b]112\x07",
88 expected: []any{
89 []byte("112"),
90 },
91 },
92 }
93
94 for _, c := range cases {
95 t.Run(c.name, func(t *testing.T) {
96 dispatcher := &testDispatcher{}
97 parser := testParser(dispatcher)
98 parser.data = make([]byte, maxBufferSize)
99 parser.dataLen = maxBufferSize
100 parser.Parse([]byte(c.input))
101 assertEqual(t, len(c.expected), len(dispatcher.dispatched))
102 assertEqual(t, c.expected, dispatcher.dispatched)
103 })
104 }
105 }