aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/testdir/shared.vim
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/testdir/shared.vim')
-rw-r--r--src/nvim/testdir/shared.vim116
1 files changed, 81 insertions, 35 deletions
diff --git a/src/nvim/testdir/shared.vim b/src/nvim/testdir/shared.vim
index df512e2e3f..41ff9b2bd6 100644
--- a/src/nvim/testdir/shared.vim
+++ b/src/nvim/testdir/shared.vim
@@ -1,10 +1,12 @@
" Functions shared by several tests.
" Only load this script once.
-if exists('*WaitFor')
+if exists('*PythonProg')
finish
endif
+source view_util.vim
+
" {Nvim}
" Filepath captured from output may be truncated, like this:
" /home/va...estdir/Xtest-tmpdir/nvimxbXN4i/10
@@ -69,7 +71,8 @@ endfunc
" Read the port number from the Xportnr file.
func GetPort()
let l = []
- for i in range(200)
+ " with 200 it sometimes failed
+ for i in range(400)
try
let l = readfile("Xportnr")
catch
@@ -135,39 +138,80 @@ endfunc
" Wait for up to five seconds for "expr" to become true. "expr" can be a
" stringified expression to evaluate, or a funcref without arguments.
+" Using a lambda works best. Example:
+" call WaitFor({-> status == "ok"})
"
" A second argument can be used to specify a different timeout in msec.
"
-" Return time slept in milliseconds. With the +reltime feature this can be
-" more than the actual waiting time. Without +reltime it can also be less.
+" When successful the time slept is returned.
+" When running into the timeout an exception is thrown, thus the function does
+" not return.
func WaitFor(expr, ...)
let timeout = get(a:000, 0, 5000)
+ let slept = s:WaitForCommon(a:expr, v:null, timeout)
+ if slept < 0
+ throw 'WaitFor() timed out after ' . timeout . ' msec'
+ endif
+ return slept
+endfunc
+
+" Wait for up to five seconds for "assert" to return zero. "assert" must be a
+" (lambda) function containing one assert function. Example:
+" call WaitForAssert({-> assert_equal("dead", job_status(job)})
+"
+" A second argument can be used to specify a different timeout in msec.
+"
+" Return zero for success, one for failure (like the assert function).
+func WaitForAssert(assert, ...)
+ let timeout = get(a:000, 0, 5000)
+ if s:WaitForCommon(v:null, a:assert, timeout) < 0
+ return 1
+ endif
+ return 0
+endfunc
+
+" Common implementation of WaitFor() and WaitForAssert().
+" Either "expr" or "assert" is not v:null
+" Return the waiting time for success, -1 for failure.
+func s:WaitForCommon(expr, assert, timeout)
" using reltime() is more accurate, but not always available
+ let slept = 0
if has('reltime')
let start = reltime()
- else
- let slept = 0
endif
- if type(a:expr) == v:t_func
- let Test = a:expr
- else
- let Test = {-> eval(a:expr) }
- endif
- for i in range(timeout / 10)
- if Test()
- if has('reltime')
- return float2nr(reltimefloat(reltime(start)) * 1000)
- endif
+
+ while 1
+ if type(a:expr) == v:t_func
+ let success = a:expr()
+ elseif type(a:assert) == v:t_func
+ let success = a:assert() == 0
+ else
+ let success = eval(a:expr)
+ endif
+ if success
return slept
endif
- if !has('reltime')
- let slept += 10
+
+ if slept >= a:timeout
+ break
endif
+ if type(a:assert) == v:t_func
+ " Remove the error added by the assert function.
+ call remove(v:errors, -1)
+ endif
+
sleep 10m
- endfor
- throw 'WaitFor() timed out after ' . timeout . ' msec'
+ if has('reltime')
+ let slept = float2nr(reltimefloat(reltime(start)) * 1000)
+ else
+ let slept += 10
+ endif
+ endwhile
+
+ return -1 " timed out
endfunc
+
" Wait for up to a given milliseconds.
" With the +timers feature this waits for key-input by getchar(), Resume()
" feeds key-input and resumes process. Return time waited in milliseconds.
@@ -211,6 +255,8 @@ func GetVimProg()
endif
endfunc
+let g:valgrind_cnt = 1
+
" Get the command to run Vim, with -u NONE and --headless arguments.
" If there is an argument use it instead of "NONE".
func GetVimCommand(...)
@@ -226,14 +272,25 @@ func GetVimCommand(...)
endif
let cmd .= ' --headless -i NONE'
let cmd = substitute(cmd, 'VIMRUNTIME=.*VIMRUNTIME;', '', '')
+
+ " If using valgrind, make sure every run uses a different log file.
+ if cmd =~ 'valgrind.*--log-file='
+ let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
+ let g:valgrind_cnt += 1
+ endif
+
return cmd
endfunc
-" Get the command to run Vim, with --clean.
+" Get the command to run Vim, with --clean instead of "-u NONE".
func GetVimCommandClean()
let cmd = GetVimCommand()
let cmd = substitute(cmd, '-u NONE', '--clean', '')
let cmd = substitute(cmd, '--headless', '', '')
+
+ " Optionally run Vim under valgrind
+ " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
+
return cmd
endfunc
@@ -249,9 +306,6 @@ endfunc
func RunVimPiped(before, after, arguments, pipecmd)
let $NVIM_LOG_FILE = exists($NVIM_LOG_FILE) ? $NVIM_LOG_FILE : 'Xnvim.log'
let cmd = GetVimCommand()
- if cmd == ''
- return 0
- endif
let args = ''
if len(a:before) > 0
call writefile(a:before, 'Xbefore.vim')
@@ -262,6 +316,9 @@ func RunVimPiped(before, after, arguments, pipecmd)
let args .= ' -S Xafter.vim'
endif
+ " Optionally run Vim under valgrind
+ " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
+
exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
if len(a:before) > 0
@@ -273,17 +330,6 @@ func RunVimPiped(before, after, arguments, pipecmd)
return 1
endfunc
-" Get line "lnum" as displayed on the screen.
-" Trailing white space is trimmed.
-func! Screenline(lnum)
- let chars = []
- for c in range(1, winwidth(0))
- call add(chars, nr2char(screenchar(a:lnum, c)))
- endfor
- let line = join(chars, '')
- return matchstr(line, '^.\{-}\ze\s*$')
-endfunc
-
func CanRunGui()
return has('gui') && ($DISPLAY != "" || has('gui_running'))
endfunc