blob: 89f3c791786bfadb0cbbd632274ddc88b9f03199 [file] [log] [blame]
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001" Utility functions for testing vim9 script
2
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003" Use a different file name for each run.
4let s:sequence = 1
5
Bram Moolenaar08018222021-12-22 18:45:37 +00006" Check that "lines" inside a ":def" function has no error when called.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02007func CheckDefSuccess(lines)
Bram Moolenaar090728a2020-12-20 15:43:31 +01008 let cwd = getcwd()
9 let fname = 'XdefSuccess' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010010 let s:sequence += 1
11 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +010012 try
13 exe 'so ' .. fname
14 call Func()
Bram Moolenaar090728a2020-12-20 15:43:31 +010015 finally
16 call chdir(cwd)
17 call delete(fname)
Bram Moolenaar88c89c72021-08-14 14:01:05 +020018 delfunc! Func
Bram Moolenaar090728a2020-12-20 15:43:31 +010019 endtry
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +020020endfunc
21
Bram Moolenaar08018222021-12-22 18:45:37 +000022" Check that "lines" inside a ":def" function has no error when compiled.
23func CheckDefCompileSuccess(lines)
24 let fname = 'XdefSuccess' .. s:sequence
25 let s:sequence += 1
26 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname)
27 try
28 exe 'so ' .. fname
29 finally
30 call delete(fname)
31 delfunc! Func
32 endtry
33endfunc
34
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020035" Check that "lines" inside ":def" results in an "error" message.
Bram Moolenaar1d634542020-08-18 13:41:50 +020036" If "lnum" is given check that the error is reported for this line.
Bram Moolenaar3affe7a2020-08-18 20:34:13 +020037" Add a line before and after to make it less likely that the line number is
38" accidentally correct.
39func CheckDefFailure(lines, error, lnum = -3)
Bram Moolenaar090728a2020-12-20 15:43:31 +010040 let cwd = getcwd()
41 let fname = 'XdefFailure' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010042 let s:sequence += 1
Bram Moolenaar090728a2020-12-20 15:43:31 +010043 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile'], fname)
44 try
45 call assert_fails('so ' .. fname, a:error, a:lines, a:lnum + 1)
46 finally
47 call chdir(cwd)
48 call delete(fname)
49 delfunc! Func
50 endtry
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020051endfunc
52
Bram Moolenaar015f4262020-05-05 21:25:22 +020053" Check that "lines" inside ":def" results in an "error" message when executed.
Bram Moolenaar1d634542020-08-18 13:41:50 +020054" If "lnum" is given check that the error is reported for this line.
Bram Moolenaar3affe7a2020-08-18 20:34:13 +020055" Add a line before and after to make it less likely that the line number is
56" accidentally correct.
57func CheckDefExecFailure(lines, error, lnum = -3)
Bram Moolenaar090728a2020-12-20 15:43:31 +010058 let cwd = getcwd()
59 let fname = 'XdefExecFailure' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010060 let s:sequence += 1
61 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef'], fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +010062 try
63 exe 'so ' .. fname
64 call assert_fails('call Func()', a:error, a:lines, a:lnum + 1)
65 finally
66 call chdir(cwd)
67 call delete(fname)
68 delfunc! Func
69 endtry
Bram Moolenaar015f4262020-05-05 21:25:22 +020070endfunc
71
Bram Moolenaar3affe7a2020-08-18 20:34:13 +020072def CheckScriptFailure(lines: list<string>, error: string, lnum = -3)
Bram Moolenaar090728a2020-12-20 15:43:31 +010073 var cwd = getcwd()
74 var fname = 'XScriptFailure' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +010075 s:sequence += 1
76 writefile(lines, fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +010077 try
78 assert_fails('so ' .. fname, error, lines, lnum)
79 finally
80 chdir(cwd)
81 delete(fname)
82 endtry
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020083enddef
84
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010085def CheckScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3)
86 var cwd = getcwd()
87 var fname = 'XScriptFailure' .. s:sequence
88 s:sequence += 1
89 writefile(lines, fname)
90 try
91 assert_fails('so ' .. fname, errors, lines, lnum)
92 finally
93 chdir(cwd)
94 delete(fname)
95 endtry
96enddef
97
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020098def CheckScriptSuccess(lines: list<string>)
Bram Moolenaar090728a2020-12-20 15:43:31 +010099 var cwd = getcwd()
100 var fname = 'XScriptSuccess' .. s:sequence
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100101 s:sequence += 1
102 writefile(lines, fname)
Bram Moolenaar090728a2020-12-20 15:43:31 +0100103 try
104 exe 'so ' .. fname
105 finally
106 chdir(cwd)
107 delete(fname)
108 endtry
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200109enddef
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200110
Bram Moolenaar2e800952020-08-23 19:34:48 +0200111def CheckDefAndScriptSuccess(lines: list<string>)
112 CheckDefSuccess(lines)
113 CheckScriptSuccess(['vim9script'] + lines)
114enddef
115
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000116" Check that a command fails when used in a :def function and when used in
117" Vim9 script.
118" 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
120" fails with "error[1]".
121def CheckDefAndScriptFailure(lines: list<string>, error: any, lnum = -3)
122 var errorDef: string
123 var errorScript: string
124 if type(error) == v:t_string
125 errorDef = error
126 errorScript = error
127 elseif type(error) == v:t_list && len(error) == 2
128 errorDef = error[0]
129 errorScript = error[1]
130 else
131 echoerr 'error argument must be a string or a list with two items'
132 return
133 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200134 CheckDefFailure(lines, errorDef, lnum)
135 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
136enddef
137
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000138" Check that a command fails when executed in a :def function and when used in
139" Vim9 script.
140" 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
142" fails with "error[1]".
143def CheckDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3)
144 var errorDef: string
145 var errorScript: string
146 if type(error) == v:t_string
147 errorDef = error
148 errorScript = error
149 elseif type(error) == v:t_list && len(error) == 2
150 errorDef = error[0]
151 errorScript = error[1]
152 else
153 echoerr 'error argument must be a string or a list with two items'
154 return
155 endif
Bram Moolenaar90193e62021-04-04 20:49:50 +0200156 CheckDefExecFailure(lines, errorDef, lnum)
157 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
158enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200159
160
161" Check that "lines" inside a legacy function has no error.
162func CheckLegacySuccess(lines)
163 let cwd = getcwd()
164 let fname = 'XlegacySuccess' .. s:sequence
165 let s:sequence += 1
166 call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
167 try
168 exe 'so ' .. fname
169 call Func()
Bram Moolenaar68452172021-04-12 21:21:02 +0200170 finally
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200171 delfunc! Func
172 call chdir(cwd)
173 call delete(fname)
174 endtry
175endfunc
176
177" Check that "lines" inside a legacy function results in the expected error
178func CheckLegacyFailure(lines, error)
179 let cwd = getcwd()
180 let fname = 'XlegacyFails' .. s:sequence
181 let s:sequence += 1
182 call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
183 try
184 call assert_fails('so ' .. fname, a:error)
185 finally
186 delfunc! Func
Bram Moolenaar68452172021-04-12 21:21:02 +0200187 call chdir(cwd)
188 call delete(fname)
189 endtry
190endfunc
191
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200192" Execute "lines" in a legacy function, translated as in
193" CheckLegacyAndVim9Success()
194def CheckTransLegacySuccess(lines: list<string>)
Bram Moolenaar68452172021-04-12 21:21:02 +0200195 var legacylines = lines->mapnew((_, v) =>
196 v->substitute('\<VAR\>', 'let', 'g')
197 ->substitute('\<LET\>', 'let', 'g')
Bram Moolenaaref982572021-08-12 19:27:57 +0200198 ->substitute('\<LSTART\>', '{', 'g')
199 ->substitute('\<LMIDDLE\>', '->', 'g')
200 ->substitute('\<LEND\>', '}', 'g')
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000201 ->substitute('\<TRUE\>', '1', 'g')
202 ->substitute('\<FALSE\>', '0', 'g')
Bram Moolenaar68452172021-04-12 21:21:02 +0200203 ->substitute('#"', ' "', 'g'))
204 CheckLegacySuccess(legacylines)
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200205enddef
Bram Moolenaar68452172021-04-12 21:21:02 +0200206
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000207def Vim9Trans(lines: list<string>): list<string>
208 return lines->mapnew((_, v) =>
209 v->substitute('\<VAR\>', 'var', 'g')
210 ->substitute('\<LET ', '', 'g')
211 ->substitute('\<LSTART\>', '(', 'g')
212 ->substitute('\<LMIDDLE\>', ') =>', 'g')
213 ->substitute(' *\<LEND\> *', '', 'g')
214 ->substitute('\<TRUE\>', 'true', 'g')
215 ->substitute('\<FALSE\>', 'false', 'g'))
216enddef
217
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200218" Execute "lines" in a :def function, translated as in
219" CheckLegacyAndVim9Success()
220def CheckTransDefSuccess(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000221 CheckDefSuccess(Vim9Trans(lines))
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200222enddef
223
224" Execute "lines" in a Vim9 script, translated as in
225" CheckLegacyAndVim9Success()
226def CheckTransVim9Success(lines: list<string>)
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000227 CheckScriptSuccess(['vim9script'] + Vim9Trans(lines))
Bram Moolenaar68452172021-04-12 21:21:02 +0200228enddef
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200229
230" Execute "lines" in a legacy function, :def function and Vim9 script.
231" Use 'VAR' for a declaration.
232" Use 'LET' for an assignment
233" Use ' #"' for a comment
Bram Moolenaaref982572021-08-12 19:27:57 +0200234" Use LSTART arg LMIDDLE expr LEND for lambda
Bram Moolenaarfea43e42021-12-19 21:34:05 +0000235" Use 'TRUE' for 1 in legacy, true in Vim9
236" Use 'FALSE' for 0 in legacy, false in Vim9
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200237def CheckLegacyAndVim9Success(lines: list<string>)
238 CheckTransLegacySuccess(lines)
239 CheckTransDefSuccess(lines)
240 CheckTransVim9Success(lines)
241enddef
242
243" Execute "lines" in a legacy function, :def function and Vim9 script.
244" Use 'VAR' for a declaration.
245" Use 'LET' for an assignment
246" Use ' #"' for a comment
Bram Moolenaar0e3ff192021-04-14 20:35:23 +0200247def CheckLegacyAndVim9Failure(lines: list<string>, error: any)
248 var legacyError: string
249 var defError: string
250 var scriptError: string
251
252 if type(error) == type('string')
253 legacyError = error
254 defError = error
255 scriptError = error
256 else
257 legacyError = error[0]
258 defError = error[1]
259 scriptError = error[2]
260 endif
261
262 var legacylines = lines->mapnew((_, v) =>
263 v->substitute('\<VAR\>', 'let', 'g')
264 ->substitute('\<LET\>', 'let', 'g')
265 ->substitute('#"', ' "', 'g'))
266 CheckLegacyFailure(legacylines, legacyError)
267
268 var vim9lines = lines->mapnew((_, v) =>
269 v->substitute('\<VAR\>', 'var', 'g')
270 ->substitute('\<LET ', '', 'g'))
271 CheckDefExecFailure(vim9lines, defError)
272 CheckScriptFailure(['vim9script'] + vim9lines, scriptError)
273enddef