1" Tests for user defined commands
2
3" Test for <mods> in user defined commands
4function Test_cmdmods()
5  let g:mods = ''
6
7  command! -nargs=* MyCmd let g:mods .= '<mods> '
8
9  MyCmd
10  aboveleft MyCmd
11  abo MyCmd
12  belowright MyCmd
13  bel MyCmd
14  botright MyCmd
15  bo MyCmd
16  browse MyCmd
17  bro MyCmd
18  confirm MyCmd
19  conf MyCmd
20  hide MyCmd
21  hid MyCmd
22  keepalt MyCmd
23  keepa MyCmd
24  keepjumps MyCmd
25  keepj MyCmd
26  keepmarks MyCmd
27  kee MyCmd
28  keeppatterns MyCmd
29  keepp MyCmd
30  leftabove MyCmd  " results in :aboveleft
31  lefta MyCmd
32  lockmarks MyCmd
33  loc MyCmd
34  " noautocmd MyCmd
35  noswapfile MyCmd
36  nos MyCmd
37  rightbelow MyCmd " results in :belowright
38  rightb MyCmd
39  " sandbox MyCmd
40  silent MyCmd
41  sil MyCmd
42  tab MyCmd
43  topleft MyCmd
44  to MyCmd
45  " unsilent MyCmd
46  verbose MyCmd
47  verb MyCmd
48  vertical MyCmd
49  vert MyCmd
50
51  aboveleft belowright botright browse confirm hide keepalt keepjumps
52	      \ keepmarks keeppatterns lockmarks noswapfile silent tab
53	      \ topleft verbose vertical MyCmd
54
55  call assert_equal(' aboveleft aboveleft belowright belowright botright ' .
56      \ 'botright browse browse confirm confirm hide hide ' .
57      \ 'keepalt keepalt keepjumps keepjumps keepmarks keepmarks ' .
58      \ 'keeppatterns keeppatterns aboveleft aboveleft lockmarks lockmarks noswapfile ' .
59      \ 'noswapfile belowright belowright silent silent tab topleft topleft verbose verbose ' .
60      \ 'vertical vertical ' .
61      \ 'aboveleft belowright botright browse confirm hide keepalt keepjumps ' .
62      \ 'keepmarks keeppatterns lockmarks noswapfile silent tab topleft ' .
63      \ 'verbose vertical ', g:mods)
64
65  let g:mods = ''
66  command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
67
68  vertical MyQCmd
69  call assert_equal('"vertical" ', g:mods)
70
71  delcommand MyCmd
72  delcommand MyQCmd
73  unlet g:mods
74endfunction
75
76func Test_Ambiguous()
77  command Doit let g:didit = 'yes'
78  command Dothat let g:didthat = 'also'
79  call assert_fails('Do', 'E464:')
80  Doit
81  call assert_equal('yes', g:didit)
82  Dothat
83  call assert_equal('also', g:didthat)
84  unlet g:didit
85  unlet g:didthat
86
87  delcommand Doit
88  Do
89  call assert_equal('also', g:didthat)
90  delcommand Dothat
91endfunc
92
93func Test_redefine_on_reload()
94  call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
95  call assert_equal(0, exists(':ExistingCommand'))
96  source Xcommandexists
97  call assert_equal(2, exists(':ExistingCommand'))
98  " Redefining a command when reloading a script is OK.
99  source Xcommandexists
100  call assert_equal(2, exists(':ExistingCommand'))
101
102  " But redefining in another script is not OK.
103  call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
104  call assert_fails('source Xcommandexists2', 'E174:')
105  call delete('Xcommandexists2')
106
107  " And defining twice in one script is not OK.
108  delcommand ExistingCommand
109  call assert_equal(0, exists(':ExistingCommand'))
110  call writefile([
111	\ 'command ExistingCommand echo "yes"',
112	\ 'command ExistingCommand echo "no"',
113	\ ], 'Xcommandexists')
114  call assert_fails('source Xcommandexists', 'E174:')
115  call assert_equal(2, exists(':ExistingCommand'))
116
117  call delete('Xcommandexists')
118  delcommand ExistingCommand
119endfunc
120
121func Test_CmdUndefined()
122  call assert_fails('Doit', 'E492:')
123  au CmdUndefined Doit :command Doit let g:didit = 'yes'
124  Doit
125  call assert_equal('yes', g:didit)
126  delcommand Doit
127
128  call assert_fails('Dothat', 'E492:')
129  au CmdUndefined * let g:didnot = 'yes'
130  call assert_fails('Dothat', 'E492:')
131  call assert_equal('yes', g:didnot)
132endfunc
133
134func Test_CmdErrors()
135  call assert_fails('com! docmd :', 'E183:')
136  call assert_fails('com! \<Tab> :', 'E182:')
137  call assert_fails('com! _ :', 'E182:')
138  call assert_fails('com! X :', 'E841:')
139  call assert_fails('com! - DoCmd :', 'E175:')
140  call assert_fails('com! -xxx DoCmd :', 'E181:')
141  call assert_fails('com! -addr DoCmd :', 'E179:')
142  call assert_fails('com! -complete DoCmd :', 'E179:')
143  call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
144  call assert_fails('com! -complete=custom DoCmd :', 'E467:')
145  call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
146  call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
147  call assert_fails('com! -nargs=x DoCmd :', 'E176:')
148  call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
149  call assert_fails('com! -count=x DoCmd :', 'E178:')
150  call assert_fails('com! -range=x DoCmd :', 'E178:')
151
152  com! -nargs=0 DoCmd :
153  call assert_fails('DoCmd x', 'E488:')
154
155  com! -nargs=1 DoCmd :
156  call assert_fails('DoCmd', 'E471:')
157
158  com! -nargs=+ DoCmd :
159  call assert_fails('DoCmd', 'E471:')
160
161  call assert_fails('com DoCmd :', 'E174:')
162  comclear
163  call assert_fails('delcom DoCmd', 'E184:')
164endfunc
165
166func CustomComplete(A, L, P)
167  return "January\nFebruary\nMars\n"
168endfunc
169
170func CustomCompleteList(A, L, P)
171  return [ "Monday", "Tuesday", "Wednesday" ]
172endfunc
173
174func Test_CmdCompletion()
175  call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
176  call assert_equal('"com -addr bang bar buffer complete count nargs range register', @:)
177
178  call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
179  call assert_equal('"com -nargs=0 -addr bang bar buffer complete count nargs range register', @:)
180
181  call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
182  call assert_equal('"com -nargs=* + 0 1 ?', @:)
183
184  call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
185  call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
186
187  call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
188  call assert_equal('"com -complete=color command compiler', @:)
189
190  command! DoCmd1 :
191  command! DoCmd2 :
192  call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
193  call assert_equal('"com DoCmd1 DoCmd2', @:)
194
195  call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
196  call assert_equal('"DoCmd1 DoCmd2', @:)
197
198  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
199  call assert_equal('"delcom DoCmd1 DoCmd2', @:)
200
201  delcom DoCmd1
202  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
203  call assert_equal('"delcom DoCmd2', @:)
204
205  call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
206  call assert_equal('"com DoCmd2', @:)
207
208  delcom DoCmd2
209  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
210  call assert_equal('"delcom DoC', @:)
211
212  call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
213  call assert_equal('"com DoC', @:)
214
215  com! -complete=behave DoCmd :
216  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
217  call assert_equal('"DoCmd mswin xterm', @:)
218
219  " This does not work. Why?
220  "call feedkeys(":DoCmd x\<C-A>\<C-B>\"\<CR>", 'tx')
221  "call assert_equal('"DoCmd xterm', @:)
222
223  com! -complete=custom,CustomComplete DoCmd :
224  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
225  call assert_equal('"DoCmd January February Mars', @:)
226
227  com! -complete=customlist,CustomCompleteList DoCmd :
228  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
229  call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
230
231  com! -complete=custom,CustomCompleteList DoCmd :
232  call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
233
234  com! -complete=customlist,CustomComp DoCmd :
235  call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
236endfunc
237
238func CallExecute(A, L, P)
239  " Drop first '\n'
240  return execute('echo "hi"')[1:]
241endfunc
242
243func Test_use_execute_in_completion()
244  command! -nargs=* -complete=custom,CallExecute DoExec :
245  call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
246  call assert_equal('"DoExec hi', @:)
247  delcommand DoExec
248endfunc
249
250func Test_addr_all()
251  command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
252  %DoSomething
253  call assert_equal(1, g:a1)
254  call assert_equal(line('$'), g:a2)
255
256  command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
257  args one two three
258  %DoSomething
259  call assert_equal(1, g:a1)
260  call assert_equal(3, g:a2)
261
262  command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
263  %DoSomething
264  for low in range(1, bufnr('$'))
265    if buflisted(low)
266      break
267    endif
268  endfor
269  call assert_equal(low, g:a1)
270  call assert_equal(bufnr('$'), g:a2)
271
272  command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
273  %DoSomething
274  for low in range(1, bufnr('$'))
275    if bufloaded(low)
276      break
277    endif
278  endfor
279  call assert_equal(low, g:a1)
280  for up in range(bufnr('$'), 1, -1)
281    if bufloaded(up)
282      break
283    endif
284  endfor
285  call assert_equal(up, g:a2)
286
287  command! -addr=windows DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
288  new
289  %DoSomething
290  call assert_equal(1, g:a1)
291  call assert_equal(winnr('$'), g:a2)
292  bwipe
293
294  command! -addr=tabs DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
295  tabnew
296  %DoSomething
297  call assert_equal(1, g:a1)
298  call assert_equal(len(gettabinfo()), g:a2)
299  bwipe
300
301  command! -addr=other DoSomething echo 'nothing'
302  DoSomething
303  call assert_fails('%DoSomething')
304
305  delcommand DoSomething
306endfunc
307