comparison src/testdir/test_source.vim @ 28158:e1d1fa6ba1ed v8.2.4603

patch 8.2.4603: sourcing buffer lines is too complicated Commit: https://github.com/vim/vim/commit/85b43c6cb7d56919e245622f4e42db6d8bee4194 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Mon Mar 21 19:45:17 2022 +0000 patch 8.2.4603: sourcing buffer lines is too complicated Problem: Sourcing buffer lines is too complicated. Solution: Simplify the code. Make it possible to source Vim9 script lines. (Yegappan Lakshmanan, closes #9974)
author Bram Moolenaar <Bram@vim.org>
date Mon, 21 Mar 2022 21:00:04 +0100
parents f34afadbef47
children b0b712d48225
comparison
equal deleted inserted replaced
28157:81034dff92a4 28158:e1d1fa6ba1ed
144 3source 144 3source
145 call assert_equal(40, g:a) 145 call assert_equal(40, g:a)
146 2,3source 146 2,3source
147 call assert_equal(90, g:a) 147 call assert_equal(90, g:a)
148 148
149 " Make sure the script line number is correct when sourcing a range of
150 " lines.
151 %d _
152 let lines =<< trim END
153 Line 1
154 Line 2
155 func Xtestfunc()
156 return expand("<sflnum>")
157 endfunc
158 Line 3
159 Line 4
160 END
161 call setline(1, lines)
162 3,5source
163 call assert_equal('4', Xtestfunc())
164 delfunc Xtestfunc
165
149 " Source a script with line continuation lines 166 " Source a script with line continuation lines
150 %d _ 167 %d _
151 let lines =<< trim END 168 let lines =<< trim END
152 let m = [ 169 let m = [
153 \ 1, 170 \ 1,
325 call setline(2, ' return "three"') 342 call setline(2, ' return "three"')
326 source 343 source
327 call assert_equal("three", Xtestfunc()) 344 call assert_equal("three", Xtestfunc())
328 delfunc Xtestfunc 345 delfunc Xtestfunc
329 346
347 " test for using try/catch
348 %d _
349 let lines =<< trim END
350 let Trace = '1'
351 try
352 let a1 = b1
353 catch
354 let Trace ..= '2'
355 finally
356 let Trace ..= '3'
357 endtry
358 END
359 call setline(1, lines)
360 source
361 call assert_equal("123", g:Trace)
362
363 " test with the finish command
364 %d _
365 let lines =<< trim END
366 let g:Color = 'blue'
367 finish
368 let g:Color = 'green'
369 END
370 call setline(1, lines)
371 source
372 call assert_equal('blue', g:Color)
373
374 " Test for the SourcePre and SourcePost autocmds
375 augroup Xtest
376 au!
377 au SourcePre * let g:XsourcePre=4
378 \ | let g:XsourcePreFile = expand("<afile>")
379 au SourcePost * let g:XsourcePost=6
380 \ | let g:XsourcePostFile = expand("<afile>")
381 augroup END
382 %d _
383 let lines =<< trim END
384 let a = 1
385 END
386 call setline(1, lines)
387 source
388 call assert_equal(4, g:XsourcePre)
389 call assert_equal(6, g:XsourcePost)
390 call assert_equal(':source buffer=' .. bufnr(), g:XsourcePreFile)
391 call assert_equal(':source buffer=' .. bufnr(), g:XsourcePostFile)
392 augroup Xtest
393 au!
394 augroup END
395 augroup! Xtest
396
397 %bw!
398 endfunc
399
400 " Test for sourcing a Vim9 script from the current buffer
401 func Test_source_buffer_vim9()
402 new
403
330 " test for sourcing a Vim9 script 404 " test for sourcing a Vim9 script
331 %d _ 405 %d _
332 let lines =<< trim END 406 let lines =<< trim END
333 vim9script 407 vim9script
334 408
340 END 414 END
341 call setline(1, lines) 415 call setline(1, lines)
342 source 416 source
343 call assert_equal(10, Xtestfunc()) 417 call assert_equal(10, Xtestfunc())
344 418
419 " test for sourcing a vim9 script with line continuation
420 %d _
421 let lines =<< trim END
422 vim9script
423
424 g:Str1 = "hello "
425 .. "world"
426 .. ", how are you?"
427 g:Colors = [
428 'red',
429 # comment
430 'blue'
431 ]
432 g:Dict = {
433 a: 22,
434 # comment
435 b: 33
436 }
437
438 # calling a function with line continuation
439 def Sum(...values: list<number>): number
440 var sum: number = 0
441 for v in values
442 sum += v
443 endfor
444 return sum
445 enddef
446 g:Total1 = Sum(10,
447 20,
448 30)
449
450 var i: number = 0
451 while i < 10
452 # while loop
453 i +=
454 1
455 endwhile
456 g:Count1 = i
457
458 # for loop
459 g:Count2 = 0
460 for j in range(10, 20)
461 g:Count2 +=
462 i
463 endfor
464
465 g:Total2 = 10 +
466 20 -
467 5
468
469 g:Result1 = g:Total2 > 1
470 ? 'red'
471 : 'blue'
472
473 g:Str2 = 'x'
474 ->repeat(10)
475 ->trim()
476 ->strpart(4)
477
478 g:Result2 = g:Dict
479 .a
480
481 augroup Test
482 au!
483 au BufNewFile Xfile g:readFile = 1
484 | g:readExtra = 2
485 augroup END
486 g:readFile = 0
487 g:readExtra = 0
488 new Xfile
489 bwipe!
490 augroup Test
491 au!
492 augroup END
493 END
494 call setline(1, lines)
495 source
496 call assert_equal("hello world, how are you?", g:Str1)
497 call assert_equal(['red', 'blue'], g:Colors)
498 call assert_equal(#{a: 22, b: 33}, g:Dict)
499 call assert_equal(60, g:Total1)
500 call assert_equal(10, g:Count1)
501 call assert_equal(110, g:Count2)
502 call assert_equal(25, g:Total2)
503 call assert_equal('red', g:Result1)
504 call assert_equal('xxxxxx', g:Str2)
505 call assert_equal(22, g:Result2)
506 call assert_equal(1, g:readFile)
507 call assert_equal(2, g:readExtra)
508
509 " test for sourcing the same buffer multiple times after changing a function
510 %d _
511 let lines =<< trim END
512 vim9script
513 def g:Xtestfunc(): string
514 return "one"
515 enddef
516 END
517 call setline(1, lines)
518 source
519 call assert_equal("one", Xtestfunc())
520 call setline(3, ' return "two"')
521 source
522 call assert_equal("two", Xtestfunc())
523 call setline(3, ' return "three"')
524 source
525 call assert_equal("three", Xtestfunc())
526 delfunc Xtestfunc
527
528 " Test for sourcing a range of lines. Make sure the script line number is
529 " correct.
530 %d _
531 let lines =<< trim END
532 Line 1
533 Line 2
534 vim9script
535 def g:Xtestfunc(): string
536 return expand("<sflnum>")
537 enddef
538 Line 3
539 Line 4
540 END
541 call setline(1, lines)
542 3,6source
543 call assert_equal('5', Xtestfunc())
544 delfunc Xtestfunc
545
546 " test for sourcing a heredoc
547 %d _
548 let lines =<< trim END
549 vim9script
550 var a = 1
551 g:heredoc =<< trim DATA
552 red
553 green
554 blue
555 DATA
556 var b = 2
557 END
558 call setline(1, lines)
559 source
560 call assert_equal(['red', ' green', 'blue'], g:heredoc)
561
562 " test for using the :vim9cmd modifier
563 %d _
564 let lines =<< trim END
565 first line
566 g:Math = {
567 pi: 3.12,
568 e: 2.71828
569 }
570 g:Editors = [
571 'vim',
572 # comment
573 'nano'
574 ]
575 last line
576 END
577 call setline(1, lines)
578 vim9cmd :2,10source
579 call assert_equal(#{pi: 3.12, e: 2.71828}, g:Math)
580 call assert_equal(['vim', 'nano'], g:Editors)
581
582 " test for using try/catch
583 %d _
584 let lines =<< trim END
585 vim9script
586 g:Trace = '1'
587 try
588 a1 = b1
589 catch
590 g:Trace ..= '2'
591 finally
592 g:Trace ..= '3'
593 endtry
594 END
595 call setline(1, lines)
596 source
597 call assert_equal('123', g:Trace)
598
599 " test with the finish command
600 %d _
601 let lines =<< trim END
602 vim9script
603 g:Color = 'red'
604 finish
605 g:Color = 'blue'
606 END
607 call setline(1, lines)
608 source
609 call assert_equal('red', g:Color)
610
345 %bw! 611 %bw!
346 endfunc 612 endfunc
347 613
348 " vim: shiftwidth=2 sts=2 expandtab 614 " vim: shiftwidth=2 sts=2 expandtab