Skip to content
Myr

fzf.vader

This file

Path

test/vim/fzf.vader

255 lines · 8.3 KiB

Last changed by

[vim] Open fzf in a tmux or Zellij floating pane by default (#4905)Junegunn Choibefore adoptiona52ac84

History of this file

Take it

1Execute (Setup):2  let g:dir = fnamemodify(g:vader_file, ':p:h')3  unlet! g:fzf_layout g:fzf_action g:fzf_history_dir4  Log 'Test directory: ' . g:dir5  Save &acd67  " fzf#run runs fzf in a terminal buffer and returns immediately, so the8  " assertions have to wait for the run to finish. 'exit' is called from the9  " same handler that runs the sink, just before it, so once it has fired the10  " sink has run too. It also fires when nothing is selected, unlike the sink.11  function! g:FzfExit(code)12    let g:fzf_done = 113  endfunction1415  " Collects the output without displacing a 'sink' the spec already has.16  " s:callback runs both17  function! g:FzfCollect(lines)18    call extend(g:fzf_lines, a:lines)19  endfunction2021  function! g:FzfRun(spec) abort22    let g:fzf_done = 023    let g:fzf_lines = []24    call fzf#run(extend(copy(a:spec),25    \ { 'exit': function('g:FzfExit'), 'sinklist': function('g:FzfCollect') }))26    let started = reltime()27    while !g:fzf_done && reltimefloat(reltime(started)) < 1028      sleep 10m29    endwhile30    if !g:fzf_done31      throw 'Timed out waiting for fzf#run'32    endif33    return g:fzf_lines34  endfunction3536Execute (fzf#run with dir option):37  let cwd = getcwd()38  let result = g:FzfRun({ 'source': 'git ls-files', 'options': '--filter=vdr', 'dir': g:dir })39  AssertEqual ['fzf.vader'], result40  AssertEqual 0, haslocaldir()41  AssertEqual getcwd(), cwd4243  execute 'lcd' fnameescape(cwd)44  let result = sort(g:FzfRun({ 'source': 'git ls-files', 'options': '--filter e', 'dir': g:dir }))45  AssertEqual ['fzf.vader'], result46  AssertEqual 1, haslocaldir()47  AssertEqual getcwd(), cwd4849Execute (fzf#run with Funcref command):50  let g:ret = []51  function! g:FzfTest(e)52    call add(g:ret, a:e)53  endfunction54  let result = sort(g:FzfRun({ 'source': 'git ls-files', 'sink': function('g:FzfTest'), 'options': '--filter e', 'dir': g:dir }))55  AssertEqual ['fzf.vader'], result56  AssertEqual ['fzf.vader'], sort(g:ret)5758Execute (fzf#run with string source):59  let result = sort(g:FzfRun({ 'source': 'echo hi', 'options': '-f i' }))60  AssertEqual ['hi'], result6162Execute (fzf#run with list source):63  let result = sort(g:FzfRun({ 'source': ['hello', 'world'], 'options': '-f e' }))64  AssertEqual ['hello'], result65  let result = sort(g:FzfRun({ 'source': ['hello', 'world'], 'options': '-f o' }))66  AssertEqual ['hello', 'world'], result6768Execute (fzf#run with string source):69  let result = sort(g:FzfRun({ 'source': 'echo hi', 'options': '-f i' }))70  AssertEqual ['hi'], result7172Execute (fzf#run with dir option and noautochdir):73  set noacd74  let cwd = getcwd()75  call g:FzfRun({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})76  " No change in working directory77  AssertEqual cwd, getcwd()7879  call g:FzfRun({'source': ['/foobar'], 'sink': 'tabe', 'dir': '/tmp', 'options': '-1'})80  AssertEqual cwd, getcwd()81  tabclose82  AssertEqual cwd, getcwd()8384Execute (Incomplete fzf#run with dir option and autochdir):85  set acd86  let cwd = getcwd()87  call g:FzfRun({'source': [], 'sink': 'e', 'dir': '/tmp', 'options': '-0'})88  " No change in working directory even if &acd is set89  AssertEqual cwd, getcwd()9091Execute (FIXME: fzf#run with dir option and autochdir):92  set acd93  call g:FzfRun({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})94  " Working directory changed due to &acd95  AssertEqual '/foobar', expand('%')96  AssertEqual '/', getcwd()9798Execute (fzf#run with dir option and autochdir when final cwd is same as dir):99  set acd100  cd /tmp101  call g:FzfRun({'source': ['/foobar'], 'sink': 'e', 'dir': '/', 'options': '-1'})102  " Working directory changed due to &acd103  AssertEqual '/', getcwd()104105Execute (Default layout):106  unlet! g:fzf_layout g:fzf_height107  let layout_keys = ['window', 'popup', 'tmux', 'up', 'down', 'left', 'right']108109  let opts = fzf#wrap('foobar')110  Log opts111  let found = filter(copy(layout_keys), 'has_key(opts, v:val)')112  AssertEqual 1, len(found)113  if found[0] ==# 'tmux'114    " Only where fzf opens a floating pane, which can be left while fzf runs115    Assert !empty($TMUX) || !empty($ZELLIJ)116    AssertEqual '90%,60%', opts.tmux117  elseif found[0] ==# 'window'118    AssertEqual 0.9, opts.window.width119  else120    " No popup support in this build121    AssertEqual '~40%', opts.down122  endif123124  " Fullscreen strips it, whichever it was125  let opts = fzf#wrap('foobar', {}, 1)126  Log opts127  AssertEqual [], filter(copy(layout_keys), 'has_key(opts, v:val)')128129Execute (fzf#wrap):130  AssertThrows fzf#wrap({'foo': 'bar'})131132  " Pin the layout so the assertions do not depend on the environment133  let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }134135  let opts = fzf#wrap('foobar')136  Log opts137  AssertEqual 0.9, opts.window.width138  Assert opts.options =~ '--expect='139  Assert !has_key(opts, 'sink')140  Assert has_key(opts, 'sink*')141142  let opts = fzf#wrap('foobar', {}, 0)143  Log opts144  AssertEqual 0.9, opts.window.width145146  let opts = fzf#wrap('foobar', {}, 1)147  Log opts148  Assert !has_key(opts, 'window')149150  let opts = fzf#wrap('foobar', {'down': '50%'})151  Log opts152  AssertEqual '50%', opts.down153154  let opts = fzf#wrap('foobar', {'down': '50%'}, 1)155  Log opts156  Assert !has_key(opts, 'down')157158  let opts = fzf#wrap('foobar', {'sink': 'e'})159  Log opts160  AssertEqual 'e', opts.sink161  Assert !has_key(opts, 'sink*')162163  let opts = fzf#wrap('foobar', {'options': '--reverse'})164  Log opts165  Assert opts.options =~ '--expect='166  Assert opts.options =~ '--reverse'167168  let g:fzf_layout = {'window': 'enew'}169  let opts = fzf#wrap('foobar')170  Log opts171  AssertEqual 'enew', opts.window172173  let opts = fzf#wrap('foobar', {}, 1)174  Log opts175  Assert !has_key(opts, 'window')176177  let opts = fzf#wrap('foobar', {'right': '50%'})178  Log opts179  Assert !has_key(opts, 'window')180  AssertEqual '50%', opts.right181182  let opts = fzf#wrap('foobar', {'right': '50%'}, 1)183  Log opts184  Assert !has_key(opts, 'window')185  Assert !has_key(opts, 'right')186187  let g:fzf_action = {'a': 'tabe'}188  let opts = fzf#wrap('foobar')189  Log opts190  Assert opts.options =~ '--expect=a'191  Assert !has_key(opts, 'sink')192  Assert has_key(opts, 'sink*')193194  let opts = fzf#wrap('foobar', {'sink': 'e'})195  Log opts196  AssertEqual 'e', opts.sink197  Assert !has_key(opts, 'sink*')198199  let g:fzf_history_dir = '/tmp'200  let opts = fzf#wrap('foobar', {'options': '--color light'})201  Log opts202  Assert opts.options =~ "--history '/tmp/foobar'"203  Assert opts.options =~ '--color light'204205  let g:fzf_colors = { 'fg': ['fg', 'Error'] }206  let opts = fzf#wrap({})207  Assert opts.options =~ '--color=fg:'208209Execute (popup is a synonym of tmux):210  unlet! g:fzf_layout211212  " Treated as a layout option, so g:fzf_layout is not applied on top213  let opts = fzf#wrap('foobar', {'popup': '90%,70%'})214  AssertEqual '90%,70%', opts.popup215  Assert !has_key(opts, 'window')216217  " And stripped in fullscreen, like the others218  let opts = fzf#wrap('foobar', {'popup': '90%,70%'}, 1)219  Assert !has_key(opts, 'popup')220221  " Accepted in g:fzf_layout222  let g:fzf_layout = {'popup': '90%,70%'}223  let opts = fzf#wrap('foobar')224  AssertEqual '90%,70%', opts.popup225  unlet g:fzf_layout226227  " Listed as a valid key228  let g:fzf_layout = {'bogus': 1}229  AssertThrows call fzf#wrap('foobar')230  Assert g:vader_exception =~ 'popup'231  unlet g:fzf_layout232233Execute (fzf#shellescape with sh):234  AssertEqual '''''', fzf#shellescape('', 'sh')235  AssertEqual '''\''', fzf#shellescape('\', 'sh')236  AssertEqual '''""''', fzf#shellescape('""', 'sh')237  AssertEqual '''foobar>''', fzf#shellescape('foobar>', 'sh')238  AssertEqual '''\\\"\\\''', fzf#shellescape('\\\"\\\', 'sh')239  AssertEqual '''echo ''\''''a''\'''' && echo ''\''''b''\''''''', fzf#shellescape('echo ''a'' && echo ''b''', 'sh')240241Execute (fzf#shellescape with cmd.exe):242  AssertEqual '^"^"', fzf#shellescape('', 'cmd.exe')243  AssertEqual '^"\\^"', fzf#shellescape('\', 'cmd.exe')244  AssertEqual '^"\^"\^"^"', fzf#shellescape('""', 'cmd.exe')245  AssertEqual '^"foobar^>^"', fzf#shellescape('foobar>', 'cmd.exe')246  AssertEqual '^"\\\\\\\^"\\\\\\^"', fzf#shellescape('\\\"\\\', 'cmd.exe')247  AssertEqual '^"echo ''a'' ^&^& echo ''b''^"', fzf#shellescape('echo ''a'' && echo ''b''', 'cmd.exe')248249  AssertEqual '^"C:\Program Files ^(x86^)\\^"', fzf#shellescape('C:\Program Files (x86)\', 'cmd.exe')250  AssertEqual '^"C:/Program Files ^(x86^)/^"', fzf#shellescape('C:/Program Files (x86)/', 'cmd.exe')251  AssertEqual '^"%%USERPROFILE%%^"', fzf#shellescape('%USERPROFILE%', 'cmd.exe')252253Execute (Cleanup):254  unlet g:dir255  Restore