diff options
Diffstat (limited to 'test/acceptance/acceptance_test.rb')
-rw-r--r-- | test/acceptance/acceptance_test.rb | 191 |
1 files changed, 164 insertions, 27 deletions
diff --git a/test/acceptance/acceptance_test.rb b/test/acceptance/acceptance_test.rb index 4a11cac..52c6af0 100644 --- a/test/acceptance/acceptance_test.rb +++ b/test/acceptance/acceptance_test.rb @@ -5,8 +5,11 @@ require 'ttytest' class FzyTest < Minitest::Test FZY_PATH = File.expand_path('../../../fzy', __FILE__) + LEFT = "\e[D" + RIGHT = "\e[C" + def test_empty_list - @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[], before: "placeholder") @tty.assert_cursor_position(y: 1, x: 2) @tty.assert_matches <<~TTY placeholder @@ -36,7 +39,7 @@ class FzyTest < Minitest::Test end def test_one_item - @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "test" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[test], before: "placeholder") @tty.assert_matches <<~TTY placeholder > @@ -68,7 +71,7 @@ class FzyTest < Minitest::Test end def test_two_items - @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "test\nfoo" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[test foo], before: "placeholder") @tty.assert_cursor_position(y: 1, x: 2) @tty.assert_matches <<~TTY placeholder @@ -105,7 +108,7 @@ class FzyTest < Minitest::Test end def test_editing - @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "test\nfoo" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[test foo], before: "placeholder") @tty.assert_cursor_position(y: 1, x: 2) @tty.assert_matches <<~TTY placeholder @@ -146,7 +149,7 @@ class FzyTest < Minitest::Test end def test_ctrl_d - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.assert_matches ">\nfoo\nbar" @tty.send_keys('foo') @@ -158,7 +161,7 @@ class FzyTest < Minitest::Test end def test_ctrl_c - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.assert_matches ">\nfoo\nbar" @tty.send_keys('foo') @@ -170,25 +173,25 @@ class FzyTest < Minitest::Test end def test_down_arrow - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.assert_matches ">\nfoo\nbar" @tty.send_keys("\e[A\r") @tty.assert_matches "bar" - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.assert_matches ">\nfoo\nbar" @tty.send_keys("\eOA\r") @tty.assert_matches "bar" end def test_up_arrow - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.assert_matches ">\nfoo\nbar" @tty.send_keys("\e[A") # first down @tty.send_keys("\e[B\r") # and back up @tty.assert_matches "foo" - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.assert_matches ">\nfoo\nbar" @tty.send_keys("\eOA") # first down @tty.send_keys("\e[B\r") # and back up @@ -196,45 +199,48 @@ class FzyTest < Minitest::Test end def test_lines - @tty = TTYtest.new_terminal(%{seq 10 | #{FZY_PATH}}) + input10 = (1..10).map(&:to_s) + input20 = (1..20).map(&:to_s) + + @tty = interactive_fzy(input: input10) @tty.assert_matches ">\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10" - @tty = TTYtest.new_terminal(%{seq 20 | #{FZY_PATH}}) + @tty = interactive_fzy(input: input20) @tty.assert_matches ">\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10" - @tty = TTYtest.new_terminal(%{seq 10 | #{FZY_PATH} -l 5}) + @tty = interactive_fzy(input: input10, args: "-l 5") @tty.assert_matches ">\n1\n2\n3\n4\n5" - @tty = TTYtest.new_terminal(%{seq 10 | #{FZY_PATH} --lines=5}) + @tty = interactive_fzy(input: input10, args: "--lines=5") @tty.assert_matches ">\n1\n2\n3\n4\n5" end def test_prompt - @tty = TTYtest.new_terminal(%{echo -n "" | #{FZY_PATH}}) + @tty = interactive_fzy @tty.send_keys("foo") @tty.assert_matches '> foo' - @tty = TTYtest.new_terminal(%{echo -n "" | #{FZY_PATH} -p 'C:\\'}) + @tty = interactive_fzy(args: "-p 'C:\\'") @tty.send_keys("foo") @tty.assert_matches 'C:\foo' - @tty = TTYtest.new_terminal(%{echo -n "" | #{FZY_PATH} --prompt="foo bar "}) + @tty = interactive_fzy(args: "--prompt=\"foo bar \"") @tty.send_keys("baz") @tty.assert_matches "foo bar baz" end def test_show_scores expected_score = '( inf)' - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -s}) + @tty = interactive_fzy(input: %w[foo bar], args: "-s") @tty.send_keys('foo') @tty.assert_matches "> foo\n#{expected_score} foo" - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} --show-scores}) + @tty = interactive_fzy(input: %w[foo bar], args: "--show-scores") @tty.send_keys('foo') @tty.assert_matches "> foo\n#{expected_score} foo" expected_score = '( 0.89)' - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -s}) + @tty = interactive_fzy(input: %w[foo bar], args: "-s") @tty.send_keys('f') @tty.assert_matches "> f\n#{expected_score} foo" end @@ -252,7 +258,7 @@ class FzyTest < Minitest::Test end def test_worker_count - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -j1}) + @tty = interactive_fzy(input: %w[foo bar], args: "-j1") @tty.send_keys('foo') @tty.assert_matches "> foo\nfoo" @@ -266,29 +272,29 @@ class FzyTest < Minitest::Test end def test_initial_query - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -q fo}) + @tty = interactive_fzy(input: %w[foo bar], args: "-q fo") @tty.assert_matches "> fo\nfoo" @tty.send_keys("o") @tty.assert_matches "> foo\nfoo" @tty.send_keys("o") @tty.assert_matches "> fooo" - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -q asdf}) + @tty = interactive_fzy(input: %w[foo bar], args: "-q asdf") @tty.assert_matches "> asdf" end def test_non_interactive - @tty = TTYtest.new_terminal(%{echo before; echo -n "foo\nbar" | #{FZY_PATH} -e foo; echo after}) + @tty = interactive_fzy(input: %w[foo bar], args: "-e foo", before: "before", after: "after") @tty.assert_matches "before\nfoo\nafter" end def test_moving_text_cursor - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}; echo after}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.send_keys("br") @tty.assert_matches "> br\nbar" @tty.assert_cursor_position(y: 0, x: 4) - @tty.send_keys("\e[D") # left + @tty.send_keys(LEFT) @tty.assert_cursor_position(y: 0, x: 3) @tty.assert_matches "> br\nbar" @tty.send_keys("a") @@ -314,12 +320,132 @@ class FzyTest < Minitest::Test # https://github.com/jhawthorn/fzy/issues/42 # https://cirw.in/blog/bracketed-paste def test_bracketed_paste_characters - @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}}) + @tty = interactive_fzy(input: %w[foo bar]) @tty.assert_matches ">\nfoo\nbar" @tty.send_keys("\e[200~foo\e[201~") @tty.assert_matches "> foo\nfoo" end + # https://github.com/jhawthorn/fzy/issues/81 + def test_slow_stdin_fast_user + @tty = TTYtest.new_terminal(%{(sleep 0.5; echo aa; echo bc; echo bd) | #{FZY_PATH}}) + + # Before input has all come in, but wait for fzy to at least start + sleep 0.1 + + @tty.send_keys("b\r") + @tty.assert_matches "bc" + end + + def test_unicode + @tty = interactive_fzy(input: %w[English Français 日本語]) + @tty.assert_matches <<~TTY + > + English + Français + 日本語 + TTY + @tty.assert_cursor_position(y: 0, x: 2) + + @tty.send_keys("ç") + @tty.assert_matches <<~TTY + > ç + Français + TTY + @tty.assert_cursor_position(y: 0, x: 3) + + @tty.send_keys("\r") + @tty.assert_matches "Français" + end + + def test_unicode_backspace + @tty = interactive_fzy + @tty.send_keys "Français" + @tty.assert_matches "> Français" + @tty.assert_cursor_position(y: 0, x: 10) + + @tty.send_keys(ctrl('H') * 3) + @tty.assert_matches "> Franç" + @tty.assert_cursor_position(y: 0, x: 7) + + @tty.send_keys(ctrl('H')) + @tty.assert_matches "> Fran" + @tty.assert_cursor_position(y: 0, x: 6) + + @tty.send_keys('ce') + @tty.assert_matches "> France" + + @tty = interactive_fzy + @tty.send_keys "日本語" + @tty.assert_matches "> 日本語" + @tty.send_keys(ctrl('H')) + @tty.assert_matches "> 日本" + @tty.send_keys(ctrl('H')) + @tty.assert_matches "> 日" + @tty.send_keys(ctrl('H')) + @tty.assert_matches "> " + @tty.assert_cursor_position(y: 0, x: 2) + end + + def test_unicode_delete_word + @tty = interactive_fzy + @tty.send_keys "Je parle Français" + @tty.assert_matches "> Je parle Français" + @tty.assert_cursor_position(y: 0, x: 19) + + @tty.send_keys(ctrl('W')) + @tty.assert_matches "> Je parle" + @tty.assert_cursor_position(y: 0, x: 11) + + @tty = interactive_fzy + @tty.send_keys "日本語" + @tty.assert_matches "> 日本語" + @tty.send_keys(ctrl('W')) + @tty.assert_matches "> " + @tty.assert_cursor_position(y: 0, x: 2) + end + + def test_unicode_cursor_movement + @tty = interactive_fzy + @tty.send_keys "Français" + @tty.assert_cursor_position(y: 0, x: 10) + + @tty.send_keys(LEFT*5) + @tty.assert_cursor_position(y: 0, x: 5) + + @tty.send_keys(RIGHT*3) + @tty.assert_cursor_position(y: 0, x: 8) + + @tty = interactive_fzy + @tty.send_keys "日本語" + @tty.assert_matches "> 日本語" + @tty.assert_cursor_position(y: 0, x: 8) + @tty.send_keys(LEFT) + @tty.assert_cursor_position(y: 0, x: 6) + @tty.send_keys(LEFT) + @tty.assert_cursor_position(y: 0, x: 4) + @tty.send_keys(LEFT) + @tty.assert_cursor_position(y: 0, x: 2) + @tty.send_keys(LEFT) + @tty.assert_cursor_position(y: 0, x: 2) + @tty.send_keys(RIGHT*3) + @tty.assert_cursor_position(y: 0, x: 8) + @tty.send_keys(RIGHT) + @tty.assert_cursor_position(y: 0, x: 8) + end + + def test_long_strings + ascii = "LongStringOfText" * 6 + unicode = "LongStringOfText" * 3 + + @tty = interactive_fzy(input: [ascii, unicode]) + @tty.assert_matches <<~TTY + > + LongStringOfTextLongStringOfTextLongStringOfTextLongStringOfTextLongStringOfText + LongStringOfTextLongStringOfTextLongStri + TTY + end + def test_help @tty = TTYtest.new_terminal(%{#{FZY_PATH} --help}) @tty.assert_matches <<TTY @@ -335,4 +461,15 @@ Usage: fzy [OPTION]... -v, --version Output version information and exit TTY end + + private + + def interactive_fzy(input: [], before: nil, after: nil, args: "") + cmd = [] + cmd << %{echo "#{before}"} if before + cmd << %{printf "#{input.join("\\n")}" | #{FZY_PATH} #{args}} + cmd << %{echo "#{after}"} if after + cmd = cmd.join("; ") + TTYtest.new_terminal(cmd) + end end |