Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1 | " Tests for setting 'buftype' to "prompt" |
| 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature channel |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 5 | |
| 6 | source shared.vim |
| 7 | source screendump.vim |
| 8 | |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 9 | func CanTestPromptBuffer() |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 10 | " We need to use a terminal window to be able to feed keys without leaving |
| 11 | " Insert mode. |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 12 | CheckFeature terminal |
| 13 | |
| 14 | " TODO: make the tests work on MS-Windows |
| 15 | CheckNotMSWindows |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 16 | endfunc |
| 17 | |
| 18 | func WriteScript(name) |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 19 | call writefile([ |
| 20 | \ 'func TextEntered(text)', |
| 21 | \ ' if a:text == "exit"', |
Bram Moolenaar | 71ef1ba | 2018-06-21 12:07:04 +0200 | [diff] [blame] | 22 | \ ' " Reset &modified to allow the buffer to be closed.', |
| 23 | \ ' set nomodified', |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 24 | \ ' stopinsert', |
| 25 | \ ' close', |
| 26 | \ ' else', |
| 27 | \ ' " Add the output above the current prompt.', |
| 28 | \ ' call append(line("$") - 1, "Command: \"" . a:text . "\"")', |
| 29 | \ ' " Reset &modified to allow the buffer to be closed.', |
| 30 | \ ' set nomodified', |
| 31 | \ ' call timer_start(20, {id -> TimerFunc(a:text)})', |
| 32 | \ ' endif', |
| 33 | \ 'endfunc', |
| 34 | \ '', |
| 35 | \ 'func TimerFunc(text)', |
| 36 | \ ' " Add the output above the current prompt.', |
| 37 | \ ' call append(line("$") - 1, "Result: \"" . a:text . "\"")', |
Bram Moolenaar | 3339d3d | 2018-06-03 17:10:40 +0200 | [diff] [blame] | 38 | \ ' " Reset &modified to allow the buffer to be closed.', |
| 39 | \ ' set nomodified', |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 40 | \ 'endfunc', |
| 41 | \ '', |
| 42 | \ 'call setline(1, "other buffer")', |
Bram Moolenaar | 3339d3d | 2018-06-03 17:10:40 +0200 | [diff] [blame] | 43 | \ 'set nomodified', |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 44 | \ 'new', |
| 45 | \ 'set buftype=prompt', |
| 46 | \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))', |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 47 | \ 'eval bufnr("")->prompt_setprompt("cmd: ")', |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 48 | \ 'startinsert', |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 49 | \ ], a:name) |
| 50 | endfunc |
| 51 | |
| 52 | func Test_prompt_basic() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 53 | call CanTestPromptBuffer() |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 54 | let scriptName = 'XpromptscriptBasic' |
| 55 | call WriteScript(scriptName) |
| 56 | |
| 57 | let buf = RunVimInTerminal('-S ' . scriptName, {}) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 58 | call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))}) |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 59 | |
| 60 | call term_sendkeys(buf, "hello\<CR>") |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 61 | call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))}) |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 62 | call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))}) |
| 63 | call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))}) |
| 64 | |
| 65 | call term_sendkeys(buf, "exit\<CR>") |
| 66 | call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))}) |
| 67 | |
| 68 | call StopVimInTerminal(buf) |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 69 | call delete(scriptName) |
| 70 | endfunc |
| 71 | |
| 72 | func Test_prompt_editing() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 73 | call CanTestPromptBuffer() |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 74 | let scriptName = 'XpromptscriptEditing' |
| 75 | call WriteScript(scriptName) |
| 76 | |
| 77 | let buf = RunVimInTerminal('-S ' . scriptName, {}) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 78 | call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))}) |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 79 | |
| 80 | let bs = "\<BS>" |
| 81 | call term_sendkeys(buf, "hello" . bs . bs) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 82 | call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))}) |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 83 | |
| 84 | let left = "\<Left>" |
| 85 | call term_sendkeys(buf, left . left . left . bs . '-') |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 86 | call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))}) |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 87 | |
| 88 | let end = "\<End>" |
| 89 | call term_sendkeys(buf, end . "x") |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 90 | call WaitForAssert({-> assert_equal('cmd: -helx', term_getline(buf, 1))}) |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 91 | |
| 92 | call term_sendkeys(buf, "\<C-U>exit\<CR>") |
| 93 | call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))}) |
| 94 | |
| 95 | call StopVimInTerminal(buf) |
| 96 | call delete(scriptName) |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 97 | endfunc |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 98 | |
| 99 | func Test_prompt_garbage_collect() |
| 100 | func MyPromptCallback(x, text) |
| 101 | " NOP |
| 102 | endfunc |
| 103 | func MyPromptInterrupt(x) |
| 104 | " NOP |
| 105 | endfunc |
| 106 | |
| 107 | new |
| 108 | set buftype=prompt |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 109 | eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}])) |
| 110 | eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}])) |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 111 | call test_garbagecollect_now() |
| 112 | " Must not crash |
| 113 | call feedkeys("\<CR>\<C-C>", 'xt') |
| 114 | call assert_true(v:true) |
| 115 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 116 | call assert_fails("call prompt_setcallback(bufnr(), [])", 'E921:') |
| 117 | call assert_equal(0, prompt_setcallback({}, '')) |
| 118 | call assert_fails("call prompt_setinterrupt(bufnr(), [])", 'E921:') |
| 119 | call assert_equal(0, prompt_setinterrupt({}, '')) |
| 120 | |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 121 | delfunc MyPromptCallback |
| 122 | bwipe! |
| 123 | endfunc |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 124 | |
| 125 | " Test for editing the prompt buffer |
| 126 | func Test_prompt_buffer_edit() |
| 127 | new |
| 128 | set buftype=prompt |
| 129 | normal! i |
| 130 | call assert_beeps('normal! dd') |
| 131 | call assert_beeps('normal! ~') |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 132 | call assert_beeps('normal! o') |
| 133 | call assert_beeps('normal! O') |
| 134 | call assert_beeps('normal! p') |
| 135 | call assert_beeps('normal! P') |
| 136 | call assert_beeps('normal! u') |
| 137 | call assert_beeps('normal! ra') |
| 138 | call assert_beeps('normal! s') |
| 139 | call assert_beeps('normal! S') |
| 140 | call assert_beeps("normal! \<C-A>") |
| 141 | call assert_beeps("normal! \<C-X>") |
Bram Moolenaar | 845e0ee | 2020-06-20 16:05:32 +0200 | [diff] [blame] | 142 | " pressing CTRL-W in the prompt buffer should trigger the window commands |
| 143 | call assert_equal(1, winnr()) |
| 144 | exe "normal A\<C-W>\<C-W>" |
| 145 | call assert_equal(2, winnr()) |
| 146 | wincmd w |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 147 | close! |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 148 | call assert_equal(0, prompt_setprompt([], '')) |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 149 | endfunc |
| 150 | |
| 151 | " vim: shiftwidth=2 sts=2 expandtab |