blob: 8fc496d1e1136d87c9d4702975d95ec87e72cc1e [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 Moolenaar7a39dd72019-06-23 00:50:15 +02004if exists('*VerifyScreenDump')
Bram Moolenaarda650582018-02-20 15:51:40 +01005 finish
6endif
7
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02008source shared.vim
9source term_util.vim
Bram Moolenaarf2732452018-06-03 14:47:35 +020010
11" Skip the rest if there is no terminal feature at all.
12if !has('terminal')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010013 finish
14endif
15
Bram Moolenaarda650582018-02-20 15:51:40 +010016" Verify that Vim running in terminal buffer "buf" matches the screen dump.
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010017" "options" is passed to term_dumpwrite().
Bram Moolenaarda650582018-02-20 15:51:40 +010018" The file name used is "dumps/{filename}.dump".
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020019" Optionally an extra argument can be passed which is prepended to the error
20" message. Use this when using the same dump file with different options.
Bram Moolenaarda650582018-02-20 15:51:40 +010021" Will wait for up to a second for the screen dump to match.
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020022" Returns non-zero when verification fails.
23func VerifyScreenDump(buf, filename, options, ...)
Bram Moolenaarda650582018-02-20 15:51:40 +010024 let reference = 'dumps/' . a:filename . '.dump'
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010025 let testfile = 'failed/' . a:filename . '.dump'
26
Bram Moolenaar3cdcb092020-03-18 19:18:10 +010027 " Starting a terminal to make a screendump is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +010028 let g:test_is_flaky = 1
Bram Moolenaar3cdcb092020-03-18 19:18:10 +010029
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000030 " wait for the pending updates to be handled.
31 call TermWait(a:buf)
32
Bram Moolenaar1bc353b2019-09-01 14:45:28 +020033 " Redraw to execute the code that updates the screen. Otherwise we get the
Bram Moolenaar87dcfd72019-04-13 22:35:29 +020034 " text and attributes only from the internal buffer.
35 redraw
36
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010037 let did_mkdir = 0
38 if !isdirectory('failed')
39 let did_mkdir = 1
40 call mkdir('failed')
41 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010042
43 let i = 0
44 while 1
Bram Moolenaarb6fc7282018-12-04 22:24:16 +010045 " leave some time for updating the original window
46 sleep 10m
Bram Moolenaarda650582018-02-20 15:51:40 +010047 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010048 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaar353aca12019-02-21 17:05:59 +010049 let testdump = readfile(testfile)
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020050 if filereadable(reference)
51 let refdump = readfile(reference)
52 else
53 " Must be a new screendump, always fail
54 let refdump = []
55 endif
Bram Moolenaar353aca12019-02-21 17:05:59 +010056 if refdump == testdump
Bram Moolenaarda650582018-02-20 15:51:40 +010057 call delete(testfile)
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010058 if did_mkdir
59 call delete('failed', 'd')
60 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010061 break
62 endif
63 if i == 100
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020064 " Leave the failed dump around for inspection.
65 if filereadable(reference)
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020066 let msg = 'See dump file difference: call term_dumpdiff("testdir/' .. testfile .. '", "testdir/' .. reference .. '")'
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020067 if a:0 == 1
68 let msg = a:1 . ': ' . msg
69 endif
70 if len(testdump) != len(refdump)
71 let msg = msg . '; line count is ' . len(testdump) . ' instead of ' . len(refdump)
72 endif
73 else
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020074 let msg = 'See new dump file: call term_dumpload("testdir/' .. testfile .. '")'
Bram Moolenaar353aca12019-02-21 17:05:59 +010075 endif
76 for i in range(len(refdump))
77 if i >= len(testdump)
78 break
79 endif
80 if testdump[i] != refdump[i]
81 let msg = msg . '; difference in line ' . (i + 1) . ': "' . testdump[i] . '"'
82 endif
83 endfor
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020084 call assert_report(msg)
85 return 1
Bram Moolenaarda650582018-02-20 15:51:40 +010086 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010087 let i += 1
88 endwhile
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020089 return 0
Bram Moolenaarda650582018-02-20 15:51:40 +010090endfunc