neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_execute_func.vim (6352B)


      1 " test execute()
      2 
      3 source view_util.vim
      4 source check.vim
      5 source vim9.vim
      6 source term_util.vim
      7 
      8 func NestedEval()
      9  let nested = execute('echo "nested\nlines"')
     10  echo 'got: "' . nested . '"'
     11 endfunc
     12 
     13 func NestedRedir()
     14  redir => var
     15  echo 'broken'
     16  redir END
     17 endfunc
     18 
     19 func Test_execute_string()
     20  call assert_equal("\nnocompatible", execute('set compatible?'))
     21  call assert_equal("\nsomething\nnice", execute('echo "something\nnice"'))
     22  call assert_equal("noendofline", execute('echon "noendofline"'))
     23  call assert_equal("", execute(123))
     24 
     25  call assert_equal("\ngot: \"\nnested\nlines\"", execute('call NestedEval()'))
     26  redir => redired
     27  echo 'this'
     28  let evaled = execute('echo "that"')
     29  echo 'theend'
     30  redir END
     31 " Nvim supports execute('... :redir ...'), so this test is intentionally
     32 " disabled.
     33 "  call assert_equal("\nthis\ntheend", redired)
     34  call assert_equal("\nthat", evaled)
     35 
     36  call assert_fails('call execute("doesnotexist")', 'E492:')
     37 " Nvim supports execute('... :redir ...'), so this test is intentionally
     38 " disabled.
     39 "  call assert_fails('call execute("call NestedRedir()")', 'E930:')
     40 
     41  call assert_equal("\nsomething", execute('echo "something"', ''))
     42  call assert_equal("\nsomething", execute('echo "something"', 'silent'))
     43  call assert_equal("\nsomething", execute('echo "something"', 'silent!'))
     44  call assert_equal("", execute('burp', 'silent!'))
     45  if has('float')
     46    call assert_fails('call execute(3.4)', 'E492:')
     47    call assert_equal("\nx", execute("echo \"x\"", 3.4))
     48    call CheckDefExecAndScriptFailure(['execute("echo \"x\"", 3.4)'], 'E806:')
     49  endif
     50 endfunc
     51 
     52 func Test_execute_list()
     53  call assert_equal("\nsomething\nnice", execute(['echo "something"', 'echo "nice"']))
     54  let l = ['for n in range(0, 3)',
     55 \  'echo n',
     56 \  'endfor']
     57  call assert_equal("\n0\n1\n2\n3", execute(l))
     58 
     59  call assert_equal("", execute([]))
     60 endfunc
     61 
     62 func Test_execute_does_not_change_col()
     63  echo ''
     64  echon 'abcd'
     65  let x = execute('silent echo 234343')
     66  echon 'xyz'
     67  let text = ''
     68  for col in range(1, 7)
     69    let text .= nr2char(screenchar(&lines, col))
     70  endfor
     71  call assert_equal('abcdxyz', text)
     72 endfunc
     73 
     74 func Test_execute_not_silent()
     75  echo ''
     76  echon 'abcd'
     77  let x = execute('echon 234', '')
     78  echo 'xyz'
     79  let text1 = ''
     80  for col in range(1, 8)
     81    let text1 .= nr2char(screenchar(&lines - 1, col))
     82  endfor
     83  call assert_equal('abcd234 ', text1)
     84  let text2 = ''
     85  for col in range(1, 4)
     86    let text2 .= nr2char(screenchar(&lines, col))
     87  endfor
     88  call assert_equal('xyz ', text2)
     89 endfunc
     90 
     91 func Test_win_execute()
     92  let thiswin = win_getid()
     93  new
     94  let otherwin = win_getid()
     95  call setline(1, 'the new window')
     96  call win_gotoid(thiswin)
     97  let line = win_execute(otherwin, 'echo getline(1)')
     98  call assert_match('the new window', line)
     99  let line = win_execute(134343, 'echo getline(1)')
    100  call assert_equal('', line)
    101 
    102  if has('popupwin')
    103    let popupwin = popup_create('the popup win', {'line': 2, 'col': 3})
    104    redraw
    105    let line = 'echo getline(1)'->win_execute(popupwin)
    106    call assert_match('the popup win', line)
    107 
    108    call popup_close(popupwin)
    109  endif
    110 
    111  call win_gotoid(otherwin)
    112  bwipe!
    113 
    114  " check :lcd in another window does not change directory
    115  let curid = win_getid()
    116  let curdir = getcwd()
    117  split Xother
    118  lcd ..
    119  " Use :pwd to get the actual current directory
    120  let otherdir = execute('pwd')
    121  call win_execute(curid, 'lcd testdir')
    122  call assert_equal(otherdir, execute('pwd'))
    123  bwipe!
    124  execute 'cd ' .. curdir
    125 endfunc
    126 
    127 func Test_win_execute_update_ruler()
    128  CheckFeature quickfix
    129 
    130  enew
    131  call setline(1, range(500))
    132  20
    133  split
    134  let winid = win_getid()
    135  set ruler
    136  wincmd w
    137  let height = winheight(winid)
    138  redraw
    139  call assert_match('20,1', Screenline(height + 1))
    140  let line = win_execute(winid, 'call cursor(100, 1)')
    141  redraw
    142  call assert_match('100,1', Screenline(height + 1))
    143 
    144  bwipe!
    145 endfunc
    146 
    147 func Test_win_execute_other_tab()
    148  let thiswin = win_getid()
    149  tabnew
    150  call win_execute(thiswin, 'let xyz = 1')
    151  call assert_equal(1, xyz)
    152  tabclose
    153  unlet xyz
    154 endfunc
    155 
    156 func Test_win_execute_visual_redraw()
    157  call setline(1, ['a', 'b', 'c'])
    158  new
    159  wincmd p
    160  " start Visual in current window, redraw in other window with fewer lines
    161  call feedkeys("G\<C-V>", 'txn')
    162  call win_execute(winnr('#')->win_getid(), 'redraw')
    163  call feedkeys("\<Esc>", 'txn')
    164  bwipe!
    165  bwipe!
    166 
    167  enew
    168  new
    169  call setline(1, ['a', 'b', 'c'])
    170  let winid = win_getid()
    171  wincmd p
    172  " start Visual in current window, extend it in other window with more lines
    173  call feedkeys("\<C-V>", 'txn')
    174  call win_execute(winid, 'call feedkeys("G\<C-V>", ''txn'')')
    175  redraw
    176 
    177  bwipe!
    178  bwipe!
    179 endfunc
    180 
    181 func Test_win_execute_on_startup()
    182  CheckRunVimInTerminal
    183 
    184  let lines =<< trim END
    185      vim9script
    186      [repeat('x', &columns)]->writefile('Xfile1')
    187      silent tabedit Xfile2
    188      var id = win_getid()
    189      silent tabedit Xfile3
    190      autocmd VimEnter * win_execute(id, 'close')
    191  END
    192  call writefile(lines, 'XwinExecute')
    193  let buf = RunVimInTerminal('-p Xfile1 -Nu XwinExecute', {})
    194 
    195  " this was crashing on exit with EXITFREE defined
    196  call StopVimInTerminal(buf)
    197 
    198  call delete('XwinExecute')
    199  call delete('Xfile1')
    200 endfunc
    201 
    202 func Test_execute_cmd_with_null()
    203  call assert_equal("", execute(v:_null_string))
    204  call assert_equal("", execute(v:_null_list))
    205  call assert_fails('call execute(v:_null_dict)', 'E731:')
    206  call assert_fails('call execute(v:_null_blob)', 'E976:')
    207  " Nvim doesn't have null partials
    208  " call assert_fails('call execute(test_null_partial())','E729:')
    209  if has('job')
    210    call assert_fails('call execute(test_null_job())', 'E908:')
    211    call assert_fails('call execute(test_null_channel())', 'E908:')
    212  endif
    213 endfunc
    214 
    215 func Test_win_execute_tabpagewinnr()
    216  belowright split
    217  tab split
    218  belowright split
    219  call assert_equal(2, tabpagewinnr(1))
    220 
    221  tabprevious
    222  wincmd p
    223  call assert_equal(1, tabpagenr())
    224  call assert_equal(1, tabpagewinnr(1))
    225  call assert_equal(2, tabpagewinnr(2))
    226 
    227  call win_execute(win_getid(1, 2),
    228        \      'call assert_equal(2, tabpagenr())'
    229        \ .. '| call assert_equal(1, tabpagewinnr(1))'
    230        \ .. '| call assert_equal(1, tabpagewinnr(2))')
    231 
    232  call assert_equal(1, tabpagenr())
    233  call assert_equal(1, tabpagewinnr(1))
    234  call assert_equal(2, tabpagewinnr(2))
    235 
    236  %bwipe!
    237 endfunc
    238 
    239 " vim: shiftwidth=2 sts=2 expandtab