comparison src/testdir/test_vim9_class.vim @ 33381:17301c641749 v9.0.1949

patch 9.0.1949: Vim9: allows reserved keywords as members Commit: https://github.com/vim/vim/commit/f057aca1cc2480e820b3ca5d8d407e3976369777 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Thu Sep 28 22:28:15 2023 +0200 patch 9.0.1949: Vim9: allows reserved keywords as members Problem: Vim9: allows reserved keywords as members Solution: Disallow reserved keywords, disallow duplicate object and class variables closes: #13209 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Thu, 28 Sep 2023 22:45:04 +0200
parents 7c9124711f99
children b5ad84fdc702
comparison
equal deleted inserted replaced
33380:7ba7c38a9914 33381:17301c641749
4148 endclass 4148 endclass
4149 var c = C.new() 4149 var c = C.new()
4150 assert_equal(10, C.val) 4150 assert_equal(10, C.val)
4151 assert_equal(20, c.val) 4151 assert_equal(20, c.val)
4152 END 4152 END
4153 v9.CheckSourceSuccess(lines) 4153 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4)
4154 4154
4155 # Duplicate object member variable in a derived class 4155 # Duplicate object member variable in a derived class
4156 lines =<< trim END 4156 lines =<< trim END
4157 vim9script 4157 vim9script
4158 class A 4158 class A
5766 Test_GetResult() 5766 Test_GetResult()
5767 END 5767 END
5768 v9.CheckSourceSuccess(lines) 5768 v9.CheckSourceSuccess(lines)
5769 enddef 5769 enddef
5770 5770
5771 " Test for duplicate object and class variable
5772 def Test_duplicate_variable()
5773 # Object variable name is same as the class variable name
5774 var lines =<< trim END
5775 vim9script
5776 class A
5777 public static sval: number
5778 public this.sval: number
5779 endclass
5780 var a = A.new()
5781 END
5782 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: sval', 4)
5783
5784 # Duplicate variable name and calling a class method
5785 lines =<< trim END
5786 vim9script
5787 class A
5788 public static sval: number
5789 public this.sval: number
5790 def F1()
5791 echo this.sval
5792 enddef
5793 static def F2()
5794 echo sval
5795 enddef
5796 endclass
5797 A.F2()
5798 END
5799 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: sval', 4)
5800
5801 # Duplicate variable with an empty constructor
5802 lines =<< trim END
5803 vim9script
5804 class A
5805 public static sval: number
5806 public this.sval: number
5807 def new()
5808 enddef
5809 endclass
5810 var a = A.new()
5811 END
5812 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: sval', 4)
5813 enddef
5814
5815 " Test for using a reserved keyword as a variable name
5816 def Test_reserved_varname()
5817 for kword in ['true', 'false', 'null', 'null_blob', 'null_dict',
5818 'null_function', 'null_list', 'null_partial', 'null_string',
5819 'null_channel', 'null_job', 'super', 'this']
5820
5821 var lines =<< trim eval END
5822 vim9script
5823 class C
5824 public this.{kword}: list<number> = [1, 2, 3]
5825 endclass
5826 var o = C.new()
5827 END
5828 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)
5829
5830 lines =<< trim eval END
5831 vim9script
5832 class C
5833 public this.{kword}: list<number> = [1, 2, 3]
5834 def new()
5835 enddef
5836 endclass
5837 var o = C.new()
5838 END
5839 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)
5840
5841 lines =<< trim eval END
5842 vim9script
5843 class C
5844 public this.{kword}: list<number> = [1, 2, 3]
5845 def new()
5846 enddef
5847 def F()
5848 echo this.{kword}
5849 enddef
5850 endclass
5851 var o = C.new()
5852 o.F()
5853 END
5854 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)
5855 endfor
5856 enddef
5857
5771 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 5858 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker