blob: 093b483f10dcf44cabb1a1db99f7223d2b6b7bc2 [file] [log] [blame]
Bram Moolenaarda650582018-02-20 15:51:40 +01001" Functions shared by tests making screen dumps.
2
3" Only load this script once.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004if exists('*CanRunVimInTerminal')
Bram Moolenaarda650582018-02-20 15:51:40 +01005 finish
6endif
7
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01008" 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.
10if !has('terminal') || has('win32')
11 func CanRunVimInTerminal()
12 return 0
13 endfunc
14 finish
15endif
16
17func CanRunVimInTerminal()
18 return 1
19endfunc
20
Bram Moolenaarda650582018-02-20 15:51:40 +010021source 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"
27" Options is a dictionary (not used yet).
28func RunVimInTerminal(arguments, options)
Bram Moolenaar948a7962018-03-23 20:37:45 +010029 " If Vim doesn't exit a swap file remains, causing other tests to fail.
30 " Remove it here.
31 call delete(".swp")
32
Bram Moolenaare7499dd2018-03-24 17:56:13 +010033 if exists('$COLORFGBG')
34 " Clear $COLORFGBG to avoid 'background' being set to "dark", which will
35 " only be corrected if the response to t_RB is received, which may be too
36 " late.
37 let $COLORFGBG = ''
38 endif
39
Bram Moolenaarda650582018-02-20 15:51:40 +010040 " Make a horizontal and vertical split, so that we can get exactly the right
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +020041 " size terminal window. Works only when the current window is full width.
42 call assert_equal(&columns, winwidth(0))
Bram Moolenaarda650582018-02-20 15:51:40 +010043 split
44 vsplit
45
Bram Moolenaar6acadda2018-02-24 16:51:32 +010046 " Always do this with 256 colors and a light background.
47 set t_Co=256 background=light
48 hi Normal ctermfg=NONE ctermbg=NONE
Bram Moolenaarda650582018-02-20 15:51:40 +010049
50 let cmd = GetVimCommandClean()
Bram Moolenaarb7ea7cb2018-02-24 14:38:51 +010051 " Add -v to have gvim run in the terminal (if possible)
52 let cmd .= ' -v ' . a:arguments
Bram Moolenaarda650582018-02-20 15:51:40 +010053 let buf = term_start(cmd, {'curwin': 1, 'term_rows': 20, 'term_cols': 75})
54 call assert_equal([20, 75], term_getsize(buf))
55
56 return buf
57endfunc
58
59" Stop a Vim running in terminal buffer "buf".
60func StopVimInTerminal(buf)
61 call assert_equal("running", term_getstatus(a:buf))
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010062 call term_sendkeys(a:buf, "\<Esc>\<Esc>:qa!\<cr>")
Bram Moolenaarda650582018-02-20 15:51:40 +010063 call WaitFor('term_getstatus(' . a:buf . ') == "finished"')
64 only!
65endfunc
66
67" Verify that Vim running in terminal buffer "buf" matches the screen dump.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010068" "options" is passed to term_dumpwrite().
Bram Moolenaarda650582018-02-20 15:51:40 +010069" The file name used is "dumps/{filename}.dump".
70" Will wait for up to a second for the screen dump to match.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010071func VerifyScreenDump(buf, filename, options)
Bram Moolenaarda650582018-02-20 15:51:40 +010072 let reference = 'dumps/' . a:filename . '.dump'
73 let testfile = a:filename . '.dump.failed'
74
75 let i = 0
76 while 1
77 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010078 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaarda650582018-02-20 15:51:40 +010079 if readfile(reference) == readfile(testfile)
80 call delete(testfile)
81 break
82 endif
83 if i == 100
84 " Leave the test file around for inspection.
85 call assert_report('See dump file difference: call term_dumpdiff("' . testfile . '", "' . reference . '")')
86 break
87 endif
88 sleep 10m
89 let i += 1
90 endwhile
91endfunc