xref: /vim-8.2.3635/src/testdir/vim9.vim (revision 88c89c77)
1" Utility functions for testing vim9 script
2
3" Use a different file name for each run.
4let s:sequence = 1
5
6" Check that "lines" inside a ":def" function has no error.
7func CheckDefSuccess(lines)
8  let cwd = getcwd()
9  let fname = 'XdefSuccess' .. s:sequence
10  let s:sequence += 1
11  call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname)
12  try
13    exe 'so ' .. fname
14    call Func()
15  finally
16    call chdir(cwd)
17    call delete(fname)
18    delfunc! Func
19  endtry
20endfunc
21
22" Check that "lines" inside ":def" results in an "error" message.
23" If "lnum" is given check that the error is reported for this line.
24" Add a line before and after to make it less likely that the line number is
25" accidentally correct.
26func CheckDefFailure(lines, error, lnum = -3)
27  let cwd = getcwd()
28  let fname = 'XdefFailure' .. s:sequence
29  let s:sequence += 1
30  call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile'], fname)
31  try
32    call assert_fails('so ' .. fname, a:error, a:lines, a:lnum + 1)
33  finally
34    call chdir(cwd)
35    call delete(fname)
36    delfunc! Func
37  endtry
38endfunc
39
40" Check that "lines" inside ":def" results in an "error" message when executed.
41" If "lnum" is given check that the error is reported for this line.
42" Add a line before and after to make it less likely that the line number is
43" accidentally correct.
44func CheckDefExecFailure(lines, error, lnum = -3)
45  let cwd = getcwd()
46  let fname = 'XdefExecFailure' .. s:sequence
47  let s:sequence += 1
48  call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef'], fname)
49  try
50    exe 'so ' .. fname
51    call assert_fails('call Func()', a:error, a:lines, a:lnum + 1)
52  finally
53    call chdir(cwd)
54    call delete(fname)
55    delfunc! Func
56  endtry
57endfunc
58
59def CheckScriptFailure(lines: list<string>, error: string, lnum = -3)
60  var cwd = getcwd()
61  var fname = 'XScriptFailure' .. s:sequence
62  s:sequence += 1
63  writefile(lines, fname)
64  try
65    assert_fails('so ' .. fname, error, lines, lnum)
66  finally
67    chdir(cwd)
68    delete(fname)
69  endtry
70enddef
71
72def CheckScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3)
73  var cwd = getcwd()
74  var fname = 'XScriptFailure' .. s:sequence
75  s:sequence += 1
76  writefile(lines, fname)
77  try
78    assert_fails('so ' .. fname, errors, lines, lnum)
79  finally
80    chdir(cwd)
81    delete(fname)
82  endtry
83enddef
84
85def CheckScriptSuccess(lines: list<string>)
86  var cwd = getcwd()
87  var fname = 'XScriptSuccess' .. s:sequence
88  s:sequence += 1
89  writefile(lines, fname)
90  try
91    exe 'so ' .. fname
92  finally
93    chdir(cwd)
94    delete(fname)
95  endtry
96enddef
97
98def CheckDefAndScriptSuccess(lines: list<string>)
99  CheckDefSuccess(lines)
100  CheckScriptSuccess(['vim9script'] + lines)
101enddef
102
103" Check that a command fails with the same error when used in a :def function
104" and when used in Vim9 script.
105def CheckDefAndScriptFailure(lines: list<string>, error: string, lnum = -3)
106  CheckDefFailure(lines, error, lnum)
107  CheckScriptFailure(['vim9script'] + lines, error, lnum + 1)
108enddef
109
110" As CheckDefAndScriptFailure() but with two different expected errors.
111def CheckDefAndScriptFailure2(
112  	lines: list<string>,
113	errorDef: string,
114	errorScript: string,
115	lnum = -3)
116  CheckDefFailure(lines, errorDef, lnum)
117  CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
118enddef
119
120" Check that a command fails with the same error  when executed in a :def
121" function and when used in Vim9 script.
122def CheckDefExecAndScriptFailure(lines: list<string>, error: string, lnum = -3)
123  CheckDefExecFailure(lines, error, lnum)
124  CheckScriptFailure(['vim9script'] + lines, error, lnum + 1)
125enddef
126
127" As CheckDefExecAndScriptFailure() but with two different expected errors.
128def CheckDefExecAndScriptFailure2(
129  	lines: list<string>,
130	errorDef: string,
131	errorScript: string,
132	lnum = -3)
133  CheckDefExecFailure(lines, errorDef, lnum)
134  CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1)
135enddef
136
137
138" Check that "lines" inside a legacy function has no error.
139func CheckLegacySuccess(lines)
140  let cwd = getcwd()
141  let fname = 'XlegacySuccess' .. s:sequence
142  let s:sequence += 1
143  call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
144  try
145    exe 'so ' .. fname
146    call Func()
147  finally
148    delfunc! Func
149    call chdir(cwd)
150    call delete(fname)
151  endtry
152endfunc
153
154" Check that "lines" inside a legacy function results in the expected error
155func CheckLegacyFailure(lines, error)
156  let cwd = getcwd()
157  let fname = 'XlegacyFails' .. s:sequence
158  let s:sequence += 1
159  call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
160  try
161    call assert_fails('so ' .. fname, a:error)
162  finally
163    delfunc! Func
164    call chdir(cwd)
165    call delete(fname)
166  endtry
167endfunc
168
169" Execute "lines" in a legacy function, translated as in
170" CheckLegacyAndVim9Success()
171def CheckTransLegacySuccess(lines: list<string>)
172  var legacylines = lines->mapnew((_, v) =>
173  				v->substitute('\<VAR\>', 'let', 'g')
174		           	 ->substitute('\<LET\>', 'let', 'g')
175		           	 ->substitute('\<LSTART\>', '{', 'g')
176		           	 ->substitute('\<LMIDDLE\>', '->', 'g')
177				 ->substitute('\<LEND\>', '}', 'g')
178		           	 ->substitute('#"', ' "', 'g'))
179  CheckLegacySuccess(legacylines)
180enddef
181
182" Execute "lines" in a :def function, translated as in
183" CheckLegacyAndVim9Success()
184def CheckTransDefSuccess(lines: list<string>)
185  var vim9lines = lines->mapnew((_, v) =>
186  				v->substitute('\<VAR\>', 'var', 'g')
187		           	 ->substitute('\<LET ', '', 'g')
188		           	 ->substitute('\<LSTART\>', '(', 'g')
189		           	 ->substitute('\<LMIDDLE\>', ') =>', 'g')
190				 ->substitute(' *\<LEND\> *', '', 'g'))
191  CheckDefSuccess(vim9lines)
192enddef
193
194" Execute "lines" in a Vim9 script, translated as in
195" CheckLegacyAndVim9Success()
196def CheckTransVim9Success(lines: list<string>)
197  var vim9lines = lines->mapnew((_, v) =>
198  				v->substitute('\<VAR\>', 'var', 'g')
199		           	 ->substitute('\<LET ', '', 'g')
200		           	 ->substitute('\<LSTART\>', '(', 'g')
201		           	 ->substitute('\<LMIDDLE\>', ') =>', 'g')
202				 ->substitute(' *\<LEND\> *', '', 'g'))
203  CheckScriptSuccess(['vim9script'] + vim9lines)
204enddef
205
206" Execute "lines" in a legacy function, :def function and Vim9 script.
207" Use 'VAR' for a declaration.
208" Use 'LET' for an assignment
209" Use ' #"' for a comment
210" Use LSTART arg LMIDDLE expr LEND for lambda
211def CheckLegacyAndVim9Success(lines: list<string>)
212  CheckTransLegacySuccess(lines)
213  CheckTransDefSuccess(lines)
214  CheckTransVim9Success(lines)
215enddef
216
217" Execute "lines" in a legacy function, :def function and Vim9 script.
218" Use 'VAR' for a declaration.
219" Use 'LET' for an assignment
220" Use ' #"' for a comment
221def CheckLegacyAndVim9Failure(lines: list<string>, error: any)
222  var legacyError: string
223  var defError: string
224  var scriptError: string
225
226  if type(error) == type('string')
227    legacyError = error
228    defError = error
229    scriptError = error
230  else
231    legacyError = error[0]
232    defError = error[1]
233    scriptError = error[2]
234  endif
235
236  var legacylines = lines->mapnew((_, v) =>
237  				v->substitute('\<VAR\>', 'let', 'g')
238		           	 ->substitute('\<LET\>', 'let', 'g')
239		           	 ->substitute('#"', ' "', 'g'))
240  CheckLegacyFailure(legacylines, legacyError)
241
242  var vim9lines = lines->mapnew((_, v) =>
243  				v->substitute('\<VAR\>', 'var', 'g')
244		           	 ->substitute('\<LET ', '', 'g'))
245  CheckDefExecFailure(vim9lines, defError)
246  CheckScriptFailure(['vim9script'] + vim9lines, scriptError)
247enddef
248