comparison src/testdir/test_vim9_class.vim @ 33913:a259471e74fe v9.0.2156

patch 9.0.2156: Vim9: can use typealias in assignment Commit: https://github.com/vim/vim/commit/9ed53752df1020a6881ac68d1bde2852c9a680aa Author: Ernie Rael <errael@raelity.com> Date: Mon Dec 11 17:40:46 2023 +0100 patch 9.0.2156: Vim9: can use typealias in assignment Problem: Vim9: can use typealias in an assignment Solution: Generate errors when class/typealias involved in the rhs of an assignment closes: #13637 Signed-off-by: Ernie Rael <errael@raelity.com> Signed-off-by: Christian Brabandt <cb@256bit.org> Generate errors when class/typealias involved in assignment.
author Christian Brabandt <cb@256bit.org>
date Mon, 11 Dec 2023 17:45:06 +0100
parents cd7acb9bc4fd
children ccdb948c7273
comparison
equal deleted inserted replaced
33912:bc834a974df8 33913:a259471e74fe
3091 assert_equal(['A'], g:result) 3091 assert_equal(['A'], g:result)
3092 END 3092 END
3093 v9.CheckSourceSuccess(lines) 3093 v9.CheckSourceSuccess(lines)
3094 enddef 3094 enddef
3095 3095
3096 def Test_call_constructor_from_legacy() 3096 def Test_construct_object_from_legacy()
3097 var lines =<< trim END 3097 # Cannot directly invoke constructor from legacy
3098 vim9script 3098 var lines =<< trim END
3099 3099 vim9script
3100 var newCalled = 'false' 3100
3101 3101 var newCalled = false
3102 class A 3102
3103 class A
3104 def new(arg: string)
3105 newCalled = true
3106 enddef
3107 endclass
3108
3109 export def CreateA(...args: list<any>): A
3110 return call(A.new, args)
3111 enddef
3112
3113 g:P = CreateA
3114 legacy call g:P('some_arg')
3115 assert_equal(true, newCalled)
3116 unlet g:P
3117 END
3118 v9.CheckSourceSuccess(lines)
3119
3120 lines =<< trim END
3121 vim9script
3122
3123 var newCalled = false
3124
3125 class A
3126 static def CreateA(options = {}): any
3127 return A.new()
3128 enddef
3103 def new() 3129 def new()
3104 newCalled = 'true' 3130 newCalled = true
3105 enddef 3131 enddef
3106 endclass 3132 endclass
3107 3133
3108 export def F(options = {}): any 3134 g:P = A.CreateA
3109 return A 3135 legacy call g:P()
3110 enddef 3136 assert_equal(true, newCalled)
3111 3137 unlet g:P
3112 g:p = F() 3138 END
3113 legacy call p.new() 3139 v9.CheckSourceSuccess(lines)
3114 assert_equal('true', newCalled) 3140
3141 # This also tests invoking "new()" with "call"
3142 lines =<< trim END
3143 vim9script
3144
3145 var createdObject: any
3146
3147 class A
3148 this.val1: number
3149 this.val2: number
3150 static def CreateA(...args: list<any>): any
3151 createdObject = call(A.new, args)
3152 return createdObject
3153 enddef
3154 endclass
3155
3156 g:P = A.CreateA
3157 legacy call g:P(3, 5)
3158 assert_equal(3, createdObject.val1)
3159 assert_equal(5, createdObject.val2)
3160 legacy call g:P()
3161 assert_equal(0, createdObject.val1)
3162 assert_equal(0, createdObject.val2)
3163 legacy call g:P(7)
3164 assert_equal(7, createdObject.val1)
3165 assert_equal(0, createdObject.val2)
3166 unlet g:P
3115 END 3167 END
3116 v9.CheckSourceSuccess(lines) 3168 v9.CheckSourceSuccess(lines)
3117 enddef 3169 enddef
3118 3170
3119 def Test_defer_with_object() 3171 def Test_defer_with_object()