1" Functions shared by tests making screen dumps. 2 3" Only load this script once. 4if exists('*VerifyScreenDump') 5 finish 6endif 7 8source shared.vim 9source term_util.vim 10 11" Skip the rest if there is no terminal feature at all. 12if !has('terminal') 13 finish 14endif 15 16" Verify that Vim running in terminal buffer "buf" matches the screen dump. 17" "options" is passed to term_dumpwrite(). 18" The file name used is "dumps/{filename}.dump". 19" 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. 21" Will wait for up to a second for the screen dump to match. 22" Returns non-zero when verification fails. 23func VerifyScreenDump(buf, filename, options, ...) 24 let reference = 'dumps/' . a:filename . '.dump' 25 let testfile = 'failed/' . a:filename . '.dump' 26 27 " Starting a terminal to make a screendump is always considered flaky. 28 let g:test_is_flaky = 1 29 30 " Redraw to execute the code that updates the screen. Otherwise we get the 31 " text and attributes only from the internal buffer. 32 redraw 33 34 let did_mkdir = 0 35 if !isdirectory('failed') 36 let did_mkdir = 1 37 call mkdir('failed') 38 endif 39 40 let i = 0 41 while 1 42 " leave some time for updating the original window 43 sleep 10m 44 call delete(testfile) 45 call term_dumpwrite(a:buf, testfile, a:options) 46 let testdump = readfile(testfile) 47 if filereadable(reference) 48 let refdump = readfile(reference) 49 else 50 " Must be a new screendump, always fail 51 let refdump = [] 52 endif 53 if refdump == testdump 54 call delete(testfile) 55 if did_mkdir 56 call delete('failed', 'd') 57 endif 58 break 59 endif 60 if i == 100 61 " Leave the failed dump around for inspection. 62 if filereadable(reference) 63 let msg = 'See dump file difference: call term_dumpdiff("testdir/' .. testfile .. '", "testdir/' .. reference .. '")' 64 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 71 let msg = 'See new dump file: call term_dumpload("testdir/' .. testfile .. '")' 72 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 81 call assert_report(msg) 82 return 1 83 endif 84 let i += 1 85 endwhile 86 return 0 87endfunc 88