diff options
| author | Justin M. Keyes <justinkz@gmail.com> | 2019-09-21 14:07:50 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-21 14:07:50 -0700 |
| commit | fd82ce4a3a9c9833e07db079debf6c9b94a2cfd3 (patch) | |
| tree | 30520e000a28c2b2099c641ce33826aea0c07541 /src/nvim/testdir/shared.vim | |
| parent | b3e56957f8e9468497e5db508d97d7b560ccfe85 (diff) | |
| parent | 111d34849a0670842b56c17c3922dbf0576bb39b (diff) | |
| download | rneovim-fd82ce4a3a9c9833e07db079debf6c9b94a2cfd3.tar.gz rneovim-fd82ce4a3a9c9833e07db079debf6c9b94a2cfd3.tar.bz2 rneovim-fd82ce4a3a9c9833e07db079debf6c9b94a2cfd3.zip | |
Merge #11060 from janlazo/vim-8.1.1783
vim-patch:8.0.{1109,1529,1539,1621,1733,1771,1776},8.1.{1783,2054,2058}
Diffstat (limited to 'src/nvim/testdir/shared.vim')
| -rw-r--r-- | src/nvim/testdir/shared.vim | 77 |
1 files changed, 59 insertions, 18 deletions
diff --git a/src/nvim/testdir/shared.vim b/src/nvim/testdir/shared.vim index df512e2e3f..84f636077d 100644 --- a/src/nvim/testdir/shared.vim +++ b/src/nvim/testdir/shared.vim @@ -135,39 +135,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. |