Mercurial > vim
comparison src/testdir/test_functions.vim @ 19783:546bdeef35f1 v8.2.0448
patch 8.2.0448: various functions not properly tested
Commit: https://github.com/vim/vim/commit/0e05de46226eb4e5ea580beefa71831f92d613d3
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Mar 25 22:23:46 2020 +0100
patch 8.2.0448: various functions not properly tested
Problem: Various functions not properly tested.
Solution: Add more tests, especially for failures. (Yegappan Lakshmanan,
closes #5843)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 25 Mar 2020 22:30:04 +0100 |
parents | 810eee1b42e3 |
children | 1c7f92a1100e |
comparison
equal
deleted
inserted
replaced
19782:e5f4316b01dd | 19783:546bdeef35f1 |
---|---|
22 | 22 |
23 func Test_has() | 23 func Test_has() |
24 call assert_equal(1, has('eval')) | 24 call assert_equal(1, has('eval')) |
25 call assert_equal(1, has('eval', 1)) | 25 call assert_equal(1, has('eval', 1)) |
26 | 26 |
27 if has('unix') | |
28 call assert_equal(1, or(has('ttyin'), 1)) | |
29 call assert_equal(0, and(has('ttyout'), 0)) | |
30 call assert_equal(1, has('multi_byte_encoding')) | |
31 endif | |
32 | |
27 call assert_equal(0, has('nonexistent')) | 33 call assert_equal(0, has('nonexistent')) |
28 call assert_equal(0, has('nonexistent', 1)) | 34 call assert_equal(0, has('nonexistent', 1)) |
35 | |
36 " Will we ever have patch 9999? | |
37 let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999' | |
38 call assert_equal(0, has(ver)) | |
29 endfunc | 39 endfunc |
30 | 40 |
31 func Test_empty() | 41 func Test_empty() |
32 call assert_equal(1, empty('')) | 42 call assert_equal(1, empty('')) |
33 call assert_equal(0, empty('a')) | 43 call assert_equal(0, empty('a')) |
474 call assert_equal('de', strpart('abcdefg', 3, 2)) | 484 call assert_equal('de', strpart('abcdefg', 3, 2)) |
475 call assert_equal('ab', strpart('abcdefg', -2, 4)) | 485 call assert_equal('ab', strpart('abcdefg', -2, 4)) |
476 call assert_equal('abcdefg', 'abcdefg'->strpart(-2)) | 486 call assert_equal('abcdefg', 'abcdefg'->strpart(-2)) |
477 call assert_equal('fg', strpart('abcdefg', 5, 4)) | 487 call assert_equal('fg', strpart('abcdefg', 5, 4)) |
478 call assert_equal('defg', strpart('abcdefg', 3)) | 488 call assert_equal('defg', strpart('abcdefg', 3)) |
489 call assert_equal('', strpart('abcdefg', 10)) | |
490 call assert_fails("let s=strpart('abcdef', [])", 'E745:') | |
479 | 491 |
480 call assert_equal('lép', strpart('éléphant', 2, 4)) | 492 call assert_equal('lép', strpart('éléphant', 2, 4)) |
481 call assert_equal('léphant', strpart('éléphant', 2)) | 493 call assert_equal('léphant', strpart('éléphant', 2)) |
482 endfunc | 494 endfunc |
483 | 495 |
627 endfunc | 639 endfunc |
628 | 640 |
629 func Test_tr() | 641 func Test_tr() |
630 call assert_equal('foo', tr('bar', 'bar', 'foo')) | 642 call assert_equal('foo', tr('bar', 'bar', 'foo')) |
631 call assert_equal('zxy', 'cab'->tr('abc', 'xyz')) | 643 call assert_equal('zxy', 'cab'->tr('abc', 'xyz')) |
644 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:') | |
645 call assert_fails("let s=tr('abc', [], 'def')", 'E730:') | |
646 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:') | |
647 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') | |
648 set encoding=latin1 | |
649 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') | |
650 call assert_equal('hEllO', tr('hello', 'eo', 'EO')) | |
651 call assert_equal('hello', tr('hello', 'xy', 'ab')) | |
652 set encoding=utf8 | |
632 endfunc | 653 endfunc |
633 | 654 |
634 " Tests for the mode() function | 655 " Tests for the mode() function |
635 let current_modes = '' | 656 let current_modes = '' |
636 func Save_mode() | 657 func Save_mode() |
838 call assert_equal(3, stridx('hello', 'l', 3)) | 859 call assert_equal(3, stridx('hello', 'l', 3)) |
839 call assert_equal(-1, stridx('hello', 'l', 4)) | 860 call assert_equal(-1, stridx('hello', 'l', 4)) |
840 call assert_equal(-1, stridx('hello', 'l', 10)) | 861 call assert_equal(-1, stridx('hello', 'l', 10)) |
841 call assert_equal(2, stridx('hello', 'll')) | 862 call assert_equal(2, stridx('hello', 'll')) |
842 call assert_equal(-1, stridx('hello', 'hello world')) | 863 call assert_equal(-1, stridx('hello', 'hello world')) |
864 call assert_fails("let n=stridx('hello', [])", 'E730:') | |
865 call assert_fails("let n=stridx([], 'l')", 'E730:') | |
843 endfunc | 866 endfunc |
844 | 867 |
845 func Test_strridx() | 868 func Test_strridx() |
846 call assert_equal(-1, strridx('', 'l')) | 869 call assert_equal(-1, strridx('', 'l')) |
847 call assert_equal(0, strridx('', '')) | 870 call assert_equal(0, strridx('', '')) |
854 call assert_equal(-1, strridx('hello', 'l', 1)) | 877 call assert_equal(-1, strridx('hello', 'l', 1)) |
855 call assert_equal(-1, strridx('hello', 'l', 0)) | 878 call assert_equal(-1, strridx('hello', 'l', 0)) |
856 call assert_equal(-1, strridx('hello', 'l', -1)) | 879 call assert_equal(-1, strridx('hello', 'l', -1)) |
857 call assert_equal(2, strridx('hello', 'll')) | 880 call assert_equal(2, strridx('hello', 'll')) |
858 call assert_equal(-1, strridx('hello', 'hello world')) | 881 call assert_equal(-1, strridx('hello', 'hello world')) |
882 call assert_fails("let n=strridx('hello', [])", 'E730:') | |
883 call assert_fails("let n=strridx([], 'l')", 'E730:') | |
859 endfunc | 884 endfunc |
860 | 885 |
861 func Test_match_func() | 886 func Test_match_func() |
862 call assert_equal(4, match('testing', 'ing')) | 887 call assert_equal(4, match('testing', 'ing')) |
863 call assert_equal(4, 'testing'->match('ing', 2)) | 888 call assert_equal(4, 'testing'->match('ing', 2)) |
864 call assert_equal(-1, match('testing', 'ing', 5)) | 889 call assert_equal(-1, match('testing', 'ing', 5)) |
865 call assert_equal(-1, match('testing', 'ing', 8)) | 890 call assert_equal(-1, match('testing', 'ing', 8)) |
866 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) | 891 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) |
867 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) | 892 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) |
893 call assert_fails("let x=match('vim', [])", 'E730:') | |
894 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1)) | |
895 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5)) | |
896 call assert_equal(4, match('testing', 'ing', -1)) | |
897 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:') | |
868 endfunc | 898 endfunc |
869 | 899 |
870 func Test_matchend() | 900 func Test_matchend() |
871 call assert_equal(7, matchend('testing', 'ing')) | 901 call assert_equal(7, matchend('testing', 'ing')) |
872 call assert_equal(7, 'testing'->matchend('ing', 2)) | 902 call assert_equal(7, 'testing'->matchend('ing', 2)) |
969 | 999 |
970 set endofline& fixendofline& fileformat& | 1000 set endofline& fixendofline& fileformat& |
971 bw! | 1001 bw! |
972 endfunc | 1002 endfunc |
973 | 1003 |
1004 " Test for byteidx() and byteidxcomp() functions | |
974 func Test_byteidx() | 1005 func Test_byteidx() |
975 let a = '.é.' " one char of two bytes | 1006 let a = '.é.' " one char of two bytes |
976 call assert_equal(0, byteidx(a, 0)) | 1007 call assert_equal(0, byteidx(a, 0)) |
977 call assert_equal(0, byteidxcomp(a, 0)) | 1008 call assert_equal(0, byteidxcomp(a, 0)) |
978 call assert_equal(1, byteidx(a, 1)) | 1009 call assert_equal(1, byteidx(a, 1)) |
988 call assert_equal(0, b->byteidx(0)) | 1019 call assert_equal(0, b->byteidx(0)) |
989 call assert_equal(1, b->byteidx(1)) | 1020 call assert_equal(1, b->byteidx(1)) |
990 call assert_equal(4, b->byteidx(2)) | 1021 call assert_equal(4, b->byteidx(2)) |
991 call assert_equal(5, b->byteidx(3)) | 1022 call assert_equal(5, b->byteidx(3)) |
992 call assert_equal(-1, b->byteidx(4)) | 1023 call assert_equal(-1, b->byteidx(4)) |
1024 call assert_fails("call byteidx([], 0)", 'E730:') | |
993 | 1025 |
994 call assert_equal(0, b->byteidxcomp(0)) | 1026 call assert_equal(0, b->byteidxcomp(0)) |
995 call assert_equal(1, b->byteidxcomp(1)) | 1027 call assert_equal(1, b->byteidxcomp(1)) |
996 call assert_equal(2, b->byteidxcomp(2)) | 1028 call assert_equal(2, b->byteidxcomp(2)) |
997 call assert_equal(4, b->byteidxcomp(3)) | 1029 call assert_equal(4, b->byteidxcomp(3)) |
998 call assert_equal(5, b->byteidxcomp(4)) | 1030 call assert_equal(5, b->byteidxcomp(4)) |
999 call assert_equal(-1, b->byteidxcomp(5)) | 1031 call assert_equal(-1, b->byteidxcomp(5)) |
1032 call assert_fails("call byteidxcomp([], 0)", 'E730:') | |
1000 endfunc | 1033 endfunc |
1001 | 1034 |
1002 func Test_count() | 1035 func Test_count() |
1003 let l = ['a', 'a', 'A', 'b'] | 1036 let l = ['a', 'a', 'A', 'b'] |
1004 call assert_equal(2, count(l, 'a')) | 1037 call assert_equal(2, count(l, 'a')) |
1174 xmap <expr> <F2> T() | 1207 xmap <expr> <F2> T() |
1175 exe "normal gg3|ve\<F2>" | 1208 exe "normal gg3|ve\<F2>" |
1176 call assert_equal(3, g:Vcol) | 1209 call assert_equal(3, g:Vcol) |
1177 xunmap <F2> | 1210 xunmap <F2> |
1178 delfunc T | 1211 delfunc T |
1212 | |
1213 " Test for the visual line start and end marks '< and '> | |
1214 call setline(1, ['one', 'one two', 'one two three']) | |
1215 "normal! ggVG | |
1216 call feedkeys("ggVG\<Esc>", 'xt') | |
1217 call assert_equal(1, col("'<")) | |
1218 call assert_equal(14, col("'>")) | |
1219 " Delete the last line of the visually selected region | |
1220 $d | |
1221 call assert_notequal(14, col("'>")) | |
1222 | |
1223 " Test with 'virtualedit' | |
1224 set virtualedit=all | |
1225 call cursor(1, 10) | |
1226 call assert_equal(4, col('.')) | |
1227 set virtualedit& | |
1179 | 1228 |
1180 bw! | 1229 bw! |
1181 endfunc | 1230 endfunc |
1182 | 1231 |
1183 " Test for input() | 1232 " Test for input() |
1341 call assert_equal("a", trim("a", "")) | 1390 call assert_equal("a", trim("a", "")) |
1342 call assert_equal("", trim("", "a")) | 1391 call assert_equal("", trim("", "a")) |
1343 | 1392 |
1344 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '') | 1393 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '') |
1345 call assert_equal("x", trim(chars . "x" . chars)) | 1394 call assert_equal("x", trim(chars . "x" . chars)) |
1395 | |
1396 call assert_fails('let c=trim([])', 'E730:') | |
1346 endfunc | 1397 endfunc |
1347 | 1398 |
1348 " Test for reg_recording() and reg_executing() | 1399 " Test for reg_recording() and reg_executing() |
1349 func Test_reg_executing_and_recording() | 1400 func Test_reg_executing_and_recording() |
1350 let s:reg_stat = '' | 1401 let s:reg_stat = '' |
1724 endfunc | 1775 endfunc |
1725 | 1776 |
1726 func Test_char2nr() | 1777 func Test_char2nr() |
1727 call assert_equal(12354, char2nr('あ', 1)) | 1778 call assert_equal(12354, char2nr('あ', 1)) |
1728 call assert_equal(120, 'x'->char2nr()) | 1779 call assert_equal(120, 'x'->char2nr()) |
1780 set encoding=latin1 | |
1781 call assert_equal(120, 'x'->char2nr()) | |
1782 set encoding=utf-8 | |
1729 endfunc | 1783 endfunc |
1730 | 1784 |
1731 func Test_eventhandler() | 1785 func Test_eventhandler() |
1732 call assert_equal(0, eventhandler()) | 1786 call assert_equal(0, eventhandler()) |
1733 endfunc | 1787 endfunc |
1929 call assert_equal(4, get(range(1, 10), 3)) | 1983 call assert_equal(4, get(range(1, 10), 3)) |
1930 call assert_equal(-1, get(range(1, 10), 42, -1)) | 1984 call assert_equal(-1, get(range(1, 10), 42, -1)) |
1931 | 1985 |
1932 " index() | 1986 " index() |
1933 call assert_equal(1, index(range(1, 5), 2)) | 1987 call assert_equal(1, index(range(1, 5), 2)) |
1988 call assert_fails("echo index([1, 2], 1, [])", 'E745:') | |
1934 | 1989 |
1935 " inputlist() | 1990 " inputlist() |
1936 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x') | 1991 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x') |
1937 call assert_equal(1, result) | 1992 call assert_equal(1, result) |
1938 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x') | 1993 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x') |
2087 " type() | 2142 " type() |
2088 call assert_equal(v:t_list, type(range(5))) | 2143 call assert_equal(v:t_list, type(range(5))) |
2089 | 2144 |
2090 " uniq() | 2145 " uniq() |
2091 call assert_equal([0, 1, 2, 3, 4], uniq(range(5))) | 2146 call assert_equal([0, 1, 2, 3, 4], uniq(range(5))) |
2147 | |
2148 " errors | |
2149 call assert_fails('let x=range(2, 8, 0)', 'E726:') | |
2150 call assert_fails('let x=range(3, 1)', 'E727:') | |
2151 call assert_fails('let x=range(1, 3, -2)', 'E727:') | |
2092 endfunc | 2152 endfunc |
2093 | 2153 |
2094 func Test_echoraw() | 2154 func Test_echoraw() |
2095 CheckScreendump | 2155 CheckScreendump |
2096 | 2156 |
2105 " clean up | 2165 " clean up |
2106 call StopVimInTerminal(buf) | 2166 call StopVimInTerminal(buf) |
2107 call delete('XTest_echoraw') | 2167 call delete('XTest_echoraw') |
2108 endfunc | 2168 endfunc |
2109 | 2169 |
2170 " Test for the eval() function | |
2171 func Test_eval() | |
2172 call assert_fails("call eval('5 a')", 'E488:') | |
2173 endfunc | |
2174 | |
2175 " Test for the nr2char() function | |
2176 func Test_nr2char() | |
2177 set encoding=latin1 | |
2178 call assert_equal('@', nr2char(64)) | |
2179 set encoding=utf8 | |
2180 call assert_equal('a', nr2char(97, 1)) | |
2181 call assert_equal('a', nr2char(97, 0)) | |
2182 endfunc | |
2183 | |
2184 " Test for screenattr(), screenchar() and screenchars() functions | |
2185 func Test_screen_functions() | |
2186 call assert_equal(-1, screenattr(-1, -1)) | |
2187 call assert_equal(-1, screenchar(-1, -1)) | |
2188 call assert_equal([], screenchars(-1, -1)) | |
2189 endfunc | |
2190 | |
2110 " vim: shiftwidth=2 sts=2 expandtab | 2191 " vim: shiftwidth=2 sts=2 expandtab |