Mercurial > vim
comparison 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 |
comparison
equal
deleted
inserted
replaced
33729:9a846ba607bb | 33730:1a0886e98382 |
---|---|
3055 assert_equal({a: {b: 1}}, d) | 3055 assert_equal({a: {b: 1}}, d) |
3056 END | 3056 END |
3057 v9.CheckSourceSuccess(lines) | 3057 v9.CheckSourceSuccess(lines) |
3058 enddef | 3058 enddef |
3059 | 3059 |
3060 " Test for using various types (dict, list, blob, funcref, class) as variable | |
3061 " in assignments with a different type | |
3062 def Test_type_check() | |
3063 var lines =<< trim END | |
3064 vim9script | |
3065 class A | |
3066 endclass | |
3067 var N: number = 1 | |
3068 var S: string = 'abc' | |
3069 var d: dict<number> = {} | |
3070 var l: list<number> = [] | |
3071 var b: blob = 0z10 | |
3072 var Fn: func = function('min') | |
3073 var j: job = test_null_job() | |
3074 var ch: channel = test_null_channel() | |
3075 var o: A = A.new() | |
3076 | |
3077 # Assign a number | |
3078 assert_fails('d = N', 'E1012: Type mismatch; expected dict<number> but got number') | |
3079 assert_fails('l = N', 'E1012: Type mismatch; expected list<number> but got number') | |
3080 assert_fails('b = N', 'E1012: Type mismatch; expected blob but got number') | |
3081 assert_fails('Fn = N', 'E1012: Type mismatch; expected func(...): unknown but got number') | |
3082 assert_fails('j = N', 'E1012: Type mismatch; expected job but got number') | |
3083 assert_fails('ch = N', 'E1012: Type mismatch; expected channel but got number') | |
3084 assert_fails('A = N', 'E1012: Type mismatch; expected class<A> but got number') | |
3085 assert_fails('o = N', 'E1012: Type mismatch; expected object<A> but got number') | |
3086 | |
3087 # Use a compound operator with different LHS types | |
3088 assert_fails('d += N', 'E734: Wrong variable type for +=') | |
3089 assert_fails('l += N', 'E734: Wrong variable type for +=') | |
3090 assert_fails('b += N', 'E734: Wrong variable type for +=') | |
3091 assert_fails('Fn += N', 'E734: Wrong variable type for +=') | |
3092 assert_fails('j += N', 'E734: Wrong variable type for +=') | |
3093 assert_fails('ch += N', 'E734: Wrong variable type for +=') | |
3094 assert_fails('A += N', 'E734: Wrong variable type for +=') | |
3095 assert_fails('o += N', 'E734: Wrong variable type for +=') | |
3096 | |
3097 # Assign to a number variable | |
3098 assert_fails('N = d', 'E1012: Type mismatch; expected number but got dict<number>') | |
3099 assert_fails('N = l', 'E1012: Type mismatch; expected number but got list<number>') | |
3100 assert_fails('N = b', 'E1012: Type mismatch; expected number but got blob') | |
3101 assert_fails('N = Fn', 'E1012: Type mismatch; expected number but got func([unknown]): number') | |
3102 assert_fails('N = j', 'E1012: Type mismatch; expected number but got job') | |
3103 assert_fails('N = ch', 'E1012: Type mismatch; expected number but got channel') | |
3104 assert_fails('N = A', 'E1012: Type mismatch; expected number but got class<A>') | |
3105 assert_fails('N = o', 'E1012: Type mismatch; expected number but got object<A>') | |
3106 | |
3107 # Use a compound operator with different RHS types | |
3108 assert_fails('N += d', 'E734: Wrong variable type for +=') | |
3109 assert_fails('N += l', 'E734: Wrong variable type for +=') | |
3110 assert_fails('N += b', 'E974: Using a Blob as a Number') | |
3111 assert_fails('N += Fn', 'E734: Wrong variable type for +=') | |
3112 assert_fails('N += j', 'E910: Using a Job as a Number') | |
3113 assert_fails('N += ch', 'E913: Using a Channel as a Number') | |
3114 assert_fails('N += A', 'E1319: Using a Class as a Number') | |
3115 assert_fails('N += o', 'E1320: Using an Object as a Number') | |
3116 | |
3117 # Initialize multiple variables using [] | |
3118 assert_fails('var [X1: number, Y: number] = [1, d]', 'E1012: Type mismatch; expected number but got dict<number>') | |
3119 assert_fails('var [X2: number, Y: number] = [1, l]', 'E1012: Type mismatch; expected number but got list<number>') | |
3120 assert_fails('var [X3: number, Y: number] = [1, b]', 'E1012: Type mismatch; expected number but got blob') | |
3121 assert_fails('var [X4: number, Y: number] = [1, Fn]', 'E1012: Type mismatch; expected number but got func([unknown]): number') | |
3122 assert_fails('var [X5: number, Y: number] = [1, j]', 'E1012: Type mismatch; expected number but got job') | |
3123 assert_fails('var [X6: number, Y: number] = [1, ch]', 'E1012: Type mismatch; expected number but got channel') | |
3124 assert_fails('var [X7: number, Y: number] = [1, A]', 'E1012: Type mismatch; expected number but got class<A>') | |
3125 assert_fails('var [X8: number, Y: number] = [1, o]', 'E1012: Type mismatch; expected number but got object<A>') | |
3126 | |
3127 # String concatenation with various LHS types | |
3128 assert_fails('S ..= d', 'E734: Wrong variable type for .=') | |
3129 assert_fails('S ..= l', 'E734: Wrong variable type for .=') | |
3130 assert_fails('S ..= b', 'E976: Using a Blob as a String') | |
3131 assert_fails('S ..= Fn', 'E734: Wrong variable type for .=') | |
3132 assert_fails('S ..= j', 'E908: Using an invalid value as a String: job') | |
3133 assert_fails('S ..= ch', 'E908: Using an invalid value as a String: channel') | |
3134 assert_fails('S ..= A', 'E1323: Using a Class as a String') | |
3135 assert_fails('S ..= o', 'E1324: Using an Object as a String') | |
3136 | |
3137 # String concatenation with various RHS types | |
3138 assert_fails('d ..= S', 'E734: Wrong variable type for .=') | |
3139 assert_fails('l ..= S', 'E734: Wrong variable type for .=') | |
3140 assert_fails('b ..= S', 'E734: Wrong variable type for .=') | |
3141 assert_fails('Fn ..= S', 'E734: Wrong variable type for .=') | |
3142 assert_fails('j ..= S', 'E734: Wrong variable type for .=') | |
3143 assert_fails('ch ..= S', 'E734: Wrong variable type for .=') | |
3144 assert_fails('A ..= S', 'E734: Wrong variable type for .=') | |
3145 assert_fails('o ..= S', 'E734: Wrong variable type for .=') | |
3146 END | |
3147 v9.CheckSourceSuccess(lines) | |
3148 enddef | |
3149 | |
3150 " Test for checking the argument type of a def function | |
3151 def Test_func_argtype_check() | |
3152 var lines =<< trim END | |
3153 vim9script | |
3154 | |
3155 # Passing different types as argument to a function expecting a number | |
3156 def IntArg(n: number) | |
3157 enddef | |
3158 | |
3159 class A | |
3160 endclass | |
3161 var N: number = 1 | |
3162 var S: string = 'abc' | |
3163 var d: dict<number> = {} | |
3164 var l: list<number> = [] | |
3165 var b: blob = 0z10 | |
3166 var Fn: func = function('min') | |
3167 var j: job = test_null_job() | |
3168 var ch: channel = test_null_channel() | |
3169 var o: A = A.new() | |
3170 | |
3171 assert_fails('IntArg(d)', 'E1013: Argument 1: type mismatch, expected number but got dict<number>') | |
3172 assert_fails('IntArg(l)', 'E1013: Argument 1: type mismatch, expected number but got list<number>') | |
3173 assert_fails('IntArg(b)', 'E1013: Argument 1: type mismatch, expected number but got blob') | |
3174 assert_fails('IntArg(Fn)', 'E1013: Argument 1: type mismatch, expected number but got func([unknown]): number') | |
3175 assert_fails('IntArg(j)', 'E1013: Argument 1: type mismatch, expected number but got job') | |
3176 assert_fails('IntArg(ch)', 'E1013: Argument 1: type mismatch, expected number but got channel') | |
3177 assert_fails('IntArg(A)', 'E1013: Argument 1: type mismatch, expected number but got class<A>') | |
3178 assert_fails('IntArg(o)', 'E1013: Argument 1: type mismatch, expected number but got object<A>') | |
3179 | |
3180 # Passing a number to functions accepting different argument types | |
3181 def DictArg(_: dict<number>) | |
3182 enddef | |
3183 assert_fails('DictArg(N)', 'E1013: Argument 1: type mismatch, expected dict<number> but got number') | |
3184 | |
3185 def ListArg(_: list<number>) | |
3186 enddef | |
3187 assert_fails('ListArg(N)', 'E1013: Argument 1: type mismatch, expected list<number> but got number') | |
3188 | |
3189 def BlobArg(_: blob) | |
3190 enddef | |
3191 assert_fails('BlobArg(N)', 'E1013: Argument 1: type mismatch, expected blob but got number') | |
3192 | |
3193 def FuncArg(Fn_arg: func) | |
3194 enddef | |
3195 assert_fails('FuncArg(N)', 'E1013: Argument 1: type mismatch, expected func(...): unknown but got number') | |
3196 | |
3197 def JobArg(_: job) | |
3198 enddef | |
3199 assert_fails('JobArg(N)', 'E1013: Argument 1: type mismatch, expected job but got number') | |
3200 | |
3201 def ChannelArg(_: channel) | |
3202 enddef | |
3203 assert_fails('ChannelArg(N)', 'E1013: Argument 1: type mismatch, expected channel but got number') | |
3204 | |
3205 def ObjectArg(_: A) | |
3206 enddef | |
3207 assert_fails('ObjectArg(N)', 'E1013: Argument 1: type mismatch, expected object<A> but got number') | |
3208 END | |
3209 v9.CheckSourceSuccess(lines) | |
3210 | |
3211 # Calling a function expecting a number type with different argument types | |
3212 # from another function | |
3213 var pre_lines =<< trim END | |
3214 vim9script | |
3215 class A | |
3216 endclass | |
3217 def IntArg(n: number) | |
3218 enddef | |
3219 def Foo() | |
3220 END | |
3221 var post_lines =<< trim END | |
3222 enddef | |
3223 defcompile | |
3224 END | |
3225 lines = pre_lines + ['var d: dict<number> = {}', 'IntArg(d)'] + post_lines | |
3226 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got dict<number>', 2) | |
3227 lines = pre_lines + ['var l: list<number> = []', 'IntArg(l)'] + post_lines | |
3228 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got list<number>', 2) | |
3229 lines = pre_lines + ['var b: blob = 0z12', 'IntArg(b)'] + post_lines | |
3230 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got blob', 2) | |
3231 lines = pre_lines + ['var Fn: func = function("min")', 'IntArg(Fn)'] + post_lines | |
3232 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got func(...): unknown', 2) | |
3233 lines = pre_lines + ['var j: job = test_null_job()', 'IntArg(j)'] + post_lines | |
3234 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got job', 2) | |
3235 lines = pre_lines + ['var ch: channel = test_null_channel()', 'IntArg(ch)'] + post_lines | |
3236 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got channel', 2) | |
3237 lines = pre_lines + ['IntArg(A)'] + post_lines | |
3238 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got class<A>', 1) | |
3239 lines = pre_lines + ['var o: A = A.new()', 'IntArg(o)'] + post_lines | |
3240 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got object<A>', 2) | |
3241 enddef | |
3242 | |
3243 " Test for checking the return type of a def function | |
3244 def Test_func_rettype_check() | |
3245 var lines =<< trim END | |
3246 vim9script | |
3247 def Fn(): dict<number> | |
3248 return 10 | |
3249 enddef | |
3250 defcompile | |
3251 END | |
3252 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected dict<number> but got number', 1) | |
3253 | |
3254 lines =<< trim END | |
3255 vim9script | |
3256 def Fn(): list<number> | |
3257 return 10 | |
3258 enddef | |
3259 defcompile | |
3260 END | |
3261 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected list<number> but got number', 1) | |
3262 | |
3263 lines =<< trim END | |
3264 vim9script | |
3265 def Fn(): blob | |
3266 return 10 | |
3267 enddef | |
3268 defcompile | |
3269 END | |
3270 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected blob but got number', 1) | |
3271 | |
3272 lines =<< trim END | |
3273 vim9script | |
3274 def Fn(): func | |
3275 return 10 | |
3276 enddef | |
3277 defcompile | |
3278 END | |
3279 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(...): unknown but got number', 1) | |
3280 | |
3281 lines =<< trim END | |
3282 vim9script | |
3283 def Fn(): job | |
3284 return 10 | |
3285 enddef | |
3286 defcompile | |
3287 END | |
3288 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected job but got number', 1) | |
3289 | |
3290 lines =<< trim END | |
3291 vim9script | |
3292 def Fn(): channel | |
3293 return 10 | |
3294 enddef | |
3295 defcompile | |
3296 END | |
3297 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected channel but got number', 1) | |
3298 | |
3299 lines =<< trim END | |
3300 vim9script | |
3301 class A | |
3302 endclass | |
3303 def Fn(): A | |
3304 return 10 | |
3305 enddef | |
3306 defcompile | |
3307 END | |
3308 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<A> but got number', 1) | |
3309 enddef | |
3310 | |
3060 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker | 3311 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |