Refactor tests to be isolated.

This commit is contained in:
Andy Stewart
2014-11-13 12:32:42 +01:00
parent a82bc509a9
commit b9f4173f7c
14 changed files with 92 additions and 100 deletions

View File

@@ -9,13 +9,18 @@ $ ./test.sh
### Add a new test
1. Add new test function to `test.vim`.
2. Add a call to the test function in `test.vim`.
3. Run the tests.
4. Inspect output from the new test function. If good, copy it to `<blah>.ok`.
5. Run the tests to ensure new test's output is verified.
6. `rm *.out` and commit changes.
- Add a test file named like `testFoo.vim`. It should have this structure:
### Potential test pitfalls
```viml
source helper.vim
call Setup()
Currently the tests are order-dependent because the sign id counter isn't reset between tests.
" test code here
quit!
```
- Run the tests.
- Inspect output from the new test. If good, copy it to `foo.ok`.
- Run the tests to ensure new test's output is verified.
- `rm *.out` and commit changes.

15
test/helper.vim Normal file
View File

@@ -0,0 +1,15 @@
set runtimepath+=../
source ../plugin/gitgutter.vim
function! Setup()
call system('git checkout fixture.txt')
edit! fixture.txt
sign unplace *
endfunction
function! DumpSigns(filename)
execute 'redir! > ' a:filename.'.out'
silent execute 'sign place'
redir END
endfunction

View File

@@ -1,4 +1,4 @@
--- Signs ---
Signs for fixture.txt:
line=1 id=3001 name=GitGutterLineModified
line=1 id=3000 name=GitGutterLineModified

View File

@@ -1,4 +1,4 @@
--- Signs ---
Signs for fixture.txt:
line=6 id=3005 name=GitGutterLineAdded
line=6 id=3001 name=GitGutterLineAdded

View File

@@ -1,4 +1,4 @@
--- Signs ---
Signs for fixture.txt:
line=1 id=3003 name=GitGutterLineRemovedFirstLine
line=1 id=3000 name=GitGutterLineRemovedFirstLine

View File

@@ -1,4 +1,4 @@
--- Signs ---
Signs for fixture.txt:
line=4 id=3002 name=GitGutterLineRemoved
line=4 id=3000 name=GitGutterLineRemoved

View File

@@ -1,13 +1,19 @@
#!/usr/bin/env bash
# TODO: exit with non-zero status code when tests fail.
# TODO: quit vim after each testcase inside loop below (instead of in testcase).
rm -f *.out
# Run the tests.
vim -N -u NONE -S test.vim
# Verify the outputs.
for expected in *.ok; do
name=${expected%.*}
for testcase in test*.vim; do
vim -N -u NONE -S $testcase # testFoo.vim
testname=${testcase%.*} # testFoo
name=${testname:4} # Foo
name="$(tr '[:upper:]' '[:lower:]' <<< ${name:0:1})${name:1}" # foo
expected=$name.ok
actual=$name.out
diff $expected $actual && echo "$name ok" || echo "$name failed"
done
git checkout fixture.txt

View File

@@ -1,82 +0,0 @@
set runtimepath+=../
source ../plugin/gitgutter.vim
function! s:setup()
call system('git checkout fixture.txt')
edit! fixture.txt
sign unplace *
endfunction
function! s:dumpSigns(filename)
execute 'redir! > ' a:filename.'.out'
silent execute 'sign place'
redir END
endfunction
"
" The tests.
"
function! s:testNoModifications()
call s:setup()
call s:dumpSigns('noModifications')
endfunction
function! s:testAddLines()
call s:setup()
normal ggo*
write
call s:dumpSigns('addLines')
endfunction
function! s:testModifyLines()
call s:setup()
normal ggi*
write
call s:dumpSigns('modifyLines')
endfunction
function! s:testRemoveLines()
call s:setup()
execute '5d'
write
call s:dumpSigns('removeLines')
endfunction
function! s:testRemoveFirstLines()
call s:setup()
execute '1d'
write
call s:dumpSigns('removeFirstLines')
endfunction
function! s:testOrphanedSigns()
call s:setup()
execute "normal 5GoX\<CR>Y"
write
execute '6d'
write
call s:dumpSigns('orphanedSigns')
endfunction
"
" Execute the tests.
"
call s:testNoModifications()
call s:testAddLines()
call s:testModifyLines()
call s:testRemoveLines()
call s:testRemoveFirstLines()
call s:testOrphanedSigns()
"
" Cleanup.
"
call system('git checkout fixture.txt')
quit!

8
test/testAddLines.vim Normal file
View File

@@ -0,0 +1,8 @@
source helper.vim
call Setup()
normal ggo*
write
call DumpSigns('addLines')
quit!

8
test/testModifyLines.vim Normal file
View File

@@ -0,0 +1,8 @@
source helper.vim
call Setup()
normal ggi*
write
call DumpSigns('modifyLines')
quit!

View File

@@ -0,0 +1,6 @@
source helper.vim
call Setup()
call DumpSigns('noModifications')
quit!

View File

@@ -0,0 +1,10 @@
source helper.vim
call Setup()
execute "normal 5GoX\<CR>Y"
write
execute '6d'
write
call DumpSigns('orphanedSigns')
quit!

View File

@@ -0,0 +1,8 @@
source helper.vim
call Setup()
execute '1d'
write
call DumpSigns('removeFirstLines')
quit!

8
test/testRemoveLines.vim Normal file
View File

@@ -0,0 +1,8 @@
source helper.vim
call Setup()
execute '5d'
write
call DumpSigns('removeLines')
quit!