Skip to content

Commit

Permalink
Improve testing of private functions
Browse files Browse the repository at this point in the history
  • Loading branch information
airblade committed Apr 25, 2022
1 parent 5dd8ab6 commit 774add0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 61 deletions.
24 changes: 0 additions & 24 deletions autoload/gitgutter/diff_highlight.vim
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,6 @@ function! s:lcs(s1, s2)
return a:s1[endindex - maxlength + 1 : endindex]
endfunction

if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#lcs(s1, s2)
return s:lcs(a:s1, a:s2)
endfunction
endif


" Returns 0-based index of last character of common prefix
" If there is no common prefix, returns -1.
Expand All @@ -175,12 +169,6 @@ function! s:common_prefix(a, b)
return i
endfunction

if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#common_prefix(a, b)
return s:common_prefix(a:a, a:b)
endfunction
endif


" Returns 0-based indices of start of common suffix
"
Expand All @@ -199,12 +187,6 @@ function! s:common_suffix(a, b, start)
return [sa+1, sb+1]
endfunction

if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#common_suffix(a, b, start)
return s:common_suffix(a:a, a:b, a:start)
endfunction
endif


" Split a string on another string.
" Assumes 1 occurrence of the delimiter.
Expand All @@ -217,9 +199,3 @@ function! s:split(str, delimiter)

return [a:str[:i-1], a:str[i+len(a:delimiter):]]
endfunction

if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#split(str, delimiter)
return s:split(a:str, a:delimiter)
endfunction
endif
6 changes: 0 additions & 6 deletions autoload/gitgutter/hunk.vim
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,6 @@ function! s:fix_file_references(filepath, hunk_diff)
return join(lines, "\n")."\n"
endfunction

if $VIM_GITGUTTER_TEST
function! gitgutter#hunk#fix_file_references(filepath, hunk_diff)
return s:fix_file_references(a:filepath, a:hunk_diff)
endfunction
endif


function! s:adjust_hunk_summary(hunk_diff) abort
let line_adjustment = s:line_adjustment_for_current_hunk()
Expand Down
77 changes: 46 additions & 31 deletions test/test_gitgutter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@ endfunction


function Test_fix_file_references()
let sid = matchstr(execute('filter autoload/gitgutter/hunk.vim scriptnames'), '\d\+')
let FixFileReferences = function("<SNR>".sid."_fix_file_references")

" No special characters
let hunk_diff = join([
\ 'diff --git a/fixture.txt b/fixture.txt',
Expand All @@ -869,7 +872,7 @@ function Test_fix_file_references()
\ '+x'
\ ], "\n")."\n"

call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
call assert_equal(expected, FixFileReferences(filepath, hunk_diff))

" diff.mnemonicPrefix; spaces in filename
let hunk_diff = join([
Expand All @@ -891,7 +894,7 @@ function Test_fix_file_references()
\ '+x'
\ ], "\n")."\n"

call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
call assert_equal(expected, FixFileReferences(filepath, hunk_diff))

" Backslashes in filename; quotation marks
let hunk_diff = join([
Expand All @@ -913,7 +916,7 @@ function Test_fix_file_references()
\ '+x'
\ ], "\n")."\n"

call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
call assert_equal(expected, FixFileReferences(filepath, hunk_diff))
endfunction


Expand Down Expand Up @@ -989,39 +992,45 @@ endfunction


function Test_common_prefix()
let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
let CommonPrefix = function("<SNR>".sid."_common_prefix")

