comparison src/testdir/test_vim9_expr.vim @ 23551:1bb7fa4f9b35 v8.2.2318

patch 8.2.2318: Vim9: string and list index work differently Commit: https://github.com/vim/vim/commit/e7525c552060dd04aacdbca6bb5fe6460cf4da60 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jan 9 13:20:37 2021 +0100 patch 8.2.2318: Vim9: string and list index work differently Problem: Vim9: string and list index work differently. Solution: Make string index work like list index. (closes https://github.com/vim/vim/issues/7643)
author Bram Moolenaar <Bram@vim.org>
date Sat, 09 Jan 2021 13:30:04 +0100
parents 54ec7c8b7459
children 0f7bb6f706f0
comparison
equal deleted inserted replaced
23550:33435ed56bf8 23551:1bb7fa4f9b35
2302 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 2302 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1])
2303 2303
2304 # string is permissive, index out of range accepted 2304 # string is permissive, index out of range accepted
2305 g:teststring = 'abcdef' 2305 g:teststring = 'abcdef'
2306 assert_equal('b', g:teststring[1]) 2306 assert_equal('b', g:teststring[1])
2307 assert_equal('', g:teststring[-1]) 2307 assert_equal('f', g:teststring[-1])
2308 assert_equal('', g:teststring[99]) 2308 assert_equal('', g:teststring[99])
2309 2309
2310 assert_equal('b', g:teststring[1 : 1]) 2310 assert_equal('b', g:teststring[1 : 1])
2311 assert_equal('bcdef', g:teststring[1 :]) 2311 assert_equal('bcdef', g:teststring[1 :])
2312 assert_equal('abcd', g:teststring[: 3]) 2312 assert_equal('abcd', g:teststring[: 3])
2366 CheckDefExecFailure(['echo g:testblob[2]'], 'E979:', 1) 2366 CheckDefExecFailure(['echo g:testblob[2]'], 'E979:', 1)
2367 CheckScriptFailure(['vim9script', 'echo g:testblob[2]'], 'E979:', 2) 2367 CheckScriptFailure(['vim9script', 'echo g:testblob[2]'], 'E979:', 2)
2368 CheckDefExecFailure(['echo g:testblob[-3]'], 'E979:', 1) 2368 CheckDefExecFailure(['echo g:testblob[-3]'], 'E979:', 1)
2369 CheckScriptFailure(['vim9script', 'echo g:testblob[-3]'], 'E979:', 2) 2369 CheckScriptFailure(['vim9script', 'echo g:testblob[-3]'], 'E979:', 2)
2370 2370
2371 CheckDefExecFailure(['echo g:testlist[4]'], 'E684:', 1) 2371 CheckDefExecFailure(['echo g:testlist[4]'], 'E684: list index out of range: 4', 1)
2372 CheckScriptFailure(['vim9script', 'echo g:testlist[4]'], 'E684:', 2) 2372 CheckScriptFailure(['vim9script', 'echo g:testlist[4]'], 'E684:', 2)
2373 CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1) 2373 CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1)
2374 CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684:', 2) 2374 CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684: list index out of range: -5', 2)
2375 2375
2376 CheckDefExecFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1) 2376 CheckDefExecFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1)
2377 CheckScriptFailure(['vim9script', 'echo g:testdict["a" : "b"]'], 'E719:', 2) 2377 CheckScriptFailure(['vim9script', 'echo g:testdict["a" : "b"]'], 'E719:', 2)
2378 CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1) 2378 CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1)
2379 CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2) 2379 CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2)
2800 enddef 2800 enddef
2801 2801
2802 def Test_expr7_string_subscript() 2802 def Test_expr7_string_subscript()
2803 var lines =<< trim END 2803 var lines =<< trim END
2804 var text = 'abcdef' 2804 var text = 'abcdef'
2805 assert_equal('', text[-1]) 2805 assert_equal('f', text[-1])
2806 assert_equal('a', text[0]) 2806 assert_equal('a', text[0])
2807 assert_equal('e', text[4]) 2807 assert_equal('e', text[4])
2808 assert_equal('f', text[5]) 2808 assert_equal('f', text[5])
2809 assert_equal('', text[6]) 2809 assert_equal('', text[6])
2810 2810
2811 text = 'ábçdë'
2812 assert_equal('ë', text[-1])
2813 assert_equal('d', text[-2])
2814 assert_equal('ç', text[-3])
2815 assert_equal('b', text[-4])
2816 assert_equal('á', text[-5])
2817 assert_equal('', text[-6])
2818
2811 text = 'ábçdëf' 2819 text = 'ábçdëf'
2812 assert_equal('', text[-999]) 2820 assert_equal('', text[-999])
2813 assert_equal('', text[-1]) 2821 assert_equal('f', text[-1])
2814 assert_equal('á', text[0]) 2822 assert_equal('á', text[0])
2815 assert_equal('b', text[1]) 2823 assert_equal('b', text[1])
2816 assert_equal('ç', text[2]) 2824 assert_equal('ç', text[2])
2817 assert_equal('d', text[3]) 2825 assert_equal('d', text[3])
2818 assert_equal('ë', text[4]) 2826 assert_equal('ë', text[4])
2902 assert_equal([0, 1, 2], list[0 : -3]) 2910 assert_equal([0, 1, 2], list[0 : -3])
2903 assert_equal([0], list[0 : -5]) 2911 assert_equal([0], list[0 : -5])
2904 assert_equal([], list[0 : -6]) 2912 assert_equal([], list[0 : -6])
2905 assert_equal([], list[0 : -99]) 2913 assert_equal([], list[0 : -99])
2906 END 2914 END
2907 CheckDefSuccess(lines) 2915 CheckDefAndScriptSuccess(lines)
2908 CheckScriptSuccess(['vim9script'] + lines)
2909 2916
2910 lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 2917 lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]']
2911 CheckDefExecFailure(lines, 'E1012:') 2918 CheckDefExecFailure(lines, 'E1012:')
2912 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 3) 2919 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 3)
2913 enddef 2920 enddef