Permalink
Browse files

Modernise tests.

Use Vim's built-in assert_*() functions and vim-contest test runner.
  • Loading branch information...
1 parent 64a445b commit ee481585d7532c3ca2df855ab895a0b430469723 @airblade committed Oct 24, 2016
Showing with 581 additions and 454 deletions.
  1. +6 −0 autoload/gitgutter/sign.vim
  2. 0 test/=fixture=.txt
  3. +0 −30 test/README.markdown
  4. +0 −4 test/addLines.expected
  5. +0 −4 test/addLinesFish.expected
  6. +0 −4 test/ambiguousFile.expected
  7. +0 −4 test/fileAddedToGit.expected
  8. +0 −5 test/filenameWithEquals.expected
  9. +0 −5 test/filenameWithSquareBrackets.expected
  10. 0 test/fix[tu]re.txt
  11. +0 −4 test/followSymlink.expected
  12. +0 −27 test/helper.vim
  13. 0 test/hunkHunkOutsideNoopStageGitDiffStaged.expected
  14. 0 test/hunkHunkOutsideNoopUndoGitDiffStaged.expected
  15. +0 −2 test/hunkOutsideNoopStageSigns.expected
  16. +0 −2 test/hunkOutsideNoopUndoSigns.expected
  17. +0 −13 test/hunkStageGitDiff.expected
  18. +0 −13 test/hunkStageNearbyGitDiff.expected
  19. +0 −12 test/hunkStageNearbyGitDiffStaged.expected
  20. +0 −6 test/hunkStageNearbySigns.expected
  21. +0 −2 test/hunkStageSigns.expected
  22. 0 test/hunkUndoGitDiff.expected
  23. +0 −13 test/hunkUndoNearbyGitDiff.expected
  24. +0 −6 test/hunkUndoNearbySigns.expected
  25. +0 −2 test/hunkUndoSigns.expected
  26. +0 −3 test/keepAlt.expected
  27. +0 −2 test/keepModified.expected
  28. +0 −4 test/keepOpMarks.expected
  29. +0 −4 test/modifyLines.expected
  30. +0 −2 test/noModifications.expected
  31. +0 −4 test/orphanedSigns.expected
  32. +0 −4 test/removeFirstLines.expected
  33. +0 −4 test/removeLines.expected
  34. +162 −0 test/runner.vim
  35. +0 −4 test/signColumnAlways.expected
  36. +13 −44 test/test
  37. +0 −6 test/testAddLines.vim
  38. +0 −7 test/testAddLinesFish.vim
  39. +0 −9 test/testEditAmbiguousFile.vim
  40. +0 −12 test/testFileAddedToGit.vim
  41. +0 −12 test/testFilenameWithEquals.vim
  42. +0 −9 test/testFilenameWithSquareBrackets.vim
  43. +0 −10 test/testFollowSymlink.vim
  44. +0 −13 test/testHunkOutsideNoop.vim
  45. +0 −13 test/testHunkStage.vim
  46. +0 −10 test/testHunkStageNearbyHunk.vim
  47. +0 −12 test/testHunkUndo.vim
  48. +0 −9 test/testHunkUndoNearbyHunk.vim
  49. +0 −12 test/testKeepAlt.vim
  50. +0 −8 test/testKeepModified.vim
  51. +0 −10 test/testKeepOpMarks.vim
  52. +0 −6 test/testModifyLines.vim
  53. +0 −4 test/testNoModifications.vim
  54. +0 −8 test/testOrphanedSigns.vim
  55. +0 −6 test/testRemoveFirstLines.vim
  56. +0 −6 test/testRemoveLines.vim
  57. +0 −7 test/testSignColumnAlways.vim
  58. +0 −6 test/testUntrackedFileOutsideRepo.vim
  59. +0 −10 test/testUntrackedFileSquareBracketsWithinRepo.vim
  60. +0 −10 test/testUntrackedFileWithinRepo.vim
  61. +400 −0 test/test_gitgutter.vim
  62. +0 −2 test/untrackedFileOutsideRepo.expected
  63. +0 −2 test/untrackedFileSquareBracketsWithinRepo.expected
  64. +0 −2 test/untrackedFileWithinRepo.expected
