blob: 0f43a8c6ebb2dd75f19a5fbc4d952e17f45b4ece [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
Christian Brabandta0f659c2022-04-09 13:35:00 +010062func Test_jumplist_invalid()
63 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:])
Christian Brabandta0f659c2022-04-09 13:35:00 +010075endfunc
76
77" Test for '' mark in an empty buffer
78
79func Test_empty_buffer()
80 new
81 insert
82a
83b
84c
85d
86.
87 call assert_equal(1, line("''"))
88 bwipe!
89endfunc
90
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020091" Test for 'jumpoptions'
92func Test_jumpoptions()
93 new
94 call setline(1, range(1, 200))
95 clearjumps
96 set jumpoptions=stack
97
98 " Jump around to add some locations to the jump list.
99 normal 10G
100 normal 20G
101 normal 30G
102 normal 40G
103 normal 50G
104 let bnr = bufnr()
105
106 " discards the tail when navigating from the middle
107 exe "normal \<C-O>\<C-O>"
108 call assert_equal([
109 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
110 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
111 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
112 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
113 \ {'lnum': 40, 'bufnr': bnr, 'col': 0, 'coladd': 0},
114 \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0}
115 \ ], 3], getjumplist())
116
117 " new jump location is added immediately after the last one
118 normal 90G
119 call assert_equal([
120 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
121 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
122 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
123 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
124 \ ], 4], getjumplist())
125
126 " does not add the same location twice adjacently
127 normal 60G
128 normal 60G
129 call assert_equal([
130 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
131 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
132 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
133 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
134 \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
135 \ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
136 \ ], 6], getjumplist())
137
138 " does add the same location twice non adjacently
139 normal 10G
140 normal 20G
141 call assert_equal([
142 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
143 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
144 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
145 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
146 \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
147 \ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
148 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
149 \ ], 7], getjumplist())
150
151 set jumpoptions&
152 %bw!
153endfunc
154
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200155" vim: shiftwidth=2 sts=2 expandtab