comparison src/testdir/test_substitute.vim @ 19969:b07672d13ff9 v8.2.0540

patch 8.2.0540: regexp and other code not tested Commit: https://github.com/vim/vim/commit/004a6781b3cf15ca5dd632c38cc09bb3b253d1f8 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Apr 11 17:09:31 2020 +0200 patch 8.2.0540: regexp and other code not tested Problem: Regexp and other code not tested. Solution: Add more tests. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/5904)
author Bram Moolenaar <Bram@vim.org>
date Sat, 11 Apr 2020 17:15:06 +0200
parents 12518b40c161
children 6a4806e326dd
comparison
equal deleted inserted replaced
19968:1908e92b02fd 19969:b07672d13ff9
280 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", '')) 280 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
281 call assert_equal("w\\w", substitute('wWw', 'W', "\\", '')) 281 call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
282 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", '')) 282 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
283 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', '')) 283 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
284 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', '')) 284 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
285 " \v or \V after $
286 call assert_equal('abxx', substitute('abcd', 'xy$\v|cd$', 'xx', ''))
287 call assert_equal('abxx', substitute('abcd', 'xy$\V\|cd\$', 'xx', ''))
285 endfunc 288 endfunc
286 289
287 func Test_sub_replace_2() 290 func Test_sub_replace_2()
288 " Run the tests with 'magic' off 291 " Run the tests with 'magic' off
289 set nomagic 292 set nomagic
840 call delete('Xresult') 843 call delete('Xresult')
841 endfunc 844 endfunc
842 845
843 func Test_substitute() 846 func Test_substitute()
844 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g')) 847 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
848 " Substitute with special keys
849 call assert_equal("a\<End>c", substitute('abc', "a.c", "a\<End>c", ''))
850 endfunc
851
852 func Test_substitute_expr()
853 let g:val = 'XXX'
854 call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', ''))
855 call assert_equal('XXX', substitute('yyy', 'y*', {-> g:val}, ''))
856 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
857 \ '\=nr2char("0x" . submatch(1))', 'g'))
858 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
859 \ {-> nr2char("0x" . submatch(1))}, 'g'))
860
861 call assert_equal('231', substitute('123', '\(.\)\(.\)\(.\)',
862 \ {-> submatch(2) . submatch(3) . submatch(1)}, ''))
863
864 func Recurse()
865 return substitute('yyy', 'y\(.\)y', {-> submatch(1)}, '')
866 endfunc
867 " recursive call works
868 call assert_equal('-y-x-', substitute('xxx', 'x\(.\)x', {-> '-' . Recurse() . '-' . submatch(1) . '-'}, ''))
869
870 call assert_fails("let s=submatch([])", 'E745:')
871 call assert_fails("let s=submatch(2, [])", 'E745:')
872 endfunc
873
874 func Test_invalid_submatch()
875 " This was causing invalid memory access in Vim-7.4.2232 and older
876 call assert_fails("call substitute('x', '.', {-> submatch(10)}, '')", 'E935:')
877 call assert_fails('eval submatch(-1)', 'E935:')
878 call assert_equal('', submatch(0))
879 call assert_equal('', submatch(1))
880 call assert_equal([], submatch(0, 1))
881 call assert_equal([], submatch(1, 1))
882 endfunc
883
884 func Test_substitute_expr_arg()
885 call assert_equal('123456789-123456789=', substitute('123456789',
886 \ '\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
887 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
888
889 call assert_equal('123456-123456=789', substitute('123456789',
890 \ '\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)',
891 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
892
893 call assert_equal('123456789-123456789x=', substitute('123456789',
894 \ '\(.\)\(.\)\(.*\)',
895 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . 'x' . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
896
897 call assert_fails("call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')", 'E742:')
898 call assert_fails("call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')", 'E742:')
899 call assert_fails("call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')", 'E742:')
900 call assert_fails("call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')", 'E742:')
901 endfunc
902
903 " Test for using a function to supply the substitute string
904 func Test_substitute_using_func()
905 func Xfunc()
906 return '1234'
907 endfunc
908 call assert_equal('a1234f', substitute('abcdef', 'b..e',
909 \ function("Xfunc"), ''))
910 delfunc Xfunc
911 endfunc
912
913 " Test for using submatch() with a multiline match
914 func Test_substitute_multiline_submatch()
915 new
916 call setline(1, ['line1', 'line2', 'line3', 'line4'])
917 %s/^line1\(\_.\+\)line4$/\=submatch(1)/
918 call assert_equal(['', 'line2', 'line3', ''], getline(1, '$'))
919 close!
845 endfunc 920 endfunc
846 921
847 " vim: shiftwidth=2 sts=2 expandtab 922 " vim: shiftwidth=2 sts=2 expandtab