Bram Moolenaar | 59cb041 | 2019-12-18 22:26:31 +0100 | [diff] [blame] | 1 | " Test editing line in Ex mode (see :help Q and :help gQ). |
| 2 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 3 | source check.vim |
| 4 | |
Bram Moolenaar | 59cb041 | 2019-12-18 22:26:31 +0100 | [diff] [blame] | 5 | " Helper function to test editing line in Q Ex mode |
| 6 | func Ex_Q(cmd) |
| 7 | " Is there a simpler way to test editing Ex line? |
| 8 | call feedkeys("Q" |
| 9 | \ .. "let s:test_ex =<< END\<CR>" |
| 10 | \ .. a:cmd .. "\<CR>" |
| 11 | \ .. "END\<CR>" |
| 12 | \ .. "visual\<CR>", 'tx') |
| 13 | return s:test_ex[0] |
| 14 | endfunc |
| 15 | |
| 16 | " Helper function to test editing line in gQ Ex mode |
| 17 | func Ex_gQ(cmd) |
| 18 | call feedkeys("gQ" .. a:cmd .. "\<C-b>\"\<CR>", 'tx') |
| 19 | let ret = @:[1:] " Remove leading quote. |
| 20 | call feedkeys("visual\<CR>", 'tx') |
| 21 | return ret |
| 22 | endfunc |
| 23 | |
| 24 | " Helper function to test editing line with both Q and gQ Ex mode. |
| 25 | func Ex(cmd) |
| 26 | return [Ex_Q(a:cmd), Ex_gQ(a:cmd)] |
| 27 | endfunc |
| 28 | |
| 29 | " Test editing line in Ex mode (both Q and gQ) |
| 30 | func Test_ex_mode() |
| 31 | let encoding_save = &encoding |
| 32 | set sw=2 |
| 33 | |
| 34 | for e in ['utf8', 'latin1'] |
| 35 | exe 'set encoding=' . e |
| 36 | |
| 37 | call assert_equal(['bar', 'bar'], Ex("foo bar\<C-u>bar"), e) |
| 38 | call assert_equal(["1\<C-u>2", "1\<C-u>2"], Ex("1\<C-v>\<C-u>2"), e) |
| 39 | call assert_equal(["1\<C-b>2\<C-e>3", '213'], Ex("1\<C-b>2\<C-e>3"), e) |
| 40 | call assert_equal(['0123', '2013'], Ex("01\<Home>2\<End>3"), e) |
| 41 | call assert_equal(['0123', '0213'], Ex("01\<Left>2\<Right>3"), e) |
| 42 | call assert_equal(['01234', '0342'], Ex("012\<Left>\<Left>\<Insert>3\<Insert>4"), e) |
| 43 | call assert_equal(["foo bar\<C-w>", 'foo '], Ex("foo bar\<C-w>"), e) |
| 44 | call assert_equal(['foo', 'foo'], Ex("fooba\<Del>\<Del>"), e) |
| 45 | call assert_equal(["foo\tbar", 'foobar'], Ex("foo\<Tab>bar"), e) |
| 46 | call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\<Tab>"), e) |
| 47 | call assert_equal([' 1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>"), e) |
| 48 | call assert_equal([' 1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>\<C-d>"), e) |
| 49 | call assert_equal([' foo', ' foo'], Ex(" foo\<C-d>"), e) |
| 50 | call assert_equal(['foo', ' foo0'], Ex(" foo0\<C-d>"), e) |
| 51 | call assert_equal(['foo', ' foo^'], Ex(" foo^\<C-d>"), e) |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame] | 52 | call assert_equal(['foo', 'foo'], |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 53 | \ Ex("\<BS>\<C-H>\<Del>\<kDel>foo"), e) |
| 54 | " default wildchar <Tab> interferes with this test |
| 55 | set wildchar=<c-e> |
| 56 | call assert_equal(["a\tb", "a\tb"], Ex("a\t\t\<C-H>b"), e) |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 57 | call assert_equal(["\t mn", "\tm\<C-T>n"], Ex("\tm\<C-T>n"), e) |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 58 | set wildchar& |
Bram Moolenaar | 59cb041 | 2019-12-18 22:26:31 +0100 | [diff] [blame] | 59 | endfor |
| 60 | |
| 61 | set sw& |
| 62 | let &encoding = encoding_save |
| 63 | endfunc |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 64 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 65 | " Test substitute confirmation prompt :%s/pat/str/c in Ex mode |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 66 | func Test_Ex_substitute() |
| 67 | CheckRunVimInTerminal |
| 68 | let buf = RunVimInTerminal('', {'rows': 6}) |
| 69 | |
| 70 | call term_sendkeys(buf, ":call setline(1, ['foo foo', 'foo foo', 'foo foo'])\<CR>") |
| 71 | call term_sendkeys(buf, ":set number\<CR>") |
| 72 | call term_sendkeys(buf, "gQ") |
| 73 | call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000) |
| 74 | |
| 75 | call term_sendkeys(buf, "%s/foo/bar/gc\<CR>") |
| 76 | call WaitForAssert({-> assert_match(' 1 foo foo', term_getline(buf, 5))}, |
| 77 | \ 1000) |
| 78 | call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, 1000) |
| 79 | call term_sendkeys(buf, "n\<CR>") |
| 80 | call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, |
| 81 | \ 1000) |
| 82 | call term_sendkeys(buf, "y\<CR>") |
| 83 | |
| 84 | call term_sendkeys(buf, "q\<CR>") |
| 85 | call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000) |
| 86 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 87 | " Pressing enter in ex mode should print the current line |
| 88 | call term_sendkeys(buf, "\<CR>") |
| 89 | call WaitForAssert({-> assert_match(' 3 foo foo', |
| 90 | \ term_getline(buf, 5))}, 1000) |
| 91 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 92 | call term_sendkeys(buf, ":vi\<CR>") |
| 93 | call WaitForAssert({-> assert_match('foo bar', term_getline(buf, 1))}, 1000) |
| 94 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 95 | call StopVimInTerminal(buf) |
| 96 | endfunc |
| 97 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 98 | " Test for displaying lines from an empty buffer in Ex mode |
| 99 | func Test_Ex_emptybuf() |
| 100 | new |
| 101 | call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E749:') |
| 102 | call setline(1, "abc") |
| 103 | call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E501:') |
| 104 | call assert_fails('call feedkeys("Q%d\<CR>", "xt")', 'E749:') |
| 105 | close! |
| 106 | endfunc |
| 107 | |
| 108 | " Test for the :open command |
| 109 | func Test_open_command() |
| 110 | new |
| 111 | call setline(1, ['foo foo', 'foo bar', 'foo baz']) |
| 112 | call feedkeys("Qopen\<CR>j", 'xt') |
| 113 | call assert_equal('foo bar', getline('.')) |
| 114 | call feedkeys("Qopen /bar/\<CR>", 'xt') |
| 115 | call assert_equal(5, col('.')) |
| 116 | call assert_fails('call feedkeys("Qopen /baz/\<CR>", "xt")', 'E479:') |
| 117 | close! |
| 118 | endfunc |
| 119 | |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 120 | " Test for :g/pat/visual to run vi commands in Ex mode |
| 121 | " This used to hang Vim before 8.2.0274. |
| 122 | func Test_Ex_global() |
Bram Moolenaar | 9e2bcb5 | 2020-02-18 21:33:00 +0100 | [diff] [blame] | 123 | new |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 124 | call setline(1, ['', 'foo', 'bar', 'foo', 'bar', 'foo']) |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 125 | call feedkeys("Q\<bs>g/bar/visual\<CR>$rxQ$ryQvisual\<CR>j", "xt") |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 126 | call assert_equal('bax', getline(3)) |
| 127 | call assert_equal('bay', getline(5)) |
Bram Moolenaar | 9e2bcb5 | 2020-02-18 21:33:00 +0100 | [diff] [blame] | 128 | bwipe! |
| 129 | endfunc |
| 130 | |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 131 | " In Ex-mode, a backslash escapes a newline |
| 132 | func Test_Ex_escape_enter() |
| 133 | call feedkeys("gQlet l = \"a\\\<kEnter>b\"\<cr>vi\<cr>", 'xt') |
| 134 | call assert_equal("a\rb", l) |
| 135 | endfunc |
| 136 | |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame] | 137 | " Test for :append! command in Ex mode |
| 138 | func Test_Ex_append() |
| 139 | new |
| 140 | call setline(1, "\t abc") |
| 141 | call feedkeys("Qappend!\npqr\nxyz\n.\nvisual\n", 'xt') |
| 142 | call assert_equal(["\t abc", "\t pqr", "\t xyz"], getline(1, '$')) |
| 143 | close! |
| 144 | endfunc |
| 145 | |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 146 | " In Ex-mode, backslashes at the end of a command should be halved. |
| 147 | func Test_Ex_echo_backslash() |
| 148 | " This test works only when the language is English |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 149 | CheckEnglish |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 150 | let bsl = '\\\\' |
| 151 | let bsl2 = '\\\' |
| 152 | call assert_fails('call feedkeys("Qecho " .. bsl .. "\nvisual\n", "xt")', |
| 153 | \ "E15: Invalid expression: \\\\") |
| 154 | call assert_fails('call feedkeys("Qecho " .. bsl2 .. "\nm\nvisual\n", "xt")', |
| 155 | \ "E15: Invalid expression: \\\nm") |
| 156 | endfunc |
| 157 | |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 158 | func Test_ex_mode_errors() |
| 159 | " Not allowed to enter ex mode when text is locked |
| 160 | au InsertCharPre <buffer> normal! gQ<CR> |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 161 | let caught_e565 = 0 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 162 | try |
| 163 | call feedkeys("ix\<esc>", 'xt') |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 164 | catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 |
| 165 | let caught_e565 = 1 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 166 | endtry |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 167 | call assert_equal(1, caught_e565) |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 168 | au! InsertCharPre |
Bram Moolenaar | 158ea17 | 2020-06-18 17:28:39 +0200 | [diff] [blame^] | 169 | |
| 170 | new |
| 171 | au CmdLineEnter * call ExEnterFunc() |
| 172 | func ExEnterFunc() |
| 173 | |
| 174 | endfunc |
| 175 | call feedkeys("gQvi\r", 'xt') |
| 176 | |
| 177 | au! CmdLineEnter |
| 178 | delfunc ExEnterFunc |
| 179 | quit |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 180 | endfunc |
| 181 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 182 | " vim: shiftwidth=2 sts=2 expandtab |