Created
October 23, 2017 15:31
-
-
Save lifepillar/b5018945561e024eeb9fc57650fc5d61 to your computer and use it in GitHub Desktop.
Minimal unit-testing framework for Vim scripts (Stripped-down version of Vim testing functions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun! Test_this_should_be_fine() | |
call assert_equal(2, 1 + 1) | |
endf | |
fun! Test_this_should_fail() | |
call assert_equal(3, 1 + 1) | |
endf | |
" ----------------------------------------------------------------------------- | |
" DO NOT MODIFY BELOW THIS LINE | |
" ----------------------------------------------------------------------------- | |
let s:messages = [] | |
let s:errors = [] | |
let s:done = 0 | |
let s:fail = 0 | |
fun! RunTheTest(test) | |
let l:message = a:test . '… ' | |
let s:done += 1 | |
try | |
exe 'call ' . a:test | |
catch | |
call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) | |
endtry | |
if len(v:errors) > 0 | |
let s:fail += 1 | |
let l:message .= 'FAILED' | |
call add(s:errors, a:test) | |
call extend(s:errors, v:errors) | |
let v:errors = [] | |
else | |
let l:message .= 'ok' | |
endif | |
call add(s:messages, l:message) | |
" Close any extra windows and make the current one not modified. | |
while 1 | |
let wincount = winnr('$') | |
if wincount == 1 | |
break | |
endif | |
bwipe! | |
if wincount == winnr('$') | |
" Did not manage to close a window. | |
only! | |
break | |
endif | |
endwhile | |
set nomodified | |
endfunc | |
fun! FinishTesting() | |
call add(s:messages, '') | |
call add(s:messages, 'Run ' . s:done . (s:done > 1 ? ' tests' : ' test')) | |
if s:fail == 0 | |
call add(s:messages, 'ALL TESTS PASSED!') | |
else | |
call add(s:messages, s:fail . (s:fail > 1 ? ' tests' : ' test') . ' failed') | |
endif | |
botright new +setlocal\ buftype=nofile\ bufhidden=wipe\ nobuflisted\ noswapfile\ nowrap | |
call append(line('$'), s:messages) | |
call append(line('$'), '') | |
call append(line('$'), s:errors) | |
endf | |
fun! RunBabyRun() | |
" Locate Test_ functions and execute them. | |
redir @q | |
silent function /^Test_ | |
redir END | |
let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g')) | |
for s:test in sort(s:tests) " Run the tests in lexicographic order | |
call RunTheTest(s:test) | |
endfor | |
call FinishTesting() | |
endf | |
call RunBabyRun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment