comparison src/testdir/test_vim9_script.vim @ 21353:fb8c8fcb7b60 v8.2.1227

patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing Commit: https://github.com/vim/vim/commit/f5be8cdb77786f93c23237d7d8162feca92067e2 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jul 17 20:36:00 2020 +0200 patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing Problem: Vim9: allowing both quoted and # comments is confusing. Solution: Only support # comments in Vim9 script.
author Bram Moolenaar <Bram@vim.org>
date Fri, 17 Jul 2020 20:45:06 +0200
parents 6a4806e326dd
children e3711ce8133b
comparison
equal deleted inserted replaced
21352:92c30752e9d2 21353:fb8c8fcb7b60
111 111
112 def Test_vim9_single_char_vars() 112 def Test_vim9_single_char_vars()
113 let lines =<< trim END 113 let lines =<< trim END
114 vim9script 114 vim9script
115 115
116 " single character variable declarations work 116 # single character variable declarations work
117 let a: string 117 let a: string
118 let b: number 118 let b: number
119 let l: list<any> 119 let l: list<any>
120 let s: string 120 let s: string
121 let t: number 121 let t: number
122 let v: number 122 let v: number
123 let w: number 123 let w: number
124 124
125 " script-local variables can be used without s: prefix 125 # script-local variables can be used without s: prefix
126 a = 'script-a' 126 a = 'script-a'
127 b = 111 127 b = 111
128 l = [1, 2, 3] 128 l = [1, 2, 3]
129 s = 'script-s' 129 s = 'script-s'
130 t = 222 130 t = 222
173 let dict2: dict<number> = #{one: 1, two: 2} 173 let dict2: dict<number> = #{one: 1, two: 2}
174 let dict3: dict<string> = #{key: 'value'} 174 let dict3: dict<string> = #{key: 'value'}
175 let dict4: dict<any> = #{one: 1, two: '2'} 175 let dict4: dict<any> = #{one: 1, two: '2'}
176 let dict5: dict<blob> = #{one: 0z01, two: 0z02} 176 let dict5: dict<blob> = #{one: 0z01, two: 0z02}
177 177
178 " overwrite 178 # overwrite
179 dict3['key'] = 'another' 179 dict3['key'] = 'another'
180 180
181 call CheckDefExecFailure(['let dd = {}', 'dd[""] = 6'], 'E713:') 181 call CheckDefExecFailure(['let dd = {}', 'dd[""] = 6'], 'E713:')
182 182
183 # type becomes dict<any> 183 # type becomes dict<any>
184 let somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'} 184 let somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'}
185 enddef 185 enddef
186 186
187 def Test_assignment_local() 187 def Test_assignment_local()
188 " Test in a separated file in order not to the current buffer/window/tab is 188 # Test in a separated file in order not to the current buffer/window/tab is
189 " changed. 189 # changed.
190 let script_lines: list<string> =<< trim END 190 let script_lines: list<string> =<< trim END
191 let b:existing = 'yes' 191 let b:existing = 'yes'
192 let w:existing = 'yes' 192 let w:existing = 'yes'
193 let t:existing = 'yes' 193 let t:existing = 'yes'
194 194
257 257
258 let thechannel: channel 258 let thechannel: channel
259 assert_equal(test_null_channel(), thechannel) 259 assert_equal(test_null_channel(), thechannel)
260 260
261 if has('unix') && executable('cat') 261 if has('unix') && executable('cat')
262 " check with non-null job and channel, types must match 262 # check with non-null job and channel, types must match
263 thejob = job_start("cat ", #{}) 263 thejob = job_start("cat ", #{})
264 thechannel = job_getchannel(thejob) 264 thechannel = job_getchannel(thejob)
265 job_stop(thejob, 'kill') 265 job_stop(thejob, 'kill')
266 endif 266 endif
267 endif 267 endif
392 unlet $ENVVAR 392 unlet $ENVVAR
393 assert_equal('', $ENVVAR) 393 assert_equal('', $ENVVAR)
394 enddef 394 enddef
395 395
396 def Test_delfunction() 396 def Test_delfunction()
397 " Check function is defined in script namespace 397 # Check function is defined in script namespace
398 CheckScriptSuccess([ 398 CheckScriptSuccess([
399 'vim9script', 399 'vim9script',
400 'func CheckMe()', 400 'func CheckMe()',
401 ' return 123', 401 ' return 123',
402 'endfunc', 402 'endfunc',
403 'assert_equal(123, s:CheckMe())', 403 'assert_equal(123, s:CheckMe())',
404 ]) 404 ])
405 405
406 " Check function in script namespace cannot be deleted 406 # Check function in script namespace cannot be deleted
407 CheckScriptFailure([ 407 CheckScriptFailure([
408 'vim9script', 408 'vim9script',
409 'func DeleteMe1()', 409 'func DeleteMe1()',
410 'endfunc', 410 'endfunc',
411 'delfunction DeleteMe1', 411 'delfunction DeleteMe1',
584 call CheckDefFailure(['throw'], 'E1015:') 584 call CheckDefFailure(['throw'], 'E1015:')
585 call CheckDefFailure(['throw xxx'], 'E1001:') 585 call CheckDefFailure(['throw xxx'], 'E1001:')
586 enddef 586 enddef
587 587
588 def Test_throw_vimscript() 588 def Test_throw_vimscript()
589 " only checks line continuation 589 # only checks line continuation
590 let lines =<< trim END 590 let lines =<< trim END
591 vim9script 591 vim9script
592 try 592 try
593 throw 'one' 593 throw 'one'
594 .. 'two' 594 .. 'two'
598 END 598 END
599 CheckScriptSuccess(lines) 599 CheckScriptSuccess(lines)
600 enddef 600 enddef
601 601
602 def Test_cexpr_vimscript() 602 def Test_cexpr_vimscript()
603 " only checks line continuation 603 # only checks line continuation
604 set errorformat=File\ %f\ line\ %l 604 set errorformat=File\ %f\ line\ %l
605 let lines =<< trim END 605 let lines =<< trim END
606 vim9script 606 vim9script
607 cexpr 'File' 607 cexpr 'File'
608 .. ' someFile' .. 608 .. ' someFile' ..
725 enddef 725 enddef
726 ImportInDef() 726 ImportInDef()
727 END 727 END
728 writefile(import_in_def_lines, 'Ximport2.vim') 728 writefile(import_in_def_lines, 'Ximport2.vim')
729 source Ximport2.vim 729 source Ximport2.vim
730 " TODO: this should be 9879 730 # TODO: this should be 9879
731 assert_equal(9876, g:imported) 731 assert_equal(9876, g:imported)
732 assert_equal(9883, g:imported_added) 732 assert_equal(9883, g:imported_added)
733 unlet g:imported 733 unlet g:imported
734 unlet g:imported_added 734 unlet g:imported_added
735 delete('Ximport2.vim') 735 delete('Ximport2.vim')
800 import * from './Xexport.vim' 800 import * from './Xexport.vim'
801 END 801 END
802 writefile(import_star_lines, 'Ximport.vim') 802 writefile(import_star_lines, 'Ximport.vim')
803 assert_fails('source Ximport.vim', 'E1045:') 803 assert_fails('source Ximport.vim', 'E1045:')
804 804
805 " try to import something that exists but is not exported 805 # try to import something that exists but is not exported
806 let import_not_exported_lines =<< trim END 806 let import_not_exported_lines =<< trim END
807 vim9script 807 vim9script
808 import name from './Xexport.vim' 808 import name from './Xexport.vim'
809 END 809 END
810 writefile(import_not_exported_lines, 'Ximport.vim') 810 writefile(import_not_exported_lines, 'Ximport.vim')
811 assert_fails('source Ximport.vim', 'E1049:') 811 assert_fails('source Ximport.vim', 'E1049:')
812 812
813 " try to import something that is already defined 813 # try to import something that is already defined
814 let import_already_defined =<< trim END 814 let import_already_defined =<< trim END
815 vim9script 815 vim9script
816 let exported = 'something' 816 let exported = 'something'
817 import exported from './Xexport.vim' 817 import exported from './Xexport.vim'
818 END 818 END
819 writefile(import_already_defined, 'Ximport.vim') 819 writefile(import_already_defined, 'Ximport.vim')
820 assert_fails('source Ximport.vim', 'E1073:') 820 assert_fails('source Ximport.vim', 'E1073:')
821 821
822 " try to import something that is already defined 822 # try to import something that is already defined
823 import_already_defined =<< trim END 823 import_already_defined =<< trim END
824 vim9script 824 vim9script
825 let exported = 'something' 825 let exported = 'something'
826 import * as exported from './Xexport.vim' 826 import * as exported from './Xexport.vim'
827 END 827 END
828 writefile(import_already_defined, 'Ximport.vim') 828 writefile(import_already_defined, 'Ximport.vim')
829 assert_fails('source Ximport.vim', 'E1073:') 829 assert_fails('source Ximport.vim', 'E1073:')
830 830
831 " try to import something that is already defined 831 # try to import something that is already defined
832 import_already_defined =<< trim END 832 import_already_defined =<< trim END
833 vim9script 833 vim9script
834 let exported = 'something' 834 let exported = 'something'
835 import {exported} from './Xexport.vim' 835 import {exported} from './Xexport.vim'
836 END 836 END
837 writefile(import_already_defined, 'Ximport.vim') 837 writefile(import_already_defined, 'Ximport.vim')
838 assert_fails('source Ximport.vim', 'E1073:') 838 assert_fails('source Ximport.vim', 'E1073:')
839 839
840 " import a very long name, requires making a copy 840 # import a very long name, requires making a copy
841 let import_long_name_lines =<< trim END 841 let import_long_name_lines =<< trim END
842 vim9script 842 vim9script
843 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim' 843 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
844 END 844 END
845 writefile(import_long_name_lines, 'Ximport.vim') 845 writefile(import_long_name_lines, 'Ximport.vim')
875 875
876 delete('Ximport.vim') 876 delete('Ximport.vim')
877 delete('Ximport3.vim') 877 delete('Ximport3.vim')
878 delete('Xexport.vim') 878 delete('Xexport.vim')
879 879
880 " Check that in a Vim9 script 'cpo' is set to the Vim default. 880 # Check that in a Vim9 script 'cpo' is set to the Vim default.
881 set cpo&vi 881 set cpo&vi
882 let cpo_before = &cpo 882 let cpo_before = &cpo
883 let lines =<< trim END 883 let lines =<< trim END
884 vim9script 884 vim9script
885 g:cpo_in_vim9script = &cpo 885 g:cpo_in_vim9script = &cpo
960 TheFunc() 960 TheFunc()
961 END 961 END
962 writefile(testlines, 'Ximport.vim') 962 writefile(testlines, 'Ximport.vim')
963 source Ximport.vim 963 source Ximport.vim
964 964
965 " Test that when not using "morelines" GetValtwo() and valtwo are still 965 # Test that when not using "morelines" GetValtwo() and valtwo are still
966 " defined, because import doesn't reload a script. 966 # defined, because import doesn't reload a script.
967 writefile(lines, 'Xreload.vim') 967 writefile(lines, 'Xreload.vim')
968 source Ximport.vim 968 source Ximport.vim
969 969
970 " cannot declare a var twice 970 # cannot declare a var twice
971 lines =<< trim END 971 lines =<< trim END
972 vim9script 972 vim9script
973 let valone = 1234 973 let valone = 1234
974 let valone = 5678 974 let valone = 5678
975 END 975 END
1183 writefile(import_lines, 'Ximport.vim') 1183 writefile(import_lines, 'Ximport.vim')
1184 1184
1185 try 1185 try
1186 source Ximport.vim 1186 source Ximport.vim
1187 catch /E1001/ 1187 catch /E1001/
1188 " Error should be fore the Xexported.vim file. 1188 # Error should be fore the Xexported.vim file.
1189 assert_match('E1001: variable not found: notDefined', v:exception) 1189 assert_match('E1001: variable not found: notDefined', v:exception)
1190 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint) 1190 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1191 endtry 1191 endtry
1192 1192
1193 delete('Xexported.vim') 1193 delete('Xexported.vim')
1194 delete('Ximport.vim') 1194 delete('Ximport.vim')
1195 enddef 1195 enddef
1196 1196
1197 def Test_fixed_size_list() 1197 def Test_fixed_size_list()
1198 " will be allocated as one piece of memory, check that changes work 1198 # will be allocated as one piece of memory, check that changes work
1199 let l = [1, 2, 3, 4] 1199 let l = [1, 2, 3, 4]
1200 l->remove(0) 1200 l->remove(0)
1201 l->add(5) 1201 l->add(5)
1202 l->insert(99, 1) 1202 l->insert(99, 1)
1203 assert_equal([2, 99, 3, 4, 5], l) 1203 assert_equal([2, 99, 3, 4, 5], l)
1355 1355
1356 def RunNested(i: number): number 1356 def RunNested(i: number): number
1357 let x: number = 0 1357 let x: number = 0
1358 if i % 2 1358 if i % 2
1359 if 1 1359 if 1
1360 " comment 1360 # comment
1361 else 1361 else
1362 " comment 1362 # comment
1363 endif 1363 endif
1364 x += 1 1364 x += 1
1365 else 1365 else
1366 x += 1000 1366 x += 1000
1367 endif 1367 endif
1399 call CheckDefFailure(['execute xxx'], 'E1001:') 1399 call CheckDefFailure(['execute xxx'], 'E1001:')
1400 call CheckDefFailure(['execute "cmd"# comment'], 'E488:') 1400 call CheckDefFailure(['execute "cmd"# comment'], 'E488:')
1401 enddef 1401 enddef
1402 1402
1403 def Test_execute_cmd_vimscript() 1403 def Test_execute_cmd_vimscript()
1404 " only checks line continuation 1404 # only checks line continuation
1405 let lines =<< trim END 1405 let lines =<< trim END
1406 vim9script 1406 vim9script
1407 execute 'g:someVar' 1407 execute 'g:someVar'
1408 .. ' = ' .. 1408 .. ' = ' ..
1409 '28' 1409 '28'
1439 1439
1440 call CheckDefFailure(['echomsg "xxx"# comment'], 'E488:') 1440 call CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
1441 enddef 1441 enddef
1442 1442
1443 def Test_echomsg_cmd_vimscript() 1443 def Test_echomsg_cmd_vimscript()
1444 " only checks line continuation 1444 # only checks line continuation
1445 let lines =<< trim END 1445 let lines =<< trim END
1446 vim9script 1446 vim9script
1447 echomsg 'here' 1447 echomsg 'here'
1448 .. ' is ' .. 1448 .. ' is ' ..
1449 'a message' 1449 'a message'
1459 assert_match('something wrong', v:exception) 1459 assert_match('something wrong', v:exception)
1460 endtry 1460 endtry
1461 enddef 1461 enddef
1462 1462
1463 def Test_echoerr_cmd_vimscript() 1463 def Test_echoerr_cmd_vimscript()
1464 " only checks line continuation 1464 # only checks line continuation
1465 let lines =<< trim END 1465 let lines =<< trim END
1466 vim9script 1466 vim9script
1467 try 1467 try
1468 echoerr 'this' 1468 echoerr 'this'
1469 .. ' is ' .. 1469 .. ' is ' ..
1567 def Test_automatic_line_continuation() 1567 def Test_automatic_line_continuation()
1568 let mylist = [ 1568 let mylist = [
1569 'one', 1569 'one',
1570 'two', 1570 'two',
1571 'three', 1571 'three',
1572 ] " comment 1572 ] # comment
1573 assert_equal(['one', 'two', 'three'], mylist) 1573 assert_equal(['one', 'two', 'three'], mylist)
1574 1574
1575 let mydict = { 1575 let mydict = {
1576 'one': 1, 1576 'one': 1,
1577 'two': 2, 1577 'two': 2,
1578 'three': 1578 'three':
1579 3, 1579 3,
1580 } " comment 1580 } # comment
1581 assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict) 1581 assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict)
1582 mydict = #{ 1582 mydict = #{
1583 one: 1, # comment 1583 one: 1, # comment
1584 two: # comment 1584 two: # comment
1585 2, # comment 1585 2, # comment
1752 CheckScriptSuccess([ 1752 CheckScriptSuccess([
1753 'vim9script', 1753 'vim9script',
1754 'hi clear This # comment', 1754 'hi clear This # comment',
1755 'hi clear # comment', 1755 'hi clear # comment',
1756 ]) 1756 ])
1757 " not tested, because it doesn't give an error but a warning: 1757 # not tested, because it doesn't give an error but a warning:
1758 " hi clear This# comment', 1758 # hi clear This# comment',
1759 CheckScriptFailure([ 1759 CheckScriptFailure([
1760 'vim9script', 1760 'vim9script',
1761 'hi clear# comment', 1761 'hi clear# comment',
1762 ], 'E416:') 1762 ], 'E416:')
1763 1763
2089 ':$', 2089 ':$',
2090 'dsearch /pat/ #comment', 2090 'dsearch /pat/ #comment',
2091 'bwipe!', 2091 'bwipe!',
2092 ]) 2092 ])
2093 2093
2094 " CheckScriptFailure([ 2094 CheckScriptFailure([
2095 " 'vim9script', 2095 'vim9script',
2096 " 'new' 2096 'new'
2097 " 'call setline(1, ["# define pat", "last"])', 2097 'call setline(1, ["# define pat", "last"])',
2098 " ':$', 2098 ':$',
2099 " 'dsearch /pat/#comment', 2099 'dsearch /pat/#comment',
2100 " 'bwipe!', 2100 'bwipe!',
2101 " ], 'E488:') 2101 ], 'E488:')
2102 " 2102
2103 " CheckScriptFailure([ 2103 CheckScriptFailure([
2104 " 'vim9script', 2104 'vim9script',
2105 " 'func! SomeFunc()', 2105 'func! SomeFunc()',
2106 " ], 'E477:') 2106 ], 'E477:')
2107 enddef 2107 enddef
2108 2108
2109 def Test_finish() 2109 def Test_finish()
2110 let lines =<< trim END 2110 let lines =<< trim END
2111 vim9script 2111 vim9script
2133 let g:count = 1 2133 let g:count = 1
2134 endif 2134 endif
2135 return 'this' 2135 return 'this'
2136 endfunc 2136 endfunc
2137 let val: string = GetValue() 2137 let val: string = GetValue()
2138 " env var is always a string 2138 # env var is always a string
2139 let env = $TERM 2139 let env = $TERM
2140 END 2140 END
2141 writefile(lines, 'Xfinished') 2141 writefile(lines, 'Xfinished')
2142 source Xfinished 2142 source Xfinished
2143 " GetValue() is not called during discovery phase 2143 # GetValue() is not called during discovery phase
2144 assert_equal(1, g:count) 2144 assert_equal(1, g:count)
2145 2145
2146 unlet g:count 2146 unlet g:count
2147 delete('Xfinished') 2147 delete('Xfinished')
2148 enddef 2148 enddef
2167 vim9script 2167 vim9script
2168 let var: string 2168 let var: string
2169 g:var_uninit = var 2169 g:var_uninit = var
2170 var = 'text' 2170 var = 'text'
2171 g:var_test = var 2171 g:var_test = var
2172 " prefixing s: is optional 2172 # prefixing s: is optional
2173 s:var = 'prefixed' 2173 s:var = 'prefixed'
2174 g:var_prefixed = s:var 2174 g:var_prefixed = s:var
2175 2175
2176 let s:other: number 2176 let s:other: number
2177 other = 1234 2177 other = 1234
2279 writefile(vim9_lines, 'Xvim9_script.vim') 2279 writefile(vim9_lines, 'Xvim9_script.vim')
2280 2280
2281 source Xlegacy_script.vim 2281 source Xlegacy_script.vim
2282 2282
2283 assert_equal('global', g:global) 2283 assert_equal('global', g:global)
2284 " unlet g:global 2284 unlet g:global
2285 2285
2286 delete('Xlegacy_script.vim') 2286 delete('Xlegacy_script.vim')
2287 delete('Xvim9_script.vim') 2287 delete('Xvim9_script.vim')
2288 enddef 2288 enddef
2289 2289
2299 setline(1, 'something') 2299 setline(1, 'something')
2300 :substitute(some(other( 2300 :substitute(some(other(
2301 assert_equal('otherthing', getline(1)) 2301 assert_equal('otherthing', getline(1))
2302 bwipe! 2302 bwipe!
2303 2303
2304 " also when the context is Vim9 script 2304 # also when the context is Vim9 script
2305 let lines =<< trim END 2305 let lines =<< trim END
2306 vim9script 2306 vim9script
2307 new 2307 new
2308 setline(1, 'something') 2308 setline(1, 'something')
2309 :substitute(some(other( 2309 :substitute(some(other(