" zero length
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('', 'foo'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('foo', ''))
call assert_equal(-1, CommonPrefix('', 'foo'))
call assert_equal(-1, CommonPrefix('foo', ''))
" nothing in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+pqrst'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('abcde', 'pqrst'))
call assert_equal(-1, CommonPrefix('-abcde', '+pqrst'))
call assert_equal(-1, CommonPrefix('abcde', 'pqrst'))
" something in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abcpq'))
call assert_equal(2, gitgutter#diff_highlight#common_prefix('abcde', 'abcpq'))
call assert_equal(0, gitgutter#diff_highlight#common_prefix('abc', 'apq'))
call assert_equal(-1, CommonPrefix('-abcde', '+abcpq'))
call assert_equal(2, CommonPrefix('abcde', 'abcpq'))
call assert_equal(0, CommonPrefix('abc', 'apq'))
" everything in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abcde'))
call assert_equal(4, gitgutter#diff_highlight#common_prefix('abcde', 'abcde'))
call assert_equal(-1, CommonPrefix('-abcde', '+abcde'))
call assert_equal(4, CommonPrefix('abcde', 'abcde'))
" different lengths
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abx'))
call assert_equal(1, gitgutter#diff_highlight#common_prefix('abcde', 'abx'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abx', '+abcde'))
call assert_equal(1, gitgutter#diff_highlight#common_prefix('abx', 'abcde'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abc'))
call assert_equal(2, gitgutter#diff_highlight#common_prefix('abcde', 'abc'))
call assert_equal(-1, CommonPrefix('-abcde', '+abx'))
call assert_equal(1, CommonPrefix('abcde', 'abx'))
call assert_equal(-1, CommonPrefix('-abx', '+abcde'))
call assert_equal(1, CommonPrefix('abx', 'abcde'))
call assert_equal(-1, CommonPrefix('-abcde', '+abc'))
call assert_equal(2, CommonPrefix('abcde', 'abc'))
endfunction


function Test_common_suffix()
let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
let CommonSuffix = function("<SNR>".sid."_common_suffix")

" nothing in common
call assert_equal([6,6], gitgutter#diff_highlight#common_suffix('-abcde', '+pqrst', 0))
call assert_equal([6,6], CommonSuffix('-abcde', '+pqrst', 0))
" something in common
call assert_equal([3,3], gitgutter#diff_highlight#common_suffix('-abcde', '+pqcde', 0))
call assert_equal([3,3], CommonSuffix('-abcde', '+pqcde', 0))
" everything in common
call assert_equal([5,5], gitgutter#diff_highlight#common_suffix('-abcde', '+abcde', 5))
call assert_equal([5,5], CommonSuffix('-abcde', '+abcde', 5))
" different lengths
call assert_equal([4,2], gitgutter#diff_highlight#common_suffix('-abcde', '+xde', 0))
call assert_equal([2,4], gitgutter#diff_highlight#common_suffix('-xde', '+abcde', 0))
call assert_equal([4,2], CommonSuffix('-abcde', '+xde', 0))
call assert_equal([2,4], CommonSuffix('-xde', '+abcde', 0))
endfunction


Expand Down Expand Up @@ -1110,18 +1119,24 @@ endfunction


function Test_lcs()
call assert_equal('', gitgutter#diff_highlight#lcs('', 'foo'))
call assert_equal('', gitgutter#diff_highlight#lcs('foo', ''))
call assert_equal('bar', gitgutter#diff_highlight#lcs('foobarbaz', 'bbart'))
call assert_equal('transaction', gitgutter#diff_highlight#lcs('transaction.unexplained_amount', 'amount(transaction)'))
let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
let Lcs = function("<SNR>".sid."_lcs")

call assert_equal('', Lcs('', 'foo'))
call assert_equal('', Lcs('foo', ''))
call assert_equal('bar', Lcs('foobarbaz', 'bbart'))
call assert_equal('transaction', Lcs('transaction.unexplained_amount', 'amount(transaction)'))
endfunction


function Test_split()
call assert_equal(['foo', 'baz'], gitgutter#diff_highlight#split('foobarbaz', 'bar'))
call assert_equal(['', 'barbaz'], gitgutter#diff_highlight#split('foobarbaz', 'foo'))
call assert_equal(['foobar', ''], gitgutter#diff_highlight#split('foobarbaz', 'baz'))
call assert_equal(['1', '2'], gitgutter#diff_highlight#split('1~2', '~'))
let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
let Split = function("<SNR>".sid."_split")

call assert_equal(['foo', 'baz'], Split('foobarbaz', 'bar'))
call assert_equal(['', 'barbaz'], Split('foobarbaz', 'foo'))
call assert_equal(['foobar', ''], Split('foobarbaz', 'baz'))
call assert_equal(['1', '2'], Split('1~2', '~'))
endfunction


Expand Down

0 comments on commit 774add0

Please sign in to comment.