From 774add0c59ef16af0ba75639c3713e4885e59c34 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Mon, 25 Apr 2022 13:47:08 +0100 Subject: [PATCH] Improve testing of private functions --- autoload/gitgutter/diff_highlight.vim | 24 --------- autoload/gitgutter/hunk.vim | 6 --- test/test_gitgutter.vim | 77 ++++++++++++++++----------- 3 files changed, 46 insertions(+), 61 deletions(-) diff --git a/autoload/gitgutter/diff_highlight.vim b/autoload/gitgutter/diff_highlight.vim index ec21b442..536e52ed 100644 --- a/autoload/gitgutter/diff_highlight.vim +++ b/autoload/gitgutter/diff_highlight.vim @@ -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. @@ -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 " @@ -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. @@ -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 diff --git a/autoload/gitgutter/hunk.vim b/autoload/gitgutter/hunk.vim index 349b7ca7..8d3074c8 100644 --- a/autoload/gitgutter/hunk.vim +++ b/autoload/gitgutter/hunk.vim @@ -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() diff --git a/test/test_gitgutter.vim b/test/test_gitgutter.vim index 412b15f1..df527b86 100644 --- a/test/test_gitgutter.vim +++ b/test/test_gitgutter.vim @@ -849,6 +849,9 @@ endfunction function Test_fix_file_references() + let sid = matchstr(execute('filter autoload/gitgutter/hunk.vim scriptnames'), '\d\+') + let FixFileReferences = function("".sid."_fix_file_references") + " No special characters let hunk_diff = join([ \ 'diff --git a/fixture.txt b/fixture.txt', @@ -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([ @@ -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([ @@ -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 @@ -989,39 +992,45 @@ endfunction function Test_common_prefix() + let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+') + let CommonPrefix = function("".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("".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 @@ -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("".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("".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