blob: 14863f9ae4014e2b05b7f70a261c884387d7ef3b [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.
28 let test_is_flaky = 1
29
Bram Moolenaar1bc353b2019-09-01 14:45:28 +020030 " Redraw to execute the code that updates the screen. Otherwise we get the
Bram Moolenaar87dcfd72019-04-13 22:35:29 +020031 " text and attributes only from the internal buffer.
32 redraw
33
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010034 let did_mkdir = 0
35 if !isdirectory('failed')
36 let did_mkdir = 1
37 call mkdir('failed')
38 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010039
40 let i = 0
41 while 1
Bram Moolenaarb6fc7282018-12-04 22:24:16 +010042 " leave some time for updating the original window
43 sleep 10m
Bram Moolenaarda650582018-02-20 15:51:40 +010044 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010045 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaar353aca12019-02-21 17:05:59 +010046 let testdump = readfile(testfile)
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020047 if filereadable(reference)
48 let refdump = readfile(reference)
49 else
50 " Must be a new screendump, always fail
51 let refdump = []
52 endif
Bram Moolenaar353aca12019-02-21 17:05:59 +010053 if refdump == testdump
Bram Moolenaarda650582018-02-20 15:51:40 +010054 call delete(testfile)
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010055 if did_mkdir
56 call delete('failed', 'd')
57 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010058 break
59 endif
60 if i == 100
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020061 " Leave the failed dump around for inspection.
62 if filereadable(reference)
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020063 let msg = 'See dump file difference: call term_dumpdiff("testdir/' .. testfile .. '", "testdir/' .. reference .. '")'
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020064 if a:0 == 1
65 let msg = a:1 . ': ' . msg
66 endif
67 if len(testdump) != len(refdump)
68 let msg = msg . '; line count is ' . len(testdump) . ' instead of ' . len(refdump)
69 endif
70 else
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020071 let msg = 'See new dump file: call term_dumpload("testdir/' .. testfile .. '")'
Bram Moolenaar353aca12019-02-21 17:05:59 +010072 endif
73 for i in range(len(refdump))
74 if i >= len(testdump)
75 break
76 endif
77 if testdump[i] != refdump[i]
78 let msg = msg . '; difference in line ' . (i + 1) . ': "' . testdump[i] . '"'
79 endif
80 endfor
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020081 call assert_report(msg)
82 return 1
Bram Moolenaarda650582018-02-20 15:51:40 +010083 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010084 let i += 1
85 endwhile
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020086 return 0
Bram Moolenaarda650582018-02-20 15:51:40 +010087endfunc