xref: /vim-8.2.3635/src/testdir/test_ex_mode.vim (revision 130cbfc3)
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  let encoding_save = &encoding
33  set sw=2
34
35  for e in ['utf8', 'latin1']
36    exe 'set encoding=' . e
37
38    call assert_equal(['bar', 'bar'],             Ex("foo bar\<C-u>bar"), e)
39    call assert_equal(["1\<C-u>2", "1\<C-u>2"],   Ex("1\<C-v>\<C-u>2"), e)
40    call assert_equal(["1\<C-b>2\<C-e>3", '213'], Ex("1\<C-b>2\<C-e>3"), e)
41    call assert_equal(['0123', '2013'],           Ex("01\<Home>2\<End>3"), e)
42    call assert_equal(['0123', '0213'],           Ex("01\<Left>2\<Right>3"), e)
43    call assert_equal(['01234', '0342'],          Ex("012\<Left>\<Left>\<Insert>3\<Insert>4"), e)
44    call assert_equal(["foo bar\<C-w>", 'foo '],  Ex("foo bar\<C-w>"), e)
45    call assert_equal(['foo', 'foo'],             Ex("fooba\<Del>\<Del>"), e)
46    call assert_equal(["foo\tbar", 'foobar'],     Ex("foo\<Tab>bar"), e)
47    call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\<Tab>"), e)
48    call assert_equal(['    1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>"), e)
49    call assert_equal(['  1', "1\<C-t>\<C-t>"],   Ex("1\<C-t>\<C-t>\<C-d>"), e)
50    call assert_equal(['  foo', '    foo'],       Ex("    foo\<C-d>"), e)
51    call assert_equal(['foo', '    foo0'],        Ex("    foo0\<C-d>"), e)
52    call assert_equal(['foo', '    foo^'],        Ex("    foo^\<C-d>"), e)
53    call assert_equal(['foo', 'foo'],
54          \ Ex("\<BS>\<C-H>\<Del>\<kDel>foo"), e)
55    " default wildchar <Tab> interferes with this test
56    set wildchar=<c-e>
57    call assert_equal(["a\tb", "a\tb"],           Ex("a\t\t\<C-H>b"), e)
58    call assert_equal(["\t  mn", "\tm\<C-T>n"],        Ex("\tm\<C-T>n"), e)
59    set wildchar&
60  endfor
61
62  set sw&
63  let &encoding = encoding_save
64endfunc
65
66" Test substitute confirmation prompt :%s/pat/str/c in Ex mode
67func Test_Ex_substitute()
68  CheckRunVimInTerminal
69  let buf = RunVimInTerminal('', {'rows': 6})
70
71  call term_sendkeys(buf, ":call setline(1, ['foo foo', 'foo foo', 'foo foo'])\<CR>")
72  call term_sendkeys(buf, ":set number\<CR>")
73  call term_sendkeys(buf, "gQ")
74  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
75
76  call term_sendkeys(buf, "%s/foo/bar/gc\<CR>")
77  call WaitForAssert({-> assert_match('  1 foo foo', term_getline(buf, 5))},
78        \ 1000)
79  call WaitForAssert({-> assert_match('    ^^^', term_getline(buf, 6))}, 1000)
80  call term_sendkeys(buf, "N\<CR>")
81  call term_wait(buf)
82  call WaitForAssert({-> assert_match('    ^^^', term_getline(buf, 6))}, 1000)
83  call term_sendkeys(buf, "n\<CR>")
84  call WaitForAssert({-> assert_match('        ^^^', term_getline(buf, 6))},
85        \ 1000)
86  call term_sendkeys(buf, "y\<CR>")
87
88  call term_sendkeys(buf, "q\<CR>")
89  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
90
91  " Pressing enter in ex mode should print the current line
92  call term_sendkeys(buf, "\<CR>")
93  call WaitForAssert({-> assert_match('  3 foo foo',
94        \ term_getline(buf, 5))}, 1000)
95
96  call term_sendkeys(buf, ":vi\<CR>")
97  call WaitForAssert({-> assert_match('foo bar', term_getline(buf, 1))}, 1000)
98
99  call StopVimInTerminal(buf)
100endfunc
101
102" Test for displaying lines from an empty buffer in Ex mode
103func Test_Ex_emptybuf()
104  new
105  call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E749:')
106  call setline(1, "abc")
107  call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E501:')
108  call assert_fails('call feedkeys("Q%d\<CR>", "xt")', 'E749:')
109  close!
110endfunc
111
112" Test for the :open command
113func Test_open_command()
114  new
115  call setline(1, ['foo foo', 'foo bar', 'foo baz'])
116  call feedkeys("Qopen\<CR>j", 'xt')
117  call assert_equal('foo bar', getline('.'))
118  call feedkeys("Qopen /bar/\<CR>", 'xt')
119  call assert_equal(5, col('.'))
120  call assert_fails('call feedkeys("Qopen /baz/\<CR>", "xt")', 'E479:')
121  close!
122endfunc
123
124" Test for :g/pat/visual to run vi commands in Ex mode
125" This used to hang Vim before 8.2.0274.
126func Test_Ex_global()
127  new
128  call setline(1, ['', 'foo', 'bar', 'foo', 'bar', 'foo'])
129  call feedkeys("Q\<bs>g/bar/visual\<CR>$rxQ$ryQvisual\<CR>j", "xt")
130  call assert_equal('bax', getline(3))
131  call assert_equal('bay', getline(5))
132  bwipe!
133endfunc
134
135" In Ex-mode, a backslash escapes a newline
136func Test_Ex_escape_enter()
137  call feedkeys("gQlet l = \"a\\\<kEnter>b\"\<cr>vi\<cr>", 'xt')
138  call assert_equal("a\rb", l)
139endfunc
140
141" Test for :append! command in Ex mode
142func Test_Ex_append()
143  new
144  call setline(1, "\t   abc")
145  call feedkeys("Qappend!\npqr\nxyz\n.\nvisual\n", 'xt')
146  call assert_equal(["\t   abc", "\t   pqr", "\t   xyz"], getline(1, '$'))
147  close!
148endfunc
149
150" In Ex-mode, backslashes at the end of a command should be halved.
151func Test_Ex_echo_backslash()
152  " This test works only when the language is English
153  CheckEnglish
154  let bsl = '\\\\'
155  let bsl2 = '\\\'
156  call assert_fails('call feedkeys("Qecho " .. bsl .. "\nvisual\n", "xt")',
157        \ "E15: Invalid expression: \\\\")
158  call assert_fails('call feedkeys("Qecho " .. bsl2 .. "\nm\nvisual\n", "xt")',
159        \ "E15: Invalid expression: \\\nm")
160endfunc
161
162func Test_ex_mode_errors()
163  " Not allowed to enter ex mode when text is locked
164  au InsertCharPre <buffer> normal! gQ<CR>
165  let caught_e565 = 0
166  try
167    call feedkeys("ix\<esc>", 'xt')
168  catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
169    let caught_e565 = 1
170  endtry
171  call assert_equal(1, caught_e565)
172  au! InsertCharPre
173
174  new
175  au CmdLineEnter * call ExEnterFunc()
176  func ExEnterFunc()
177
178  endfunc
179  call feedkeys("gQvi\r", 'xt')
180
181  au! CmdLineEnter
182  delfunc ExEnterFunc
183  quit
184endfunc
185
186func Test_ex_mode_with_global()
187  CheckNotGui
188  CheckFeature timers
189
190  " This will get stuck in Normal mode after the failed "J", use a timer to
191  " get going again.
192  let lines =<< trim END
193    call ch_logfile('logfile', 'w')
194    pedit
195    func FeedQ(id)
196      call feedkeys('Q', 't')
197    endfunc
198    call timer_start(10, 'FeedQ')
199    g/^/vi|HJ
200    call writefile(['done'], 'Xdidexmode')
201    qall!
202  END
203  call writefile(lines, 'Xexmodescript')
204  call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript'))
205  call assert_equal(['done'], readfile('Xdidexmode'))
206
207  call delete('logfile')
208  call delete('Xdidexmode')
209  call delete('Xexmodescript')
210endfunc
211
212func Test_ex_mode_count_overflow()
213  " The multiplication causes an integer overflow
214  CheckNotAsan
215
216  " this used to cause a crash
217  let lines =<< trim END
218    call feedkeys("\<Esc>Q\<CR>")
219    v9|9silent! vi|333333233333y32333333%O
220    call writefile(['done'], 'Xdidexmode')
221    qall!
222  END
223  call writefile(lines, 'Xexmodescript')
224  call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript -c qa'))
225  call assert_equal(['done'], readfile('Xdidexmode'))
226
227  call delete('Xdidexmode')
228  call delete('Xexmodescript')
229endfunc
230
231" vim: shiftwidth=2 sts=2 expandtab
232