diff options
author | zeertzjq <zeertzjq@outlook.com> | 2024-01-16 08:00:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-16 08:00:08 +0800 |
commit | 73e1942abe7a96d63ce3749af4187f2cdff87e69 (patch) | |
tree | bd508b354771b2c01bbcc41dc83fb82a3d0d38b7 /test | |
parent | ae48d965d70cc721a3165c40ba0c34d95408e229 (diff) | |
download | rneovim-73e1942abe7a96d63ce3749af4187f2cdff87e69.tar.gz rneovim-73e1942abe7a96d63ce3749af4187f2cdff87e69.tar.bz2 rneovim-73e1942abe7a96d63ce3749af4187f2cdff87e69.zip |
vim-patch:9.1.0009: Cannot easily get the list of matches (#27028)
Problem: Cannot easily get the list of matches
Solution: Add the matchstrlist() and matchbufline() Vim script
functions (Yegappan Lakshmanan)
closes: vim/vim#13766
Omit CHECK_LIST_MATERIALIZE(): it populates a List with numbers only,
and there is a check for strings below.
https://github.com/vim/vim/commit/f93b1c881a99fa847a1bafa71877d7e16f18e6ef
vim-patch:eb3475df0d92
runtime(doc): Replace non-breaking space with normal space (vim/vim#13868)
https://github.com/vim/vim/commit/eb3475df0d927a178789cf8e7fc4983932e1cdbe
Co-authored-by: Yegappan Lakshmanan <4298407+yegappan@users.noreply.github.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/old/testdir/test_functions.vim | 188 | ||||
-rw-r--r-- | test/old/testdir/vim9.vim | 43 |
2 files changed, 230 insertions, 1 deletions
diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim index d277ff72df..938928cd0b 100644 --- a/test/old/testdir/test_functions.vim +++ b/test/old/testdir/test_functions.vim @@ -1044,6 +1044,192 @@ func Test_matchstrpos() call assert_equal(['', -1, -1], matchstrpos(v:_null_list, '\a')) endfunc +" Test for matchstrlist() +func Test_matchstrlist() + let lines =<< trim END + #" Basic match + call assert_equal([{'idx': 0, 'byteidx': 1, 'text': 'bout'}, + \ {'idx': 1, 'byteidx': 1, 'text': 'bove'}], + \ matchstrlist(['about', 'above'], 'bo.*')) + #" no match + call assert_equal([], matchstrlist(['about', 'above'], 'xy.*')) + #" empty string + call assert_equal([], matchstrlist([''], '.')) + #" empty pattern + call assert_equal([{'idx': 0, 'byteidx': 0, 'text': ''}], matchstrlist(['abc'], '')) + #" method call + call assert_equal([{'idx': 0, 'byteidx': 2, 'text': 'it'}], ['editor']->matchstrlist('ed\zsit\zeor')) + #" single character matches + call assert_equal([{'idx': 0, 'byteidx': 5, 'text': 'r'}], + \ ['editor']->matchstrlist('r')) + call assert_equal([{'idx': 0, 'byteidx': 0, 'text': 'a'}], ['a']->matchstrlist('a')) + call assert_equal([{'idx': 0, 'byteidx': 0, 'text': ''}], + \ matchstrlist(['foobar'], '\zs')) + #" string with tabs + call assert_equal([{'idx': 0, 'byteidx': 1, 'text': 'foo'}], + \ matchstrlist(["\tfoobar"], 'foo')) + #" string with multibyte characters + call assert_equal([{'idx': 0, 'byteidx': 2, 'text': '😊😊'}], + \ matchstrlist(["\t\t😊😊"], '\k\+')) + + #" null string + call assert_equal([], matchstrlist(v:_null_list, 'abc')) + call assert_equal([], matchstrlist([v:_null_string], 'abc')) + call assert_equal([{'idx': 0, 'byteidx': 0, 'text': ''}], + \ matchstrlist(['abc'], v:_null_string)) + + #" sub matches + call assert_equal([{'idx': 0, 'byteidx': 0, 'text': 'acd', 'submatches': ['a', '', 'c', 'd', '', '', '', '', '']}], matchstrlist(['acd'], '\(a\)\?\(b\)\?\(c\)\?\(.*\)', {'submatches': v:true})) + + #" null dict argument + call assert_equal([{'idx': 0, 'byteidx': 0, 'text': 'vim'}], + \ matchstrlist(['vim'], '\w\+', v:_null_dict)) + + #" Error cases + call assert_fails("echo matchstrlist('abc', 'a')", 'E1211: List required for argument 1') + call assert_fails("echo matchstrlist(['abc'], {})", 'E1174: String required for argument 2') + call assert_fails("echo matchstrlist(['abc'], '.', [])", 'E1206: Dictionary required for argument 3') + call assert_fails("echo matchstrlist(['abc'], 'a', {'submatches': []})", 'E475: Invalid value for argument submatches') + call assert_fails("echo matchstrlist(['abc'], '\\@=')", 'E866: (NFA regexp) Misplaced @') + END + call CheckLegacyAndVim9Success(lines) + + let lines =<< trim END + vim9script + # non string items + matchstrlist([0z10, {'a': 'x'}], 'x') + END + call CheckSourceSuccess(lines) + + let lines =<< trim END + vim9script + def Foo() + # non string items + assert_equal([], matchstrlist([0z10, {'a': 'x'}], 'x')) + enddef + Foo() + END + call CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected list<string> but got list<any>', 2) +endfunc + +" Test for matchbufline() +func Test_matchbufline() + let lines =<< trim END + #" Basic match + new + call setline(1, ['about', 'above', 'below']) + VAR bnr = bufnr() + wincmd w + call assert_equal([{'lnum': 1, 'byteidx': 1, 'text': 'bout'}, + \ {'lnum': 2, 'byteidx': 1, 'text': 'bove'}], + \ matchbufline(bnr, 'bo.*', 1, '$')) + #" multiple matches in a line + call setbufline(bnr, 1, ['about about', 'above above', 'below']) + call assert_equal([{'lnum': 1, 'byteidx': 1, 'text': 'bout'}, + \ {'lnum': 1, 'byteidx': 7, 'text': 'bout'}, + \ {'lnum': 2, 'byteidx': 1, 'text': 'bove'}, + \ {'lnum': 2, 'byteidx': 7, 'text': 'bove'}], + \ matchbufline(bnr, 'bo\k\+', 1, '$')) + #" no match + call assert_equal([], matchbufline(bnr, 'xy.*', 1, '$')) + #" match on a particular line + call assert_equal([{'lnum': 2, 'byteidx': 7, 'text': 'bove'}], + \ matchbufline(bnr, 'bo\k\+$', 2, 2)) + #" match on a particular line + call assert_equal([], matchbufline(bnr, 'bo.*', 3, 3)) + #" empty string + call deletebufline(bnr, 1, '$') + call assert_equal([], matchbufline(bnr, '.', 1, '$')) + #" empty pattern + call setbufline(bnr, 1, 'abc') + call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': ''}], + \ matchbufline(bnr, '', 1, '$')) + #" method call + call setbufline(bnr, 1, 'editor') + call assert_equal([{'lnum': 1, 'byteidx': 2, 'text': 'it'}], + \ bnr->matchbufline('ed\zsit\zeor', 1, 1)) + #" single character matches + call assert_equal([{'lnum': 1, 'byteidx': 5, 'text': 'r'}], + \ matchbufline(bnr, 'r', 1, '$')) + call setbufline(bnr, 1, 'a') + call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'a'}], + \ matchbufline(bnr, 'a', 1, '$')) + #" zero-width match + call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': ''}], + \ matchbufline(bnr, '\zs', 1, '$')) + #" string with tabs + call setbufline(bnr, 1, "\tfoobar") + call assert_equal([{'lnum': 1, 'byteidx': 1, 'text': 'foo'}], + \ matchbufline(bnr, 'foo', 1, '$')) + #" string with multibyte characters + call setbufline(bnr, 1, "\t\t😊😊") + call assert_equal([{'lnum': 1, 'byteidx': 2, 'text': '😊😊'}], + \ matchbufline(bnr, '\k\+', 1, '$')) + #" empty buffer + call deletebufline(bnr, 1, '$') + call assert_equal([], matchbufline(bnr, 'abc', 1, '$')) + + #" Non existing buffer + call setbufline(bnr, 1, 'abc') + call assert_fails("echo matchbufline(5000, 'abc', 1, 1)", 'E158: Invalid buffer name: 5000') + #" null string + call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': ''}], + \ matchbufline(bnr, v:_null_string, 1, 1)) + #" invalid starting line number + call assert_equal([], matchbufline(bnr, 'abc', 100, 100)) + #" ending line number greater than the last line + call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'abc'}], + \ matchbufline(bnr, 'abc', 1, 100)) + #" ending line number greater than the starting line number + call setbufline(bnr, 1, ['one', 'two']) + call assert_fails($"echo matchbufline({bnr}, 'abc', 2, 1)", 'E475: Invalid value for argument end_lnum') + + #" sub matches + call deletebufline(bnr, 1, '$') + call setbufline(bnr, 1, 'acd') + call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'acd', 'submatches': ['a', '', 'c', 'd', '', '', '', '', '']}], + \ matchbufline(bnr, '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 1, '$', {'submatches': v:true})) + + #" null dict argument + call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'acd'}], + \ matchbufline(bnr, '\w\+', '$', '$', v:_null_dict)) + + #" Error cases + call assert_fails("echo matchbufline([1], 'abc', 1, 1)", 'E1220: String or Number required for argument 1') + call assert_fails("echo matchbufline(1, {}, 1, 1)", 'E1174: String required for argument 2') + call assert_fails("echo matchbufline(1, 'abc', {}, 1)", 'E1220: String or Number required for argument 3') + call assert_fails("echo matchbufline(1, 'abc', 1, {})", 'E1220: String or Number required for argument 4') + call assert_fails($"echo matchbufline({bnr}, 'abc', -1, '$')", 'E475: Invalid value for argument lnum') + call assert_fails($"echo matchbufline({bnr}, 'abc', 1, -1)", 'E475: Invalid value for argument end_lnum') + call assert_fails($"echo matchbufline({bnr}, '\\@=', 1, 1)", 'E866: (NFA regexp) Misplaced @') + call assert_fails($"echo matchbufline({bnr}, 'abc', 1, 1, {{'submatches': []}})", 'E475: Invalid value for argument submatches') + :%bdelete! + call assert_fails($"echo matchbufline({bnr}, 'abc', 1, '$'))", 'E681: Buffer is not loaded') + END + call CheckLegacyAndVim9Success(lines) + + call assert_fails($"echo matchbufline('', 'abc', 'abc', 1)", 'E475: Invalid value for argument lnum') + call assert_fails($"echo matchbufline('', 'abc', 1, 'abc')", 'E475: Invalid value for argument end_lnum') + + let lines =<< trim END + vim9script + def Foo() + echo matchbufline('', 'abc', 'abc', 1) + enddef + Foo() + END + call CheckSourceFailure(lines, 'E1030: Using a String as a Number: "abc"', 1) + + let lines =<< trim END + vim9script + def Foo() + echo matchbufline('', 'abc', 1, 'abc') + enddef + Foo() + END + call CheckSourceFailure(lines, 'E1030: Using a String as a Number: "abc"', 1) +endfunc + func Test_nextnonblank_prevnonblank() new insert @@ -2104,7 +2290,7 @@ func Test_trim() call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) call assert_equal("RESERVE", trim("ä½ RESERVE好", "ä½ å¥½")) call assert_equal("您R E SER V Eæ—©", trim("ä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ ", "ä½ å¥½")) - call assert_equal("ä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ ", trim(" \n\r\r ä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ \t \x0B")) + call assert_equal("ä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ ", trim(" \n\r\r ä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ \t \x0B", )) call assert_equal("您R E SER V Eæ—©å¥½ä½ ä½ \t \x0B", trim(" ä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ \t \x0B", " ä½ å¥½")) call assert_equal("您R E SER V Eæ—©å¥½ä½ ä½ \t \x0B", trim(" tteessttttä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ \t \x0B ttestt", " ä½ å¥½tes")) call assert_equal("您R E SER V Eæ—©å¥½ä½ ä½ \t \x0B", trim(" tteessttttä½ å¥½æ‚¨R E SER V Eæ—©å¥½ä½ ä½ \t \x0B ttestt", " ä½ ä½ ä½ å¥½å¥½å¥½tttsses")) diff --git a/test/old/testdir/vim9.vim b/test/old/testdir/vim9.vim index 825f01c9d6..f8a90c1e97 100644 --- a/test/old/testdir/vim9.vim +++ b/test/old/testdir/vim9.vim @@ -42,6 +42,49 @@ func CheckScriptSuccess(lines) endtry endfunc +" :source a list of "lines" and check whether it fails with "error" +func CheckSourceFailure(lines, error, lnum = -3) + if get(a:lines, 0, '') ==# 'vim9script' + return + endif + new + call setline(1, a:lines) + try + call assert_fails('source', a:error, a:lines, a:lnum) + finally + bw! + endtry +endfunc + +" :source a list of "lines" and check whether it fails with the list of +" "errors" +func CheckSourceFailureList(lines, errors, lnum = -3) + if get(a:lines, 0, '') ==# 'vim9script' + return + endif + new + call setline(1, a:lines) + try + call assert_fails('source', a:errors, a:lines, a:lnum) + finally + bw! + endtry +endfunc + +" :source a list of "lines" and check whether it succeeds +func CheckSourceSuccess(lines) + if get(a:lines, 0, '') ==# 'vim9script' + return + endif + new + call setline(1, a:lines) + try + :source + finally + bw! + endtry +endfunc + func CheckDefAndScriptSuccess(lines) return endfunc |