From 8caf3eb173c125355906fe4904d274f178abf59f Mon Sep 17 00:00:00 2001 From: "Abinoam P. Marques Jr" Date: Fri, 6 Jan 2023 22:10:09 -0300 Subject: Add tests for Ctrl-U (erase line) handling --- test/test_highline.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/test_highline.rb b/test/test_highline.rb index a5c4e26..627f52c 100755 --- a/test/test_highline.rb +++ b/test/test_highline.rb @@ -349,6 +349,44 @@ class TestHighLine < Minitest::Test assert_equal(4, @output.string.count("\b")) end + def test_erase_line_does_not_enter_prompt + @input << "\cU\cU\cU\cU\cU\cU\cU\cU\cU" + @input.rewind + answer = @terminal.ask("Please enter your password: ") do |q| + q.echo = "*" + end + assert_equal("", answer) + assert_equal("Please enter your password: \n", @output.string) + + # There's no backspaces as the line is already empty + assert_equal(0, @output.string.count("\b")) + end + + def test_after_some_chars_erase_line_does_not_enter_prompt_when_ascii + @input << "apple\cU\cU\cU\cU\cU\cU\cU\cU" + @input.rewind + answer = @terminal.ask("Please enter your password: ") do |q| + q.echo = "*" + end + assert_equal("", answer) + + # There's only enough backspaces to clear the given string + assert_equal(5, @output.string.count("\b")) + end + + + def test_after_some_chars_erase_line_does_not_enter_prompt_when_utf8 + @input << "maçã\cU\cU\cU\cU\cU\cU\cU\cU" + @input.rewind + answer = @terminal.ask("Please enter your password: ") do |q| + q.echo = "*" + end + assert_equal("", answer) + + # There's only enough backspaces to clear the given string + assert_equal(4, @output.string.count("\b")) + end + def test_readline_mode # # Rubinius (and JRuby) seems to be ignoring -- cgit v1.2.1