diff options
author | Thijs <thijs@thijsschreijer.nl> | 2024-05-21 22:09:28 +0200 |
---|---|---|
committer | Thijs <thijs@thijsschreijer.nl> | 2024-05-21 22:19:15 +0200 |
commit | 96e278e67729f1897496916b650e6be337a571d6 (patch) | |
tree | 270f628431fd75fa0de0137f58f3b2772c1c7d7c | |
parent | 1ed4a7325932192da7ec98318a4841752d2a952a (diff) | |
download | luasystem-96e278e67729f1897496916b650e6be337a571d6.tar.gz luasystem-96e278e67729f1897496916b650e6be337a571d6.tar.bz2 luasystem-96e278e67729f1897496916b650e6be337a571d6.zip |
add tests for readkey and readansi
-rw-r--r-- | spec/04-term_spec.lua | 147 |
1 files changed, 142 insertions, 5 deletions
diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua index 83750ec..b6e0daf 100644 --- a/spec/04-term_spec.lua +++ b/spec/04-term_spec.lua | |||
@@ -324,9 +324,12 @@ describe("Terminal:", function() | |||
324 | 324 | ||
325 | 325 | ||
326 | 326 | ||
327 | pending("termsize()", function() | 327 | describe("termsize() #manual", function() |
328 | 328 | ||
329 | pending("sets the consoleflags, if called with flags", function() | 329 | it("gets the terminal size", function() |
330 | local w, h = system.termsize() | ||
331 | assert.is_number(w) | ||
332 | assert.is_number(h) | ||
330 | end) | 333 | end) |
331 | 334 | ||
332 | end) | 335 | end) |
@@ -407,13 +410,147 @@ describe("Terminal:", function() | |||
407 | 410 | ||
408 | 411 | ||
409 | 412 | ||
410 | pending("readkey()", function() | 413 | describe("keyboard input", function() |
414 | |||
415 | local old_readkey = system._readkey | ||
416 | local current_buffer | ||
417 | local function setbuffer(str) | ||
418 | assert(type(str) == "string", "setbuffer() expects a string") | ||
419 | if str == "" then | ||
420 | current_buffer = nil | ||
421 | else | ||
422 | current_buffer = str | ||
423 | end | ||
424 | end | ||
425 | |||
426 | |||
427 | setup(function() | ||
428 | system._readkey = function() | ||
429 | if not current_buffer then | ||
430 | return nil | ||
431 | end | ||
432 | local ch = current_buffer:byte(1, 1) | ||
433 | if #current_buffer == 1 then | ||
434 | current_buffer = nil | ||
435 | else | ||
436 | current_buffer = current_buffer:sub(2, -1) | ||
437 | end | ||
438 | return ch | ||
439 | end | ||
440 | end) | ||
441 | |||
442 | |||
443 | teardown(function() | ||
444 | system._readkey = old_readkey | ||
445 | end) | ||
446 | |||
447 | |||
448 | |||
449 | describe("readkey()", function() | ||
450 | |||
451 | it("fails without a timeout", function() | ||
452 | assert.has.error(function() | ||
453 | system.readkey() | ||
454 | end, "arg #1 to readkey, expected timeout in seconds, got nil") | ||
455 | end) | ||
456 | |||
457 | |||
458 | it("reads a single byte for single-byte characters", function() | ||
459 | setbuffer("abc") | ||
460 | assert.equals(string.byte("a"), system.readkey(0)) | ||
461 | assert.equals(string.byte("b"), system.readkey(0)) | ||
462 | assert.equals(string.byte("c"), system.readkey(0)) | ||
463 | end) | ||
464 | |||
465 | |||
466 | it("reads a single byte for multi-byte chars", function() | ||
467 | setbuffer(string.char(226, 130, 172) .. -- "€" single | ||
468 | string.char(240, 159, 154, 128)) -- "🚀" double | ||
469 | assert.equals(226, system.readkey(0)) | ||
470 | assert.equals(130, system.readkey(0)) | ||
471 | assert.equals(172, system.readkey(0)) | ||
472 | assert.equals(240, system.readkey(0)) | ||
473 | assert.equals(159, system.readkey(0)) | ||
474 | assert.equals(154, system.readkey(0)) | ||
475 | assert.equals(128, system.readkey(0)) | ||
476 | end) | ||
477 | |||
478 | |||
479 | it("times out", function() | ||
480 | setbuffer("") | ||
481 | local timing = system.gettime() | ||
482 | assert.same({ nil, "timeout" }, { system.readkey(0.5) }) | ||
483 | |||
484 | timing = system.gettime() - timing | ||
485 | -- assert.is.near(0.5, timing, 0.1) -- this works in CI for Unix and Windows, but not MacOS (locally it passes) | ||
486 | assert.is.near(1, timing, 0.5) -- this also works for MacOS in CI | ||
487 | end) | ||
488 | |||
489 | end) | ||
490 | |||
491 | |||
492 | |||
493 | describe("readansi()", function() | ||
494 | |||
495 | it("fails without a timeout", function() | ||
496 | assert.has.error(function() | ||
497 | system.readansi() | ||
498 | end, "arg #1 to readansi, expected timeout in seconds, got nil") | ||
499 | end) | ||
500 | |||
501 | |||
502 | it("reads a single byte for single-byte characters", function() | ||
503 | setbuffer("abc") | ||
504 | assert.are.same({"a", "char"}, {system.readansi(0)}) | ||
505 | assert.are.same({"b", "char"}, {system.readansi(0)}) | ||
506 | assert.are.same({"c", "char"}, {system.readansi(0)}) | ||
507 | end) | ||
508 | |||
509 | |||
510 | it("reads a multi-byte characters one at a time", function() | ||
511 | setbuffer(string.char(226, 130, 172) .. -- "€" single | ||
512 | string.char(240, 159, 154, 128)) -- "🚀" double | ||
513 | assert.are.same({"€", "char"}, {system.readansi(0)}) | ||
514 | assert.are.same({"🚀", "char"}, {system.readansi(0)}) | ||
515 | end) | ||
411 | 516 | ||
412 | end) | ||
413 | 517 | ||
518 | it("reads ANSI escape sequences, marked by '<esc>['", function() | ||
519 | setbuffer("\27[A\27[B\27[C\27[D") | ||
520 | assert.are.same({"\27[A", "ansi"}, {system.readansi(0)}) | ||
521 | assert.are.same({"\27[B", "ansi"}, {system.readansi(0)}) | ||
522 | assert.are.same({"\27[C", "ansi"}, {system.readansi(0)}) | ||
523 | assert.are.same({"\27[D", "ansi"}, {system.readansi(0)}) | ||
524 | end) | ||
414 | 525 | ||
415 | 526 | ||
416 | pending("readansi()", function() | 527 | it("reads ANSI escape sequences, marked by '<esc>O'", function() |
528 | setbuffer("\27OA\27OB\27OC\27OD") | ||
529 | assert.are.same({"\27OA", "ansi"}, {system.readansi(0)}) | ||
530 | assert.are.same({"\27OB", "ansi"}, {system.readansi(0)}) | ||
531 | assert.are.same({"\27OC", "ansi"}, {system.readansi(0)}) | ||
532 | assert.are.same({"\27OD", "ansi"}, {system.readansi(0)}) | ||
533 | end) | ||
534 | |||
535 | |||
536 | it("returns a single <esc> character if no sequence is found", function() | ||
537 | setbuffer("\27\27[A") | ||
538 | assert.are.same({"\27", "char"}, {system.readansi(0)}) | ||
539 | assert.are.same({"\27[A", "ansi"}, {system.readansi(0)}) | ||
540 | end) | ||
541 | |||
542 | |||
543 | it("times out", function() | ||
544 | setbuffer("") | ||
545 | local timing = system.gettime() | ||
546 | assert.same({ nil, "timeout" }, { system.readansi(0.5) }) | ||
547 | |||
548 | timing = system.gettime() - timing | ||
549 | -- assert.is.near(0.5, timing, 0.1) -- this works in CI for Unix and Windows, but not MacOS (locally it passes) | ||
550 | assert.is.near(1, timing, 0.5) -- this also works for MacOS in CI | ||
551 | end) | ||
552 | |||
553 | end) | ||
417 | 554 | ||
418 | end) | 555 | end) |
419 | 556 | ||