diff src/testdir/test_vim9_assign.vim @ 33730:1a0886e98382 v9.0.2094

patch 9.0.2094: Vim9: need more assignment tests Commit: https://github.com/vim/vim/commit/1fc47c2c9bf3121268ebab03a0662e0cf2e5cb26 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Wed Nov 8 21:02:48 2023 +0100 patch 9.0.2094: Vim9: need more assignment tests Problem: Vim9: need more assignment tests Solution: Add test for using different types in assignment, function arguments and return values closes: #13491 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Wed, 08 Nov 2023 21:15:04 +0100
parents 2fc593290679
children 902b7bdf2afb
line wrap: on
line diff
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -3057,4 +3057,255 @@ def Test_dict_item_assign()
   v9.CheckSourceSuccess(lines)
 enddef
 
+" Test for using various types (dict, list, blob, funcref, class) as variable
+" in assignments with a different type
+def Test_type_check()
+  var lines =<< trim END
+    vim9script
+    class A
+    endclass
+    var N: number = 1
+    var S: string = 'abc'
+    var d: dict<number> = {}
+    var l: list<number> = []
+    var b: blob = 0z10
+    var Fn: func = function('min')
+    var j: job = test_null_job()
+    var ch: channel = test_null_channel()
+    var o: A = A.new()
+
+    # Assign a number
+    assert_fails('d = N', 'E1012: Type mismatch; expected dict<number> but got number')
+    assert_fails('l = N', 'E1012: Type mismatch; expected list<number> but got number')
+    assert_fails('b = N', 'E1012: Type mismatch; expected blob but got number')
+    assert_fails('Fn = N', 'E1012: Type mismatch; expected func(...): unknown but got number')
+    assert_fails('j = N', 'E1012: Type mismatch; expected job but got number')
+    assert_fails('ch = N', 'E1012: Type mismatch; expected channel but got number')
+    assert_fails('A = N', 'E1012: Type mismatch; expected class<A> but got number')
+    assert_fails('o = N', 'E1012: Type mismatch; expected object<A> but got number')
+
+    # Use a compound operator with different LHS types
+    assert_fails('d += N', 'E734: Wrong variable type for +=')
+    assert_fails('l += N', 'E734: Wrong variable type for +=')
+    assert_fails('b += N', 'E734: Wrong variable type for +=')
+    assert_fails('Fn += N', 'E734: Wrong variable type for +=')
+    assert_fails('j += N', 'E734: Wrong variable type for +=')
+    assert_fails('ch += N', 'E734: Wrong variable type for +=')
+    assert_fails('A += N', 'E734: Wrong variable type for +=')
+    assert_fails('o += N', 'E734: Wrong variable type for +=')
+
+    # Assign to a number variable
+    assert_fails('N = d', 'E1012: Type mismatch; expected number but got dict<number>')
+    assert_fails('N = l', 'E1012: Type mismatch; expected number but got list<number>')
+    assert_fails('N = b', 'E1012: Type mismatch; expected number but got blob')
+    assert_fails('N = Fn', 'E1012: Type mismatch; expected number but got func([unknown]): number')
+    assert_fails('N = j', 'E1012: Type mismatch; expected number but got job')
+    assert_fails('N = ch', 'E1012: Type mismatch; expected number but got channel')
+    assert_fails('N = A', 'E1012: Type mismatch; expected number but got class<A>')
+    assert_fails('N = o', 'E1012: Type mismatch; expected number but got object<A>')
+
+    # Use a compound operator with different RHS types
+    assert_fails('N += d', 'E734: Wrong variable type for +=')
+    assert_fails('N += l', 'E734: Wrong variable type for +=')
+    assert_fails('N += b', 'E974: Using a Blob as a Number')
+    assert_fails('N += Fn', 'E734: Wrong variable type for +=')
+    assert_fails('N += j', 'E910: Using a Job as a Number')
+    assert_fails('N += ch', 'E913: Using a Channel as a Number')
+    assert_fails('N += A', 'E1319: Using a Class as a Number')
+    assert_fails('N += o', 'E1320: Using an Object as a Number')
+
+    # Initialize multiple variables using []
+    assert_fails('var [X1: number, Y: number] = [1, d]', 'E1012: Type mismatch; expected number but got dict<number>')
+    assert_fails('var [X2: number, Y: number] = [1, l]', 'E1012: Type mismatch; expected number but got list<number>')
+    assert_fails('var [X3: number, Y: number] = [1, b]', 'E1012: Type mismatch; expected number but got blob')
+    assert_fails('var [X4: number, Y: number] = [1, Fn]', 'E1012: Type mismatch; expected number but got func([unknown]): number')
+    assert_fails('var [X5: number, Y: number] = [1, j]', 'E1012: Type mismatch; expected number but got job')
+    assert_fails('var [X6: number, Y: number] = [1, ch]', 'E1012: Type mismatch; expected number but got channel')
+    assert_fails('var [X7: number, Y: number] = [1, A]', 'E1012: Type mismatch; expected number but got class<A>')
+    assert_fails('var [X8: number, Y: number] = [1, o]', 'E1012: Type mismatch; expected number but got object<A>')
+
+    # String concatenation with various LHS types
+    assert_fails('S ..= d', 'E734: Wrong variable type for .=')
+    assert_fails('S ..= l', 'E734: Wrong variable type for .=')
+    assert_fails('S ..= b', 'E976: Using a Blob as a String')
+    assert_fails('S ..= Fn', 'E734: Wrong variable type for .=')
+    assert_fails('S ..= j', 'E908: Using an invalid value as a String: job')
+    assert_fails('S ..= ch', 'E908: Using an invalid value as a String: channel')
+    assert_fails('S ..= A', 'E1323: Using a Class as a String')
+    assert_fails('S ..= o', 'E1324: Using an Object as a String')
+
+    # String concatenation with various RHS types
+    assert_fails('d ..= S', 'E734: Wrong variable type for .=')
+    assert_fails('l ..= S', 'E734: Wrong variable type for .=')
+    assert_fails('b ..= S', 'E734: Wrong variable type for .=')
+    assert_fails('Fn ..= S', 'E734: Wrong variable type for .=')
+    assert_fails('j ..= S', 'E734: Wrong variable type for .=')
+    assert_fails('ch ..= S', 'E734: Wrong variable type for .=')
+    assert_fails('A ..= S', 'E734: Wrong variable type for .=')
+    assert_fails('o ..= S', 'E734: Wrong variable type for .=')
+  END
+  v9.CheckSourceSuccess(lines)
+enddef
+
+" Test for checking the argument type of a def function
+def Test_func_argtype_check()
+  var lines =<< trim END
+    vim9script
+
+    # Passing different types as argument to a function expecting a number
+    def IntArg(n: number)
+    enddef
+
+    class A
+    endclass
+    var N: number = 1
+    var S: string = 'abc'
+    var d: dict<number> = {}
+    var l: list<number> = []
+    var b: blob = 0z10
+    var Fn: func = function('min')
+    var j: job = test_null_job()
+    var ch: channel = test_null_channel()
+    var o: A = A.new()
+
+    assert_fails('IntArg(d)', 'E1013: Argument 1: type mismatch, expected number but got dict<number>')
+    assert_fails('IntArg(l)', 'E1013: Argument 1: type mismatch, expected number but got list<number>')
+    assert_fails('IntArg(b)', 'E1013: Argument 1: type mismatch, expected number but got blob')
+    assert_fails('IntArg(Fn)', 'E1013: Argument 1: type mismatch, expected number but got func([unknown]): number')
+    assert_fails('IntArg(j)', 'E1013: Argument 1: type mismatch, expected number but got job')
+    assert_fails('IntArg(ch)', 'E1013: Argument 1: type mismatch, expected number but got channel')
+    assert_fails('IntArg(A)', 'E1013: Argument 1: type mismatch, expected number but got class<A>')
+    assert_fails('IntArg(o)', 'E1013: Argument 1: type mismatch, expected number but got object<A>')
+
+    # Passing a number to functions accepting different argument types
+    def DictArg(_: dict<number>)
+    enddef
+    assert_fails('DictArg(N)', 'E1013: Argument 1: type mismatch, expected dict<number> but got number')
+
+    def ListArg(_: list<number>)
+    enddef
+    assert_fails('ListArg(N)', 'E1013: Argument 1: type mismatch, expected list<number> but got number')
+
+    def BlobArg(_: blob)
+    enddef
+    assert_fails('BlobArg(N)', 'E1013: Argument 1: type mismatch, expected blob but got number')
+
+    def FuncArg(Fn_arg: func)
+    enddef
+    assert_fails('FuncArg(N)', 'E1013: Argument 1: type mismatch, expected func(...): unknown but got number')
+
+    def JobArg(_: job)
+    enddef
+    assert_fails('JobArg(N)', 'E1013: Argument 1: type mismatch, expected job but got number')
+
+    def ChannelArg(_: channel)
+    enddef
+    assert_fails('ChannelArg(N)', 'E1013: Argument 1: type mismatch, expected channel but got number')
+
+    def ObjectArg(_: A)
+    enddef
+    assert_fails('ObjectArg(N)', 'E1013: Argument 1: type mismatch, expected object<A> but got number')
+  END
+  v9.CheckSourceSuccess(lines)
+
+  # Calling a function expecting a number type with different argument types
+  # from another function
+  var pre_lines =<< trim END
+    vim9script
+    class A
+    endclass
+    def IntArg(n: number)
+    enddef
+    def Foo()
+  END
+  var post_lines =<< trim END
+    enddef
+    defcompile
+  END
+  lines = pre_lines + ['var d: dict<number> = {}', 'IntArg(d)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got dict<number>', 2)
+  lines = pre_lines + ['var l: list<number> = []', 'IntArg(l)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got list<number>', 2)
+  lines = pre_lines + ['var b: blob = 0z12', 'IntArg(b)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got blob', 2)
+  lines = pre_lines + ['var Fn: func = function("min")', 'IntArg(Fn)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got func(...): unknown', 2)
+  lines = pre_lines + ['var j: job = test_null_job()', 'IntArg(j)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got job', 2)
+  lines = pre_lines + ['var ch: channel = test_null_channel()', 'IntArg(ch)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got channel', 2)
+  lines = pre_lines + ['IntArg(A)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got class<A>', 1)
+  lines = pre_lines + ['var o: A = A.new()', 'IntArg(o)'] + post_lines
+  v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got object<A>', 2)
+enddef
+
+" Test for checking the return type of a def function
+def Test_func_rettype_check()
+  var lines =<< trim END
+    vim9script
+    def Fn(): dict<number>
+      return 10
+    enddef
+    defcompile
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected dict<number> but got number', 1)
+
+  lines =<< trim END
+    vim9script
+    def Fn(): list<number>
+      return 10
+    enddef
+    defcompile
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected list<number> but got number', 1)
+
+  lines =<< trim END
+    vim9script
+    def Fn(): blob
+      return 10
+    enddef
+    defcompile
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected blob but got number', 1)
+
+  lines =<< trim END
+    vim9script
+    def Fn(): func
+      return 10
+    enddef
+    defcompile
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(...): unknown but got number', 1)
+
+  lines =<< trim END
+    vim9script
+    def Fn(): job
+      return 10
+    enddef
+    defcompile
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected job but got number', 1)
+
+  lines =<< trim END
+    vim9script
+    def Fn(): channel
+      return 10
+    enddef
+    defcompile
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected channel but got number', 1)
+
+  lines =<< trim END
+    vim9script
+    class A
+    endclass
+    def Fn(): A
+      return 10
+    enddef
+    defcompile
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<A> but got number', 1)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker