blob: 7e50e78ba41a737522528b891b0a40c6456c124d [file] [log] [blame]
Bram Moolenaar12a96de2018-03-11 14:44:18 +01001" Tests for exiting Vim.
2
3source shared.vim
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +00004source check.vim
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005
6func Test_exiting()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02007 let after =<< trim [CODE]
8 au QuitPre * call writefile(["QuitPre"], "Xtestout")
9 au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")
10 quit
11 [CODE]
12
Bram Moolenaar12a96de2018-03-11 14:44:18 +010013 if RunVim([], after, '')
14 call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout'))
15 endif
16 call delete('Xtestout')
17
Bram Moolenaarc79745a2019-05-20 22:12:34 +020018 let after =<< trim [CODE]
19 au QuitPre * call writefile(["QuitPre"], "Xtestout")
20 au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")
21 help
22 wincmd w
23 quit
24 [CODE]
25
Bram Moolenaar12a96de2018-03-11 14:44:18 +010026 if RunVim([], after, '')
27 call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout'))
28 endif
29 call delete('Xtestout')
30
Bram Moolenaarc79745a2019-05-20 22:12:34 +020031 let after =<< trim [CODE]
32 au QuitPre * call writefile(["QuitPre"], "Xtestout")
33 au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")
34 split
35 new
36 qall
37 [CODE]
38
Bram Moolenaar12a96de2018-03-11 14:44:18 +010039 if RunVim([], after, '')
40 call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout'))
41 endif
42 call delete('Xtestout')
43
Bram Moolenaar34ba06b2019-10-20 22:27:10 +020044 " ExitPre autocommand splits the window, so that it's no longer the last one.
Bram Moolenaarc79745a2019-05-20 22:12:34 +020045 let after =<< trim [CODE]
46 au QuitPre * call writefile(["QuitPre"], "Xtestout", "a")
47 au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")
48 augroup nasty
49 au ExitPre * split
50 augroup END
51 quit
52 augroup nasty
53 au! ExitPre
54 augroup END
55 quit
56 [CODE]
57
Bram Moolenaar12a96de2018-03-11 14:44:18 +010058 if RunVim([], after, '')
59 call assert_equal(['QuitPre', 'ExitPre', 'QuitPre', 'ExitPre'],
60 \ readfile('Xtestout'))
61 endif
62 call delete('Xtestout')
Bram Moolenaar34ba06b2019-10-20 22:27:10 +020063
64 " ExitPre autocommand splits and closes the window, so that there is still
65 " one window but it's a different one.
66 let after =<< trim [CODE]
67 au QuitPre * call writefile(["QuitPre"], "Xtestout", "a")
68 au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")
69 augroup nasty
70 au ExitPre * split | only
71 augroup END
72 quit
73 augroup nasty
74 au! ExitPre
75 augroup END
76 quit
77 [CODE]
78
79 if RunVim([], after, '')
80 call assert_equal(['QuitPre', 'ExitPre', 'QuitPre', 'ExitPre'],
81 \ readfile('Xtestout'))
82 endif
83 call delete('Xtestout')
Bram Moolenaar12a96de2018-03-11 14:44:18 +010084endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020085
Bram Moolenaarf0068c52020-11-30 17:42:10 +010086" Test for getting the Vim exit code from v:exiting
87func Test_exit_code()
88 call assert_equal(v:null, v:exiting)
89
90 let before =<< trim [CODE]
91 au QuitPre * call writefile(['qp = ' .. v:exiting], 'Xtestout', 'a')
92 au ExitPre * call writefile(['ep = ' .. v:exiting], 'Xtestout', 'a')
93 au VimLeavePre * call writefile(['lp = ' .. v:exiting], 'Xtestout', 'a')
94 au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout', 'a')
95 [CODE]
96
97 if RunVim(before, ['quit'], '')
98 call assert_equal(['qp = v:null', 'ep = v:null', 'lp = 0', 'l = 0'], readfile('Xtestout'))
99 endif
100 call delete('Xtestout')
101
102 if RunVim(before, ['cquit'], '')
103 call assert_equal(['lp = 1', 'l = 1'], readfile('Xtestout'))
104 endif
105 call delete('Xtestout')
106
107 if RunVim(before, ['cquit 4'], '')
108 call assert_equal(['lp = 4', 'l = 4'], readfile('Xtestout'))
109 endif
110 call delete('Xtestout')
111endfunc
112
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000113func Test_exit_error_reading_input()
114 CheckNotGui
Bram Moolenaar29a9e692022-02-22 18:48:11 +0000115 CheckNotMSWindows
Bram Moolenaar68eab672022-02-22 17:42:48 +0000116 " The early exit causes memory not to be freed somehow
117 CheckNotAsan
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000118
Bram Moolenaar68eab672022-02-22 17:42:48 +0000119 call writefile([":au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout')", ":tabnew", "q:"], 'Xscript', 'b')
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000120
Bram Moolenaar68eab672022-02-22 17:42:48 +0000121 if RunVim([], [], '<Xscript')
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000122 call assert_equal(['l = 1'], readfile('Xtestout'))
123 endif
124 call delete('Xscript')
125 call delete('Xtestout')
126endfun
127
128
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200129" vim: shiftwidth=2 sts=2 expandtab