blob: c84221580cbcb8b545fb3264cd23405b327dd95c [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 Moolenaarda650582018-02-20 15:51:40 +010033 " Make a horizontal and vertical split, so that we can get exactly the right
34 " size terminal window. Works only when we currently have one window.
35 call assert_equal(1, winnr('$'))
36 split
37 vsplit
38
Bram Moolenaar6acadda2018-02-24 16:51:32 +010039 " Always do this with 256 colors and a light background.
40 set t_Co=256 background=light
41 hi Normal ctermfg=NONE ctermbg=NONE
Bram Moolenaarda650582018-02-20 15:51:40 +010042
43 let cmd = GetVimCommandClean()
Bram Moolenaarb7ea7cb2018-02-24 14:38:51 +010044 " Add -v to have gvim run in the terminal (if possible)
45 let cmd .= ' -v ' . a:arguments
Bram Moolenaarda650582018-02-20 15:51:40 +010046 let buf = term_start(cmd, {'curwin': 1, 'term_rows': 20, 'term_cols': 75})
47 call assert_equal([20, 75], term_getsize(buf))
48
49 return buf
50endfunc
51
52" Stop a Vim running in terminal buffer "buf".
53func StopVimInTerminal(buf)
54 call assert_equal("running", term_getstatus(a:buf))
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010055 call term_sendkeys(a:buf, "\<Esc>\<Esc>:qa!\<cr>")
Bram Moolenaarda650582018-02-20 15:51:40 +010056 call WaitFor('term_getstatus(' . a:buf . ') == "finished"')
57 only!
58endfunc
59
60" Verify that Vim running in terminal buffer "buf" matches the screen dump.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010061" "options" is passed to term_dumpwrite().
Bram Moolenaarda650582018-02-20 15:51:40 +010062" The file name used is "dumps/{filename}.dump".
63" Will wait for up to a second for the screen dump to match.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010064func VerifyScreenDump(buf, filename, options)
Bram Moolenaarda650582018-02-20 15:51:40 +010065 let reference = 'dumps/' . a:filename . '.dump'
66 let testfile = a:filename . '.dump.failed'
67
68 let i = 0
69 while 1
70 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010071 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaarda650582018-02-20 15:51:40 +010072 if readfile(reference) == readfile(testfile)
73 call delete(testfile)
74 break
75 endif
76 if i == 100
77 " Leave the test file around for inspection.
78 call assert_report('See dump file difference: call term_dumpdiff("' . testfile . '", "' . reference . '")')
79 break
80 endif
81 sleep 10m
82 let i += 1
83 endwhile
84endfunc