comparison src/testdir/test_source.vim @ 28139:f34afadbef47 v8.2.4594

patch 8.2.4594: need to write script to a file to be able to source them Commit: https://github.com/vim/vim/commit/36a5b6867bb6c0bd69c8da7d788000ab8a0b0ab0 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sat Mar 19 12:56:51 2022 +0000 patch 8.2.4594: need to write script to a file to be able to source them Problem: Need to write script to a file to be able to source them. Solution: Make ":source" use lines from the current buffer. (Yegappan Lakshmanan et al., closes #9967)
author Bram Moolenaar <Bram@vim.org>
date Sat, 19 Mar 2022 14:00:03 +0100
parents fc859aea8cec
children e1d1fa6ba1ed
comparison
equal deleted inserted replaced
28138:d14809743aa0 28139:f34afadbef47
92 92
93 func Test_source_error() 93 func Test_source_error()
94 call assert_fails('scriptencoding utf-8', 'E167:') 94 call assert_fails('scriptencoding utf-8', 'E167:')
95 call assert_fails('finish', 'E168:') 95 call assert_fails('finish', 'E168:')
96 call assert_fails('scriptversion 2', 'E984:') 96 call assert_fails('scriptversion 2', 'E984:')
97 call assert_fails('source!', 'E471:')
98 new
99 call setline(1, ['', '', '', ''])
100 call assert_fails('1,3source Xscript.vim', 'E481:')
101 call assert_fails('1,3source! Xscript.vim', 'E481:')
102 bw!
97 endfunc 103 endfunc
98 104
99 " Test for sourcing a script recursively 105 " Test for sourcing a script recursively
100 func Test_nested_script() 106 func Test_nested_script()
101 CheckRunVimInTerminal 107 CheckRunVimInTerminal
108 call WaitForAssert({-> assert_match('E22: Scripts nested too deep\s*', term_getline(buf, 6))}) 114 call WaitForAssert({-> assert_match('E22: Scripts nested too deep\s*', term_getline(buf, 6))})
109 call delete('Xscript.vim') 115 call delete('Xscript.vim')
110 call StopVimInTerminal(buf) 116 call StopVimInTerminal(buf)
111 endfunc 117 endfunc
112 118
119 " Test for sourcing a script from the current buffer
120 func Test_source_buffer()
121 new
122 " Source a simple script
123 let lines =<< trim END
124 let a = "Test"
125 let b = 20
126
127 let c = [1.1]
128 END
129 call setline(1, lines)
130 source
131 call assert_equal(['Test', 20, [1.1]], [g:a, g:b, g:c])
132
133 " Source a range of lines in the current buffer
134 %d _
135 let lines =<< trim END
136 let a = 10
137 let a += 20
138 let a += 30
139 let a += 40
140 END
141 call setline(1, lines)
142 .source
143 call assert_equal(10, g:a)
144 3source
145 call assert_equal(40, g:a)
146 2,3source
147 call assert_equal(90, g:a)
148
149 " Source a script with line continuation lines
150 %d _
151 let lines =<< trim END
152 let m = [
153 \ 1,
154 \ 2,
155 \ ]
156 call add(m, 3)
157 END
158 call setline(1, lines)
159 source
160 call assert_equal([1, 2, 3], g:m)
161 " Source a script with line continuation lines and a comment
162 %d _
163 let lines =<< trim END
164 let m = [
165 "\ first entry
166 \ 'a',
167 "\ second entry
168 \ 'b',
169 \ ]
170 " third entry
171 call add(m, 'c')
172 END
173 call setline(1, lines)
174 source
175 call assert_equal(['a', 'b', 'c'], g:m)
176 " Source an incomplete line continuation line
177 %d _
178 let lines =<< trim END
179 let k = [
180 \
181 END
182 call setline(1, lines)
183 call assert_fails('source', 'E697:')
184 " Source a function with a for loop
185 %d _
186 let lines =<< trim END
187 let m = []
188 " test function
189 func! Xtest()
190 for i in range(5, 7)
191 call add(g:m, i)
192 endfor
193 endfunc
194 call Xtest()
195 END
196 call setline(1, lines)
197 source
198 call assert_equal([5, 6, 7], g:m)
199 " Source an empty buffer
200 %d _
201 source
202
203 " test for script local functions and variables
204 let lines =<< trim END
205 let s:var1 = 10
206 func s:F1()
207 let s:var1 += 1
208 return s:var1
209 endfunc
210 func s:F2()
211 endfunc
212 let g:ScriptID = expand("<SID>")
213 END
214 call setline(1, lines)
215 source
216 call assert_true(g:ScriptID != '')
217 call assert_true(exists('*' .. g:ScriptID .. 'F1'))
218 call assert_true(exists('*' .. g:ScriptID .. 'F2'))
219 call assert_equal(11, call(g:ScriptID .. 'F1', []))
220
221 " the same script ID should be used even if the buffer is sourced more than
222 " once
223 %d _
224 let lines =<< trim END
225 let g:ScriptID = expand("<SID>")
226 let g:Count += 1
227 END
228 call setline(1, lines)
229 let g:Count = 0
230 source
231 call assert_true(g:ScriptID != '')
232 let scid = g:ScriptID
233 source
234 call assert_equal(scid, g:ScriptID)
235 call assert_equal(2, g:Count)
236 source
237 call assert_equal(scid, g:ScriptID)
238 call assert_equal(3, g:Count)
239
240 " test for the script line number
241 %d _
242 let lines =<< trim END
243 " comment
244 let g:Slnum1 = expand("<slnum>")
245 let i = 1 +
246 \ 2 +
247 "\ comment
248 \ 3
249 let g:Slnum2 = expand("<slnum>")
250 END
251 call setline(1, lines)
252 source
253 call assert_equal('2', g:Slnum1)
254 call assert_equal('7', g:Slnum2)
255
256 " test for retaining the same script number across source calls
257 let lines =<< trim END
258 let g:ScriptID1 = expand("<SID>")
259 let g:Slnum1 = expand("<slnum>")
260 let l =<< trim END
261 let g:Slnum2 = expand("<slnum>")
262 let g:ScriptID2 = expand("<SID>")
263 END
264 new
265 call setline(1, l)
266 source
267 bw!
268 let g:ScriptID3 = expand("<SID>")
269 let g:Slnum3 = expand("<slnum>")
270 END
271 call writefile(lines, 'Xscript')
272 source Xscript
273 call assert_true(g:ScriptID1 != g:ScriptID2)
274 call assert_equal(g:ScriptID1, g:ScriptID3)
275 call assert_equal('2', g:Slnum1)
276 call assert_equal('1', g:Slnum2)
277 call assert_equal('12', g:Slnum3)
278 call delete('Xscript')
279
280 " test for sourcing a heredoc
281 %d _
282 let lines =<< trim END
283 let a = 1
284 let heredoc =<< trim DATA
285 red
286 green
287 blue
288 DATA
289 let b = 2
290 END
291 call setline(1, lines)
292 source
293 call assert_equal(['red', ' green', 'blue'], g:heredoc)
294
295 " test for a while and for statement
296 %d _
297 let lines =<< trim END
298 let a = 0
299 let b = 1
300 while b <= 10
301 let a += 10
302 let b += 1
303 endwhile
304 for i in range(5)
305 let a += 10
306 endfor
307 END
308 call setline(1, lines)
309 source
310 call assert_equal(150, g:a)
311
312 " test for sourcing the same buffer multiple times after changing a function
313 %d _
314 let lines =<< trim END
315 func Xtestfunc()
316 return "one"
317 endfunc
318 END
319 call setline(1, lines)
320 source
321 call assert_equal("one", Xtestfunc())
322 call setline(2, ' return "two"')
323 source
324 call assert_equal("two", Xtestfunc())
325 call setline(2, ' return "three"')
326 source
327 call assert_equal("three", Xtestfunc())
328 delfunc Xtestfunc
329
330 " test for sourcing a Vim9 script
331 %d _
332 let lines =<< trim END
333 vim9script
334
335 # check dict
336 var x: number = 10
337 def g:Xtestfunc(): number
338 return x
339 enddef
340 END
341 call setline(1, lines)
342 source
343 call assert_equal(10, Xtestfunc())
344
345 %bw!
346 endfunc
347
113 " vim: shiftwidth=2 sts=2 expandtab 348 " vim: shiftwidth=2 sts=2 expandtab