this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

parser: add unit tests

Test (nearly?) implementation so far

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+176 -13
+2
src/event.zig
··· 6 6 key_press: Key, 7 7 focus_in, 8 8 focus_out, 9 + paste_start, 10 + paste_end, 9 11 };
+174 -13
src/parser.zig
··· 106 106 }; 107 107 return .{ 108 108 .event = .{ .key_press = key }, 109 - .n = i, 109 + .n = i + 1, 110 110 }; 111 111 }, 112 112 } 113 113 }, 114 114 .ss3 => { 115 - state = .ground; 116 115 const key: Key = switch (b) { 117 116 'A' => .{ .codepoint = Key.up }, 118 117 'B' => .{ .codepoint = Key.down }, ··· 128 127 log.warn("unhandled ss3: {x}", .{b}); 129 128 return .{ 130 129 .event = null, 131 - .n = i, 130 + .n = i + 1, 132 131 }; 133 132 }, 134 133 }; 135 134 return .{ 136 135 .event = .{ .key_press = key }, 137 - .n = i, 136 + .n = i + 1, 138 137 }; 139 138 }, 140 139 .csi => { ··· 231 230 23 => break :blk Key.f11, 232 231 24 => break :blk Key.f12, 233 232 200 => { 234 - // TODO: bracketed paste 235 - continue; 233 + return .{ 234 + .event = .paste_start, 235 + .n = i + 1, 236 + }; 236 237 }, 237 238 201 => { 238 - // TODO: bracketed paste 239 - continue; 239 + return .{ 240 + .event = .paste_end, 241 + .n = i + 1, 242 + }; 240 243 }, 241 244 57427 => break :blk Key.kp_begin, 242 245 else => { ··· 262 265 }, 263 266 264 267 'I' => { // focus in 265 - return .{ .event = .focus_in, .n = i }; 268 + return .{ .event = .focus_in, .n = i + 1 }; 266 269 }, 267 270 'O' => { // focus out 268 - return .{ .event = .focus_out, .n = i }; 271 + return .{ .event = .focus_out, .n = i + 1 }; 269 272 }, 270 273 else => { 271 274 log.warn("unhandled csi: CSI {s}", .{input[start + 1 .. i + 1]}); ··· 316 319 } 317 320 return .{ 318 321 .event = .{ .key_press = key }, 319 - .n = i, 322 + .n = i + 1, 320 323 }; 321 324 }, 322 325 } ··· 332 335 }; 333 336 } 334 337 335 - test "parse: single legacy keypress" { 338 + test "parse: single xterm keypress" { 336 339 const input = "a"; 337 340 const result = try parse(input); 338 341 const expected_key: Key = .{ .codepoint = 'a' }; ··· 342 345 try testing.expectEqual(expected_event, result.event); 343 346 } 344 347 345 - test "parse: single legacy keypress with more buffer" { 348 + test "parse: single xterm keypress with more buffer" { 346 349 const input = "ab"; 347 350 const result = try parse(input); 348 351 const expected_key: Key = .{ .codepoint = 'a' }; ··· 351 354 try testing.expectEqual(1, result.n); 352 355 try testing.expectEqual(expected_event, result.event); 353 356 } 357 + 358 + test "parse: xterm escape keypress" { 359 + const input = "\x1b"; 360 + const result = try parse(input); 361 + const expected_key: Key = .{ .codepoint = Key.escape }; 362 + const expected_event: Event = .{ .key_press = expected_key }; 363 + 364 + try testing.expectEqual(1, result.n); 365 + try testing.expectEqual(expected_event, result.event); 366 + } 367 + 368 + test "parse: xterm ctrl+a" { 369 + const input = "\x01"; 370 + const result = try parse(input); 371 + const expected_key: Key = .{ .codepoint = 'a', .mods = .{ .ctrl = true } }; 372 + const expected_event: Event = .{ .key_press = expected_key }; 373 + 374 + try testing.expectEqual(1, result.n); 375 + try testing.expectEqual(expected_event, result.event); 376 + } 377 + 378 + test "parse: xterm alt+a" { 379 + const input = "\x1ba"; 380 + const result = try parse(input); 381 + const expected_key: Key = .{ .codepoint = 'a', .mods = .{ .alt = true } }; 382 + const expected_event: Event = .{ .key_press = expected_key }; 383 + 384 + try testing.expectEqual(2, result.n); 385 + try testing.expectEqual(expected_event, result.event); 386 + } 387 + 388 + test "parse: xterm invalid ss3" { 389 + const input = "\x1bOZ"; 390 + const result = try parse(input); 391 + 392 + try testing.expectEqual(3, result.n); 393 + try testing.expectEqual(null, result.event); 394 + } 395 + 396 + test "parse: xterm key up" { 397 + { 398 + // normal version 399 + const input = "\x1bOA"; 400 + const result = try parse(input); 401 + const expected_key: Key = .{ .codepoint = Key.up }; 402 + const expected_event: Event = .{ .key_press = expected_key }; 403 + 404 + try testing.expectEqual(3, result.n); 405 + try testing.expectEqual(expected_event, result.event); 406 + } 407 + 408 + { 409 + // application keys version 410 + const input = "\x1b[2~"; 411 + const result = try parse(input); 412 + const expected_key: Key = .{ .codepoint = Key.insert }; 413 + const expected_event: Event = .{ .key_press = expected_key }; 414 + 415 + try testing.expectEqual(4, result.n); 416 + try testing.expectEqual(expected_event, result.event); 417 + } 418 + } 419 + 420 + test "parse: xterm shift+up" { 421 + const input = "\x1b[1;2A"; 422 + const result = try parse(input); 423 + const expected_key: Key = .{ .codepoint = Key.up, .mods = .{ .shift = true } }; 424 + const expected_event: Event = .{ .key_press = expected_key }; 425 + 426 + try testing.expectEqual(6, result.n); 427 + try testing.expectEqual(expected_event, result.event); 428 + } 429 + 430 + test "parse: xterm insert" { 431 + const input = "\x1b[1;2A"; 432 + const result = try parse(input); 433 + const expected_key: Key = .{ .codepoint = Key.up, .mods = .{ .shift = true } }; 434 + const expected_event: Event = .{ .key_press = expected_key }; 435 + 436 + try testing.expectEqual(6, result.n); 437 + try testing.expectEqual(expected_event, result.event); 438 + } 439 + 440 + test "parse: paste_start" { 441 + const input = "\x1b[200~"; 442 + const result = try parse(input); 443 + const expected_event: Event = .paste_start; 444 + 445 + try testing.expectEqual(6, result.n); 446 + try testing.expectEqual(expected_event, result.event); 447 + } 448 + 449 + test "parse: paste_end" { 450 + const input = "\x1b[201~"; 451 + const result = try parse(input); 452 + const expected_event: Event = .paste_end; 453 + 454 + try testing.expectEqual(6, result.n); 455 + try testing.expectEqual(expected_event, result.event); 456 + } 457 + 458 + test "parse: focus_in" { 459 + const input = "\x1b[I"; 460 + const result = try parse(input); 461 + const expected_event: Event = .focus_in; 462 + 463 + try testing.expectEqual(3, result.n); 464 + try testing.expectEqual(expected_event, result.event); 465 + } 466 + 467 + test "parse: focus_out" { 468 + const input = "\x1b[O"; 469 + const result = try parse(input); 470 + const expected_event: Event = .focus_out; 471 + 472 + try testing.expectEqual(3, result.n); 473 + try testing.expectEqual(expected_event, result.event); 474 + } 475 + 476 + test "parse: kitty: shift+a without text reporting" { 477 + const input = "\x1b[97:65;2u"; 478 + const result = try parse(input); 479 + const expected_key: Key = .{ 480 + .codepoint = 'a', 481 + .shifted_codepoint = 'A', 482 + .mods = .{ .shift = true }, 483 + }; 484 + const expected_event: Event = .{ .key_press = expected_key }; 485 + 486 + try testing.expectEqual(10, result.n); 487 + try testing.expectEqual(expected_event, result.event); 488 + } 489 + 490 + test "parse: kitty: alt+shift+a without text reporting" { 491 + const input = "\x1b[97:65;4u"; 492 + const result = try parse(input); 493 + const expected_key: Key = .{ 494 + .codepoint = 'a', 495 + .shifted_codepoint = 'A', 496 + .mods = .{ .shift = true, .alt = true }, 497 + }; 498 + const expected_event: Event = .{ .key_press = expected_key }; 499 + 500 + try testing.expectEqual(10, result.n); 501 + try testing.expectEqual(expected_event, result.event); 502 + } 503 + 504 + test "parse: kitty: a without text reporting" { 505 + const input = "\x1b[97u"; 506 + const result = try parse(input); 507 + const expected_key: Key = .{ 508 + .codepoint = 'a', 509 + }; 510 + const expected_event: Event = .{ .key_press = expected_key }; 511 + 512 + try testing.expectEqual(5, result.n); 513 + try testing.expectEqual(expected_event, result.event); 514 + }