1" test execute()
2
3source view_util.vim
4source check.vim
5source vim9.vim
6
7func NestedEval()
8  let nested = execute('echo "nested\nlines"')
9  echo 'got: "' . nested . '"'
10endfunc
11
12func NestedRedir()
13  redir => var
14  echo 'broken'
15  redir END
16endfunc
17
18func Test_execute_string()
19  call assert_equal("\nnocompatible", execute('set compatible?'))
20  call assert_equal("\nsomething\nnice", execute('echo "something\nnice"'))
21  call assert_equal("noendofline", execute('echon "noendofline"'))
22  call assert_equal("", execute(123))
23
24  call assert_equal("\ngot: \"\nnested\nlines\"", execute('call NestedEval()'))
25  redir => redired
26  echo 'this'
27  let evaled = execute('echo "that"')
28  echo 'theend'
29  redir END
30  call assert_equal("\nthis\ntheend", redired)
31  call assert_equal("\nthat", evaled)
32
33  call assert_fails('call execute("doesnotexist")', 'E492:')
34  call assert_fails('call execute("call NestedRedir()")', 'E930:')
35
36  call assert_equal("\nsomething", execute('echo "something"', ''))
37  call assert_equal("\nsomething", execute('echo "something"', 'silent'))
38  call assert_equal("\nsomething", execute('echo "something"', 'silent!'))
39  call assert_equal("", execute('burp', 'silent!'))
40  if has('float')
41    call assert_fails('call execute(3.4)', 'E492:')
42    call assert_equal("\nx", execute("echo \"x\"", 3.4))
43    call CheckDefExecAndScriptFailure2(['execute("echo \"x\"", 3.4)'], 'E1013: Argument 2: type mismatch, expected string but got float', 'E1174:')
44  endif
45endfunc
46
47func Test_execute_list()
48  call assert_equal("\nsomething\nnice", execute(['echo "something"', 'echo "nice"']))
49  let l = ['for n in range(0, 3)',
50	\  'echo n',
51	\  'endfor']
52  call assert_equal("\n0\n1\n2\n3", execute(l))
53
54  call assert_equal("", execute([]))
55endfunc
56
57func Test_execute_does_not_change_col()
58  echo ''
59  echon 'abcd'
60  let x = execute('silent echo 234343')
61  echon 'xyz'
62  let text = ''
63  for col in range(1, 7)
64    let text .= nr2char(screenchar(&lines, col))
65  endfor
66  call assert_equal('abcdxyz', text)
67endfunc
68
69func Test_execute_not_silent()
70  echo ''
71  echon 'abcd'
72  let x = execute('echon 234', '')
73  echo 'xyz'
74  let text1 = ''
75  for col in range(1, 8)
76    let text1 .= nr2char(screenchar(&lines - 1, col))
77  endfor
78  call assert_equal('abcd234 ', text1)
79  let text2 = ''
80  for col in range(1, 4)
81    let text2 .= nr2char(screenchar(&lines, col))
82  endfor
83  call assert_equal('xyz ', text2)
84endfunc
85
86func Test_win_execute()
87  let thiswin = win_getid()
88  new
89  let otherwin = win_getid()
90  call setline(1, 'the new window')
91  call win_gotoid(thiswin)
92  let line = win_execute(otherwin, 'echo getline(1)')
93  call assert_match('the new window', line)
94  let line = win_execute(134343, 'echo getline(1)')
95  call assert_equal('', line)
96
97  if has('popupwin')
98    let popupwin = popup_create('the popup win', {'line': 2, 'col': 3})
99    redraw
100    let line = 'echo getline(1)'->win_execute(popupwin)
101    call assert_match('the popup win', line)
102
103    call popup_close(popupwin)
104  endif
105
106  call win_gotoid(otherwin)
107  bwipe!
108
109  " check :lcd in another window does not change directory
110  let curid = win_getid()
111  let curdir = getcwd()
112  split Xother
113  lcd ..
114  " Use :pwd to get the actual current directory
115  let otherdir = execute('pwd')
116  call win_execute(curid, 'lcd testdir')
117  call assert_equal(otherdir, execute('pwd'))
118  bwipe!
119  execute 'cd ' .. curdir
120endfunc
121
122func Test_win_execute_update_ruler()
123  CheckFeature quickfix
124
125  enew
126  call setline(1, range(500))
127  20
128  split
129  let winid = win_getid()
130  set ruler
131  wincmd w
132  let height = winheight(winid)
133  redraw
134  call assert_match('20,1', Screenline(height + 1))
135  let line = win_execute(winid, 'call cursor(100, 1)')
136  redraw
137  call assert_match('100,1', Screenline(height + 1))
138
139  bwipe!
140endfunc
141
142func Test_win_execute_other_tab()
143  let thiswin = win_getid()
144  tabnew
145  call win_execute(thiswin, 'let xyz = 1')
146  call assert_equal(1, xyz)
147  tabclose
148  unlet xyz
149endfunc
150
151func Test_execute_func_with_null()
152  call assert_equal("", execute(test_null_string()))
153  call assert_equal("", execute(test_null_list()))
154  call assert_fails('call execute(test_null_dict())', 'E731:')
155  call assert_fails('call execute(test_null_blob())', 'E976:')
156  call assert_fails('call execute(test_null_partial())','E729:')
157  if has('job')
158    call assert_fails('call execute(test_null_job())', 'E908:')
159    call assert_fails('call execute(test_null_channel())', 'E908:')
160  endif
161endfunc
162
163" vim: shiftwidth=2 sts=2 expandtab
164