comparison src/testdir/test_vim9_assign.vim @ 26346:8be6413a8e27 v8.2.3704

patch 8.2.3704: Vim9: cannot use a list declaration in a :def function Commit: https://github.com/vim/vim/commit/ab36e6ae7b87b0295fb19270e4339a734875c6b1 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Nov 30 16:14:49 2021 +0000 patch 8.2.3704: Vim9: cannot use a list declaration in a :def function Problem: Vim9: cannot use a list declaration in a :def function. Solution: Make it work.
author Bram Moolenaar <Bram@vim.org>
date Tue, 30 Nov 2021 17:15:03 +0100
parents 55e658312376
children f5727e2603f0
comparison
equal deleted inserted replaced
26345:1707aa6d46a2 26346:8be6413a8e27
730 730
731 list3 += ['end'] 731 list3 += ['end']
732 assert_equal(['sdf', 'asdf', 'end'], list3) 732 assert_equal(['sdf', 'asdf', 'end'], list3)
733 733
734 CheckDefExecFailure(['var ll = [1, 2, 3]', 'll[-4] = 6'], 'E684:') 734 CheckDefExecFailure(['var ll = [1, 2, 3]', 'll[-4] = 6'], 'E684:')
735 CheckDefExecFailure(['var [v1, v2] = [1, 2]'], 'E1092:')
736 735
737 # type becomes list<any> 736 # type becomes list<any>
738 var somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c'] 737 var somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c']
739 738
740 var lines =<< trim END 739 var lines =<< trim END
749 def TwoArgs(x: bool, y: bool) 748 def TwoArgs(x: bool, y: bool)
750 enddef 749 enddef
751 var fl: list<func(bool, bool, bool)> = [OneArg, TwoArgs] 750 var fl: list<func(bool, bool, bool)> = [OneArg, TwoArgs]
752 END 751 END
753 CheckDefExecAndScriptFailure(lines, 'E1012:', 5) 752 CheckDefExecAndScriptFailure(lines, 'E1012:', 5)
753 enddef
754
755 def Test_list_declaration()
756 var [v1, v2] = [1, 2]
757 v1 += 3
758 assert_equal(4, v1)
759 v2 *= 3
760 assert_equal(6, v2)
761
762 var lines =<< trim END
763 var [v1, v2] = [1]
764 END
765 CheckDefExecAndScriptFailure2(lines, 'E1093: Expected 2 items but got 1', 'E688:')
766 lines =<< trim END
767 var testlist = [1]
768 var [v1, v2] = testlist
769 END
770 CheckDefExecAndScriptFailure2(lines, 'E1093: Expected 2 items but got 1', 'E688:')
771 lines =<< trim END
772 var [v1, v2] = [1, 2, 3]
773 END
774 CheckDefExecAndScriptFailure2(lines, 'E1093: Expected 2 items but got 3', 'E687:')
775 lines =<< trim END
776 var testlist = [1, 2, 3]
777 var [v1, v2] = testlist
778 END
779 CheckDefExecAndScriptFailure2(lines, 'E1093: Expected 2 items but got 3', 'E687:')
780
781 var [vnr, vstr] = [123, 'text']
782 vnr += 3
783 assert_equal(126, vnr)
784 vstr ..= 'end'
785 assert_equal('textend', vstr)
786
787 var [vnr2: number, vstr2: string] = [123, 'text']
788 vnr2 += 3
789 assert_equal(126, vnr2)
790 vstr2 ..= 'end'
791 assert_equal('textend', vstr2)
792
793 var [vnr3: number; vlist: list<string>] = [123, 'foo', 'bar']
794 vnr3 += 5
795 assert_equal(128, vnr3)
796 assert_equal(['foo', 'bar'], vlist)
797
798 lines =<< trim END
799 var [vnr2: number, vstr2: number] = [123, 'text']
800 END
801 CheckDefExecAndScriptFailure2(lines, 'E1163: Variable 2: type mismatch, expected number but got string', 'E1012: Type mismatch; expected number but got string')
802 lines =<< trim END
803 var testlist = [234, 'text']
804 var [vnr2: number, vstr2: number] = testlist
805 END
806 CheckDefExecAndScriptFailure2(lines, 'E1163: Variable 2: type mismatch, expected number but got string', 'E1012: Type mismatch; expected number but got string')
754 enddef 807 enddef
755 808
756 def PartFuncBool(b: bool): string 809 def PartFuncBool(b: bool): string
757 return 'done' 810 return 'done'
758 enddef 811 enddef