Debian Codegraph @

termseq_test.go

raw
1 package main
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/gdamore/tcell/v3"
8 "github.com/gdamore/tcell/v3/color"
9 )
10
11 func TestStripTermSequence(t *testing.T) {
12 tests := []struct {
13 s string
14 exp string
15 }{
16 {"", ""}, // empty
17 {"foo bar", "foo bar"}, // plain text
18 {"\033[31mRed\033[0m", "Red"}, // octal syntax
19 {"\x1b[31mRed\x1b[0m", "Red"}, // hexadecimal syntax
20 {"foo\x1b[31mRed", "fooRed"}, // no reset parameter
21 {
22 "foo\x1b[1;31;102mBoldRedGreen\x1b[0mbar",
23 "fooBoldRedGreenbar",
24 }, // multiple attributes
25 {
26 "misc.go:func \x1b[01;31m\x1b[KstripTermSequence\x1b[m\x1b[K(s string) string {",
27 "misc.go:func stripTermSequence(s string) string {",
28 }, // `grep` output containing `erase in line` sequence
29
30 // OSC 8 hyperlinks
31 {
32 "\x1b]8;;https://example.com\x1b\\example.com\x1b]8;;\x1b\\",
33 "example.com",
34 }, // open/close with ST (ESC\)
35 {
36 "\x1b]8;;https://example.com\x07example.com\x1b]8;;\x07",
37 "example.com",
38 }, // open/close with BEL
39 {
40 "\x1b]8;id=42;https://example.com\x1b\\label\x1b]8;;\x1b\\",
41 "label",
42 }, // params present
43 {
44 "\x1b]8;;https://example.com\x1b\\example.com",
45 "example.com",
46 }, // open without close
47 }
48
49 for _, test := range tests {
50 if got := stripTermSequence(test.s); got != test.exp {
51 t.Errorf("at input %q expected %q but got %q", test.s, test.exp, got)
52 }
53 // we rely on both functions extracting the same runes
54 // to avoid misalignment
55 if printLength(test.s) != len(stripTermSequence(test.s)) {
56 t.Errorf("at input %q expected '%d' but got '%d'", test.s, printLength(test.s), len(stripTermSequence(test.s)))
57 }
58 }
59 }
60
61 func TestReadTermSequence(t *testing.T) {
62 tests := []struct {
63 s, exp string
64 }{
65 {"", ""}, // empty
66 {"foo bar", ""}, // plain text
67 {"\x1b", ""}, // lone ESC
68 {"\x1bX", ""}, // unknown ESC sequence
69
70 {"\x1b[31m", "\x1b[31m"}, // CSI SGR
71 {"\x1b[K", "\x1b[K"}, // CSI EL
72 {"\x1b[1;31m", "\x1b[1;31m"}, // CSI SGR (multiple params)
73 {"\x1b[31", ""}, // CSI incomplete (no terminator)
74 {"foo\x1b[31m", ""}, // doesn't start with ESC
75
76 // CSI sequences with non-SGR/EL final bytes must be rejected.
77 // The final byte is the first byte in 0x40-0x7E (per ECMA-48).
78 {"\x1b[2J", ""}, // erase display (final byte 'J')
79 {"\x1b[H", ""}, // cursor home (final byte 'H')
80 {"\x1b[6n", ""}, // device status report (final byte 'n')
81 {"\x1b[?25l", ""}, // cursor hide (final byte 'l')
82 {"\x1b[?25h", ""}, // cursor show (final byte 'h')
83 {"\x1b[10;1H", ""}, // cursor position (final byte 'H')
84 {"\x1b[3S", ""}, // scroll up (final byte 'S')
85 {"\x1b[3T", ""}, // scroll down (final byte 'T')
86 {"\x1b[?1049h", ""}, // alt screen (final byte 'h')
87
88 // Regression: dangerous CSI followed by text containing 'm' or 'K'.
89 // The old parser searched for 'm'/'K' anywhere in the next 64 bytes,
90 // which would incorrectly consume the text after the CSI sequence.
91 {"\x1b[2Jsome text with m", ""}, // 'J' is the final byte, not 'm'
92 {"\x1b[6nfile.mkv", ""}, // 'n' is the final byte, not the later 'k'
93 {"\x1b[Htext_with_K_in_it", ""}, // 'H' is the final byte, not 'K'
94 {"\x1b[?25lhidden_cursor_m", ""}, // 'l' is the final byte, not 'm'
95 {"\x1b[2JKKKK", ""}, // 'J' is the final byte, not 'K'
96 {"\x1b[6nmmm", ""}, // 'n' is the final byte, not 'm'
97 {"\x1b[3SMakefile", ""}, // 'S' is the final byte, not later 'e'
98
99 // Valid SGR and EL must still work when followed by text with m/K
100 {"\x1b[31mtext with K", "\x1b[31m"}, // 'm' is correctly the final byte
101 {"\x1b[Krest of line m", "\x1b[K"}, // 'K' is correctly the final byte
102 {"\x1b[0Kmore text m", "\x1b[0K"}, // 'K' is correctly the final byte
103
104 // Every CSI final byte (0x40-0x7E): only 'm' (0x6D) and 'K' (0x4B) accepted
105 {"\x1b[1@", ""}, // ICH
106 {"\x1b[1A", ""}, // CUU
107 {"\x1b[1B", ""}, // CUD
108 {"\x1b[1C", ""}, // CUF
109 {"\x1b[1D", ""}, // CUB
110 {"\x1b[1E", ""}, // CNL
111 {"\x1b[1F", ""}, // CPL
112 {"\x1b[1G", ""}, // CHA
113 {"\x1b[1J", ""}, // ED
114 {"\x1b[1K", "\x1b[1K"}, // EL — accepted
115 {"\x1b[1L", ""}, // IL
116 {"\x1b[1M", ""}, // DL
117 {"\x1b[1P", ""}, // DCH
118 {"\x1b[1S", ""}, // SU
119 {"\x1b[1T", ""}, // SD
120 {"\x1b[1X", ""}, // ECH
121 {"\x1b[1Z", ""}, // CBT
122 {"\x1b[1`", ""}, // HPA
123 {"\x1b[1a", ""}, // HPR
124 {"\x1b[1d", ""}, // VPA
125 {"\x1b[1f", ""}, // HVP
126 {"\x1b[1h", ""}, // SM
127 {"\x1b[1l", ""}, // RM
128 {"\x1b[1m", "\x1b[1m"}, // SGR — accepted
129 {"\x1b[1n", ""}, // DSR
130 {"\x1b[1p", ""}, // DECSTR (private)
131 {"\x1b[1q", ""}, // DECLL
132 {"\x1b[1r", ""}, // DECSTBM
133 {"\x1b[1t", ""}, // window ops
134 {"\x1b[1~", ""}, // vt key
135
136 {"\x1b]8;;https://example.com\x1b\\", "\x1b]8;;https://example.com\x1b\\"}, // OSC 8 (ST terminator)
137 {"\x1b]8;;https://example.com\x07", "\x1b]8;;https://example.com\x07"}, // OSC 8 (BEL terminator)
138 {"\x1b]0;title\x07", ""}, // non-OSC8 OSC (ignored)
139 }
140
141 for _, tc := range tests {
142 if got := readTermSequence(tc.s); got != tc.exp {
143 t.Errorf("input %q: got %q, want %q", tc.s, got, tc.exp)
144 }
145 }
146 }
147
148 func TestOptionToFmtstr(t *testing.T) {
149 tests := []struct {
150 s string
151 exp string
152 }{
153 {"\033[1m", "\033[1m%s\033[0m"},
154 {"\033[1;7;31;42m", "\033[1;7;31;42m%s\033[0m"},
155 }
156
157 for _, test := range tests {
158 if got := optionToFmtstr(test.s); got != test.exp {
159 t.Errorf("at input %q expected %q but got %q", test.s, test.exp, got)
160 }
161 }
162 }
163
164 func TestParseEscapeSequence(t *testing.T) {
165 tests := []struct {
166 s string
167 exp tcell.Style
168 }{
169 {"\033[1m", tcell.StyleDefault.Bold(true)},
170 {"\033[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(color.Maroon).Background(color.Green)},
171 }
172
173 for _, test := range tests {
174 if got := parseEscapeSequence(test.s); got != test.exp {
175 t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got)
176 }
177 }
178 }
179
180 func TestApplyTermSequence(t *testing.T) {
181 tests := []struct {
182 s string
183 exp tcell.Style
184 }{
185 {"", tcell.StyleDefault},
186 {"\x1b[1m", tcell.StyleDefault.Bold(true)},
187 {"\x1b[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(color.Maroon).Background(color.Green)},
188 {"\x1b]8;;https://example.com\x1b\\", tcell.StyleDefault.Url("https://example.com")}, // OSC 8 terminated with ST (ESC\), no `id` provided
189 {"\x1b]8;;https://example.com\x07", tcell.StyleDefault.Url("https://example.com")}, // OSC 8 terminated with BEL, no `id` provided
190 {"\x1b]8;id=42;https://example.com\x1b\\", tcell.StyleDefault.Url("https://example.com").UrlId("42")}, // OSC 8, `id` provided
191 }
192
193 for _, test := range tests {
194 if got := applyTermSequence(test.s, tcell.StyleDefault); !reflect.DeepEqual(got, test.exp) {
195 t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got)
196 }
197 }
198 }
199
200 func TestApplySGR(t *testing.T) {
201 none := tcell.StyleDefault
202
203 tests := []struct {
204 s string
205 st tcell.Style
206 stExp tcell.Style
207 }{
208 {"", none, none},
209 {"", none.Foreground(color.Maroon).Background(color.Maroon), none},
210 {"", none.Bold(true), none},
211 {"", none.Foreground(color.Maroon).Bold(true), none},
212
213 {"0", none, none},
214 {"0", none.Foreground(color.Maroon).Background(color.Maroon), none},
215 {"0", none.Bold(true), none},
216 {"0", none.Foreground(color.Maroon).Bold(true), none},
217
218 {"1", none, none.Bold(true)},
219 {"4", none, none.Underline(true)},
220 {"7", none, none.Reverse(true)},
221
222 {"1", none.Foreground(color.Maroon), none.Foreground(color.Maroon).Bold(true)},
223 {"4", none.Foreground(color.Maroon), none.Foreground(color.Maroon).Underline(true)},
224 {"7", none.Foreground(color.Maroon), none.Foreground(color.Maroon).Reverse(true)},
225
226 {"4", none.Bold(true), none.Bold(true).Underline(true)},
227 {"4", none.Foreground(color.Maroon).Bold(true), none.Foreground(color.Maroon).Bold(true).Underline(true)},
228
229 {"4:0", none, none},
230 {"4:0", none.Underline(true), none},
231 {"4:1", none, none.Underline(true)},
232 {"4:2", none, none.Underline(tcell.UnderlineStyleDouble)},
233 {"4:3", none, none.Underline(tcell.UnderlineStyleCurly)},
234 {"4:4", none, none.Underline(tcell.UnderlineStyleDotted)},
235 {"4:5", none, none.Underline(tcell.UnderlineStyleDashed)},
236
237 {"22", none.Italic(true).Bold(true).Dim(true), none.Italic(true)},
238 {"23", none.Bold(true).Italic(true), none.Bold(true)},
239 {"24", none.Bold(true).Underline(true), none.Bold(true)},
240 {"25", none.Bold(true).Blink(true), none.Bold(true)},
241 {"27", none.Bold(true).Reverse(true), none.Bold(true)},
242 {"29", none.Bold(true).StrikeThrough(true), none.Bold(true)},
243
244 {"31", none, none.Foreground(color.Maroon)},
245 {"31", none.Foreground(color.Green), none.Foreground(color.Maroon)},
246 {"31", none.Foreground(color.Green).Bold(true), none.Foreground(color.Maroon).Bold(true)},
247
248 {"41", none, none.Background(color.Maroon)},
249 {"41", none.Background(color.Green), none.Background(color.Maroon)},
250
251 {"1;31", none, none.Foreground(color.Maroon).Bold(true)},
252 {"1;31", none.Foreground(color.Green), none.Foreground(color.Maroon).Bold(true)},
253
254 {"01;31", none, none.Foreground(color.Maroon).Bold(true)},
255 {"01;31", none.Foreground(color.Green), none.Foreground(color.Maroon).Bold(true)},
256
257 {"38;5;0", none, none.Foreground(color.Black)},
258 {"38;5;1", none, none.Foreground(color.Maroon)},
259 {"38;5;8", none, none.Foreground(color.Gray)},
260 {"38;5;16", none, none.Foreground(color.XTerm16)},
261 {"38;5;232", none, none.Foreground(color.XTerm232)},
262
263 {"38;5;1", none.Foreground(color.Green), none.Foreground(color.Maroon)},
264 {"38;5;1", none.Foreground(color.Green).Bold(true), none.Foreground(color.Maroon).Bold(true)},
265
266 {"48;5;0", none, none.Background(color.Black)},
267 {"48;5;1", none, none.Background(color.Maroon)},
268 {"48;5;8", none, none.Background(color.Gray)},
269 {"48;5;16", none, none.Background(color.XTerm16)},
270 {"48;5;232", none, none.Background(color.XTerm232)},
271
272 {"48;5;1", none.Background(color.Green), none.Background(color.Maroon)},
273
274 {"1;38;5;1", none, none.Foreground(color.Maroon).Bold(true)},
275 {"1;38;5;1", none.Foreground(color.Green), none.Foreground(color.Maroon).Bold(true)},
276
277 {"38;2;5;102;8", none, none.Foreground(tcell.NewRGBColor(5, 102, 8))},
278 {"48;2;0;48;143", none, none.Background(tcell.NewRGBColor(0, 48, 143))},
279
280 // Fixes color construction issue: https://github.com/gokcehan/lf/pull/439#issuecomment-674409446
281 {"38;5;34;1", none, none.Foreground(color.XTerm34).Bold(true)},
282 }
283
284 for _, test := range tests {
285 if stGot := applySGR(test.s, test.st); stGot != test.stExp {
286 t.Errorf("at input '%s' with '%v' expected '%v' but got '%v'",
287 test.s, test.st, test.stExp, stGot)
288 }
289 }
290 }