blob: 782809bde89c47eb9b9bf8f435ce8feb6d1efe3f [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)
115 new
116 setline(1, lines)
117 try
118 assert_fails('source', error, lines, lnum)
119 finally
120 bw!
121 endtry
122enddef
123
124# :source a list of "lines" and check whether it fails with the list of
125# "errors"
126export def CheckSourceFailureList(lines: list<string>, errors: list<string>, lnum = -3)
127 new
128 setline(1, lines)
129 try
130 assert_fails('source', errors, lines, lnum)
131 finally
132 bw!
133 endtry
134enddef
135
136# :source a list of "lines" and check whether it succeeds
137export def CheckSourceSuccess(lines: list<string>)
138 new
139 setline(1, lines)
140 try
141 :source
142 finally
143 bw!
144 endtry
145enddef
146
Bram Moolenaar62aec932022-01-29 21:45:34 +0000147export def CheckDefAndScriptSuccess(lines: list<string>)
Bram Moolenaar2e800952020-08-23 19:34:48 +0200148 CheckDefSuccess(lines)
149 CheckScriptSuccess(['vim9script'] + lines)
150enddef
151
Bram Moolenaar62aec932022-01-29 21:45:34 +0000152# Check that a command fails when used in a :def function and when used in
153# Vim9 script.
154# When "error" is a string, both with the same error.
155# When "error" is a list, the :def function fails with "error[0]" , the script
156# fails with "error[1]".
157export def CheckDefAndScriptFailure(lines: list<string>, error: any, lnum = -3)
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000158 var errorDef: string
159 var errorScript: string
160 if type(error) == v:t_string
161 errorDef = error
162 errorScript = error
163 elseif type(error) == v:t_list && len(error) == 2
164 errorDef = error[0]
165 errorScript = error[1]
166 else
167 echoerr 'error argument must be a string or a list with two items'
168 return
169 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200170 CheckDefFailure(lines, errorDef, lnum)
171 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
172enddef
173
Bram Moolenaar62aec932022-01-29 21:45:34 +0000174# Check that a command fails when executed in a :def function and when used in
175# Vim9 script.
176# When "error" is a string, both with the same error.
177# When "error" is a list, the :def function fails with "error[0]" , the script
178# fails with "error[1]".
179export def CheckDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3)
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000180 var errorDef: string
181 var errorScript: string
182 if type(error) == v:t_string
183 errorDef = error
184 errorScript = error
185 elseif type(error) == v:t_list && len(error) == 2
186 errorDef = error[0]
187 errorScript = error[1]
188 else
189 echoerr 'error argument must be a string or a list with two items'
190 return
191 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200192 CheckDefExecFailure(lines, errorDef, lnum)
193 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
194enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200195
196
Bram Moolenaar62aec932022-01-29 21:45:34 +0000197# Check that "lines" inside a legacy function has no error.
198export func CheckLegacySuccess(lines)
Bram Moolenaar68452172021-04-12 21:21:02 +0200199 let cwd = getcwd()
200 let fname = 'XlegacySuccess' .. s:sequence
201 let s:sequence += 1
202 call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
203 try
204 exe 'so ' .. fname
205 call Func()
Bram Moolenaar68452172021-04-12 21:21:02 +0200206 finally
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200207 delfunc! Func
208 call chdir(cwd)
209 call delete(fname)
210 endtry
211endfunc
212
Bram Moolenaar62aec932022-01-29 21:45:34 +0000213# Check that "lines" inside a legacy function results in the expected error
214export func CheckLegacyFailure(lines, error)
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200215 let cwd = getcwd()
216 let fname = 'XlegacyFails' .. s:sequence
217 let s:sequence += 1
218 call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
219 try
220 call assert_fails('so ' .. fname, a:error)
221 finally
222 delfunc! Func
Bram Moolenaar68452172021-04-12 21:21:02 +0200223 call chdir(cwd)
224 call delete(fname)
225 endtry
226endfunc
227
Bram Moolenaar62aec932022-01-29 21:45:34 +0000228# Execute "lines" in a legacy function, translated as in
229# CheckLegacyAndVim9Success()
230export def CheckTransLegacySuccess(lines: list<string>)
Bram Moolenaar68452172021-04-12 21:21:02 +0200231 var legacylines = lines->mapnew((_, v) =>
Bram Moolenaar94722c52023-01-28 19:19:03 +0000232 v->substitute('\<VAR\>', 'let', 'g')
233 ->substitute('\<LET\>', 'let', 'g')
234 ->substitute('\<LSTART\>', '{', 'g')
235 ->substitute('\<LMIDDLE\>', '->', 'g')
Bram Moolenaaref982572021-08-12 19:27:57 +0200236 ->substitute('\<LEND\>', '}', 'g')
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000237 ->substitute('\<TRUE\>', '1', 'g')
238 ->substitute('\<FALSE\>', '0', 'g')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000239 ->substitute('#"', ' "', 'g'))
Bram Moolenaar68452172021-04-12 21:21:02 +0200240 CheckLegacySuccess(legacylines)
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200241enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200242
Bram Moolenaar62aec932022-01-29 21:45:34 +0000243export def Vim9Trans(lines: list<string>): list<string>
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000244 return lines->mapnew((_, v) =>
245 v->substitute('\<VAR\>', 'var', 'g')
246 ->substitute('\<LET ', '', 'g')
247 ->substitute('\<LSTART\>', '(', 'g')
248 ->substitute('\<LMIDDLE\>', ') =>', 'g')
249 ->substitute(' *\<LEND\> *', '', 'g')
250 ->substitute('\<TRUE\>', 'true', 'g')
251 ->substitute('\<FALSE\>', 'false', 'g'))
252enddef
253
Bram Moolenaar62aec932022-01-29 21:45:34 +0000254# Execute "lines" in a :def function, translated as in
255# CheckLegacyAndVim9Success()
256export def CheckTransDefSuccess(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000257 CheckDefSuccess(Vim9Trans(lines))
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200258enddef
259
Bram Moolenaar62aec932022-01-29 21:45:34 +0000260# Execute "lines" in a Vim9 script, translated as in
261# CheckLegacyAndVim9Success()
262export def CheckTransVim9Success(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000263 CheckScriptSuccess(['vim9script'] + Vim9Trans(lines))
Bram Moolenaar68452172021-04-12 21:21:02 +0200264enddef
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200265
Bram Moolenaar62aec932022-01-29 21:45:34 +0000266# Execute "lines" in a legacy function, :def function and Vim9 script.
267# Use 'VAR' for a declaration.
268# Use 'LET' for an assignment
269# Use ' #"' for a comment
270# Use LSTART arg LMIDDLE expr LEND for lambda
271# Use 'TRUE' for 1 in legacy, true in Vim9
272# Use 'FALSE' for 0 in legacy, false in Vim9
273export def CheckLegacyAndVim9Success(lines: list<string>)
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200274 CheckTransLegacySuccess(lines)
275 CheckTransDefSuccess(lines)
276 CheckTransVim9Success(lines)
277enddef
278
Bram Moolenaar62aec932022-01-29 21:45:34 +0000279# Execute "lines" in a legacy function, :def function and Vim9 script.
280# Use 'VAR' for a declaration.
281# Use 'LET' for an assignment
282# Use ' #"' for a comment
283export def CheckLegacyAndVim9Failure(lines: list<string>, error: any)
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200284 var legacyError: string
285 var defError: string
286 var scriptError: string
287
288 if type(error) == type('string')
289 legacyError = error
290 defError = error
291 scriptError = error
292 else
293 legacyError = error[0]
294 defError = error[1]
295 scriptError = error[2]
296 endif
297
298 var legacylines = lines->mapnew((_, v) =>
Bram Moolenaar94722c52023-01-28 19:19:03 +0000299 v->substitute('\<VAR\>', 'let', 'g')
300 ->substitute('\<LET\>', 'let', 'g')
301 ->substitute('#"', ' "', 'g'))
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200302 CheckLegacyFailure(legacylines, legacyError)
303
304 var vim9lines = lines->mapnew((_, v) =>
Bram Moolenaar94722c52023-01-28 19:19:03 +0000305 v->substitute('\<VAR\>', 'var', 'g')
306 ->substitute('\<LET ', '', 'g'))
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200307 CheckDefExecFailure(vim9lines, defError)
308 CheckScriptFailure(['vim9script'] + vim9lines, scriptError)
309enddef