blob: d04d96d983a110348bc4db44cbdfb13637a789e5 [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 Moolenaar1bc353b2019-09-01 14:45:28 +020027 " Redraw to execute the code that updates the screen. Otherwise we get the
Bram Moolenaar87dcfd72019-04-13 22:35:29 +020028 " text and attributes only from the internal buffer.
29 redraw
30
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010031 let did_mkdir = 0
32 if !isdirectory('failed')
33 let did_mkdir = 1
34 call mkdir('failed')
35 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010036
37 let i = 0
38 while 1
Bram Moolenaarb6fc7282018-12-04 22:24:16 +010039 " leave some time for updating the original window
40 sleep 10m
Bram Moolenaarda650582018-02-20 15:51:40 +010041 call delete(testfile)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +010042 call term_dumpwrite(a:buf, testfile, a:options)
Bram Moolenaar353aca12019-02-21 17:05:59 +010043 let testdump = readfile(testfile)
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020044 if filereadable(reference)
45 let refdump = readfile(reference)
46 else
47 " Must be a new screendump, always fail
48 let refdump = []
49 endif
Bram Moolenaar353aca12019-02-21 17:05:59 +010050 if refdump == testdump
Bram Moolenaarda650582018-02-20 15:51:40 +010051 call delete(testfile)
Bram Moolenaaref7f0e32019-03-30 15:59:51 +010052 if did_mkdir
53 call delete('failed', 'd')
54 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010055 break
56 endif
57 if i == 100
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020058 " Leave the failed dump around for inspection.
59 if filereadable(reference)
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020060 let msg = 'See dump file difference: call term_dumpdiff("testdir/' .. testfile .. '", "testdir/' .. reference .. '")'
Bram Moolenaar2d7260d2019-04-06 20:51:52 +020061 if a:0 == 1
62 let msg = a:1 . ': ' . msg
63 endif
64 if len(testdump) != len(refdump)
65 let msg = msg . '; line count is ' . len(testdump) . ' instead of ' . len(refdump)
66 endif
67 else
Bram Moolenaar8a5c7ef2019-06-16 16:14:20 +020068 let msg = 'See new dump file: call term_dumpload("testdir/' .. testfile .. '")'
Bram Moolenaar353aca12019-02-21 17:05:59 +010069 endif
70 for i in range(len(refdump))
71 if i >= len(testdump)
72 break
73 endif
74 if testdump[i] != refdump[i]
75 let msg = msg . '; difference in line ' . (i + 1) . ': "' . testdump[i] . '"'
76 endif
77 endfor
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020078 call assert_report(msg)
79 return 1
Bram Moolenaarda650582018-02-20 15:51:40 +010080 endif
Bram Moolenaarda650582018-02-20 15:51:40 +010081 let i += 1
82 endwhile
Bram Moolenaar6f8bdab2018-09-09 22:02:24 +020083 return 0
Bram Moolenaarda650582018-02-20 15:51:40 +010084endfunc