1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
" Use a different file name for each run.
let s:sequence = 1
func CheckDefSuccess(lines)
return
endfunc
func CheckDefFailure(lines, error, lnum = -3)
return
endfunc
func CheckDefExecFailure(lines, error, lnum = -3)
return
endfunc
func CheckScriptFailure(lines, error, lnum = -3)
if get(a:lines, 0, '') ==# 'vim9script'
return
endif
let cwd = getcwd()
let fname = 'XScriptFailure' .. s:sequence
let s:sequence += 1
call writefile(a:lines, fname)
try
call assert_fails('so ' .. fname, a:error, a:lines, a:lnum)
finally
call chdir(cwd)
call delete(fname)
endtry
endfunc
func CheckScriptSuccess(lines)
if get(a:lines, 0, '') ==# 'vim9script'
return
endif
let cwd = getcwd()
let fname = 'XScriptSuccess' .. s:sequence
let s:sequence += 1
call writefile(a:lines, fname)
try
exe 'so ' .. fname
finally
call chdir(cwd)
call delete(fname)
endtry
endfunc
" :source a list of "lines" and check whether it fails with "error"
func CheckSourceFailure(lines, error, lnum = -3)
if get(a:lines, 0, '') ==# 'vim9script'
return
endif
new
call setline(1, a:lines)
try
call assert_fails('source', a:error, a:lines, a:lnum)
finally
bw!
endtry
endfunc
" :source a list of "lines" and check whether it fails with the list of
" "errors"
func CheckSourceFailureList(lines, errors, lnum = -3)
if get(a:lines, 0, '') ==# 'vim9script'
return
endif
new
call setline(1, a:lines)
try
call assert_fails('source', a:errors, a:lines, a:lnum)
finally
bw!
endtry
endfunc
" :source a list of "lines" and check whether it succeeds
func CheckSourceSuccess(lines)
if get(a:lines, 0, '') ==# 'vim9script'
return
endif
new
call setline(1, a:lines)
try
:source
finally
bw!
endtry
endfunc
func CheckDefAndScriptSuccess(lines)
return
endfunc
func CheckDefAndScriptFailure(lines, error, lnum = -3)
return
endfunc
func CheckDefExecAndScriptFailure(lines, error, lnum = -3)
return
endfunc
" Check that "lines" inside a legacy function has no error.
func CheckLegacySuccess(lines)
let cwd = getcwd()
let fname = 'XlegacySuccess' .. s:sequence
let s:sequence += 1
call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
try
exe 'so ' .. fname
call Func()
finally
delfunc! Func
call chdir(cwd)
call delete(fname)
endtry
endfunc
" Check that "lines" inside a legacy function results in the expected error
func CheckLegacyFailure(lines, error)
let cwd = getcwd()
let fname = 'XlegacyFails' .. s:sequence
let s:sequence += 1
call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
try
call assert_fails('so ' .. fname, a:error)
finally
delfunc! Func
call chdir(cwd)
call delete(fname)
endtry
endfunc
" Execute "lines" in a legacy function, translated as in
" CheckLegacyAndVim9Success()
func CheckTransLegacySuccess(lines)
let legacylines = a:lines->mapnew({_, v ->
\ v->substitute('\<VAR\>', 'let', 'g')
\ ->substitute('\<LET\>', 'let', 'g')
\ ->substitute('\<LSTART\>', '{', 'g')
\ ->substitute('\<LMIDDLE\>', '->', 'g')
\ ->substitute('\<LEND\>', '}', 'g')
\ ->substitute('\<TRUE\>', '1', 'g')
\ ->substitute('\<FALSE\>', '0', 'g')
\ ->substitute('#"', ' "', 'g')
\ })
call CheckLegacySuccess(legacylines)
endfunc
func CheckTransDefSuccess(lines)
return
endfunc
func CheckTransVim9Success(lines)
return
endfunc
" Execute "lines" in a legacy function
" Use 'VAR' for a declaration.
" Use 'LET' for an assignment
" Use ' #"' for a comment
" Use LSTART arg LMIDDLE expr LEND for lambda
" Use 'TRUE' for 1
" Use 'FALSE' for 0
func CheckLegacyAndVim9Success(lines)
call CheckTransLegacySuccess(a:lines)
endfunc
" Execute "lines" in a legacy function
" Use 'VAR' for a declaration.
" Use 'LET' for an assignment
" Use ' #"' for a comment
func CheckLegacyAndVim9Failure(lines, error)
if type(a:error) == type('string')
let legacyError = a:error
else
let legacyError = a:error[0]
endif
let legacylines = a:lines->mapnew({_, v ->
\ v->substitute('\<VAR\>', 'let', 'g')
\ ->substitute('\<LET\>', 'let', 'g')
\ ->substitute('#"', ' "', 'g')
\ })
call CheckLegacyFailure(legacylines, legacyError)
endfunc
|