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 | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 8 | " Need to be able to run terminal Vim with 256 colors. On MS-Windows the |
| 9 | " console only has 16 colors and the GUI can't run in a terminal. |
| 10 | if !has('terminal') || has('win32') |
| 11 | func CanRunVimInTerminal() |
| 12 | return 0 |
| 13 | endfunc |
| 14 | finish |
| 15 | endif |
| 16 | |
| 17 | func CanRunVimInTerminal() |
| 18 | return 1 |
| 19 | endfunc |
| 20 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 21 | source shared.vim |
| 22 | |
| 23 | " Run Vim with "arguments" in a new terminal window. |
| 24 | " By default uses a size of 20 lines and 75 columns. |
| 25 | " Returns the buffer number of the terminal. |
| 26 | " |
Bram Moolenaar | cf67a50 | 2018-03-25 20:31:32 +0200 | [diff] [blame] | 27 | " Options is a dictionary, these items are recognized: |
| 28 | " "rows" - height of the terminal window (max. 20) |
| 29 | " "cols" - width of the terminal window (max. 78) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 30 | func RunVimInTerminal(arguments, options) |
Bram Moolenaar | 948a796 | 2018-03-23 20:37:45 +0100 | [diff] [blame] | 31 | " If Vim doesn't exit a swap file remains, causing other tests to fail. |
| 32 | " Remove it here. |
| 33 | call delete(".swp") |
| 34 | |
Bram Moolenaar | e7499dd | 2018-03-24 17:56:13 +0100 | [diff] [blame] | 35 | if exists('$COLORFGBG') |
| 36 | " Clear $COLORFGBG to avoid 'background' being set to "dark", which will |
| 37 | " only be corrected if the response to t_RB is received, which may be too |
| 38 | " late. |
| 39 | let $COLORFGBG = '' |
| 40 | endif |
| 41 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 42 | " 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] | 43 | " size terminal window. Works only when the current window is full width. |
| 44 | call assert_equal(&columns, winwidth(0)) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 45 | split |
| 46 | vsplit |
| 47 | |
Bram Moolenaar | 6acadda | 2018-02-24 16:51:32 +0100 | [diff] [blame] | 48 | " Always do this with 256 colors and a light background. |
| 49 | set t_Co=256 background=light |
| 50 | hi Normal ctermfg=NONE ctermbg=NONE |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 51 | |
Bram Moolenaar | cf67a50 | 2018-03-25 20:31:32 +0200 | [diff] [blame] | 52 | " Make the window 20 lines high and 75 columns, unless told otherwise. |
| 53 | let rows = get(a:options, 'rows', 20) |
| 54 | let cols = get(a:options, 'cols', 75) |
Bram Moolenaar | 15a1c3f | 2018-03-25 18:56:25 +0200 | [diff] [blame] | 55 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 56 | let cmd = GetVimCommandClean() |
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}) |
| 60 | call assert_equal([rows, cols], term_getsize(buf)) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 61 | |
| 62 | return buf |
| 63 | endfunc |
| 64 | |
| 65 | " Stop a Vim running in terminal buffer "buf". |
| 66 | func StopVimInTerminal(buf) |
| 67 | call assert_equal("running", term_getstatus(a:buf)) |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 68 | call term_sendkeys(a:buf, "\<Esc>\<Esc>:qa!\<cr>") |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 69 | call WaitFor('term_getstatus(' . a:buf . ') == "finished"') |
| 70 | only! |
| 71 | endfunc |
| 72 | |
| 73 | " Verify that Vim running in terminal buffer "buf" matches the screen dump. |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 74 | " "options" is passed to term_dumpwrite(). |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 75 | " The file name used is "dumps/{filename}.dump". |
| 76 | " Will wait for up to a second for the screen dump to match. |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 77 | func VerifyScreenDump(buf, filename, options) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 78 | let reference = 'dumps/' . a:filename . '.dump' |
| 79 | let testfile = a:filename . '.dump.failed' |
| 80 | |
| 81 | let i = 0 |
| 82 | while 1 |
| 83 | call delete(testfile) |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 84 | call term_dumpwrite(a:buf, testfile, a:options) |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 85 | if readfile(reference) == readfile(testfile) |
| 86 | call delete(testfile) |
| 87 | break |
| 88 | endif |
| 89 | if i == 100 |
| 90 | " Leave the test file around for inspection. |
| 91 | call assert_report('See dump file difference: call term_dumpdiff("' . testfile . '", "' . reference . '")') |
| 92 | break |
| 93 | endif |
| 94 | sleep 10m |
| 95 | let i += 1 |
| 96 | endwhile |
| 97 | endfunc |