blob: 7065fa051041604b78715b80989cd8f46f3aaa28 [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"
Bram Moolenaarcf67a502018-03-25 20:31:32 +020027" 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 Moolenaarda650582018-02-20 15:51:40 +010030func RunVimInTerminal(arguments, options)
Bram Moolenaar948a7962018-03-23 20:37:45 +010031 " If Vim doesn't exit a swap file remains, causing other tests to fail.
32 " Remove it here.
33 call delete(".swp")
34
Bram Moolenaare7499dd2018-03-24 17:56:13 +010035 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 Moolenaarda650582018-02-20 15:51:40 +010042 " Make a horizontal and vertical split, so that we can get exactly the right
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +020043 " size terminal window. Works only when the current window is full width.
44 call assert_equal(&columns, winwidth(0))
Bram Moolenaarda650582018-02-20 15:51:40 +010045 split
46 vsplit
47
Bram Moolenaar6acadda2018-02-24 16:51:32 +010048 " 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 Moolenaarda650582018-02-20 15:51:40 +010051
Bram Moolenaarcf67a502018-03-25 20:31:32 +020052 " 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 Moolenaar15a1c3f2018-03-25 18:56:25 +020055
Bram Moolenaarda650582018-02-20 15:51:40 +010056 let cmd = GetVimCommandClean()
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 Moolenaara7eef3d2018-04-15 22:25:54 +020060 if &termsize == ''
61 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 Moolenaar50182fa2018-04-28 21:34:40 +020067 " Wait for "All" or "Top" of the ruler in the status line to be shown. This
68 " can be quite slow (e.g. when using valgrind).
69 " If it fails then show the terminal contents for debugging.
70 try
71 call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1})
72 catch /timed out after/
73 let lines = map(range(1, rows), {key, val -> term_getline(buf, val)})
74 call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>"))
75 endtry
Bram Moolenaar1834d372018-03-29 17:40:46 +020076
Bram Moolenaarda650582018-02-20 15:51:40 +010077 return buf
78endfunc
79
80" Stop a Vim running in terminal buffer "buf".
81func StopVimInTerminal(buf)
82 call assert_equal("running", term_getstatus(a:buf))
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010083 call term_sendkeys(a:buf, "\<Esc>\<Esc>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +020084 call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))})
Bram Moolenaarda650582018-02-20 15:51:40 +010085 only!
86endfunc
87
88" Verify that Vim running in terminal buffer "buf" matches the screen dump.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010089" "options" is passed to term_dumpwrite().
Bram Moolenaarda650582018-02-20 15:51:40 +010090" The file name used is "dumps/{filename}.dump".
91" Will wait for up to a second for the screen dump to match.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010092func VerifyScreenDump(buf, filename, options)
Bram Moolenaarda650582018-02-20 15:51:40 +010093 let reference = 'dumps/' . a:filename . '.dump'
94 let testfile = a:filename . '.dump.failed'
95
96 let i = 0
97 while 1
98 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010099 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaarda650582018-02-20 15:51:40 +0100100 if readfile(reference) == readfile(testfile)
101 call delete(testfile)
102 break
103 endif
104 if i == 100
105 " Leave the test file around for inspection.
106 call assert_report('See dump file difference: call term_dumpdiff("' . testfile . '", "' . reference . '")')
107 break
108 endif
109 sleep 10m
110 let i += 1
111 endwhile
112endfunc