robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqtest(old): map `gQ` to `1q:` (#41025)205a1ce4c7

master
13.2 KiB424 linesraw
1" Test editing line in Ex mode (see :help Q and :help gQ).
2
3source check.vim
4source shared.vim
5
6" Helper function to test editing line in Q Ex mode
7func Ex_Q(cmd)
8  " Is there a simpler way to test editing Ex line?
9  call feedkeys("Q"
10        \    .. "let s:test_ex =<< END\<CR>"
11        \    .. a:cmd .. "\<CR>"
12        \    .. "END\<CR>"
13        \    .. "visual\<CR>", 'tx')
14  return s:test_ex[0]
15endfunc
16
17" Helper function to test editing line in gQ Ex mode
18func Ex_gQ(cmd)
19  call feedkeys("gQ" .. a:cmd .. "\<C-b>\"\<CR>", 'tx')
20  let ret = @:[1:] " Remove leading quote.
21  call feedkeys("visual\<CR>", 'tx')
22  return ret
23endfunc
24
25" Helper function to test editing line with both Q and gQ Ex mode.
26func Ex(cmd)
27 return [Ex_Q(a:cmd), Ex_gQ(a:cmd)]
28endfunc
29
30" Test editing line in Ex mode (both Q and gQ)
31func Test_ex_mode()
32  throw 'Skipped: Nvim only supports Vim Ex mode'
33  let encoding_save = &encoding
34  set sw=2
35
36  for e in ['utf8', 'latin1']
37    exe 'set encoding=' . e
38
39    call assert_equal(['bar', 'bar'],             Ex("foo bar\<C-u>bar"), e)
40    call assert_equal(["1\<C-u>2", "1\<C-u>2"],   Ex("1\<C-v>\<C-u>2"), e)
41    call assert_equal(["1\<C-b>2\<C-e>3", '213'], Ex("1\<C-b>2\<C-e>3"), e)
42    call assert_equal(['0123', '2013'],           Ex("01\<Home>2\<End>3"), e)
43    call assert_equal(['0123', '0213'],           Ex("01\<Left>2\<Right>3"), e)
44    call assert_equal(['01234', '0342'],          Ex("012\<Left>\<Left>\<Insert>3\<Insert>4"), e)
45    call assert_equal(["foo bar\<C-w>", 'foo '],  Ex("foo bar\<C-w>"), e)
46    call assert_equal(['foo', 'foo'],             Ex("fooba\<Del>\<Del>"), e)
47    call assert_equal(["foo\tbar", 'foobar'],     Ex("foo\<Tab>bar"), e)
48    call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\<Tab>"), e)
49    call assert_equal(['    1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>"), e)
50    call assert_equal(['  1', "1\<C-t>\<C-t>"],   Ex("1\<C-t>\<C-t>\<C-d>"), e)
51    call assert_equal(['  foo', '    foo'],       Ex("    foo\<C-d>"), e)
52    call assert_equal(['foo', '    foo0'],        Ex("    foo0\<C-d>"), e)
53    call assert_equal(['foo', '    foo^'],        Ex("    foo^\<C-d>"), e)
54    call assert_equal(['foo', 'foo'],
55          \ Ex("\<BS>\<C-H>\<Del>\<kDel>foo"), e)
56    " default wildchar <Tab> interferes with this test
57    set wildchar=<c-e>
58    call assert_equal(["a\tb", "a\tb"],           Ex("a\t\t\<C-H>b"), e)
59    call assert_equal(["\t  mn", "\tm\<C-T>n"],        Ex("\tm\<C-T>n"), e)
60    set wildchar&
61  endfor
62
63  set sw&
64  let &encoding = encoding_save
65endfunc
66
67" Test substitute confirmation prompt :%s/pat/str/c in Ex mode
68func Test_Ex_substitute()
69  CheckRunVimInTerminal
70  let buf = RunVimInTerminal('', {'rows': 6})
71
72  call term_sendkeys(buf, ":call setline(1, repeat(['foo foo'], 4))\<CR>")
73  call term_sendkeys(buf, ":set number\<CR>")
74  call term_sendkeys(buf, "gQ")
75  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
76
77  call term_sendkeys(buf, "%s/foo/bar/gc\<CR>")
78  call WaitForAssert({-> assert_match('  1 foo foo', term_getline(buf, 5))},
79        \ 1000)
80  call WaitForAssert({-> assert_match('    ^^^', term_getline(buf, 6))}, 1000)
81  call term_sendkeys(buf, "N\<CR>")
82  call term_wait(buf)
83  call WaitForAssert({-> assert_match('    ^^^', term_getline(buf, 6))}, 1000)
84  call term_sendkeys(buf, "n\<CR>")
85  call WaitForAssert({-> assert_match('        ^^^', term_getline(buf, 6))},
86        \ 1000)
87  call term_sendkeys(buf, "y\<CR>")
88
89  call term_sendkeys(buf, "q\<CR>")
90  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
91
92  " Pressing enter in ex mode should print the current line
93  call term_sendkeys(buf, "\<CR>")
94  call WaitForAssert({-> assert_match('  3 foo foo', term_getline(buf, 5))}, 1000)
95  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
96
97  " The printed line should overwrite the colon
98  call term_sendkeys(buf, "\<CR>")
99  call WaitForAssert({-> assert_match('  3 foo foo', term_getline(buf, 4))}, 1000)
100  call WaitForAssert({-> assert_match('  4 foo foo', term_getline(buf, 5))}, 1000)
101  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
102
103  call term_sendkeys(buf, ":vi\<CR>")
104  call WaitForAssert({-> assert_match('foo bar', term_getline(buf, 1))}, 1000)
105
106  call StopVimInTerminal(buf)
107endfunc
108
109" Test for displaying lines from an empty buffer in Ex mode
110func Test_Ex_emptybuf()
111  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
112  new
113  call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E749:')
114  call setline(1, "abc")
115  call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E501:')
116  call assert_fails('call feedkeys("Q%d\<CR>", "xt")', 'E749:')
117  bw!
118endfunc
119
120" Test for the :open command
121func Test_open_command()
122  throw 'Skipped: Nvim does not have :open'
123  new
124  call setline(1, ['foo foo', 'foo bar', 'foo baz'])
125  call feedkeys("Qopen\<CR>j", 'xt')
126  call assert_equal('foo bar', getline('.'))
127  call feedkeys("Qopen /bar/\<CR>", 'xt')
128  call assert_equal(5, col('.'))
129  call assert_fails('call feedkeys("Qopen /baz/\<CR>", "xt")', 'E479:')
130  bw!
131endfunc
132
133func Test_open_command_flush_line()
134  throw 'Skipped: Nvim does not have :open'
135  " this was accessing freed memory: the regexp match uses a pointer to the
136  " current line which becomes invalid when searching for the ') mark.
137  new
138  call setline(1, ['one', 'two. three'])
139  s/one/ONE
140  try
141    open /\%')/
142  catch /E479/
143  endtry
144  bwipe!
145endfunc
146
147" FIXME: this doesn't fail without the fix but hangs
148func Skip_Test_open_command_state()
149  " Tricky script that failed because State was not set properly
150  let lines =<< trim END
151      !ls ƒ
152      0scìi
153      so! Xsourced
154      set t_û0=0
155      v/-/o
156  END
157  call writefile(lines, 'XopenScript', '')
158
159  let sourced = ["!f\u0083\x02\<Esc>z=0"]
160  call writefile(sourced, 'Xsourced', 'b')
161
162  CheckRunVimInTerminal
163  let buf = RunVimInTerminal('-u NONE -i NONE -n -m -X -Z -e -s -S XopenScript -c qa!', #{rows: 6, wait_for_ruler: 0, no_clean: 1})
164  sleep 3
165
166  call StopVimInTerminal(buf)
167endfunc
168
169" Test for :g/pat/visual to run vi commands in Ex mode
170" This used to hang Vim before 8.2.0274.
171func Test_Ex_global()
172  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
173  new
174  call setline(1, ['', 'foo', 'bar', 'foo', 'bar', 'foo'])
175  call feedkeys("Q\<bs>g/bar/visual\<CR>$rxQ$ryQvisual\<CR>j", "xt")
176  call assert_equal('bax', getline(3))
177  call assert_equal('bay', getline(5))
178  bwipe!
179
180  throw 'Skipped: Nvim only supports Vim Ex mode'
181  new
182  call setline(1, ['foo', 'bar'])
183  call feedkeys("Qg/./i\\\na\\\n.\\\na\\\nb\\\n.", "xt")
184  call assert_equal(['a', 'b', 'foo', 'a', 'b', 'bar'], getline(1, '$'))
185  bwipe!
186endfunc
187
188func Test_Ex_shell()
189  throw 'Skipped: Nvim only supports Vim Ex mode'
190  CheckUnix
191
192  new
193  call feedkeys("Qr !echo foo\\\necho bar\n", 'xt')
194  call assert_equal(['', 'foo', 'bar'], getline(1, '$'))
195  bwipe!
196
197  new
198  call feedkeys("Qr !echo foo\\\\\nbar\n", 'xt')
199  call assert_equal(['', 'foobar'], getline(1, '$'))
200  bwipe!
201
202  new
203  call feedkeys("Qr !echo foo\\ \\\necho bar\n", 'xt')
204  call assert_equal(['', 'foo ', 'bar'], getline(1, '$'))
205  bwipe!
206
207  new
208  call setline(1, ['bar', 'baz'])
209  call feedkeys("Qg/./!echo \\\ns/b/c/", "xt")
210  call assert_equal(['car', 'caz'], getline(1, '$'))
211  bwipe!
212endfunc
213
214" Test for pressing Ctrl-C in :append inside a loop in Ex mode
215" This used to hang Vim
216func Test_Ex_append_in_loop()
217  CheckRunVimInTerminal
218  let buf = RunVimInTerminal('', {'rows': 6})
219
220  call term_sendkeys(buf, "gQ")
221  call term_sendkeys(buf, "for i in range(1)\<CR>")
222  call term_sendkeys(buf, "append\<CR>")
223  call WaitForAssert({-> assert_match(':  append', term_getline(buf, 5))}, 1000)
224  call term_sendkeys(buf, "\<C-C>")
225  " Wait for input to be flushed
226  call term_wait(buf)
227  call term_sendkeys(buf, "foo\<CR>")
228  call WaitForAssert({-> assert_match('foo', term_getline(buf, 5))}, 1000)
229  call term_sendkeys(buf, ".\<CR>")
230  call WaitForAssert({-> assert_match('.', term_getline(buf, 5))}, 1000)
231  call term_sendkeys(buf, "endfor\<CR>")
232  call term_sendkeys(buf, "vi\<CR>")
233  call WaitForAssert({-> assert_match('foo', term_getline(buf, 1))}, 1000)
234
235  call StopVimInTerminal(buf)
236endfunc
237
238" In Ex-mode, a backslash escapes a newline
239func Test_Ex_escape_enter()
240  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
241  call feedkeys("gQlet l = \"a\\\<kEnter>b\"\<cr>vi\<cr>", 'xt')
242  call assert_equal("a\rb", l)
243endfunc
244
245" Test for :append! command in Ex mode
246func Test_Ex_append()
247  " feedkeys('x') implies ex_normal_busy; Ex mode cannot run inside :normal.
248  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
249  new
250  call setline(1, "\t   abc")
251  call feedkeys("Qappend!\npqr\nxyz\n.\nvisual\n", 'xt')
252  call assert_equal(["\t   abc", "\t   pqr", "\t   xyz"], getline(1, '$'))
253  bw!
254
255  new
256  call feedkeys("Qappend\na\\\n.", 'xt')
257  call assert_equal(['a\'], getline(1, '$'))
258  bw!
259endfunc
260
261func Test_ex_mode_errors()
262  " Not allowed to enter ex mode when text is locked
263  au InsertCharPre <buffer> normal! 1q:<CR>
264  let caught_e565 = 0
265  try
266    call feedkeys("ix\<esc>", 'xt')
267  catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
268    let caught_e565 = 1
269  endtry
270  call assert_equal(1, caught_e565)
271  au! InsertCharPre
272
273  new
274  au CmdLineEnter * call ExEnterFunc()
275  func ExEnterFunc()
276
277  endfunc
278  call feedkeys("gQvi\r", 'xt')
279
280  au! CmdLineEnter
281  delfunc ExEnterFunc
282
283  au CmdlineEnter * :
284  call feedkeys("gQecho 1\r", 'xt')
285
286  au! CmdlineEnter
287
288  quit
289endfunc
290
291func Test_ex_mode_with_global()
292  CheckNotGui
293  CheckFeature timers
294
295  " This will get stuck in Normal mode after the failed "J", use a timer to
296  " get going again.
297  let lines =<< trim END
298    " call ch_logfile('logfile', 'w')
299    pedit
300    func FeedQ(id)
301      call feedkeys('1q:', 't')
302    endfunc
303    call timer_start(10, 'FeedQ')
304    g/^/vi|HJ
305    call writefile(['done'], 'Xdidexmode')
306    qall!
307  END
308  call writefile(lines, 'Xexmodescript')
309  call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript'))
310  call assert_equal(['done'], readfile('Xdidexmode'))
311
312  call delete('logfile')
313  call delete('Xdidexmode')
314  call delete('Xexmodescript')
315endfunc
316
317func Test_ex_mode_count_overflow()
318  " The multiplication causes an integer overflow
319  CheckNotAsan
320
321  " this used to cause a crash
322  let lines =<< trim END
323    call feedkeys("\<Esc>1q:\<CR>")
324    v9|9silent! vi|333333233333y32333333%O
325    call writefile(['done'], 'Xdidexmode')
326    qall!
327  END
328  call writefile(lines, 'Xexmodescript')
329  call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript -c qa'))
330  call assert_equal(['done'], readfile('Xdidexmode'))
331
332  call delete('Xdidexmode')
333  call delete('Xexmodescript')
334endfunc
335
336func Test_ex_mode_large_indent()
337  new
338  set ts=500 ai
339  call setline(1, "\t")
340  exe "normal gQi\<CR>."
341  set ts=8 noai
342  bwipe!
343endfunc
344
345" This was accessing illegal memory when using "+" for eap->cmd.
346func Test_empty_command_visual_mode()
347  let lines =<< trim END
348      r<sfile>
349      0norm0V:
350      :qall!
351  END
352  call writefile(lines, 'Xexmodescript')
353  call assert_equal(1, RunVim([], [], '-u NONE -e -s -S Xexmodescript'))
354
355  call delete('Xexmodescript')
356endfunc
357
358" Test using backslash in ex-mode
359func Test_backslash_multiline()
360  throw 'Skipped: Nvim only supports Vim Ex mode'
361  new
362  call setline(1, 'enum')
363  call feedkeys('Qg/enum/i\
\
.', "xt")
364  call assert_equal(["", "enum"], getline(1, 2))
365endfunc
366
367" Test using backslash in ex-mode after patch 9.1.0535
368func Test_backslash_multiline2()
369  throw 'Skipped: Nvim only supports Vim Ex mode'
370  new
371  call feedkeys('Qa
X \\
Y
.', "xt")
372  call assert_equal(['X \\', "Y"], getline(1, 2))
373endfunc
374
375" Testing implicit print command
376func Test_implicit_print()
377  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
378  new
379  call setline(1, ['one', 'two', 'three'])
380  call feedkeys('Q:let a=execute(":1,2")', 'xt')
381  call feedkeys('Q:let b=execute(":3")', 'xt')
382  call assert_equal('one two', a->split('\n')->join(' '))
383  call assert_equal('three', b->split('\n')->join(' '))
384  bw!
385endfunc
386
387" Test inserting text after the trailing bar
388func Test_insert_after_trailing_bar()
389  " feedkeys('x') implies ex_normal_busy; Ex mode cannot run inside :normal.
390  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
391  new
392  call feedkeys("Qi|\nfoo\n.\na|bar\nbar\n.\nc|baz\n.", "xt")
393  call assert_equal(['', 'foo', 'bar', 'baz'], getline(1, '$'))
394  bwipe!
395endfunc
396
397" Test global insert of a newline without terminating period
398func Test_global_insert_newline()
399  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
400  new
401  call setline(1, ['foo'])
402  call feedkeys("Qg/foo/i\\\n", "xt")
403  call assert_equal(['', 'foo'], getline(1, '$'))
404  bwipe!
405endfunc
406
407" An empty command followed by a newline shouldn't cause E749 in Ex mode.
408func Test_ex_empty_command_newline()
409  throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script'
410  let g:var = 0
411  call feedkeys("gQexecute \"\\nlet g:var = 1\"\r", 'xt')
412  call assert_equal(1, g:var)
413  call feedkeys("gQexecute \"  \\nlet g:var = 2\"\r", 'xt')
414  call assert_equal(2, g:var)
415  call feedkeys("gQexecute \"\\t \\nlet g:var = 3\"\r", 'xt')
416  call assert_equal(3, g:var)
417  call feedkeys("gQexecute \"\\\"?!\\nlet g:var = 4\"\r", 'xt')
418  call assert_equal(4, g:var)
419  call feedkeys("gQexecute \"  \\\"?!\\nlet g:var = 5\"\r", 'xt')
420  call assert_equal(5, g:var)
421  unlet g:var
422endfunc
423
424" vim: shiftwidth=2 sts=2 expandtab