Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 1 | " Functions shared by tests making screen dumps. |
| 2 | |
| 3 | " Only load this script once. |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 4 | if exists('*CanRunVimInTerminal') |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 5 | finish |
| 6 | endif |
| 7 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 8 | " For most tests we need to be able to run terminal Vim with 256 colors. On |
| 9 | " MS-Windows the console only has 16 colors and the GUI can't run in a |
| 10 | " terminal. |
| 11 | func CanRunVimInTerminal() |
| 12 | return has('terminal') && !has('win32') |
| 13 | endfunc |
| 14 | |
| 15 | " Skip the rest if there is no terminal feature at all. |
| 16 | if !has('terminal') |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 17 | finish |
| 18 | endif |
| 19 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 20 | source shared.vim |
| 21 | |
| 22 | " Run Vim with "arguments" in a new terminal window. |
| 23 | " By default uses a size of 20 lines and 75 columns. |
| 24 | " Returns the buffer number of the terminal. |
| 25 | " |
Bram Moolenaar | cf67a50 | 2018-03-25 20:31:32 +0200 | [diff] [blame] | 26 | " Options is a dictionary, these items are recognized: |
| 27 | " "rows" - height of the terminal window (max. 20) |
| 28 | " "cols" - width of the terminal window (max. 78) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 29 | func RunVimInTerminal(arguments, options) |
Bram Moolenaar | 948a796 | 2018-03-23 20:37:45 +0100 | [diff] [blame] | 30 | " If Vim doesn't exit a swap file remains, causing other tests to fail. |
| 31 | " Remove it here. |
| 32 | call delete(".swp") |
| 33 | |
Bram Moolenaar | e7499dd | 2018-03-24 17:56:13 +0100 | [diff] [blame] | 34 | if exists('$COLORFGBG') |
| 35 | " Clear $COLORFGBG to avoid 'background' being set to "dark", which will |
| 36 | " only be corrected if the response to t_RB is received, which may be too |
| 37 | " late. |
| 38 | let $COLORFGBG = '' |
| 39 | endif |
| 40 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 41 | " Make a horizontal and vertical split, so that we can get exactly the right |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 42 | " size terminal window. Works only when the current window is full width. |
| 43 | call assert_equal(&columns, winwidth(0)) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 44 | split |
| 45 | vsplit |
| 46 | |
Bram Moolenaar | 6acadda | 2018-02-24 16:51:32 +0100 | [diff] [blame] | 47 | " Always do this with 256 colors and a light background. |
| 48 | set t_Co=256 background=light |
| 49 | hi Normal ctermfg=NONE ctermbg=NONE |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 50 | |
Bram Moolenaar | cf67a50 | 2018-03-25 20:31:32 +0200 | [diff] [blame] | 51 | " Make the window 20 lines high and 75 columns, unless told otherwise. |
| 52 | let rows = get(a:options, 'rows', 20) |
| 53 | let cols = get(a:options, 'cols', 75) |
Bram Moolenaar | 15a1c3f | 2018-03-25 18:56:25 +0200 | [diff] [blame] | 54 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 55 | let cmd = GetVimCommandClean() |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 56 | |
Bram Moolenaar | b7ea7cb | 2018-02-24 14:38:51 +0100 | [diff] [blame] | 57 | " Add -v to have gvim run in the terminal (if possible) |
| 58 | let cmd .= ' -v ' . a:arguments |
Bram Moolenaar | cf67a50 | 2018-03-25 20:31:32 +0200 | [diff] [blame] | 59 | let buf = term_start(cmd, {'curwin': 1, 'term_rows': rows, 'term_cols': cols}) |
Bram Moolenaar | b833c1e | 2018-05-05 16:36:06 +0200 | [diff] [blame] | 60 | if &termwinsize == '' |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 61 | " in the GUI we may end up with a different size, try to set it. |
| 62 | if term_getsize(buf) != [rows, cols] |
| 63 | call term_setsize(buf, rows, cols) |
| 64 | endif |
Bram Moolenaar | a7eef3d | 2018-04-15 22:25:54 +0200 | [diff] [blame] | 65 | call assert_equal([rows, cols], term_getsize(buf)) |
| 66 | else |
| 67 | let rows = term_getsize(buf)[0] |
| 68 | let cols = term_getsize(buf)[1] |
| 69 | endif |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 70 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 71 | " Wait for "All" or "Top" of the ruler to be shown in the last line or in |
| 72 | " the status line of the last window. This can be quite slow (e.g. when |
| 73 | " using valgrind). |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 74 | " If it fails then show the terminal contents for debugging. |
| 75 | try |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 76 | call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1 || len(term_getline(buf, rows - 1)) >= cols - 1}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 77 | catch /timed out after/ |
| 78 | let lines = map(range(1, rows), {key, val -> term_getline(buf, val)}) |
| 79 | call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>")) |
| 80 | endtry |
Bram Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 81 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 82 | return buf |
| 83 | endfunc |
| 84 | |
| 85 | " Stop a Vim running in terminal buffer "buf". |
| 86 | func StopVimInTerminal(buf) |
| 87 | call assert_equal("running", term_getstatus(a:buf)) |
Bram Moolenaar | 3339d3d | 2018-06-03 17:10:40 +0200 | [diff] [blame] | 88 | |
| 89 | " CTRL-O : works both in Normal mode and Insert mode to start a command line. |
| 90 | " In Command-line it's inserted, the CTRL-U removes it again. |
Bram Moolenaar | acb9eff | 2018-06-04 19:11:11 +0200 | [diff] [blame] | 91 | call term_sendkeys(a:buf, "\<C-O>:\<C-U>qa!\<cr>") |
Bram Moolenaar | 3339d3d | 2018-06-03 17:10:40 +0200 | [diff] [blame] | 92 | |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 93 | call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))}) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 94 | only! |
| 95 | endfunc |
| 96 | |
| 97 | " Verify that Vim running in terminal buffer "buf" matches the screen dump. |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 98 | " "options" is passed to term_dumpwrite(). |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 99 | " The file name used is "dumps/{filename}.dump". |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 100 | " Optionally an extra argument can be passed which is prepended to the error |
| 101 | " message. Use this when using the same dump file with different options. |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 102 | " Will wait for up to a second for the screen dump to match. |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 103 | " Returns non-zero when verification fails. |
| 104 | func VerifyScreenDump(buf, filename, options, ...) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 105 | let reference = 'dumps/' . a:filename . '.dump' |
Bram Moolenaar | ef7f0e3 | 2019-03-30 15:59:51 +0100 | [diff] [blame] | 106 | let testfile = 'failed/' . a:filename . '.dump' |
| 107 | |
| 108 | let did_mkdir = 0 |
| 109 | if !isdirectory('failed') |
| 110 | let did_mkdir = 1 |
| 111 | call mkdir('failed') |
| 112 | endif |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 113 | |
| 114 | let i = 0 |
| 115 | while 1 |
Bram Moolenaar | b6fc728 | 2018-12-04 22:24:16 +0100 | [diff] [blame] | 116 | " leave some time for updating the original window |
| 117 | sleep 10m |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 118 | call delete(testfile) |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 119 | call term_dumpwrite(a:buf, testfile, a:options) |
Bram Moolenaar | 353aca1 | 2019-02-21 17:05:59 +0100 | [diff] [blame] | 120 | let testdump = readfile(testfile) |
Bram Moolenaar | 2d7260d | 2019-04-06 20:51:52 +0200 | [diff] [blame] | 121 | if filereadable(reference) |
| 122 | let refdump = readfile(reference) |
| 123 | else |
| 124 | " Must be a new screendump, always fail |
| 125 | let refdump = [] |
| 126 | endif |
Bram Moolenaar | 353aca1 | 2019-02-21 17:05:59 +0100 | [diff] [blame] | 127 | if refdump == testdump |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 128 | call delete(testfile) |
Bram Moolenaar | ef7f0e3 | 2019-03-30 15:59:51 +0100 | [diff] [blame] | 129 | if did_mkdir |
| 130 | call delete('failed', 'd') |
| 131 | endif |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 132 | break |
| 133 | endif |
| 134 | if i == 100 |
Bram Moolenaar | 2d7260d | 2019-04-06 20:51:52 +0200 | [diff] [blame] | 135 | " Leave the failed dump around for inspection. |
| 136 | if filereadable(reference) |
| 137 | let msg = 'See dump file difference: call term_dumpdiff("' . testfile . '", "' . reference . '")' |
| 138 | if a:0 == 1 |
| 139 | let msg = a:1 . ': ' . msg |
| 140 | endif |
| 141 | if len(testdump) != len(refdump) |
| 142 | let msg = msg . '; line count is ' . len(testdump) . ' instead of ' . len(refdump) |
| 143 | endif |
| 144 | else |
| 145 | let msg = 'See new dump file: call term_dumpload("' . testfile . '")' |
Bram Moolenaar | 353aca1 | 2019-02-21 17:05:59 +0100 | [diff] [blame] | 146 | endif |
| 147 | for i in range(len(refdump)) |
| 148 | if i >= len(testdump) |
| 149 | break |
| 150 | endif |
| 151 | if testdump[i] != refdump[i] |
| 152 | let msg = msg . '; difference in line ' . (i + 1) . ': "' . testdump[i] . '"' |
| 153 | endif |
| 154 | endfor |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 155 | call assert_report(msg) |
| 156 | return 1 |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 157 | endif |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 158 | let i += 1 |
| 159 | endwhile |
Bram Moolenaar | 6f8bdab | 2018-09-09 22:02:24 +0200 | [diff] [blame] | 160 | return 0 |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 161 | endfunc |