# frozen_string_literal: true
require_relative 'lib/common'
# Testing basic features of fzf
class TestCore < TestInteractive
def test_fzf_default_command
tmux.send_keys fzf.sub('FZF_DEFAULT_COMMAND=', "FZF_DEFAULT_COMMAND='echo hello'"), :Enter
tmux.until { |lines| assert_equal '> hello', lines[-3] }
tmux.send_keys :Enter
assert_equal 'hello', fzf_output
end
def test_fzf_default_command_failure
tmux.send_keys fzf.sub('FZF_DEFAULT_COMMAND=', 'FZF_DEFAULT_COMMAND=false'), :Enter
tmux.until { |lines| assert_includes lines[-2], ' [Command failed: false] ─' }
tmux.send_keys :Enter
end
def test_key_bindings
tmux.send_keys "#{FZF} -q 'foo bar foo-bar'", :Enter
tmux.until { |lines| assert_equal '> foo bar foo-bar', lines.last }
# CTRL-A
tmux.send_keys 'C-A', '('
tmux.until { |lines| assert_equal '> (foo bar foo-bar', lines.last }
# META-F
tmux.send_keys :Escape, :f, ')'
tmux.until { |lines| assert_equal '> (foo) bar foo-bar', lines.last }
# CTRL-B
tmux.send_keys 'C-B', 'var'
tmux.until { |lines| assert_equal '> (foovar) bar foo-bar', lines.last }
# Left, CTRL-D
tmux.send_keys :Left, :Left, 'C-D'
tmux.until { |lines| assert_equal '> (foovr) bar foo-bar', lines.last }
# META-BS
tmux.send_keys :Escape, :BSpace
tmux.until { |lines| assert_equal '> (r) bar foo-bar', lines.last }
# CTRL-Y
tmux.send_keys 'C-Y', 'C-Y'
tmux.until { |lines| assert_equal '> (foovfoovr) bar foo-bar', lines.last }
# META-B
tmux.send_keys :Escape, :b, :Space, :Space
tmux.until { |lines| assert_equal '> ( foovfoovr) bar foo-bar', lines.last }
# CTRL-F / Right
tmux.send_keys 'C-F', :Right, '/'
tmux.until { |lines| assert_equal '> ( fo/ovfoovr) bar foo-bar', lines.last }
# CTRL-H / BS
tmux.send_keys 'C-H', :BSpace
tmux.until { |lines| assert_equal '> ( fovfoovr) bar foo-bar', lines.last }
# CTRL-E
tmux.send_keys 'C-E', 'baz'
tmux.until { |lines| assert_equal '> ( fovfoovr) bar foo-barbaz', lines.last }
# CTRL-U
tmux.send_keys 'C-U'
tmux.until { |lines| assert_equal '>', lines.last }
# CTRL-Y
tmux.send_keys 'C-Y'
tmux.until { |lines| assert_equal '> ( fovfoovr) bar foo-barbaz', lines.last }
# CTRL-W
tmux.send_keys 'C-W', 'bar-foo'
tmux.until { |lines| assert_equal '> ( fovfoovr) bar bar-foo', lines.last }
# META-D
tmux.send_keys :Escape, :b, :Escape, :b, :Escape, :d, 'C-A', 'C-Y'
tmux.until { |lines| assert_equal '> bar( fovfoovr) bar -foo', lines.last }
# CTRL-M
tmux.send_keys 'C-M'
tmux.until { |lines| refute_equal '>', lines.last }
end
def test_file_word
tmux.send_keys "#{FZF} -q '--/foo bar/foo-bar/baz' --filepath-word", :Enter
tmux.until { |lines| assert_equal '> --/foo bar/foo-bar/baz', lines.last }
tmux.send_keys :Escape, :b
tmux.send_keys :Escape, :b
tmux.send_keys :Escape, :b
tmux.send_keys :Escape, :d
tmux.send_keys :Escape, :f
tmux.send_keys :Escape, :BSpace
tmux.until { |lines| assert_equal '> --///baz', lines.last }
end
def test_multi_order
tmux.send_keys "seq 1 10 | #{fzf(:multi)}", :Enter
tmux.until { |lines| assert_equal '>', lines.last }
tmux.send_keys :Tab, :Up, :Up, :Tab, :Tab, :Tab, # 3, 2
'C-K', 'C-K', 'C-K', 'C-K', :BTab, :BTab, # 5, 6
:PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7
tmux.until { |lines| assert_equal ' 10/10 (6)', lines[-2] }
tmux.send_keys 'C-M'
assert_equal %w[3 2 5 6 8 7], fzf_output_lines
end
def test_subword_forward
tmux.send_keys "#{FZF} --bind K:kill-subword,F:forward-subword -q 'foo bar foo-bar fooFooBar'", :Enter, :Home
tmux.until { |lines| assert_equal '> foo bar foo-bar fooFooBar', lines.last }
tmux.send_keys 'F', :Delete
tmux.until { |lines| assert_equal '> foobar foo-bar fooFooBar', lines.last }
tmux.send_keys 'K'
tmux.until { |lines| assert_equal '> foo foo-bar fooFooBar', lines.last }
tmux.send_keys 'F', 'K'
tmux.until { |lines| assert_equal '> foo foo fooFooBar', lines.last }
tmux.send_keys 'F', 'F', 'K'
tmux.until { |lines| assert_equal '> foo foo fooFoo', lines.last }
end
def test_subword_backward
tmux.send_keys "#{FZF} --bind K:backward-kill-subword,B:backward-subword -q 'foo bar foo-bar fooBar'", :Enter
tmux.until { |lines| assert_equal '> foo bar foo-bar fooBar', lines.last }
tmux.send_keys 'B', :BSpace
tmux.until { |lines| assert_equal '> foo bar foo-bar foBar', lines.last }
tmux.send_keys 'K'
tmux.until { |lines| assert_equal '> foo bar foo-bar Bar', lines.last }
tmux.send_keys 'B', :BSpace
tmux.until { |lines| assert_equal '> foo bar foobar Bar', lines.last }
tmux.send_keys 'B', 'B', :BSpace
tmux.until { |lines| assert_equal '> foobar foobar Bar', lines.last }
end
def test_multi_max
tmux.send_keys "seq 1 10 | #{FZF} -m 3 --bind A:select-all,T:toggle-all --preview 'echo [{+}]/{}'", :Enter
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.send_keys '1'
tmux.until do |lines|
assert_includes lines[1], ' [1]/1 '
assert lines[-2]&.start_with?(' 2/10 ')
end
tmux.send_keys 'A'
tmux.until do |lines|
assert_includes lines[1], ' [1 10]/1 '
assert lines[-2]&.start_with?(' 2/10 (2/3)')
end
tmux.send_keys :BSpace
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (2/3)') }
tmux.send_keys 'T'
tmux.until do |lines|
assert_includes lines[1], ' [2 3 4]/1 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
%w[T A].each do |key|
tmux.send_keys key
tmux.until do |lines|
assert_includes lines[1], ' [1 5 6]/1 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
end
tmux.send_keys :BTab
tmux.until do |lines|
assert_includes lines[1], ' [5 6]/2 '
assert lines[-2]&.start_with?(' 10/10 (2/3)')
end
[:BTab, :BTab, 'A'].each do |key|
tmux.send_keys key
tmux.until do |lines|
assert_includes lines[1], ' [5 6 2]/3 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
end
tmux.send_keys '2'
tmux.until { |lines| assert lines[-2]&.start_with?(' 1/10 (3/3)') }
tmux.send_keys 'T'
tmux.until do |lines|
assert_includes lines[1], ' [5 6]/2 '
assert lines[-2]&.start_with?(' 1/10 (2/3)')
end
tmux.send_keys :BSpace
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (2/3)') }
tmux.send_keys 'A'
tmux.until do |lines|
assert_includes lines[1], ' [5 6 1]/1 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
end
def test_multi_action
tmux.send_keys "seq 10 | #{FZF} --bind 'a:change-multi,b:change-multi(3),c:change-multi(xxx),d:change-multi(0)'", :Enter
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 ') }
tmux.send_keys 'a'
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (0)') }
tmux.send_keys 'b'
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (0/3)') }
tmux.send_keys :BTab
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (1/3)') }
tmux.send_keys 'c'
tmux.send_keys :BTab
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (2/3)') }
tmux.send_keys 'd'
tmux.until do |lines|
assert lines[-2]&.start_with?(' 10/10 ') && !lines[-2]&.include?('(')
end
end
def test_with_nth
[true, false].each do |multi|
tmux.send_keys "(echo ' 1st 2nd 3rd/';
echo ' first second third/') |
#{fzf(multi && :multi, :x, :nth, 2, :with_nth, '2,-1,1')}",
:Enter
tmux.until { |lines| assert_equal multi ? ' 2/2 (0)' : ' 2/2', lines[-2] }
# Transformed list
lines = tmux.capture
assert_equal ' second third/first', lines[-4]
assert_equal '> 2nd 3rd/1st', lines[-3]
# However, the output must not be transformed
if multi
tmux.send_keys :BTab, :BTab
tmux.until { |lines| assert_equal ' 2/2 (2)', lines[-2] }
tmux.send_keys :Enter
assert_equal [' 1st 2nd 3rd/', ' first second third/'], fzf_output_lines
else
tmux.send_keys '^', '3'
tmux.until { |lines| assert_equal ' 1/2', lines[-2] }
tmux.send_keys :Enter
assert_equal [' 1st 2nd 3rd/'], fzf_output_lines
end
end
end
def test_scroll
[true, false].each do |rev|
tmux.send_keys "seq 1 100 | #{fzf(rev && :reverse)}", :Enter
tmux.until { |lines| assert_equal ' 100/100', lines[rev ? 1 : -2] }
tmux.send_keys(*Array.new(110) { rev ? :Down : :Up })
tmux.until { |lines| assert_includes lines, '> 100' }
tmux.send_keys :Enter
assert_equal '100', fzf_output
end
end
def test_select_1
tmux.send_keys "seq 1 100 | #{fzf(:with_nth, '..,..', :print_query, :q, 5555, :'1')}", :Enter
assert_equal %w[5555 55], fzf_output_lines
end
def test_select_1_accept_nth
tmux.send_keys "seq 1 100 | #{fzf(:with_nth, '..,..', :print_query, :q, 5555, :'1', :accept_nth, '"{1} // {1}"')}", :Enter
assert_equal ['5555', '55 // 55'], fzf_output_lines
end
def test_exit_0
tmux.send_keys "seq 1 100 | #{fzf(:with_nth, '..,..', :print_query, :q, 555_555, :'0')}", :Enter
assert_equal %w[555555], fzf_output_lines
end
def test_select_1_exit_0_fail
[:'0', :'1', %i[1 0]].each do |opt|
tmux.send_keys "seq 1 100 | #{fzf(:print_query, :multi, :q, 5, *opt)}", :Enter
tmux.until { |lines| assert_equal '> 5', lines.last }
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 19/100 (3)', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[5 5 50 51], fzf_output_lines
end
end
def test_query_unicode
tmux.paste "(echo abc; echo