Mercurial > vim
comparison src/testdir/test_user_func.vim @ 30065:6cf788ab844c v9.0.0370
patch 9.0.0370: cleaning up afterwards can make a function messy
Commit: https://github.com/vim/vim/commit/1d84f7608f1e41dad03b8cc7925895437775f7c0
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Sep 3 21:35:53 2022 +0100
patch 9.0.0370: cleaning up afterwards can make a function messy
Problem: Cleaning up afterwards can make a function messy.
Solution: Add the :defer command.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 03 Sep 2022 22:45:03 +0200 |
parents | f24d4826e6bf |
children | 0352577f1ba5 |
comparison
equal
deleted
inserted
replaced
30064:a8f1fbaa43c8 | 30065:6cf788ab844c |
---|---|
527 "call assert_false(exists('*Xtestfunc')) | 527 "call assert_false(exists('*Xtestfunc')) |
528 "call assert_fails('delfunc Xtestfunc', 'E117:') | 528 "call assert_fails('delfunc Xtestfunc', 'E117:') |
529 bw! | 529 bw! |
530 endfunc | 530 endfunc |
531 | 531 |
532 func AddDefer(arg) | |
533 call extend(g:deferred, [a:arg]) | |
534 endfunc | |
535 | |
536 func WithDeferTwo() | |
537 call extend(g:deferred, ['in Two']) | |
538 for nr in range(3) | |
539 defer AddDefer('Two' .. nr) | |
540 endfor | |
541 call extend(g:deferred, ['end Two']) | |
542 endfunc | |
543 | |
544 func WithDeferOne() | |
545 call extend(g:deferred, ['in One']) | |
546 call writefile(['text'], 'Xfuncdefer') | |
547 defer delete('Xfuncdefer') | |
548 defer AddDefer('One') | |
549 call WithDeferTwo() | |
550 call extend(g:deferred, ['end One']) | |
551 endfunc | |
552 | |
553 func Test_defer() | |
554 let g:deferred = [] | |
555 call WithDeferOne() | |
556 | |
557 call assert_equal(['in One', 'in Two', 'end Two', 'Two2', 'Two1', 'Two0', 'end One', 'One'], g:deferred) | |
558 unlet g:deferred | |
559 | |
560 call assert_equal('', glob('Xfuncdefer')) | |
561 endfunc | |
562 | |
563 | |
532 " vim: shiftwidth=2 sts=2 expandtab | 564 " vim: shiftwidth=2 sts=2 expandtab |