blob: 4b9fcee780c09e91bdaca6a6e7d3eb1347181d4b [file] [log] [blame]
Bram Moolenaar4f505882018-02-10 21:06:32 +01001" Tests for the jumplist functionality
2
3" Tests for the getjumplist() function
4func Test_getjumplist()
Bram Moolenaar4f505882018-02-10 21:06:32 +01005 %bwipe
6 clearjumps
7 call assert_equal([[], 0], getjumplist())
8 call assert_equal([[], 0], getjumplist(1))
9 call assert_equal([[], 0], getjumplist(1, 1))
10
11 call assert_equal([], getjumplist(100))
12 call assert_equal([], getjumplist(1, 100))
13
14 let lines = []
15 for i in range(1, 100)
16 call add(lines, "Line " . i)
17 endfor
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010018 call writefile(lines, "Xtest", 'D')
Bram Moolenaar4f505882018-02-10 21:06:32 +010019
20 " Jump around and create a jump list
21 edit Xtest
22 let bnr = bufnr('%')
23 normal 50%
24 normal G
25 normal gg
26
Bram Moolenaar57ee2b62019-02-12 22:15:06 +010027 let expected = [[
Bram Moolenaar4f505882018-02-10 21:06:32 +010028 \ {'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
Bram Moolenaar4f505882018-02-10 21:06:32 +010029 \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0},
Bram Moolenaar57ee2b62019-02-12 22:15:06 +010030 \ {'lnum': 100, 'bufnr': bnr, 'col': 0, 'coladd': 0}], 3]
31 call assert_equal(expected, getjumplist())
32 " jumplist doesn't change in between calls
33 call assert_equal(expected, getjumplist())
Bram Moolenaar4f505882018-02-10 21:06:32 +010034
35 " Traverse the jump list and verify the results
36 5
37 exe "normal \<C-O>"
Bram Moolenaar4c313b12019-08-24 22:58:31 +020038 call assert_equal(2, 1->getjumplist()[1])
Bram Moolenaar4f505882018-02-10 21:06:32 +010039 exe "normal 2\<C-O>"
40 call assert_equal(0, getjumplist(1, 1)[1])
41 exe "normal 3\<C-I>"
42 call assert_equal(3, getjumplist()[1])
43 exe "normal \<C-O>"
44 normal 20%
Bram Moolenaar57ee2b62019-02-12 22:15:06 +010045 let expected = [[
Bram Moolenaar4f505882018-02-10 21:06:32 +010046 \ {'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
47 \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0},
Bram Moolenaar4f505882018-02-10 21:06:32 +010048 \ {'lnum': 5, 'bufnr': bnr, 'col': 0, 'coladd': 0},
Bram Moolenaar57ee2b62019-02-12 22:15:06 +010049 \ {'lnum': 100, 'bufnr': bnr, 'col': 0, 'coladd': 0}], 4]
50 call assert_equal(expected, getjumplist())
51 " jumplist doesn't change in between calls
52 call assert_equal(expected, getjumplist())
Bram Moolenaar4f505882018-02-10 21:06:32 +010053
54 let l = getjumplist()
55 call test_garbagecollect_now()
Bram Moolenaara7e18d22018-02-11 14:29:49 +010056 call assert_equal(4, l[1])
Bram Moolenaar4f505882018-02-10 21:06:32 +010057 clearjumps
58 call test_garbagecollect_now()
Bram Moolenaara7e18d22018-02-11 14:29:49 +010059 call assert_equal(4, l[1])
Bram Moolenaar4f505882018-02-10 21:06:32 +010060endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020061
zeertzjq2e7d89b2024-07-10 19:36:36 +020062func Test_jumplist_wipe_buf()
Christian Brabandta0f659c2022-04-09 13:35:00 +010063 new
64 clearjumps
LemonBoy4ff3a9b2024-07-09 20:03:24 +020065 " Put some random text and fill the jump list.
66 call setline(1, ['foo', 'bar', 'baz'])
67 normal G
68 normal gg
Christian Brabandta0f659c2022-04-09 13:35:00 +010069 setl nomodified bufhidden=wipe
70 e XXJumpListBuffer
LemonBoy4ff3a9b2024-07-09 20:03:24 +020071 " The jump list is empty as the buffer was wiped out.
72 call assert_equal([[], 0], getjumplist())
Christian Brabandta0f659c2022-04-09 13:35:00 +010073 let jumps = execute(':jumps')
74 call assert_equal('>', jumps[-1:])
zeertzjq2e7d89b2024-07-10 19:36:36 +020075
76 " Put some random text and fill the jump list.
77 call setline(1, ['foo', 'bar', 'baz'])
78 setl bufhidden=hide
79
80 " References to wiped buffer are deleted with multiple tabpages.
81 let [w1, t1] = [win_getid(), tabpagenr()]
82 clearjumps
83 normal G
84 normal gg
85 enew
86
87 split XXJumpListBuffer
88 let [w2, t2] = [win_getid(), tabpagenr()]
89 clearjumps
90 normal G
91 normal gg
92 enew
93
94 tabnew XXJumpListBuffer
95 let [w3, t3] = [win_getid(), tabpagenr()]
96 clearjumps
97 normal G
98 normal gg
99 enew
100
101 split XXJumpListBuffer
102 let [w4, t4] = [win_getid(), tabpagenr()]
103 clearjumps
104 normal G
105 normal gg
106 enew
107
108 for [w, t] in [[w1, t1], [w2, t2], [w3, t3], [w4, t4]]
109 call assert_equal(2, len(getjumplist(w, t)[0]))
110 endfor
111
112 bwipe! XXJumpListBuffer
113
114 for [w, t] in [[w1, t1], [w2, t2], [w3, t3], [w4, t4]]
115 call assert_equal(0, len(getjumplist(w, t)[0]))
116 endfor
117
118 %bwipe!
Christian Brabandta0f659c2022-04-09 13:35:00 +0100119endfunc
120
121" Test for '' mark in an empty buffer
122
123func Test_empty_buffer()
124 new
125 insert
126a
127b
128c
129d
130.
131 call assert_equal(1, line("''"))
132 bwipe!
133endfunc
134
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200135" Test for 'jumpoptions'
136func Test_jumpoptions()
137 new
138 call setline(1, range(1, 200))
139 clearjumps
140 set jumpoptions=stack
141
142 " Jump around to add some locations to the jump list.
143 normal 10G
144 normal 20G
145 normal 30G
146 normal 40G
147 normal 50G
148 let bnr = bufnr()
149
150 " discards the tail when navigating from the middle
151 exe "normal \<C-O>\<C-O>"
152 call assert_equal([
153 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
154 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
155 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
156 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
157 \ {'lnum': 40, 'bufnr': bnr, 'col': 0, 'coladd': 0},
158 \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0}
159 \ ], 3], getjumplist())
160
161 " new jump location is added immediately after the last one
162 normal 90G
163 call assert_equal([
164 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
165 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
166 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
167 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
168 \ ], 4], getjumplist())
169
170 " does not add the same location twice adjacently
171 normal 60G
172 normal 60G
173 call assert_equal([
174 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
175 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
176 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
177 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
178 \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
179 \ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
180 \ ], 6], getjumplist())
181
182 " does add the same location twice non adjacently
183 normal 10G
184 normal 20G
185 call assert_equal([
186 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
187 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
188 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
189 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
190 \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
191 \ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
192 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
193 \ ], 7], getjumplist())
194
195 set jumpoptions&
196 %bw!
197endfunc
198
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200199" vim: shiftwidth=2 sts=2 expandtab