Skip to content

Commit

Permalink
tests: add new test, update runttest.vim
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed Jan 1, 2017
1 parent b27031a commit 782746c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
language: ruby

sudo: required
language: go

env:
global:
Expand Down
6 changes: 3 additions & 3 deletions autoload/go/fmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function! go#fmt#Format(withGoimport) abort

let out = go#fmt#run(bin_name, l:tmpname, expand('%'))
if go#util#ShellError() == 0
call s:update_file(l:tmpname, expand('%'))
call go#fmt#update_file(l:tmpname, expand('%'))
elseif g:go_fmt_fail_silently == 0
let errors = s:parse_errors(out)
call s:show_errors(errors)
Expand All @@ -102,7 +102,7 @@ function! go#fmt#Format(withGoimport) abort
endfunction

" update_file updates the target file with the given formatted source
function! s:update_file(source, target)
function! go#fmt#update_file(source, target)
" remove undo point caused via BufWritePre
try | silent undojoin | catch | endtry

Expand All @@ -120,7 +120,7 @@ function! s:update_file(source, target)
endif

" reload buffer to reflect latest changes
silent edit!
silent! edit!

let &fileformat = old_fileformat
let &syntax = &syntax
Expand Down
17 changes: 17 additions & 0 deletions autoload/go/fmt_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ func Test_run_fmt()

call assert_equal(expected, actual)
endfunc

func Test_update_file()
let expected = join(readfile("test-fixtures/fmt/hello_golden.go"), "\n")
let source_file = tempname()
call writefile(readfile("test-fixtures/fmt/hello_golden.go"), source_file)

let target_file = tempname()
call writefile([""], target_file)

" update_file now
call go#fmt#update_file(source_file, target_file)

" this should now contain the formatted code
let actual = join(readfile(target_file), "\n")

call assert_equal(expected, actual)
endfunc
37 changes: 21 additions & 16 deletions scripts/runtest.vim
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
" add vim-go the only plugin inside the runtimepath
"
let total_started = reltime()

" add vim-go the only plugin inside the runtimepath
let git_root_path = system("git rev-parse --show-toplevel | tr -d '\\n'")
exe 'set rtp=' . git_root_path

" source test files
for s:vim_file in globpath(git_root_path . "/autoload/go", "*.vim", 0, 1)
if s:vim_file =~# '^\f\+_test\.vim$'
exec 'source ' . s:vim_file
endif
endfor
" source the passed test file
source %

" cd into the folder of the test file
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
execute cd . expand('%:p:h')

" initialize variables
let g:testname = expand('%')
let s:fail = 0
let s:done = 0
let s:errors = []
let s:messages = []

" get a list of all Test_ functions (sourced above)
" get a list of all Test_ functions for the given file
set nomore
redir @q
silent function /^Test_
redir END
let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))


" cd into autoload/go directory for tests
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
execute cd . fnameescape(git_root_path."/autoload/go")

" Iterate over all tests and execute them
for s:test in sort(s:tests)
let started = reltime()
Expand All @@ -39,7 +35,7 @@ for s:test in sort(s:tests)
let elapsed_time = reltimestr(reltime(started))
let elapsed_time = substitute(elapsed_time, '^\s*\(.\{-}\)\s*$', '\1', '')

call add(s:messages, printf("=== %s (%ss)", s:test, elapsed_time))
call add(s:messages, printf("=== %s\t(%ss)", s:test[:-3], elapsed_time))

let s:done += 1

Expand All @@ -51,12 +47,14 @@ for s:test in sort(s:tests)
endif
endfor

" pop out into the scripts folder
execute cd . fnameescape(dir)

if len(s:errors) > 0 || s:done == 0
" Append errors to test.log
split test.log
call append(line('$'), '')
call append(line('$'), 'From ' . g:testname . ':')
call append(line('$'), s:errors)
write
endif
Expand All @@ -65,12 +63,19 @@ let total_elapsed_time = reltimestr(reltime(total_started))
let total_elapsed_time = substitute(total_elapsed_time, '^\s*\(.\{-}\)\s*$', '\1', '')

let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') . '. Total test time: '. total_elapsed_time .'s'

call add(s:messages, "")
call add(s:messages, message)

" store all error messages from within vim into messages.log
redir > messages.log
silent messages
redir END

" also store all internal messages from s:messages as well
split messages.log
call append(line('$'), '')
call append(line('$'), 'From ' . g:testname . ':')
call append(line('$'), s:messages)
write

Expand Down
7 changes: 6 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ fi

fail=0

vim -u NONE -S runtest.vim
for test_file in ../autoload/go/*_test.vim
do
echo "=== Testing $test_file"
vim -u NONE -S runtest.vim $test_file
done


# test.log only exists if a test fails, output it so we see it
if [ -f "test.log" ]; then
Expand Down

0 comments on commit 782746c

Please sign in to comment.