blob: 80d51c336d857e005409d757082511e6a4b679fa [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 Moolenaarf2732452018-06-03 14:47:35 +02008" 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.
11func CanRunVimInTerminal()
12 return has('terminal') && !has('win32')
13endfunc
14
15" Skip the rest if there is no terminal feature at all.
16if !has('terminal')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010017 finish
18endif
19
Bram Moolenaarda650582018-02-20 15:51:40 +010020source 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 Moolenaarcf67a502018-03-25 20:31:32 +020026" 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 Moolenaarda650582018-02-20 15:51:40 +010029func RunVimInTerminal(arguments, options)
Bram Moolenaar948a7962018-03-23 20:37:45 +010030 " If Vim doesn't exit a swap file remains, causing other tests to fail.
31 " Remove it here.
32 call delete(".swp")
33
Bram Moolenaare7499dd2018-03-24 17:56:13 +010034 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 Moolenaarda650582018-02-20 15:51:40 +010041 " Make a horizontal and vertical split, so that we can get exactly the right
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +020042 " size terminal window. Works only when the current window is full width.
43 call assert_equal(&columns, winwidth(0))
Bram Moolenaarda650582018-02-20 15:51:40 +010044 split
45 vsplit
46
Bram Moolenaar6acadda2018-02-24 16:51:32 +010047 " 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 Moolenaarda650582018-02-20 15:51:40 +010050
Bram Moolenaarcf67a502018-03-25 20:31:32 +020051 " 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 Moolenaar15a1c3f2018-03-25 18:56:25 +020054
Bram Moolenaarda650582018-02-20 15:51:40 +010055 let cmd = GetVimCommandClean()
Bram Moolenaarf2732452018-06-03 14:47:35 +020056
Bram Moolenaarb7ea7cb2018-02-24 14:38:51 +010057 " Add -v to have gvim run in the terminal (if possible)
58 let cmd .= ' -v ' . a:arguments
Bram Moolenaarcf67a502018-03-25 20:31:32 +020059 let buf = term_start(cmd, {'curwin': 1, 'term_rows': rows, 'term_cols': cols})
Bram Moolenaarb833c1e2018-05-05 16:36:06 +020060 if &termwinsize == ''
Bram Moolenaara7eef3d2018-04-15 22:25:54 +020061 call assert_equal([rows, cols], term_getsize(buf))
62 else
63 let rows = term_getsize(buf)[0]
64 let cols = term_getsize(buf)[1]
65 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010066
Bram Moolenaarf2732452018-06-03 14:47:35 +020067 " Wait for "All" or "Top" of the ruler to be shown in the last line or in
68 " the status line of the last window. This can be quite slow (e.g. when
69 " using valgrind).
Bram Moolenaar50182fa2018-04-28 21:34:40 +020070 " If it fails then show the terminal contents for debugging.
71 try
Bram Moolenaarf2732452018-06-03 14:47:35 +020072 call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1 || len(term_getline(buf, rows - 1)) >= cols - 1})
Bram Moolenaar50182fa2018-04-28 21:34:40 +020073 catch /timed out after/
74 let lines = map(range(1, rows), {key, val -> term_getline(buf, val)})
75 call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>"))
76 endtry
Bram Moolenaar1834d372018-03-29 17:40:46 +020077
Bram Moolenaarda650582018-02-20 15:51:40 +010078 return buf
79endfunc
80
81" Stop a Vim running in terminal buffer "buf".
82func StopVimInTerminal(buf)
83 call assert_equal("running", term_getstatus(a:buf))
Bram Moolenaarf2732452018-06-03 14:47:35 +020084 call term_sendkeys(a:buf, "\<Esc>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +020085 call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))})
Bram Moolenaarda650582018-02-20 15:51:40 +010086 only!
87endfunc
88
89" Verify that Vim running in terminal buffer "buf" matches the screen dump.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010090" "options" is passed to term_dumpwrite().
Bram Moolenaarda650582018-02-20 15:51:40 +010091" The file name used is "dumps/{filename}.dump".
92" Will wait for up to a second for the screen dump to match.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010093func VerifyScreenDump(buf, filename, options)
Bram Moolenaarda650582018-02-20 15:51:40 +010094 let reference = 'dumps/' . a:filename . '.dump'
95 let testfile = a:filename . '.dump.failed'
96
97 let i = 0
98 while 1
99 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100100 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaarda650582018-02-20 15:51:40 +0100101 if readfile(reference) == readfile(testfile)
102 call delete(testfile)
103 break
104 endif
105 if i == 100
106 " Leave the test file around for inspection.
107 call assert_report('See dump file difference: call term_dumpdiff("' . testfile . '", "' . reference . '")')
108 break
109 endif
110 sleep 10m
111 let i += 1
112 endwhile
113endfunc