xref: /vim-8.2.3635/src/testdir/vim9.vim (revision 130cbfc3)
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    delfunc! Func
16  finally
17    call chdir(cwd)
18    call delete(fname)
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 exepcted 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