blob: c2d504fe145e2d10728ee4d9c716edd6d93df63d [file] [log] [blame]
Bram Moolenaar79815f12016-07-09 17:07:29 +02001" test execute()
2
Bram Moolenaar345f28d2019-10-08 22:20:35 +02003source view_util.vim
4
Bram Moolenaar79815f12016-07-09 17:07:29 +02005func NestedEval()
6 let nested = execute('echo "nested\nlines"')
7 echo 'got: "' . nested . '"'
8endfunc
9
10func NestedRedir()
11 redir => var
12 echo 'broken'
13 redir END
14endfunc
15
16func Test_execute_string()
17 call assert_equal("\nnocompatible", execute('set compatible?'))
18 call assert_equal("\nsomething\nnice", execute('echo "something\nnice"'))
19 call assert_equal("noendofline", execute('echon "noendofline"'))
20 call assert_equal("", execute(123))
21
22 call assert_equal("\ngot: \"\nnested\nlines\"", execute('call NestedEval()'))
23 redir => redired
24 echo 'this'
25 let evaled = execute('echo "that"')
26 echo 'theend'
27 redir END
28 call assert_equal("\nthis\ntheend", redired)
29 call assert_equal("\nthat", evaled)
30
31 call assert_fails('call execute("doesnotexist")', 'E492:')
32 call assert_fails('call execute(3.4)', 'E806:')
33 call assert_fails('call execute("call NestedRedir()")', 'E930:')
34
35 call assert_equal("\nsomething", execute('echo "something"', ''))
36 call assert_equal("\nsomething", execute('echo "something"', 'silent'))
37 call assert_equal("\nsomething", execute('echo "something"', 'silent!'))
38 call assert_equal("", execute('burp', 'silent!'))
39 call assert_fails('call execute("echo \"x\"", 3.4)', 'E806:')
40
41 call assert_equal("", execute(test_null_string()))
42endfunc
43
44func Test_execute_list()
45 call assert_equal("\nsomething\nnice", execute(['echo "something"', 'echo "nice"']))
46 let l = ['for n in range(0, 3)',
47 \ 'echo n',
48 \ 'endfor']
49 call assert_equal("\n0\n1\n2\n3", execute(l))
50
51 call assert_equal("", execute([]))
52 call assert_equal("", execute(test_null_list()))
53endfunc
Bram Moolenaar10ccaa12018-12-07 16:38:23 +010054
55func Test_execute_does_not_change_col()
56 echo ''
57 echon 'abcd'
58 let x = execute('silent echo 234343')
59 echon 'xyz'
60 let text = ''
61 for col in range(1, 7)
62 let text .= nr2char(screenchar(&lines, col))
63 endfor
64 call assert_equal('abcdxyz', text)
65endfunc
Bram Moolenaar446e7a32018-12-08 13:57:42 +010066
67func Test_execute_not_silent()
68 echo ''
69 echon 'abcd'
70 let x = execute('echon 234', '')
71 echo 'xyz'
72 let text1 = ''
73 for col in range(1, 8)
74 let text1 .= nr2char(screenchar(&lines - 1, col))
75 endfor
76 call assert_equal('abcd234 ', text1)
77 let text2 = ''
78 for col in range(1, 4)
79 let text2 .= nr2char(screenchar(&lines, col))
80 endfor
81 call assert_equal('xyz ', text2)
82endfunc
Bram Moolenaar868b7b62019-05-29 21:44:40 +020083
84func Test_win_execute()
85 let thiswin = win_getid()
86 new
87 let otherwin = win_getid()
88 call setline(1, 'the new window')
89 call win_gotoid(thiswin)
90 let line = win_execute(otherwin, 'echo getline(1)')
91 call assert_match('the new window', line)
92
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010093 if has('popupwin')
Bram Moolenaar868b7b62019-05-29 21:44:40 +020094 let popupwin = popup_create('the popup win', {'line': 2, 'col': 3})
95 redraw
Bram Moolenaarf92e58c2019-09-08 21:51:41 +020096 let line = 'echo getline(1)'->win_execute(popupwin)
Bram Moolenaar868b7b62019-05-29 21:44:40 +020097 call assert_match('the popup win', line)
98
Bram Moolenaar868b7b62019-05-29 21:44:40 +020099 call popup_close(popupwin)
100 endif
101
102 call win_gotoid(otherwin)
103 bwipe!
104endfunc
Bram Moolenaar820680b2019-08-09 14:56:22 +0200105
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200106func Test_win_execute_update_ruler()
107 enew
108 call setline(1, range(500))
109 20
110 split
111 let winid = win_getid()
112 set ruler
113 wincmd w
114 let height = winheight(winid)
115 redraw
116 call assert_match('20,1', Screenline(height + 1))
117 let line = win_execute(winid, 'call cursor(100, 1)')
118 redraw
119 call assert_match('100,1', Screenline(height + 1))
120
121 bwipe!
122endfunc
123
Bram Moolenaar820680b2019-08-09 14:56:22 +0200124func Test_win_execute_other_tab()
125 let thiswin = win_getid()
126 tabnew
127 call win_execute(thiswin, 'let xyz = 1')
128 call assert_equal(1, xyz)
129 tabclose
130 unlet xyz
131endfunc