blob: 8fbf39f55b11e030286654db6e71fea68ee6dc7c [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
65 " put some randome text
66 put ='a'
67 let prev = bufnr('%')
68 setl nomodified bufhidden=wipe
69 e XXJumpListBuffer
70 let bnr = bufnr('%')
71 " 1) empty jumplist
72 let expected = [[
73 \ {'lnum': 2, 'bufnr': prev, 'col': 0, 'coladd': 0}], 1]
74 call assert_equal(expected, getjumplist())
75 let jumps = execute(':jumps')
76 call assert_equal('>', jumps[-1:])
77 " now jump back
78 exe ":norm! \<c-o>"
79 let expected = [[
80 \ {'lnum': 2, 'bufnr': prev, 'col': 0, 'coladd': 0},
81 \ {'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0}], 0]
82 call assert_equal(expected, getjumplist())
83 let jumps = execute(':jumps')
84 call assert_match('> 0 2 0 -invalid-', jumps)
85endfunc
86
87" Test for '' mark in an empty buffer
88
89func Test_empty_buffer()
90 new
91 insert
92a
93b
94c
95d
96.
97 call assert_equal(1, line("''"))
98 bwipe!
99endfunc
100
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200101" Test for 'jumpoptions'
102func Test_jumpoptions()
103 new
104 call setline(1, range(1, 200))
105 clearjumps
106 set jumpoptions=stack
107
108 " Jump around to add some locations to the jump list.
109 normal 10G
110 normal 20G
111 normal 30G
112 normal 40G
113 normal 50G
114 let bnr = bufnr()
115
116 " discards the tail when navigating from the middle
117 exe "normal \<C-O>\<C-O>"
118 call assert_equal([
119 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
120 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
121 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
122 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
123 \ {'lnum': 40, 'bufnr': bnr, 'col': 0, 'coladd': 0},
124 \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0}
125 \ ], 3], getjumplist())
126
127 " new jump location is added immediately after the last one
128 normal 90G
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 \ ], 4], getjumplist())
135
136 " does not add the same location twice adjacently
137 normal 60G
138 normal 60G
139 call assert_equal([
140 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
141 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
142 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
143 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
144 \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
145 \ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
146 \ ], 6], getjumplist())
147
148 " does add the same location twice non adjacently
149 normal 10G
150 normal 20G
151 call assert_equal([
152 \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
153 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
154 \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
155 \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
156 \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
157 \ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
158 \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
159 \ ], 7], getjumplist())
160
161 set jumpoptions&
162 %bw!
163endfunc
164
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200165" vim: shiftwidth=2 sts=2 expandtab