blob: 764b6119db66820fc87936f257272d5f2c2ab435 [file] [log] [blame]
Bram Moolenaar62aec932022-01-29 21:45:34 +00001vim9script
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002
Bram Moolenaar62aec932022-01-29 21:45:34 +00003# Utility functions for testing vim9 script
Bram Moolenaar2d870f82020-12-05 13:41:01 +01004
Bram Moolenaar62aec932022-01-29 21:45:34 +00005# Use a different file name for each run.
6var sequence = 1
7
8# Check that "lines" inside a ":def" function has no error when called.
9export func CheckDefSuccess(lines)
Bram Moolenaar090728a2020-12-20 15:43:31 +010010 let cwd = getcwd()
11 let fname = 'XdefSuccess' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010012 let s:sequence += 1
13 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +010014 try
15 exe 'so ' .. fname
16 call Func()
Bram Moolenaar090728a2020-12-20 15:43:31 +010017 finally
18 call chdir(cwd)
19 call delete(fname)
Bram Moolenaar88c89c72021-08-14 14:01:05 +020020 delfunc! Func
Bram Moolenaar090728a2020-12-20 15:43:31 +010021 endtry
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +020022endfunc
23
Bram Moolenaar62aec932022-01-29 21:45:34 +000024# Check that "lines" inside a ":def" function has no error when compiled.
25export func CheckDefCompileSuccess(lines)
Bram Moolenaar08018222021-12-22 18:45:37 +000026 let fname = 'XdefSuccess' .. s:sequence
27 let s:sequence += 1
28 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname)
29 try
30 exe 'so ' .. fname
31 finally
32 call delete(fname)
33 delfunc! Func
34 endtry
35endfunc
36
Bram Moolenaar62aec932022-01-29 21:45:34 +000037# Check that "lines" inside ":def" results in an "error" message.
38# If "lnum" is given check that the error is reported for this line.
39# Add a line before and after to make it less likely that the line number is
40# accidentally correct.
41export func CheckDefFailure(lines, error, lnum = -3)
Bram Moolenaar090728a2020-12-20 15:43:31 +010042 let cwd = getcwd()
43 let fname = 'XdefFailure' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010044 let s:sequence += 1
Bram Moolenaar090728a2020-12-20 15:43:31 +010045 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile'], fname)
46 try
47 call assert_fails('so ' .. fname, a:error, a:lines, a:lnum + 1)
48 finally
49 call chdir(cwd)
50 call delete(fname)
51 delfunc! Func
52 endtry
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020053endfunc
54
Bram Moolenaar62aec932022-01-29 21:45:34 +000055# Check that "lines" inside ":def" results in an "error" message when executed.
56# If "lnum" is given check that the error is reported for this line.
57# Add a line before and after to make it less likely that the line number is
58# accidentally correct.
59export func CheckDefExecFailure(lines, error, lnum = -3)
Bram Moolenaar090728a2020-12-20 15:43:31 +010060 let cwd = getcwd()
61 let fname = 'XdefExecFailure' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010062 let s:sequence += 1
63 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef'], fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +010064 try
65 exe 'so ' .. fname
66 call assert_fails('call Func()', a:error, a:lines, a:lnum + 1)
67 finally
68 call chdir(cwd)
69 call delete(fname)
70 delfunc! Func
71 endtry
Bram Moolenaar015f4262020-05-05 21:25:22 +020072endfunc
73
Bram Moolenaar62aec932022-01-29 21:45:34 +000074export def CheckScriptFailure(lines: list<string>, error: string, lnum = -3)
Bram Moolenaar090728a2020-12-20 15:43:31 +010075 var cwd = getcwd()
Bram Moolenaara749a422022-02-12 19:52:25 +000076 var fname = 'XScriptFailure' .. sequence
77 sequence += 1
Bram Moolenaar2d870f82020-12-05 13:41:01 +010078 writefile(lines, fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +010079 try
80 assert_fails('so ' .. fname, error, lines, lnum)
81 finally
82 chdir(cwd)
83 delete(fname)
84 endtry
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020085enddef
86
Bram Moolenaar62aec932022-01-29 21:45:34 +000087export def CheckScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3)
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010088 var cwd = getcwd()
Bram Moolenaara749a422022-02-12 19:52:25 +000089 var fname = 'XScriptFailure' .. sequence
90 sequence += 1
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 writefile(lines, fname)
92 try
93 assert_fails('so ' .. fname, errors, lines, lnum)
94 finally
95 chdir(cwd)
96 delete(fname)
97 endtry
98enddef
99
Bram Moolenaar62aec932022-01-29 21:45:34 +0000100export def CheckScriptSuccess(lines: list<string>)
Bram Moolenaar090728a2020-12-20 15:43:31 +0100101 var cwd = getcwd()
Bram Moolenaara749a422022-02-12 19:52:25 +0000102 var fname = 'XScriptSuccess' .. sequence
103 sequence += 1
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100104 writefile(lines, fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +0100105 try
106 exe 'so ' .. fname
107 finally
108 chdir(cwd)
109 delete(fname)
110 endtry
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200111enddef
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200112
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200113# :source a list of "lines" and check whether it fails with "error"
114export def CheckSourceFailure(lines: list<string>, error: string, lnum = -3)
Yegappan Lakshmanan7f520212024-04-10 17:18:19 +0200115 var cwd = getcwd()
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200116 new
117 setline(1, lines)
118 try
119 assert_fails('source', error, lines, lnum)
120 finally
Yegappan Lakshmanan7f520212024-04-10 17:18:19 +0200121 chdir(cwd)
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200122 bw!
123 endtry
124enddef
125
126# :source a list of "lines" and check whether it fails with the list of
127# "errors"
128export def CheckSourceFailureList(lines: list<string>, errors: list<string>, lnum = -3)
Yegappan Lakshmanan7f520212024-04-10 17:18:19 +0200129 var cwd = getcwd()
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200130 new
131 setline(1, lines)
132 try
133 assert_fails('source', errors, lines, lnum)
134 finally
Yegappan Lakshmanan7f520212024-04-10 17:18:19 +0200135 chdir(cwd)
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200136 bw!
137 endtry
138enddef
139
140# :source a list of "lines" and check whether it succeeds
141export def CheckSourceSuccess(lines: list<string>)
Yegappan Lakshmanan7f520212024-04-10 17:18:19 +0200142 var cwd = getcwd()
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200143 new
144 setline(1, lines)
145 try
146 :source
147 finally
Yegappan Lakshmanan7f520212024-04-10 17:18:19 +0200148 chdir(cwd)
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200149 bw!
150 endtry
151enddef
152
Bram Moolenaar62aec932022-01-29 21:45:34 +0000153export def CheckDefAndScriptSuccess(lines: list<string>)
Bram Moolenaar2e800952020-08-23 19:34:48 +0200154 CheckDefSuccess(lines)
155 CheckScriptSuccess(['vim9script'] + lines)
156enddef
157
Bram Moolenaar62aec932022-01-29 21:45:34 +0000158# Check that a command fails when used in a :def function and when used in
159# Vim9 script.
160# When "error" is a string, both with the same error.
161# When "error" is a list, the :def function fails with "error[0]" , the script
162# fails with "error[1]".
163export def CheckDefAndScriptFailure(lines: list<string>, error: any, lnum = -3)
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000164 var errorDef: string
165 var errorScript: string
166 if type(error) == v:t_string
167 errorDef = error
168 errorScript = error
169 elseif type(error) == v:t_list && len(error) == 2
170 errorDef = error[0]
171 errorScript = error[1]
172 else
173 echoerr 'error argument must be a string or a list with two items'
174 return
175 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200176 CheckDefFailure(lines, errorDef, lnum)
177 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
178enddef
179
Bram Moolenaar62aec932022-01-29 21:45:34 +0000180# Check that a command fails when executed in a :def function and when used in
181# Vim9 script.
182# When "error" is a string, both with the same error.
183# When "error" is a list, the :def function fails with "error[0]" , the script
184# fails with "error[1]".
185export def CheckDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3)
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000186 var errorDef: string
187 var errorScript: string
188 if type(error) == v:t_string
189 errorDef = error
190 errorScript = error
191 elseif type(error) == v:t_list && len(error) == 2
192 errorDef = error[0]
193 errorScript = error[1]
194 else
195 echoerr 'error argument must be a string or a list with two items'
196 return
197 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200198 CheckDefExecFailure(lines, errorDef, lnum)
199 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
200enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200201
202
Bram Moolenaar62aec932022-01-29 21:45:34 +0000203# Check that "lines" inside a legacy function has no error.
204export func CheckLegacySuccess(lines)
Bram Moolenaar68452172021-04-12 21:21:02 +0200205 let cwd = getcwd()
206 let fname = 'XlegacySuccess' .. s:sequence
207 let s:sequence += 1
208 call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
209 try
210 exe 'so ' .. fname
211 call Func()
Bram Moolenaar68452172021-04-12 21:21:02 +0200212 finally
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200213 delfunc! Func
214 call chdir(cwd)
215 call delete(fname)
216 endtry
217endfunc
218
Bram Moolenaar62aec932022-01-29 21:45:34 +0000219# Check that "lines" inside a legacy function results in the expected error
220export func CheckLegacyFailure(lines, error)
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200221 let cwd = getcwd()
222 let fname = 'XlegacyFails' .. s:sequence
223 let s:sequence += 1
224 call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
225 try
226 call assert_fails('so ' .. fname, a:error)
227 finally
228 delfunc! Func
Bram Moolenaar68452172021-04-12 21:21:02 +0200229 call chdir(cwd)
230 call delete(fname)
231 endtry
232endfunc
233
Bram Moolenaar62aec932022-01-29 21:45:34 +0000234# Execute "lines" in a legacy function, translated as in
235# CheckLegacyAndVim9Success()
236export def CheckTransLegacySuccess(lines: list<string>)
Bram Moolenaar68452172021-04-12 21:21:02 +0200237 var legacylines = lines->mapnew((_, v) =>
Bram Moolenaar94722c52023-01-28 19:19:03 +0000238 v->substitute('\<VAR\>', 'let', 'g')
239 ->substitute('\<LET\>', 'let', 'g')
240 ->substitute('\<LSTART\>', '{', 'g')
241 ->substitute('\<LMIDDLE\>', '->', 'g')
Bram Moolenaaref982572021-08-12 19:27:57 +0200242 ->substitute('\<LEND\>', '}', 'g')
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000243 ->substitute('\<TRUE\>', '1', 'g')
244 ->substitute('\<FALSE\>', '0', 'g')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000245 ->substitute('#"', ' "', 'g'))
Bram Moolenaar68452172021-04-12 21:21:02 +0200246 CheckLegacySuccess(legacylines)
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200247enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200248
Bram Moolenaar62aec932022-01-29 21:45:34 +0000249export def Vim9Trans(lines: list<string>): list<string>
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000250 return lines->mapnew((_, v) =>
251 v->substitute('\<VAR\>', 'var', 'g')
252 ->substitute('\<LET ', '', 'g')
253 ->substitute('\<LSTART\>', '(', 'g')
254 ->substitute('\<LMIDDLE\>', ') =>', 'g')
255 ->substitute(' *\<LEND\> *', '', 'g')
256 ->substitute('\<TRUE\>', 'true', 'g')
257 ->substitute('\<FALSE\>', 'false', 'g'))
258enddef
259
Bram Moolenaar62aec932022-01-29 21:45:34 +0000260# Execute "lines" in a :def function, translated as in
261# CheckLegacyAndVim9Success()
262export def CheckTransDefSuccess(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000263 CheckDefSuccess(Vim9Trans(lines))
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200264enddef
265
Bram Moolenaar62aec932022-01-29 21:45:34 +0000266# Execute "lines" in a Vim9 script, translated as in
267# CheckLegacyAndVim9Success()
268export def CheckTransVim9Success(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000269 CheckScriptSuccess(['vim9script'] + Vim9Trans(lines))
Bram Moolenaar68452172021-04-12 21:21:02 +0200270enddef
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200271
Bram Moolenaar62aec932022-01-29 21:45:34 +0000272# Execute "lines" in a legacy function, :def function and Vim9 script.
273# Use 'VAR' for a declaration.
274# Use 'LET' for an assignment
275# Use ' #"' for a comment
276# Use LSTART arg LMIDDLE expr LEND for lambda
277# Use 'TRUE' for 1 in legacy, true in Vim9
278# Use 'FALSE' for 0 in legacy, false in Vim9
279export def CheckLegacyAndVim9Success(lines: list<string>)
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200280 CheckTransLegacySuccess(lines)
281 CheckTransDefSuccess(lines)
282 CheckTransVim9Success(lines)
283enddef
284
Bram Moolenaar62aec932022-01-29 21:45:34 +0000285# Execute "lines" in a legacy function, :def function and Vim9 script.
286# Use 'VAR' for a declaration.
287# Use 'LET' for an assignment
288# Use ' #"' for a comment
289export def CheckLegacyAndVim9Failure(lines: list<string>, error: any)
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200290 var legacyError: string
291 var defError: string
292 var scriptError: string
293
294 if type(error) == type('string')
295 legacyError = error
296 defError = error
297 scriptError = error
298 else
299 legacyError = error[0]
300 defError = error[1]
301 scriptError = error[2]
302 endif
303
304 var legacylines = lines->mapnew((_, v) =>
Bram Moolenaar94722c52023-01-28 19:19:03 +0000305 v->substitute('\<VAR\>', 'let', 'g')
306 ->substitute('\<LET\>', 'let', 'g')
307 ->substitute('#"', ' "', 'g'))
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200308 CheckLegacyFailure(legacylines, legacyError)
309
310 var vim9lines = lines->mapnew((_, v) =>
Bram Moolenaar94722c52023-01-28 19:19:03 +0000311 v->substitute('\<VAR\>', 'var', 'g')
312 ->substitute('\<LET ', '', 'g'))
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200313 CheckDefExecFailure(vim9lines, defError)
314 CheckScriptFailure(['vim9script'] + vim9lines, scriptError)
315enddef