Add test cases for click-header and click-footer
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Injects SGR 1006 mouse events via tmux send-keys -l to exercise FZF_CLICK_HEADER_* / FZF_CLICK_FOOTER_* across all three layouts, with and without a header border.
authored by Junegunn Choi · committed by Junegunn Choi · parent 8b66489 · Browse files · 2 files·+92 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+92 added−0 removed
- tests:+92 added−0 removed
Provenance
- Commit
faa6a7e- Tree
6906cce- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
8b66489
2 files changed+92 added−0 removed
modified:test/lib/common.rb+17 added
103 unmodified lines104 def send_keys(*args)105 go(%W[send-keys -t #{win}] + args.map(&:to_s))106 end107 108+ # Simulate a mouse click at the given 1-based column and row using the SGR mouse protocol109+ # (xterm mouse mode 1006, which fzf enables). The escape sequence is injected as literal110+ # keystrokes via tmux, and fzf parses it like a real terminal mouse event.111+ #112+ # tmux's own mouse handling intercepts these sequences when `set -g mouse on`, so we toggle113+ # mouse off for the duration of the click and restore the previous state afterwards.114+ def click(col, row, button: 0)115+ prev = go(%w[show-options -gv mouse]).first116+ go(%w[set-option -g mouse off])117+ begin118+ seq = "\e[<#{button};#{col};#{row}M\e[<#{button};#{col};#{row}m"119+ go(%W[send-keys -t #{win} -l #{seq}])120+ ensure121+ go(%W[set-option -g mouse #{prev}]) if prev && !prev.empty?122+ end123+ end124+125 def paste(str)126 system('tmux', 'setb', str, ';', 'pasteb', '-t', win, ';', 'send-keys', '-t', win, 'Enter')127 end128 140 unmodified linesmodified:test/test_layout.rb+75 added
1296 unmodified lines1297 end1298 tmux.send_keys :Enter1299 end1300 end1301+1302+ # Locate a word in the currently captured screen and click its first character.1303+ # tmux rows/columns are 1-based; capture indices are 0-based.1304+ def click_word(word)1305+ tmux.capture.each_with_index do |line, idx|1306+ col = line.index(word)1307+ return tmux.click(col + 1, idx + 1) if col1308+ end1309+ flunk("word #{word.inspect} not found on screen")1310+ end1311+1312+ # Launch fzf with a click-{header,footer} binding that echoes FZF_CLICK_* into the prompt,1313+ # then click each word in `clicks` and assert the resulting L/W values.1314+ # `clicks` is an array of [word_to_click, expected_line].1315+ def verify_clicks(kind:, opts:, input:, clicks:)1316+ var = kind.to_s.upcase # HEADER or FOOTER1317+ binding = "click-#{kind}:transform-prompt:" \1318+ "echo \"L=$FZF_CLICK_#{var}_LINE W=$FZF_CLICK_#{var}_WORD> \""1319+ # --multi makes the info line end in " (0)" so the wait regex is unambiguous.1320+ tmux.send_keys %(#{input} | #{FZF} #{opts} --multi --bind '#{binding}'), :Enter1321+ # Wait for fzf to fully render before inspecting the screen, otherwise the echoed1322+ # command line can shadow click targets.1323+ tmux.until { |lines| lines.any_include?(%r{ [0-9]+/[0-9]+ \(0\)}) }1324+ clicks.each do |word, line|1325+ click_word(word)1326+ tmux.until { |lines| assert lines.any_include?("L=#{line} W=#{word}>") }1327+ end1328+ tmux.send_keys 'Escape'1329+ end1330+1331+ # Header lines (--header-lines) are rendered in reverse display order only under1332+ # layout=default; in layout=reverse and layout=reverse-list they keep the input order.1333+ # FZF_CLICK_HEADER_LINE reflects the visual row, so the expected value flips.1334+ HEADER_CLICKS = [%w[Aaa 1], %w[Bbb 2], %w[Ccc 3]].freeze1335+1336+ %w[default reverse reverse-list].each do |layout|1337+ slug = layout.tr('-', '_')1338+1339+ # Plain --header with no border around the header section.1340+ define_method(:"test_click_header_plain_#{slug}") do1341+ verify_clicks(kind: :header,1342+ opts: %(--layout=#{layout} --header $'Aaa\\nBbb\\nCcc'),1343+ input: 'seq 5',1344+ clicks: HEADER_CLICKS)1345+ end1346+1347+ # --header with a framing border (--style full gives --header-border=rounded by default).1348+ define_method(:"test_click_header_border_rounded_#{slug}") do1349+ verify_clicks(kind: :header,1350+ opts: %(--layout=#{layout} --style full --header $'Aaa\\nBbb\\nCcc'),1351+ input: 'seq 5',1352+ clicks: HEADER_CLICKS)1353+ end1354+1355+ # --header-lines consumed from stdin, with its own framing border.1356+ define_method(:"test_click_header_lines_border_rounded_#{slug}") do1357+ clicks_hl = if layout == 'default'1358+ [%w[Xaa 3], %w[Ybb 2], %w[Zcc 1]]1359+ else1360+ [%w[Xaa 1], %w[Ybb 2], %w[Zcc 3]]1361+ end1362+ verify_clicks(kind: :header,1363+ opts: %(--layout=#{layout} --style full --header-lines 3),1364+ input: "(printf 'Xaa\\nYbb\\nZcc\\n'; seq 5)",1365+ clicks: clicks_hl)1366+ end1367+1368+ # --footer with a framing border.1369+ define_method(:"test_click_footer_border_rounded_#{slug}") do1370+ verify_clicks(kind: :footer,1371+ opts: %(--layout=#{layout} --style full --footer $'Foo\\nBar\\nBaz'),1372+ input: 'seq 5',1373+ clicks: [%w[Foo 1], %w[Bar 2], %w[Baz 3]])1374+ end1375+ end1376 end