| 1 |
package ansi |
| 2 |
|
| 3 |
import ( |
| 4 |
"slices" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/charmbracelet/x/ansi/parser" |
| 8 |
) |
| 9 |
|
| 10 |
func TestDecodeSequence(t *testing.T) { |
| 11 |
type expectedSequence struct { |
| 12 |
seq []byte |
| 13 |
n int |
| 14 |
width int |
| 15 |
params []int |
| 16 |
data []byte |
| 17 |
cmd int |
| 18 |
} |
| 19 |
cases := []struct { |
| 20 |
name string |
| 21 |
input []byte |
| 22 |
expected []expectedSequence |
| 23 |
}{ |
| 24 |
{ |
| 25 |
name: "single byte", |
| 26 |
input: []byte{0x1b}, |
| 27 |
expected: []expectedSequence{{seq: []byte{0x1b}, n: 1}}, |
| 28 |
}, |
| 29 |
{ |
| 30 |
name: "single byte 2", |
| 31 |
input: []byte{0x00}, |
| 32 |
expected: []expectedSequence{ |
| 33 |
{seq: []byte{0x00}, n: 1}, |
| 34 |
}, |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "ASCII printable", |
| 38 |
input: []byte("a"), |
| 39 |
expected: []expectedSequence{ |
| 40 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 41 |
}, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "ASCII space", |
| 45 |
input: []byte(" "), |
| 46 |
expected: []expectedSequence{ |
| 47 |
{seq: []byte{' '}, n: 1, width: 1}, |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "ASCII DEL", |
| 52 |
input: []byte{DEL}, |
| 53 |
expected: []expectedSequence{ |
| 54 |
{seq: []byte{DEL}, n: 1}, |
| 55 |
}, |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "DEL in the middle of UTF8 string", |
| 59 |
input: []byte{'a', DEL, 'b'}, |
| 60 |
expected: []expectedSequence{ |
| 61 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 62 |
{seq: []byte{DEL}, n: 1}, |
| 63 |
{seq: []byte{'b'}, n: 1, width: 1}, |
| 64 |
}, |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "DEL in the middle of DCS", |
| 68 |
input: []byte("\x1bP1;2+xa\x7fb\x1b\\"), |
| 69 |
expected: []expectedSequence{ |
| 70 |
{seq: []byte("\x1bP1;2+xa\x7fb\x1b\\"), n: 12, params: []int{1, 2}, data: []byte{'a', DEL, 'b'}, cmd: 'x' | '+'<<16}, |
| 71 |
}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "ST in the middle of DCS", |
| 75 |
input: []byte("\x1bP1;2+xa\x9cb\x1b\\"), |
| 76 |
expected: []expectedSequence{ |
| 77 |
{seq: []byte("\x1bP1;2+xa\x9c"), n: 9, params: []int{1, 2}, data: []byte{'a'}, cmd: 'x' | '+'<<16}, |
| 78 |
{seq: []byte{'b'}, n: 1, width: 1}, |
| 79 |
{seq: []byte("\x1b\\"), n: 2, cmd: '\\'}, |
| 80 |
}, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "CSI style sequence", |
| 84 |
input: []byte("\x1b[1;2;3m"), |
| 85 |
expected: []expectedSequence{{seq: []byte("\x1b[1;2;3m"), n: 8, params: []int{1, 2, 3}, cmd: 'm'}}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "invalid unterminated CSI sequence", |
| 89 |
input: []byte("\x1b[1;2;3"), |
| 90 |
expected: []expectedSequence{ |
| 91 |
{seq: []byte("\x1b[1;2;3"), n: 7, params: []int{1, 2}}, // last param gets collected during DispatchAction |
| 92 |
}, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "set title OSC sequence", |
| 96 |
input: []byte("\x1b]2;charmbracelet: ~/Source/bubbletea\x07"), |
| 97 |
expected: []expectedSequence{{seq: []byte("\x1b]2;charmbracelet: ~/Source/bubbletea\x07"), n: 38, cmd: 2, data: []byte("2;charmbracelet: ~/Source/bubbletea")}}, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "set background OSC sequence with 7-bit ST terminator", |
| 101 |
input: []byte("\x1b]11;ff/00/ff\x1b\\"), |
| 102 |
expected: []expectedSequence{{seq: []byte("\x1b]11;ff/00/ff\x1b\\"), n: 15, cmd: 11, data: []byte("11;ff/00/ff")}}, |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "set background OSC sequence with ST 8-bit terminator", |
| 106 |
input: []byte("\x1b]11;ff/00/ff\x9c\x1baa\x8fa"), |
| 107 |
expected: []expectedSequence{ |
| 108 |
{seq: []byte("\x1b]11;ff/00/ff\x9c"), n: 14, cmd: 11, data: []byte("11;ff/00/ff")}, |
| 109 |
{seq: []byte{ESC, 'a'}, n: 2, cmd: 'a'}, |
| 110 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 111 |
{seq: []byte{SS3}, n: 1}, |
| 112 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 113 |
}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "set background OSC sequence followed by ESC sequence", |
| 117 |
input: []byte("\x1b]11;ff/00/ff\x1b[1;2;3m"), |
| 118 |
expected: []expectedSequence{ |
| 119 |
{seq: []byte("\x1b]11;ff/00/ff"), n: 13, cmd: 11, data: []byte("11;ff/00/ff")}, |
| 120 |
{seq: []byte("\x1b[1;2;3m"), n: 8, params: []int{1, 2, 3}, cmd: 'm'}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "set background OSC ESC terminated", |
| 125 |
input: []byte("\x1b]11;ff/00/ff\x1b"), |
| 126 |
expected: []expectedSequence{ |
| 127 |
{seq: []byte("\x1b]11;ff/00/ff"), n: 13, cmd: 11, data: []byte("11;ff/00/ff")}, |
| 128 |
{seq: []byte{ESC}, n: 1}, |
| 129 |
}, |
| 130 |
}, |
| 131 |
{ |
| 132 |
name: "multiple sequences", |
| 133 |
input: []byte("\x1b[1;2;3m\x1b]2;charmbracelet: ~/Source/bubbletea\x07\x1b]11;ff/00/ff\x1b\\"), |
| 134 |
expected: []expectedSequence{ |
| 135 |
{seq: []byte("\x1b[1;2;3m"), n: 8, params: []int{1, 2, 3}, cmd: 'm'}, |
| 136 |
{seq: []byte("\x1b]2;charmbracelet: ~/Source/bubbletea\x07"), n: 38, cmd: 2, data: []byte("2;charmbracelet: ~/Source/bubbletea")}, |
| 137 |
{seq: []byte("\x1b]11;ff/00/ff\x1b\\"), n: 15, cmd: 11, data: []byte("11;ff/00/ff")}, |
| 138 |
}, |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "double ESC", |
| 142 |
input: []byte("\x1b\x1b"), |
| 143 |
expected: []expectedSequence{ |
| 144 |
{seq: []byte{0x1b}, n: 1}, |
| 145 |
{seq: []byte{0x1b}, n: 1}, |
| 146 |
}, |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "double ST", |
| 150 |
input: []byte("\x1b\\\x1b\\"), |
| 151 |
expected: []expectedSequence{ |
| 152 |
{seq: []byte{ESC, '\\'}, n: 2, cmd: '\\'}, |
| 153 |
{seq: []byte{ESC, '\\'}, n: 2, cmd: '\\'}, |
| 154 |
}, |
| 155 |
}, |
| 156 |
{ |
| 157 |
name: "double ST 8-bit", |
| 158 |
input: []byte("\x9c\x9c"), |
| 159 |
expected: []expectedSequence{ |
| 160 |
{seq: []byte{ST}, n: 1}, |
| 161 |
{seq: []byte{ST}, n: 1}, |
| 162 |
}, |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "ASCII printables", |
| 166 |
input: []byte("Hello, World!"), |
| 167 |
expected: []expectedSequence{ |
| 168 |
{seq: []byte{'H'}, width: 1, n: 1}, |
| 169 |
{seq: []byte{'e'}, width: 1, n: 1}, |
| 170 |
{seq: []byte{'l'}, width: 1, n: 1}, |
| 171 |
{seq: []byte{'l'}, width: 1, n: 1}, |
| 172 |
{seq: []byte{'o'}, width: 1, n: 1}, |
| 173 |
{seq: []byte{','}, width: 1, n: 1}, |
| 174 |
{seq: []byte{' '}, width: 1, n: 1}, |
| 175 |
{seq: []byte{'W'}, width: 1, n: 1}, |
| 176 |
{seq: []byte{'o'}, width: 1, n: 1}, |
| 177 |
{seq: []byte{'r'}, width: 1, n: 1}, |
| 178 |
{seq: []byte{'l'}, width: 1, n: 1}, |
| 179 |
{seq: []byte{'d'}, width: 1, n: 1}, |
| 180 |
{seq: []byte{'!'}, width: 1, n: 1}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
{ |
| 184 |
name: "rune", |
| 185 |
input: []byte("👋"), |
| 186 |
expected: []expectedSequence{ |
| 187 |
{seq: []byte("👋"), n: 4, width: 2}, |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "inavlid rune", |
| 192 |
input: []byte{0xc3}, |
| 193 |
expected: []expectedSequence{ |
| 194 |
{seq: []byte{0xc3}, n: 1, width: 1}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
{ |
| 198 |
name: "multiple sequences with UTF8 and double ESC", |
| 199 |
input: []byte("👨🏿🌾\x1b\x1b \x1b[?1:2:3mÄabc\x1b\x1bP+q\x1b\\"), |
| 200 |
expected: []expectedSequence{ |
| 201 |
{seq: []byte("👨🏿🌾"), n: 15, width: 2}, |
| 202 |
{seq: []byte{ESC}, n: 1}, |
| 203 |
{seq: []byte{ESC, ' '}, n: 2, cmd: 0 | ' '<<16}, |
| 204 |
{seq: []byte("\x1b[?1:2:3m"), n: 9, params: []int{1 | parser.HasMoreFlag, 2 | parser.HasMoreFlag, 3}, cmd: 'm' | '?'<<8}, |
| 205 |
{seq: []byte("Ä"), n: 2, width: 1}, |
| 206 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 207 |
{seq: []byte{'b'}, n: 1, width: 1}, |
| 208 |
{seq: []byte{'c'}, n: 1, width: 1}, |
| 209 |
{seq: []byte{ESC}, n: 1}, |
| 210 |
{seq: []byte("\x1bP+q\x1b\\"), n: 6, cmd: 'q' | '+'<<16}, |
| 211 |
}, |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "style sequences", |
| 215 |
input: []byte("hello, \x1b[1;2;3mworld\x1b[0m!"), |
| 216 |
expected: []expectedSequence{ |
| 217 |
{seq: []byte("h"), n: 1, width: 1}, |
| 218 |
{seq: []byte("e"), n: 1, width: 1}, |
| 219 |
{seq: []byte("l"), n: 1, width: 1}, |
| 220 |
{seq: []byte("l"), n: 1, width: 1}, |
| 221 |
{seq: []byte("o"), n: 1, width: 1}, |
| 222 |
{seq: []byte(","), n: 1, width: 1}, |
| 223 |
{seq: []byte(" "), n: 1, width: 1}, |
| 224 |
{seq: []byte("\x1b[1;2;3m"), n: 8, params: []int{1, 2, 3}, cmd: 'm'}, |
| 225 |
{seq: []byte("w"), n: 1, width: 1}, |
| 226 |
{seq: []byte("o"), n: 1, width: 1}, |
| 227 |
{seq: []byte("r"), n: 1, width: 1}, |
| 228 |
{seq: []byte("l"), n: 1, width: 1}, |
| 229 |
{seq: []byte("d"), n: 1, width: 1}, |
| 230 |
{seq: []byte("\x1b[0m"), n: 4, params: []int{0}, cmd: 'm'}, |
| 231 |
{seq: []byte("!"), n: 1, width: 1}, |
| 232 |
}, |
| 233 |
}, |
| 234 |
{ |
| 235 |
name: "set background OSC with C1", |
| 236 |
input: []byte("\x1b]11;\x90?\x1b\\"), |
| 237 |
expected: []expectedSequence{ |
| 238 |
{seq: []byte("\x1b]11;\x90?\x1b\\"), n: 9, cmd: 11, data: []byte("11;\x90?")}, |
| 239 |
}, |
| 240 |
}, |
| 241 |
{ |
| 242 |
name: "unterminated CSI with escape sequence", |
| 243 |
input: []byte("\x1b[1;2;3\x1bOa"), |
| 244 |
expected: []expectedSequence{ |
| 245 |
{seq: []byte("\x1b[1;2;3"), n: 7, params: []int{1, 2, 3}}, // params get reset and ignored when unterminated |
| 246 |
{seq: []byte("\x1bO"), n: 2, cmd: 'O'}, |
| 247 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 248 |
}, |
| 249 |
}, |
| 250 |
{ |
| 251 |
name: "SS3", |
| 252 |
input: []byte("\x1bOa"), |
| 253 |
expected: []expectedSequence{ |
| 254 |
{seq: []byte("\x1bO"), n: 2, cmd: 'O'}, |
| 255 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 256 |
}, |
| 257 |
}, |
| 258 |
{ |
| 259 |
name: "SS3 8-bit", |
| 260 |
input: []byte("\x8fa"), |
| 261 |
expected: []expectedSequence{ |
| 262 |
{seq: []byte{SS3}, n: 1}, |
| 263 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 264 |
}, |
| 265 |
}, |
| 266 |
{ |
| 267 |
name: "ESC sequence with intermediate", |
| 268 |
input: []byte("\x1b Q"), |
| 269 |
expected: []expectedSequence{ |
| 270 |
{seq: []byte("\x1b Q"), n: 3, cmd: 'Q' | ' '<<16}, |
| 271 |
}, |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "ESC followed by C0", |
| 275 |
input: []byte("\x1b[\x00a"), |
| 276 |
expected: []expectedSequence{ |
| 277 |
{seq: []byte("\x1b["), n: 2}, |
| 278 |
{seq: []byte{0x00}, n: 1}, |
| 279 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 280 |
}, |
| 281 |
}, |
| 282 |
{ |
| 283 |
name: "unterminated DCS sequence", |
| 284 |
input: []byte("\x1bP1;2+xa"), |
| 285 |
expected: []expectedSequence{ |
| 286 |
{seq: []byte("\x1bP1;2+xa"), n: 8, params: []int{1, 2}, data: []byte{'a'}, cmd: 'x' | '+'<<16}, |
| 287 |
}, |
| 288 |
}, |
| 289 |
{ |
| 290 |
name: "invalid DCS sequence", |
| 291 |
input: []byte("\x1bP\x1b\\ab"), |
| 292 |
expected: []expectedSequence{ |
| 293 |
{seq: []byte("\x1bP"), n: 2}, |
| 294 |
{seq: []byte("\x1b\\"), n: 2, cmd: '\\'}, |
| 295 |
{seq: []byte{'a'}, n: 1, width: 1}, |
| 296 |
{seq: []byte{'b'}, n: 1, width: 1}, |
| 297 |
}, |
| 298 |
}, |
| 299 |
{ |
| 300 |
name: "single param osc", |
| 301 |
input: []byte("\x1b]112\x07"), |
| 302 |
expected: []expectedSequence{ |
| 303 |
{seq: []byte("\x1b]112\x07"), n: 6, cmd: 112, data: []byte("112")}, |
| 304 |
}, |
| 305 |
}, |
| 306 |
} |
| 307 |
|
| 308 |
for _, tc := range cases { |
| 309 |
t.Run(tc.name, func(t *testing.T) { |
| 310 |
p := NewParser() |
| 311 |
p.SetParamsSize(32) |
| 312 |
p.SetDataSize(1024) |
| 313 |
|
| 314 |
var state byte |
| 315 |
input := tc.input |
| 316 |
results := make([]expectedSequence, 0) |
| 317 |
for len(input) > 0 { |
| 318 |
seq, width, n, newState := DecodeSequence(input, state, p) |
| 319 |
params := slices.Clone(p.params[:p.paramsLen]) |
| 320 |
data := slices.Clone(p.data[:p.dataLen]) |
| 321 |
results = append(results, expectedSequence{seq: seq, width: width, n: n, params: params, data: data, cmd: p.cmd}) |
| 322 |
state = newState |
| 323 |
input = input[n:] |
| 324 |
} |
| 325 |
if len(results) != len(tc.expected) { |
| 326 |
t.Fatalf("expected %d sequences, got %d\n\n%#v\n\n%#v", len(tc.expected), len(results), tc.expected, results) |
| 327 |
} |
| 328 |
for i, r := range results { |
| 329 |
if r.n != tc.expected[i].n { |
| 330 |
t.Errorf("expected %d bytes, got %d", tc.expected[i].n, r.n) |
| 331 |
} |
| 332 |
if r.width != tc.expected[i].width { |
| 333 |
t.Errorf("expected %d width, got %d", tc.expected[i].width, r.width) |
| 334 |
} |
| 335 |
if string(r.seq) != string(tc.expected[i].seq) { |
| 336 |
t.Errorf("expected %q, got %q", string(tc.expected[i].seq), string(r.seq)) |
| 337 |
} |
| 338 |
if r.cmd != tc.expected[i].cmd { |
| 339 |
t.Errorf("expected %d cmd, got %d", tc.expected[i].cmd, r.cmd) |
| 340 |
} |
| 341 |
if string(r.data) != string(tc.expected[i].data) { |
| 342 |
t.Errorf("expected %q data, got %q", string(tc.expected[i].data), string(r.data)) |
| 343 |
} |
| 344 |
if len(r.params) != len(tc.expected[i].params) { |
| 345 |
t.Fatalf("expected %d params, got %d", len(tc.expected[i].params), len(r.params)) |
| 346 |
} |
| 347 |
if len(tc.expected[i].params) > 0 { |
| 348 |
for j, p := range r.params { |
| 349 |
if p != tc.expected[i].params[j] { |
| 350 |
t.Errorf("expected param[%d] = %d, got %d", j, tc.expected[i].params[j], p) |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
}) |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
func FuzzDecodeSequence(f *testing.F) { |
| 360 |
var b byte |
| 361 |
for b < 0x80 { |
| 362 |
f.Add([]byte{b}) |
| 363 |
b++ |
| 364 |
} |
| 365 |
|
| 366 |
f.Add([]byte("\x1b")) |
| 367 |
f.Add([]byte("\x1b[1;2;3m")) |
| 368 |
f.Add([]byte("\x1b]2;charmbracelet: ~/Source/bubbletea\x07")) |
| 369 |
f.Add([]byte("\x1b]11;ff/00/ff\x1b\\")) |
| 370 |
f.Add([]byte("\x1b]11;ff/00/ff\x9c\x1baa\x8fa")) |
| 371 |
f.Add([]byte("\x1b]11;ff/00/ff\x1b[1;2;3m")) |
| 372 |
f.Add([]byte("\x1b]11;ff/00/ff\x1b")) |
| 373 |
f.Add([]byte("\x1b[1;2;3m\x1b]2;charmbracelet: ~/Source/bubbletea\x07\x1b]11;ff/00/ff\x1b\\")) |
| 374 |
f.Add([]byte("Hello, World!")) |
| 375 |
f.Add([]byte("👋a")) |
| 376 |
f.Add([]byte("👨🏿🌾")) |
| 377 |
f.Fuzz(func(t *testing.T, b []byte) { |
| 378 |
var state byte |
| 379 |
var n int |
| 380 |
for len(b) > 0 { |
| 381 |
_, _, n, state = DecodeSequence(b, state, nil) |
| 382 |
if n == 0 { |
| 383 |
break |
| 384 |
} |
| 385 |
b = b[n:] |
| 386 |
} |
| 387 |
}) |
| 388 |
} |
| 389 |
|
| 390 |
func BenchmarkDecodeSequence(b *testing.B) { |
| 391 |
var state byte |
| 392 |
var n int |
| 393 |
input := []byte("\x1b[1;2;3màbc\x90?123;456+q\x9c\x7f ") |
| 394 |
p := NewParser() |
| 395 |
p.SetParamsSize(32) |
| 396 |
p.SetDataSize(1024) |
| 397 |
b.ReportAllocs() |
| 398 |
for i := 0; i < b.N; i++ { |
| 399 |
in := input |
| 400 |
for len(in) > 0 { |
| 401 |
_, _, n, state = DecodeSequence(in, state, p) |
| 402 |
in = in[n:] |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
func BenchmarkDecodeParser(b *testing.B) { |
| 408 |
p := NewParser() |
| 409 |
p.SetParamsSize(32) |
| 410 |
p.SetDataSize(1024) |
| 411 |
input := []byte("\x1b[1;2;3màbc\x90?123;456+q\x9c\x7f ") |
| 412 |
b.ReportAllocs() |
| 413 |
for i := 0; i < b.N; i++ { |
| 414 |
p.Parse(input) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
func TestCommand(t *testing.T) { |
| 419 |
cases := []struct { |
| 420 |
name string |
| 421 |
cmd byte |
| 422 |
prefix byte |
| 423 |
inter byte |
| 424 |
expected int |
| 425 |
}{ |
| 426 |
{ |
| 427 |
name: "CUU", // Cursor Up |
| 428 |
cmd: 'A', |
| 429 |
expected: 'A', |
| 430 |
}, |
| 431 |
{ |
| 432 |
name: "DECAWM", // Auto Wrap Mode |
| 433 |
cmd: 'h', |
| 434 |
prefix: '?', |
| 435 |
expected: 'h' | '?'<<parser.PrefixShift, |
| 436 |
}, |
| 437 |
{ |
| 438 |
name: "DECSCUSR", // Set Cursor Style |
| 439 |
cmd: 'q', |
| 440 |
inter: ' ', |
| 441 |
expected: 'q' | ' '<<parser.IntermedShift, |
| 442 |
}, |
| 443 |
{ |
| 444 |
name: "imaginary cmd with both prefix and intermed", |
| 445 |
cmd: 'x', |
| 446 |
prefix: '>', |
| 447 |
inter: '(', |
| 448 |
expected: 'x' | '>'<<parser.PrefixShift | '('<<parser.IntermedShift, |
| 449 |
}, |
| 450 |
// { |
| 451 |
// name: "ignore bytes beyond the lower 8 bites", |
| 452 |
// cmd: 256 + 'x', |
| 453 |
// prefix: 256 + '>', |
| 454 |
// inter: 256 + '(', |
| 455 |
// expected: 'x' | '>'<<parser.PrefixShift | '('<<parser.IntermedShift, |
| 456 |
// }, |
| 457 |
{ |
| 458 |
name: "OSC11", // Set background color |
| 459 |
cmd: 11, |
| 460 |
expected: 11, |
| 461 |
}, |
| 462 |
} |
| 463 |
|
| 464 |
for _, tc := range cases { |
| 465 |
t.Run(tc.name, func(t *testing.T) { |
| 466 |
if cmd := Command(tc.prefix, tc.inter, tc.cmd); cmd != tc.expected { |
| 467 |
t.Errorf("expected %d, got %d", tc.expected, cmd) |
| 468 |
} |
| 469 |
}) |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
func TestParameter(t *testing.T) { |
| 474 |
cases := []struct { |
| 475 |
name string |
| 476 |
param int |
| 477 |
hasMore bool |
| 478 |
expected int |
| 479 |
}{ |
| 480 |
{ |
| 481 |
name: "single param", |
| 482 |
param: 1, |
| 483 |
expected: 1, |
| 484 |
}, |
| 485 |
{ |
| 486 |
name: "single param with hasMore", |
| 487 |
param: 1, |
| 488 |
hasMore: true, |
| 489 |
expected: 1 | parser.HasMoreFlag, |
| 490 |
}, |
| 491 |
{ |
| 492 |
name: "negative param", |
| 493 |
param: -1, |
| 494 |
expected: parser.ParamMask, |
| 495 |
}, |
| 496 |
{ |
| 497 |
name: "negative param has more", |
| 498 |
param: -1, |
| 499 |
hasMore: true, |
| 500 |
expected: -1, |
| 501 |
}, |
| 502 |
} |
| 503 |
|
| 504 |
for _, tc := range cases { |
| 505 |
t.Run(tc.name, func(t *testing.T) { |
| 506 |
if p := Parameter(tc.param, tc.hasMore); p != tc.expected { |
| 507 |
t.Errorf("expected %d, got %d", tc.expected, p) |
| 508 |
} |
| 509 |
}) |
| 510 |
} |
| 511 |
} |