blob: 92ea0bad73d9c8a6c8d1e0f4324abc0dce6d7302 [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()
76 var fname = 'XScriptFailure' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010077 s:sequence += 1
78 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()
89 var fname = 'XScriptFailure' .. s:sequence
90 s:sequence += 1
91 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()
102 var fname = 'XScriptSuccess' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100103 s:sequence += 1
104 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
Bram Moolenaar62aec932022-01-29 21:45:34 +0000113export def CheckDefAndScriptSuccess(lines: list<string>)
Bram Moolenaar2e800952020-08-23 19:34:48 +0200114 CheckDefSuccess(lines)
115 CheckScriptSuccess(['vim9script'] + lines)
116enddef
117
Bram Moolenaar62aec932022-01-29 21:45:34 +0000118# Check that a command fails when used in a :def function and when used in
119# Vim9 script.
120# When "error" is a string, both with the same error.
121# When "error" is a list, the :def function fails with "error[0]" , the script
122# fails with "error[1]".
123export def CheckDefAndScriptFailure(lines: list<string>, error: any, lnum = -3)
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000124 var errorDef: string
125 var errorScript: string
126 if type(error) == v:t_string
127 errorDef = error
128 errorScript = error
129 elseif type(error) == v:t_list && len(error) == 2
130 errorDef = error[0]
131 errorScript = error[1]
132 else
133 echoerr 'error argument must be a string or a list with two items'
134 return
135 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200136 CheckDefFailure(lines, errorDef, lnum)
137 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
138enddef
139
Bram Moolenaar62aec932022-01-29 21:45:34 +0000140# Check that a command fails when executed in a :def function and when used in
141# Vim9 script.
142# When "error" is a string, both with the same error.
143# When "error" is a list, the :def function fails with "error[0]" , the script
144# fails with "error[1]".
145export def CheckDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3)
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000146 var errorDef: string
147 var errorScript: string
148 if type(error) == v:t_string
149 errorDef = error
150 errorScript = error
151 elseif type(error) == v:t_list && len(error) == 2
152 errorDef = error[0]
153 errorScript = error[1]
154 else
155 echoerr 'error argument must be a string or a list with two items'
156 return
157 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200158 CheckDefExecFailure(lines, errorDef, lnum)
159 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
160enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200161
162
Bram Moolenaar62aec932022-01-29 21:45:34 +0000163# Check that "lines" inside a legacy function has no error.
164export func CheckLegacySuccess(lines)
Bram Moolenaar68452172021-04-12 21:21:02 +0200165 let cwd = getcwd()
166 let fname = 'XlegacySuccess' .. s:sequence
167 let s:sequence += 1
168 call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
169 try
170 exe 'so ' .. fname
171 call Func()
Bram Moolenaar68452172021-04-12 21:21:02 +0200172 finally
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200173 delfunc! Func
174 call chdir(cwd)
175 call delete(fname)
176 endtry
177endfunc
178
Bram Moolenaar62aec932022-01-29 21:45:34 +0000179# Check that "lines" inside a legacy function results in the expected error
180export func CheckLegacyFailure(lines, error)
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200181 let cwd = getcwd()
182 let fname = 'XlegacyFails' .. s:sequence
183 let s:sequence += 1
184 call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
185 try
186 call assert_fails('so ' .. fname, a:error)
187 finally
188 delfunc! Func
Bram Moolenaar68452172021-04-12 21:21:02 +0200189 call chdir(cwd)
190 call delete(fname)
191 endtry
192endfunc
193
Bram Moolenaar62aec932022-01-29 21:45:34 +0000194# Execute "lines" in a legacy function, translated as in
195# CheckLegacyAndVim9Success()
196export def CheckTransLegacySuccess(lines: list<string>)
Bram Moolenaar68452172021-04-12 21:21:02 +0200197 var legacylines = lines->mapnew((_, v) =>
198 v->substitute('\<VAR\>', 'let', 'g')
199 ->substitute('\<LET\>', 'let', 'g')
Bram Moolenaaref982572021-08-12 19:27:57 +0200200 ->substitute('\<LSTART\>', '{', 'g')
201 ->substitute('\<LMIDDLE\>', '->', 'g')
202 ->substitute('\<LEND\>', '}', 'g')
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000203 ->substitute('\<TRUE\>', '1', 'g')
204 ->substitute('\<FALSE\>', '0', 'g')
Bram Moolenaar68452172021-04-12 21:21:02 +0200205 ->substitute('#"', ' "', 'g'))
206 CheckLegacySuccess(legacylines)
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200207enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200208
Bram Moolenaar62aec932022-01-29 21:45:34 +0000209export def Vim9Trans(lines: list<string>): list<string>
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000210 return lines->mapnew((_, v) =>
211 v->substitute('\<VAR\>', 'var', 'g')
212 ->substitute('\<LET ', '', 'g')
213 ->substitute('\<LSTART\>', '(', 'g')
214 ->substitute('\<LMIDDLE\>', ') =>', 'g')
215 ->substitute(' *\<LEND\> *', '', 'g')
216 ->substitute('\<TRUE\>', 'true', 'g')
217 ->substitute('\<FALSE\>', 'false', 'g'))
218enddef
219
Bram Moolenaar62aec932022-01-29 21:45:34 +0000220# Execute "lines" in a :def function, translated as in
221# CheckLegacyAndVim9Success()
222export def CheckTransDefSuccess(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000223 CheckDefSuccess(Vim9Trans(lines))
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200224enddef
225
Bram Moolenaar62aec932022-01-29 21:45:34 +0000226# Execute "lines" in a Vim9 script, translated as in
227# CheckLegacyAndVim9Success()
228export def CheckTransVim9Success(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000229 CheckScriptSuccess(['vim9script'] + Vim9Trans(lines))
Bram Moolenaar68452172021-04-12 21:21:02 +0200230enddef
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200231
Bram Moolenaar62aec932022-01-29 21:45:34 +0000232# Execute "lines" in a legacy function, :def function and Vim9 script.
233# Use 'VAR' for a declaration.
234# Use 'LET' for an assignment
235# Use ' #"' for a comment
236# Use LSTART arg LMIDDLE expr LEND for lambda
237# Use 'TRUE' for 1 in legacy, true in Vim9
238# Use 'FALSE' for 0 in legacy, false in Vim9
239export def CheckLegacyAndVim9Success(lines: list<string>)
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200240 CheckTransLegacySuccess(lines)
241 CheckTransDefSuccess(lines)
242 CheckTransVim9Success(lines)
243enddef
244
Bram Moolenaar62aec932022-01-29 21:45:34 +0000245# Execute "lines" in a legacy function, :def function and Vim9 script.
246# Use 'VAR' for a declaration.
247# Use 'LET' for an assignment
248# Use ' #"' for a comment
249export def CheckLegacyAndVim9Failure(lines: list<string>, error: any)
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200250 var legacyError: string
251 var defError: string
252 var scriptError: string
253
254 if type(error) == type('string')
255 legacyError = error
256 defError = error
257 scriptError = error
258 else
259 legacyError = error[0]
260 defError = error[1]
261 scriptError = error[2]
262 endif
263
264 var legacylines = lines->mapnew((_, v) =>
265 v->substitute('\<VAR\>', 'let', 'g')
266 ->substitute('\<LET\>', 'let', 'g')
267 ->substitute('#"', ' "', 'g'))
268 CheckLegacyFailure(legacylines, legacyError)
269
270 var vim9lines = lines->mapnew((_, v) =>
271 v->substitute('\<VAR\>', 'var', 'g')
272 ->substitute('\<LET ', '', 'g'))
273 CheckDefExecFailure(vim9lines, defError)
274 CheckScriptFailure(['vim9script'] + vim9lines, scriptError)
275enddef