xref: /vim-8.2.3635/src/testdir/test_buffer.vim (revision 92c098d1)
1" Tests for Vim buffer
2
3" Test for the :bunload command with an offset
4func Test_bunload_with_offset()
5  %bwipe!
6  call writefile(['B1'], 'b1')
7  call writefile(['B2'], 'b2')
8  call writefile(['B3'], 'b3')
9  call writefile(['B4'], 'b4')
10
11  " Load four buffers. Unload the second and third buffers and then
12  " execute .+3bunload to unload the last buffer.
13  edit b1
14  new b2
15  new b3
16  new b4
17
18  bunload b2
19  bunload b3
20  exe bufwinnr('b1') . 'wincmd w'
21  .+3bunload
22  call assert_equal(0, getbufinfo('b4')[0].loaded)
23  call assert_equal('b1',
24        \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
25
26  " Load four buffers. Unload the third and fourth buffers. Execute .+3bunload
27  " and check whether the second buffer is unloaded.
28  ball
29  bunload b3
30  bunload b4
31  exe bufwinnr('b1') . 'wincmd w'
32  .+3bunload
33  call assert_equal(0, getbufinfo('b2')[0].loaded)
34  call assert_equal('b1',
35        \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
36
37  " Load four buffers. Unload the second and third buffers and from the last
38  " buffer execute .-3bunload to unload the first buffer.
39  ball
40  bunload b2
41  bunload b3
42  exe bufwinnr('b4') . 'wincmd w'
43  .-3bunload
44  call assert_equal(0, getbufinfo('b1')[0].loaded)
45  call assert_equal('b4',
46        \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
47
48  " Load four buffers. Unload the first and second buffers. Execute .-3bunload
49  " from the last buffer and check whether the third buffer is unloaded.
50  ball
51  bunload b1
52  bunload b2
53  exe bufwinnr('b4') . 'wincmd w'
54  .-3bunload
55  call assert_equal(0, getbufinfo('b3')[0].loaded)
56  call assert_equal('b4',
57        \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
58
59  %bwipe!
60  call delete('b1')
61  call delete('b2')
62  call delete('b3')
63  call delete('b4')
64
65  call assert_fails('1,4bunload', 'E16:')
66  call assert_fails(',100bunload', 'E16:')
67
68  " Use a try-catch for this test. When assert_fails() is used for this
69  " test, the command fails with E515: instead of E90:
70  let caught_E90 = 0
71  try
72    $bunload
73  catch /E90:/
74    let caught_E90 = 1
75  endtry
76  call assert_equal(1, caught_E90)
77  call assert_fails('$bunload', 'E515:')
78endfunc
79
80" Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified
81" commands
82func Test_buflist_browse()
83  %bwipe!
84  call assert_fails('buffer 1000', 'E86:')
85
86  call writefile(['foo1', 'foo2', 'foo3', 'foo4'], 'Xfile1')
87  call writefile(['bar1', 'bar2', 'bar3', 'bar4'], 'Xfile2')
88  call writefile(['baz1', 'baz2', 'baz3', 'baz4'], 'Xfile3')
89  edit Xfile1
90  let b1 = bufnr()
91  edit Xfile2
92  let b2 = bufnr()
93  edit +/baz4 Xfile3
94  let b3 = bufnr()
95
96  call assert_fails('buffer ' .. b1 .. ' abc', 'E488:')
97  call assert_equal(b3, bufnr())
98  call assert_equal(4, line('.'))
99  exe 'buffer +/bar2 ' .. b2
100  call assert_equal(b2, bufnr())
101  call assert_equal(2, line('.'))
102  exe 'buffer +/bar1'
103  call assert_equal(b2, bufnr())
104  call assert_equal(1, line('.'))
105
106  brewind +
107  call assert_equal(b1, bufnr())
108  call assert_equal(4, line('.'))
109
110  blast +/baz2
111  call assert_equal(b3, bufnr())
112  call assert_equal(2, line('.'))
113
114  bprevious +/bar4
115  call assert_equal(b2, bufnr())
116  call assert_equal(4, line('.'))
117
118  bnext +/baz3
119  call assert_equal(b3, bufnr())
120  call assert_equal(3, line('.'))
121
122  call assert_fails('bmodified', 'E84:')
123  call setbufvar(b2, '&modified', 1)
124  exe 'bmodified +/bar3'
125  call assert_equal(b2, bufnr())
126  call assert_equal(3, line('.'))
127
128  " With no listed buffers in the list, :bnext and :bprev should fail
129  %bwipe!
130  set nobuflisted
131  call assert_fails('bnext', 'E85:')
132  call assert_fails('bprev', 'E85:')
133  set buflisted
134
135  call assert_fails('sandbox bnext', 'E48:')
136
137  call delete('Xfile1')
138  call delete('Xfile2')
139  call delete('Xfile3')
140  %bwipe!
141endfunc
142
143" Test for :bdelete
144func Test_bdelete_cmd()
145  %bwipe!
146  call assert_fails('bdelete 5', 'E516:')
147  call assert_fails('1,1bdelete 1 2', 'E488:')
148
149  " Deleting a unlisted and unloaded buffer
150  edit Xfile1
151  let bnr = bufnr()
152  set nobuflisted
153  enew
154  call assert_fails('bdelete ' .. bnr, 'E516:')
155  %bwipe!
156endfunc
157
158func Test_buffer_error()
159  new foo1
160  new foo2
161
162  call assert_fails('buffer foo', 'E93:')
163  call assert_fails('buffer bar', 'E94:')
164  call assert_fails('buffer 0', 'E939:')
165
166  %bwipe
167endfunc
168
169" vim: shiftwidth=2 sts=2 expandtab
170