@@ -171,3 +171,9 @@ function! gitgutter#sign#next_sign_id() abort
let s:next_sign_id += 1
return next_id
endfunction
+
+
+" Only for testing.
+function! gitgutter#sign#reset()
+ let s:next_sign_id = s:first_sign_id
+endfunction
View
No changes.
View
@@ -1,30 +0,0 @@
-## Testing vim-gitgutter
-
-### Run the tests
-
-```sh
-$ cd test
-$ ./test
-```
-
-### Add a new test
-
-- Add a test file named like `testFoo.vim`. It should have this structure:
-
-```viml
-source helper.vim
-call Setup()
-
-" test code here
-```
-
-- Run the tests.
-- Inspect output (in `foo.actual`) from the new test. If good, copy it to `foo.expected`.
-- Run the tests to ensure new test's output is verified.
-- Commit changes.
-
-### Limitations
-
-- Currently tests are done by writing out a file and comparing it to a known good one.
-- There's no support for assertions within the testcase code.
-
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=2 id=3000 name=GitGutterLineAdded
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=2 id=3000 name=GitGutterLineAdded
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=5 id=3000 name=GitGutterLineModified
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fileAddedToGit.tmp:
- line=1 id=3000 name=GitGutterLineAdded
@@ -1,5 +0,0 @@
-
---- Signs ---
-Signs for =fixture=.txt:
- line=1 id=3000 name=GitGutterLineAdded
- line=2 id=3001 name=GitGutterLineAdded
@@ -1,5 +0,0 @@
-
---- Signs ---
-Signs for fix[tu]re.txt:
- line=1 id=3000 name=GitGutterLineAdded
- line=2 id=3001 name=GitGutterLineAdded
View
No changes.
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for symlink:
- line=5 id=3000 name=GitGutterLineRemoved
View
@@ -1,27 +0,0 @@
-set runtimepath+=../
-source ../plugin/gitgutter.vim
-
-function! Setup()
- edit! fixture.txt
- sign unplace *
-endfunction
-
-function! DumpSigns(filename)
- execute 'redir! > ' a:filename.'.actual'
- silent execute 'sign place'
- redir END
-endfunction
-
-function! DumpGitDiff(filename)
- call system('git diff fixture.txt > '.a:filename.'.actual')
-endfunction
-
-function! DumpGitDiffStaged(filename)
- call system('git diff --staged fixture.txt > '.a:filename.'.actual')
-endfunction
-
-function! Dump(text, filename)
- let msg = type(a:text) == 1 ? split(a:text, '\n') : a:text
- call writefile(msg, a:filename.'.actual', 'a')
-endfunction
-
@@ -1,2 +0,0 @@
-
---- Signs ---
@@ -1,2 +0,0 @@
-
---- Signs ---
@@ -1,13 +0,0 @@
-diff --git a/test/fixture.txt b/test/fixture.txt
-index f5c6aff..ae8e546 100644
---- a/test/fixture.txt
-+++ b/test/fixture.txt
-@@ -2,7 +2,7 @@ a
- b
- c
- d
--e
-+*e
- f
- g
- h
@@ -1,13 +0,0 @@
-diff --git a/test/fixture.txt b/test/fixture.txt
-index 53b13df..8fdfda7 100644
---- a/test/fixture.txt
-+++ b/test/fixture.txt
-@@ -1,5 +1,8 @@
- a
- b
-+x
-+y
-+z
- c
- e
- f
@@ -1,12 +0,0 @@
-diff --git a/test/fixture.txt b/test/fixture.txt
-index f5c6aff..53b13df 100644
---- a/test/fixture.txt
-+++ b/test/fixture.txt
-@@ -1,7 +1,6 @@
- a
- b
- c
--d
- e
- f
- g
@@ -1,6 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=3 id=3000 name=GitGutterLineAdded
- line=4 id=3001 name=GitGutterLineAdded
- line=5 id=3002 name=GitGutterLineAdded
@@ -1,2 +0,0 @@
-
---- Signs ---
No changes.
@@ -1,13 +0,0 @@
-diff --git a/test/fixture.txt b/test/fixture.txt
-index f5c6aff..3fbde56 100644
---- a/test/fixture.txt
-+++ b/test/fixture.txt
-@@ -1,5 +1,8 @@
- a
- b
-+x
-+y
-+z
- c
- d
- e
@@ -1,6 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=3 id=3000 name=GitGutterLineAdded
- line=4 id=3001 name=GitGutterLineAdded
- line=5 id=3002 name=GitGutterLineAdded
@@ -1,2 +0,0 @@
-
---- Signs ---
@@ -1,3 +0,0 @@
-buffer: fixture.txt
-altbuffer:
-altbuffer:
@@ -1,2 +0,0 @@
-modified: 1
-modified: 1
@@ -1,4 +0,0 @@
-'[ mark: 0,6,1,0
-'] mark: 0,6,2,0
-'[ mark: 0,6,1,0
-'] mark: 0,6,2,0
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=1 id=3000 name=GitGutterLineModified
@@ -1,2 +0,0 @@
-
---- Signs ---
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=6 id=3001 name=GitGutterLineAdded
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=1 id=3000 name=GitGutterLineRemovedFirstLine
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=4 id=3000 name=GitGutterLineRemoved
View
@@ -0,0 +1,162 @@
+"
+" Adapted from https://github.com/vim/vim/blob/master/src/testdir/runtest.vim
+"
+" When debugging tests it can help to write debug output:
+" call Log('oh noes')
+"
+
+function RunTest(test)
+ if exists("*SetUp")
+ call SetUp()
+ endif
+
+ try
+ execute 'call '.a:test
+ catch
+ call Exception()
+ let s:errored = 1
+ endtry
+
+ if exists("*TearDown")
+ call TearDown()
+ endif
+endfunction
+
+function Log(msg)
+ if type(a:msg) == type('')
+ call add(s:messages, a:msg)
+ elseif type(a:msg) == type([])
+ call extend(s:messages, a:msg)
+ else
+ call add(v:errors, 'Exception: unsupported type: '.type(a:msg))
+ endif
+endfunction
+
+function Exception()
+ call add(v:errors, v:throwpoint.'..'.'Exception: '.v:exception)
+endfunction
+
+" Shuffles list in place.
+function Shuffle(list)
+ " Fisher-Yates-Durstenfeld-Knuth
+ let n = len(a:list)
+ if n < 2
+ return a:list
+ endif
+ for i in range(0, n-2)
+ let j = Random(0, n-i-1)
+ let e = a:list[i]
+ let a:list[i] = a:list[i+j]
+ let a:list[i+j] = e
+ endfor
+ return a:list
+endfunction
+
+" Returns a pseudorandom integer i such that 0 <= i <= max
+function Random(min, max)
+ if has('unix')
+ let i = system('echo $RANDOM') " 0 <= i <= 32767
+ else
+ let i = system('echo %RANDOM%') " 0 <= i <= 32767
+ endif
+ return i * (a:max - a:min + 1) / 32768 + a:min
+endfunction
+
+function FriendlyName(test_name)
+ return substitute(a:test_name[5:-3], '_', ' ', 'g')
+endfunction
+
+function Align(left, right)
+ if type(a:right) == type([])
+ let result = []
+ for s in a:right
+ if empty(result)
+ call add(result, printf('%-'.s:indent.'S', a:left).s)
+ else
+ call add(result, printf('%-'.s:indent.'S', '').s)
+ endif
+ endfor
+ return result
+ endif
+
+ return printf('%-'.s:indent.'S', a:left).a:right
+endfunction
+
+let g:testname = expand('%')
+let s:errored = 0
+let s:done = 0
+let s:fail = 0
+let s:errors = 0
+let s:messages = []
+let s:indent = ''
+
+call Log(g:testname.':')
+
+" Source the test script.
+try
+ source %
+catch
+ let s:errors += 1
+ call Exception()
+endtry
+
+" Locate the test functions.
+set nomore
+redir @q
+silent function /^Test_
+redir END
+let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
+
+" If there is another argument, filter test-functions' names against it.
+if argc() > 1
+ let s:tests = filter(s:tests, 'v:val =~ argv(1)')
+endif
+
+let s:indent = max(map(copy(s:tests), {_, val -> len(FriendlyName(val))}))
+
+" Run the tests in random order.
+for test in Shuffle(s:tests)
+ call RunTest(test)
+ let s:done += 1
+
+ let friendly_name = FriendlyName(test)
+ if len(v:errors) == 0
+ call Log(Align(friendly_name, ' - ok'))
+ else
+ if s:errored
+ let s:errors += 1
+ let s:errored = 0
+ else
+ let s:fail += 1
+ endif
+ call Log(Align(friendly_name, ' - not ok'))
+
+ let i = 0
+ for error in v:errors
+ if i != 0
+ call Log(Align('',' ! ----'))
+ endif
+ for trace in reverse(split(error, '\.\.'))
+ call Log(Align('', ' ! '.trace))
+ endfor
+ let i += 1
+ endfor
+
+ let v:errors = []
+ endif
+endfor
+
+let summary = [
+ \ s:done.( s:done == 1 ? ' test' : ' tests'),
+ \ s:errors.(s:errors == 1 ? ' error' : ' errors'),
+ \ s:fail.( s:fail == 1 ? ' failure' : ' failures'),
+ \ ]
+call Log('')
+call Log(join(summary, ', '))
+
+split messages.log
+call append(line('$'), s:messages)
+write
+
+qall!
+
@@ -1,4 +0,0 @@
-
---- Signs ---
-Signs for fixture.txt:
- line=9999 id=2999 name=GitGutterDummy
Oops, something went wrong.

0 comments on commit ee48158

Please sign in to comment.