comparison src/testdir/vim9.vim @ 27457:4c16acb2525f v8.2.4257

patch 8.2.4257: Vim9: finding global function without g: prefix inconsistent Commit: https://github.com/vim/vim/commit/62aec93bfdb9e1b40d03a6d2e8e9511f8b1bdb2d Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jan 29 21:45:34 2022 +0000 patch 8.2.4257: Vim9: finding global function without g: prefix inconsistent Problem: Vim9: finding global function without g: prefix but not finding global variable is inconsistent. Solution: Require using g: for a global function. Change the vim9.vim script into a Vim9 script with exports. Fix that import in legacy script does not work.
author Bram Moolenaar <Bram@vim.org>
date Sat, 29 Jan 2022 23:00:05 +0100
parents c04b28fad0cc
children 5c4ab8d4472c
comparison
equal deleted inserted replaced
27456:a8e2d91995ce 27457:4c16acb2525f
1 " Utility functions for testing vim9 script 1 vim9script
2 2
3 " Use a different file name for each run. 3 # Utility functions for testing vim9 script
4 let s:sequence = 1 4
5 5 # Use a different file name for each run.
6 " Check that "lines" inside a ":def" function has no error when called. 6 var sequence = 1
7 func CheckDefSuccess(lines) 7
8 # Check that "lines" inside a ":def" function has no error when called.
9 export func CheckDefSuccess(lines)
8 let cwd = getcwd() 10 let cwd = getcwd()
9 let fname = 'XdefSuccess' .. s:sequence 11 let fname = 'XdefSuccess' .. s:sequence
10 let s:sequence += 1 12 let s:sequence += 1
11 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname) 13 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname)
12 try 14 try
17 call delete(fname) 19 call delete(fname)
18 delfunc! Func 20 delfunc! Func
19 endtry 21 endtry
20 endfunc 22 endfunc
21 23
22 " Check that "lines" inside a ":def" function has no error when compiled. 24 # Check that "lines" inside a ":def" function has no error when compiled.
23 func CheckDefCompileSuccess(lines) 25 export func CheckDefCompileSuccess(lines)
24 let fname = 'XdefSuccess' .. s:sequence 26 let fname = 'XdefSuccess' .. s:sequence
25 let s:sequence += 1 27 let s:sequence += 1
26 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname) 28 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname)
27 try 29 try
28 exe 'so ' .. fname 30 exe 'so ' .. fname
30 call delete(fname) 32 call delete(fname)
31 delfunc! Func 33 delfunc! Func
32 endtry 34 endtry
33 endfunc 35 endfunc
34 36
35 " Check that "lines" inside ":def" results in an "error" message. 37 # Check that "lines" inside ":def" results in an "error" message.
36 " If "lnum" is given check that the error is reported for this line. 38 # If "lnum" is given check that the error is reported for this line.
37 " Add a line before and after to make it less likely that the line number is 39 # Add a line before and after to make it less likely that the line number is
38 " accidentally correct. 40 # accidentally correct.
39 func CheckDefFailure(lines, error, lnum = -3) 41 export func CheckDefFailure(lines, error, lnum = -3)
40 let cwd = getcwd() 42 let cwd = getcwd()
41 let fname = 'XdefFailure' .. s:sequence 43 let fname = 'XdefFailure' .. s:sequence
42 let s:sequence += 1 44 let s:sequence += 1
43 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile'], fname) 45 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile'], fname)
44 try 46 try
48 call delete(fname) 50 call delete(fname)
49 delfunc! Func 51 delfunc! Func
50 endtry 52 endtry
51 endfunc 53 endfunc
52 54
53 " Check that "lines" inside ":def" results in an "error" message when executed. 55 # Check that "lines" inside ":def" results in an "error" message when executed.
54 " If "lnum" is given check that the error is reported for this line. 56 # If "lnum" is given check that the error is reported for this line.
55 " Add a line before and after to make it less likely that the line number is 57 # Add a line before and after to make it less likely that the line number is
56 " accidentally correct. 58 # accidentally correct.
57 func CheckDefExecFailure(lines, error, lnum = -3) 59 export func CheckDefExecFailure(lines, error, lnum = -3)
58 let cwd = getcwd() 60 let cwd = getcwd()
59 let fname = 'XdefExecFailure' .. s:sequence 61 let fname = 'XdefExecFailure' .. s:sequence
60 let s:sequence += 1 62 let s:sequence += 1
61 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef'], fname) 63 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef'], fname)
62 try 64 try
67 call delete(fname) 69 call delete(fname)
68 delfunc! Func 70 delfunc! Func
69 endtry 71 endtry
70 endfunc 72 endfunc
71 73
72 def CheckScriptFailure(lines: list<string>, error: string, lnum = -3) 74 export def CheckScriptFailure(lines: list<string>, error: string, lnum = -3)
73 var cwd = getcwd() 75 var cwd = getcwd()
74 var fname = 'XScriptFailure' .. s:sequence 76 var fname = 'XScriptFailure' .. s:sequence
75 s:sequence += 1 77 s:sequence += 1
76 writefile(lines, fname) 78 writefile(lines, fname)
77 try 79 try
80 chdir(cwd) 82 chdir(cwd)
81 delete(fname) 83 delete(fname)
82 endtry 84 endtry
83 enddef 85 enddef
84 86
85 def CheckScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3) 87 export def CheckScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3)
86 var cwd = getcwd() 88 var cwd = getcwd()
87 var fname = 'XScriptFailure' .. s:sequence 89 var fname = 'XScriptFailure' .. s:sequence
88 s:sequence += 1 90 s:sequence += 1
89 writefile(lines, fname) 91 writefile(lines, fname)
90 try 92 try
93 chdir(cwd) 95 chdir(cwd)
94 delete(fname) 96 delete(fname)
95 endtry 97 endtry
96 enddef 98 enddef
97 99
98 def CheckScriptSuccess(lines: list<string>) 100 export def CheckScriptSuccess(lines: list<string>)
99 var cwd = getcwd() 101 var cwd = getcwd()
100 var fname = 'XScriptSuccess' .. s:sequence 102 var fname = 'XScriptSuccess' .. s:sequence
101 s:sequence += 1 103 s:sequence += 1
102 writefile(lines, fname) 104 writefile(lines, fname)
103 try 105 try
106 chdir(cwd) 108 chdir(cwd)
107 delete(fname) 109 delete(fname)
108 endtry 110 endtry
109 enddef 111 enddef
110 112
111 def CheckDefAndScriptSuccess(lines: list<string>) 113 export def CheckDefAndScriptSuccess(lines: list<string>)
112 CheckDefSuccess(lines) 114 CheckDefSuccess(lines)
113 CheckScriptSuccess(['vim9script'] + lines) 115 CheckScriptSuccess(['vim9script'] + lines)
114 enddef 116 enddef
115 117
116 " Check that a command fails when used in a :def function and when used in 118 # Check that a command fails when used in a :def function and when used in
117 " Vim9 script. 119 # Vim9 script.
118 " When "error" is a string, both with the same error. 120 # When "error" is a string, both with the same error.
119 " When "error" is a list, the :def function fails with "error[0]" , the script 121 # When "error" is a list, the :def function fails with "error[0]" , the script
120 " fails with "error[1]". 122 # fails with "error[1]".
121 def CheckDefAndScriptFailure(lines: list<string>, error: any, lnum = -3) 123 export def CheckDefAndScriptFailure(lines: list<string>, error: any, lnum = -3)
122 var errorDef: string 124 var errorDef: string
123 var errorScript: string 125 var errorScript: string
124 if type(error) == v:t_string 126 if type(error) == v:t_string
125 errorDef = error 127 errorDef = error
126 errorScript = error 128 errorScript = error
133 endif 135 endif
134 CheckDefFailure(lines, errorDef, lnum) 136 CheckDefFailure(lines, errorDef, lnum)
135 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) 137 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
136 enddef 138 enddef
137 139
138 " Check that a command fails when executed in a :def function and when used in 140 # Check that a command fails when executed in a :def function and when used in
139 " Vim9 script. 141 # Vim9 script.
140 " When "error" is a string, both with the same error. 142 # When "error" is a string, both with the same error.
141 " When "error" is a list, the :def function fails with "error[0]" , the script 143 # When "error" is a list, the :def function fails with "error[0]" , the script
142 " fails with "error[1]". 144 # fails with "error[1]".
143 def CheckDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3) 145 export def CheckDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3)
144 var errorDef: string 146 var errorDef: string
145 var errorScript: string 147 var errorScript: string
146 if type(error) == v:t_string 148 if type(error) == v:t_string
147 errorDef = error 149 errorDef = error
148 errorScript = error 150 errorScript = error
156 CheckDefExecFailure(lines, errorDef, lnum) 158 CheckDefExecFailure(lines, errorDef, lnum)
157 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) 159 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
158 enddef 160 enddef
159 161
160 162
161 " Check that "lines" inside a legacy function has no error. 163 # Check that "lines" inside a legacy function has no error.
162 func CheckLegacySuccess(lines) 164 export func CheckLegacySuccess(lines)
163 let cwd = getcwd() 165 let cwd = getcwd()
164 let fname = 'XlegacySuccess' .. s:sequence 166 let fname = 'XlegacySuccess' .. s:sequence
165 let s:sequence += 1 167 let s:sequence += 1
166 call writefile(['func Func()'] + a:lines + ['endfunc'], fname) 168 call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
167 try 169 try
172 call chdir(cwd) 174 call chdir(cwd)
173 call delete(fname) 175 call delete(fname)
174 endtry 176 endtry
175 endfunc 177 endfunc
176 178
177 " Check that "lines" inside a legacy function results in the expected error 179 # Check that "lines" inside a legacy function results in the expected error
178 func CheckLegacyFailure(lines, error) 180 export func CheckLegacyFailure(lines, error)
179 let cwd = getcwd() 181 let cwd = getcwd()
180 let fname = 'XlegacyFails' .. s:sequence 182 let fname = 'XlegacyFails' .. s:sequence
181 let s:sequence += 1 183 let s:sequence += 1
182 call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname) 184 call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
183 try 185 try
187 call chdir(cwd) 189 call chdir(cwd)
188 call delete(fname) 190 call delete(fname)
189 endtry 191 endtry
190 endfunc 192 endfunc
191 193
192 " Execute "lines" in a legacy function, translated as in 194 # Execute "lines" in a legacy function, translated as in
193 " CheckLegacyAndVim9Success() 195 # CheckLegacyAndVim9Success()
194 def CheckTransLegacySuccess(lines: list<string>) 196 export def CheckTransLegacySuccess(lines: list<string>)
195 var legacylines = lines->mapnew((_, v) => 197 var legacylines = lines->mapnew((_, v) =>
196 v->substitute('\<VAR\>', 'let', 'g') 198 v->substitute('\<VAR\>', 'let', 'g')
197 ->substitute('\<LET\>', 'let', 'g') 199 ->substitute('\<LET\>', 'let', 'g')
198 ->substitute('\<LSTART\>', '{', 'g') 200 ->substitute('\<LSTART\>', '{', 'g')
199 ->substitute('\<LMIDDLE\>', '->', 'g') 201 ->substitute('\<LMIDDLE\>', '->', 'g')
202 ->substitute('\<FALSE\>', '0', 'g') 204 ->substitute('\<FALSE\>', '0', 'g')
203 ->substitute('#"', ' "', 'g')) 205 ->substitute('#"', ' "', 'g'))
204 CheckLegacySuccess(legacylines) 206 CheckLegacySuccess(legacylines)
205 enddef 207 enddef
206 208
207 def Vim9Trans(lines: list<string>): list<string> 209 export def Vim9Trans(lines: list<string>): list<string>
208 return lines->mapnew((_, v) => 210 return lines->mapnew((_, v) =>
209 v->substitute('\<VAR\>', 'var', 'g') 211 v->substitute('\<VAR\>', 'var', 'g')
210 ->substitute('\<LET ', '', 'g') 212 ->substitute('\<LET ', '', 'g')
211 ->substitute('\<LSTART\>', '(', 'g') 213 ->substitute('\<LSTART\>', '(', 'g')
212 ->substitute('\<LMIDDLE\>', ') =>', 'g') 214 ->substitute('\<LMIDDLE\>', ') =>', 'g')
213 ->substitute(' *\<LEND\> *', '', 'g') 215 ->substitute(' *\<LEND\> *', '', 'g')
214 ->substitute('\<TRUE\>', 'true', 'g') 216 ->substitute('\<TRUE\>', 'true', 'g')
215 ->substitute('\<FALSE\>', 'false', 'g')) 217 ->substitute('\<FALSE\>', 'false', 'g'))
216 enddef 218 enddef
217 219
218 " Execute "lines" in a :def function, translated as in 220 # Execute "lines" in a :def function, translated as in
219 " CheckLegacyAndVim9Success() 221 # CheckLegacyAndVim9Success()
220 def CheckTransDefSuccess(lines: list<string>) 222 export def CheckTransDefSuccess(lines: list<string>)
221 CheckDefSuccess(Vim9Trans(lines)) 223 CheckDefSuccess(Vim9Trans(lines))
222 enddef 224 enddef
223 225
224 " Execute "lines" in a Vim9 script, translated as in 226 # Execute "lines" in a Vim9 script, translated as in
225 " CheckLegacyAndVim9Success() 227 # CheckLegacyAndVim9Success()
226 def CheckTransVim9Success(lines: list<string>) 228 export def CheckTransVim9Success(lines: list<string>)
227 CheckScriptSuccess(['vim9script'] + Vim9Trans(lines)) 229 CheckScriptSuccess(['vim9script'] + Vim9Trans(lines))
228 enddef 230 enddef
229 231
230 " Execute "lines" in a legacy function, :def function and Vim9 script. 232 # Execute "lines" in a legacy function, :def function and Vim9 script.
231 " Use 'VAR' for a declaration. 233 # Use 'VAR' for a declaration.
232 " Use 'LET' for an assignment 234 # Use 'LET' for an assignment
233 " Use ' #"' for a comment 235 # Use ' #"' for a comment
234 " Use LSTART arg LMIDDLE expr LEND for lambda 236 # Use LSTART arg LMIDDLE expr LEND for lambda
235 " Use 'TRUE' for 1 in legacy, true in Vim9 237 # Use 'TRUE' for 1 in legacy, true in Vim9
236 " Use 'FALSE' for 0 in legacy, false in Vim9 238 # Use 'FALSE' for 0 in legacy, false in Vim9
237 def CheckLegacyAndVim9Success(lines: list<string>) 239 export def CheckLegacyAndVim9Success(lines: list<string>)
238 CheckTransLegacySuccess(lines) 240 CheckTransLegacySuccess(lines)
239 CheckTransDefSuccess(lines) 241 CheckTransDefSuccess(lines)
240 CheckTransVim9Success(lines) 242 CheckTransVim9Success(lines)
241 enddef 243 enddef
242 244
243 " Execute "lines" in a legacy function, :def function and Vim9 script. 245 # Execute "lines" in a legacy function, :def function and Vim9 script.
244 " Use 'VAR' for a declaration. 246 # Use 'VAR' for a declaration.
245 " Use 'LET' for an assignment 247 # Use 'LET' for an assignment
246 " Use ' #"' for a comment 248 # Use ' #"' for a comment
247 def CheckLegacyAndVim9Failure(lines: list<string>, error: any) 249 export def CheckLegacyAndVim9Failure(lines: list<string>, error: any)
248 var legacyError: string 250 var legacyError: string
249 var defError: string 251 var defError: string
250 var scriptError: string 252 var scriptError: string
251 253
252 if type(error) == type('string') 254 if type(error) == type('string')