blob: 1095e11a342ccfe8463a87c882fcb9395c580eae [file] [log] [blame]
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +02001" Test for the termdebug plugin
2
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +02003source shared.vim
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +02004source check.vim
5
6CheckUnix
7CheckFeature terminal
8CheckExecutable gdb
9CheckExecutable gcc
10
11let g:GDB = exepath('gdb')
12if g:GDB->empty()
Christian Brabandtf2534432023-08-27 19:59:28 +020013 throw 'Skipped: gdb is not found in $PATH'
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020014endif
15
16let g:GCC = exepath('gcc')
17if g:GCC->empty()
Christian Brabandtf2534432023-08-27 19:59:28 +020018 throw 'Skipped: gcc is not found in $PATH'
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020019endif
20
iam28th323dda12023-12-14 20:30:26 +010021function s:generate_files(bin_name)
22 let src_name = a:bin_name .. '.c'
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020023 let lines =<< trim END
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 int isprime(int n)
28 {
29 if (n <= 1)
30 return 0;
31
32 for (int i = 2; i <= n / 2; i++)
33 if (n % i == 0)
34 return 0;
35
36 return 1;
37 }
38
39 int main(int argc, char *argv[])
40 {
41 int n = 7;
42
43 printf("%d is %s prime\n", n, isprime(n) ? "a" : "not a");
44
45 return 0;
46 }
47 END
iam28th323dda12023-12-14 20:30:26 +010048 call writefile(lines, src_name)
49 call system($'{g:GCC} -g -o {a:bin_name} {src_name}')
50endfunction
51
52function s:cleanup_files(bin_name)
53 call delete(a:bin_name)
54 call delete(a:bin_name .. '.c')
55endfunction
56
57packadd termdebug
58
59func Test_termdebug_basic()
60 let bin_name = 'XTD_basic'
61 let src_name = bin_name .. '.c'
62 call s:generate_files(bin_name)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020063
64 edit XTD_basic.c
65 Termdebug ./XTD_basic
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020066 call WaitForAssert({-> assert_equal(3, winnr('$'))})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020067 let gdb_buf = winbufnr(1)
68 wincmd b
69 Break 9
70 call term_wait(gdb_buf)
71 redraw!
72 call assert_equal([
73 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
74 \ 'priority': 110, 'group': 'TermDebug'}],
75 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)
76 Run
Christian Brabandt6c93c942023-08-27 21:48:29 +020077 call term_wait(gdb_buf, 400)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020078 redraw!
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020079 call WaitForAssert({-> assert_equal([
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020080 \ {'lnum': 9, 'id': 12, 'name': 'debugPC', 'priority': 110,
81 \ 'group': 'TermDebug'},
82 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
83 \ 'priority': 110, 'group': 'TermDebug'}],
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020084 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020085 Finish
86 call term_wait(gdb_buf)
87 redraw!
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020088 call WaitForAssert({-> assert_equal([
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020089 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
90 \ 'priority': 110, 'group': 'TermDebug'},
91 \ {'lnum': 20, 'id': 12, 'name': 'debugPC',
92 \ 'priority': 110, 'group': 'TermDebug'}],
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020093 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020094 Continue
Shane-XB-Qian2dd613f2023-11-12 23:53:39 +080095 call term_wait(gdb_buf)
96
97 let i = 2
98 while i <= 258
99 Break
100 call term_wait(gdb_buf)
101 if i == 2
102 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint2.0')[0].text, '02')})
103 endif
104 if i == 10
105 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint10.0')[0].text, '0A')})
106 endif
107 if i == 168
108 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint168.0')[0].text, 'A8')})
109 endif
110 if i == 255
111 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint255.0')[0].text, 'FF')})
112 endif
113 if i == 256
114 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint256.0')[0].text, 'F+')})
115 endif
116 if i == 258
117 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint258.0')[0].text, 'F+')})
118 endif
119 let i += 1
120 endwhile
shane.xb.qianca482022023-11-08 21:59:15 +0100121
122 let cn = 0
123 " 60 is approx spaceBuffer * 3
124 if winwidth(0) <= 78 + 60
125 Var
126 call assert_equal(winnr(), winnr('$'))
127 call assert_equal(winlayout(), ['col', [['leaf', 1002], ['leaf', 1001], ['leaf', 1000], ['leaf', 1003 + cn]]])
128 let cn += 1
129 bw!
130 Asm
131 call assert_equal(winnr(), winnr('$'))
132 call assert_equal(winlayout(), ['col', [['leaf', 1002], ['leaf', 1001], ['leaf', 1000], ['leaf', 1003 + cn]]])
133 let cn += 1
134 bw!
135 endif
136 set columns=160
shane.xb.qianfdbadea2023-11-12 09:42:12 +0100137 call term_wait(gdb_buf)
Christian Brabandt305127f2023-11-11 18:59:33 +0100138 let winw = winwidth(0)
shane.xb.qianca482022023-11-08 21:59:15 +0100139 Var
Christian Brabandt305127f2023-11-11 18:59:33 +0100140 if winwidth(0) < winw
141 call assert_equal(winnr(), winnr('$') - 1)
142 call assert_equal(winlayout(), ['col', [['leaf', 1002], ['leaf', 1001], ['row', [['leaf', 1003 + cn], ['leaf', 1000]]]]])
143 let cn += 1
144 bw!
145 endif
146 let winw = winwidth(0)
shane.xb.qianca482022023-11-08 21:59:15 +0100147 Asm
Christian Brabandt305127f2023-11-11 18:59:33 +0100148 if winwidth(0) < winw
149 call assert_equal(winnr(), winnr('$') - 1)
150 call assert_equal(winlayout(), ['col', [['leaf', 1002], ['leaf', 1001], ['row', [['leaf', 1003 + cn], ['leaf', 1000]]]]])
151 let cn += 1
152 bw!
153 endif
shane.xb.qianca482022023-11-08 21:59:15 +0100154 set columns&
shane.xb.qianfdbadea2023-11-12 09:42:12 +0100155 call term_wait(gdb_buf)
shane.xb.qianca482022023-11-08 21:59:15 +0100156
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200157 wincmd t
158 quit!
159 redraw!
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100160 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200161 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
162
iam28th323dda12023-12-14 20:30:26 +0100163 call s:cleanup_files(bin_name)
164 %bw!
165endfunc
166
167func Test_termdebug_tbreak()
168 let g:test_is_flaky = 1
169 let bin_name = 'XTD_tbreak'
170 let src_name = bin_name .. '.c'
171
172 eval s:generate_files(bin_name)
173
174 execute 'edit ' .. src_name
175 execute 'Termdebug ./' .. bin_name
176
177 call WaitForAssert({-> assert_equal(3, winnr('$'))})
178 let gdb_buf = winbufnr(1)
179 wincmd b
180
181 let bp_line = 22 " 'return' statement in main
182 let temp_bp_line = 10 " 'if' statement in 'for' loop body
183 execute "Tbreak " .. temp_bp_line
184 execute "Break " .. bp_line
185
186 call term_wait(gdb_buf)
187 redraw!
188 " both temporary and normal breakpoint signs were displayed...
189 call assert_equal([
190 \ {'lnum': temp_bp_line, 'id': 1014, 'name': 'debugBreakpoint1.0',
191 \ 'priority': 110, 'group': 'TermDebug'},
192 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
193 \ 'priority': 110, 'group': 'TermDebug'}],
194 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)
195
196 Run
197 call term_wait(gdb_buf, 400)
198 redraw!
199 " debugPC sign is on the line where the temp. bp was set;
200 " temp. bp sign was removed after hit;
201 " normal bp sign is still present
202 call WaitForAssert({-> assert_equal([
203 \ {'lnum': temp_bp_line, 'id': 12, 'name': 'debugPC', 'priority': 110,
204 \ 'group': 'TermDebug'},
205 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
206 \ 'priority': 110, 'group': 'TermDebug'}],
207 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
208
209 Continue
210 call term_wait(gdb_buf)
211 redraw!
212 " debugPC is on the normal breakpoint,
213 " temp. bp on line 10 was only hit once
214 call WaitForAssert({-> assert_equal([
215 \ {'lnum': bp_line, 'id': 12, 'name': 'debugPC', 'priority': 110,
216 \ 'group': 'TermDebug'},
217 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
218 \ 'priority': 110, 'group': 'TermDebug'}],
219 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
220
221 wincmd t
222 quit!
223 redraw!
224 call WaitForAssert({-> assert_equal(1, winnr('$'))})
225 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
226
227 eval s:cleanup_files(bin_name)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200228 %bw!
229endfunc
230
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100231func Test_termdebug_mapping()
232 %bw!
Ken Takataffed1542024-05-21 17:14:56 +0200233 call assert_true(maparg('K', 'n', 0, 1)->empty())
234 call assert_true(maparg('-', 'n', 0, 1)->empty())
235 call assert_true(maparg('+', 'n', 0, 1)->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100236 Termdebug
237 call WaitForAssert({-> assert_equal(3, winnr('$'))})
238 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200239 call assert_false(maparg('K', 'n', 0, 1)->empty())
240 call assert_false(maparg('-', 'n', 0, 1)->empty())
241 call assert_false(maparg('+', 'n', 0, 1)->empty())
242 call assert_false(maparg('K', 'n', 0, 1).buffer)
243 call assert_false(maparg('-', 'n', 0, 1).buffer)
244 call assert_false(maparg('+', 'n', 0, 1).buffer)
245 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100246 wincmd t
247 quit!
248 redraw!
249 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ken Takataffed1542024-05-21 17:14:56 +0200250 call assert_true(maparg('K', 'n', 0, 1)->empty())
251 call assert_true(maparg('-', 'n', 0, 1)->empty())
252 call assert_true(maparg('+', 'n', 0, 1)->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100253
254 %bw!
255 nnoremap K :echom "K"<cr>
256 nnoremap - :echom "-"<cr>
257 nnoremap + :echom "+"<cr>
258 Termdebug
259 call WaitForAssert({-> assert_equal(3, winnr('$'))})
260 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200261 call assert_false(maparg('K', 'n', 0, 1)->empty())
262 call assert_false(maparg('-', 'n', 0, 1)->empty())
263 call assert_false(maparg('+', 'n', 0, 1)->empty())
264 call assert_false(maparg('K', 'n', 0, 1).buffer)
265 call assert_false(maparg('-', 'n', 0, 1).buffer)
266 call assert_false(maparg('+', 'n', 0, 1).buffer)
267 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100268 wincmd t
269 quit!
270 redraw!
271 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ken Takataffed1542024-05-21 17:14:56 +0200272 call assert_false(maparg('K', 'n', 0, 1)->empty())
273 call assert_false(maparg('-', 'n', 0, 1)->empty())
274 call assert_false(maparg('+', 'n', 0, 1)->empty())
275 call assert_false(maparg('K', 'n', 0, 1).buffer)
276 call assert_false(maparg('-', 'n', 0, 1).buffer)
277 call assert_false(maparg('+', 'n', 0, 1).buffer)
278 call assert_equal(':echom "K"<cr>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100279
280 %bw!
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200281
282 " -- Test that local-buffer mappings are restored in the correct buffers --
283 " local mappings for foo
284 file foo
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100285 nnoremap <buffer> K :echom "bK"<cr>
286 nnoremap <buffer> - :echom "b-"<cr>
287 nnoremap <buffer> + :echom "b+"<cr>
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200288
289 " no mappings for 'bar'
290 enew
291 file bar
292
293 " Start termdebug from foo
294 buffer foo
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100295 Termdebug
296 call WaitForAssert({-> assert_equal(3, winnr('$'))})
297 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200298 call assert_true(maparg('K', 'n', 0, 1).buffer)
299 call assert_true(maparg('-', 'n', 0, 1).buffer)
300 call assert_true(maparg('+', 'n', 0, 1).buffer)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100301 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':echom "bK"<cr>')
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200302
303 Source
304 buffer bar
305 call assert_false(maparg('K', 'n', 0, 1)->empty())
306 call assert_false(maparg('-', 'n', 0, 1)->empty())
307 call assert_false(maparg('+', 'n', 0, 1)->empty())
308 call assert_true(maparg('K', 'n', 0, 1).buffer->empty())
309 call assert_true(maparg('-', 'n', 0, 1).buffer->empty())
310 call assert_true(maparg('+', 'n', 0, 1).buffer->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100311 wincmd t
312 quit!
313 redraw!
314 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200315
316 " Termdebug session ended. Buffer 'bar' shall have no mappings
317 call assert_true(bufname() ==# 'bar')
318 call assert_false(maparg('K', 'n', 0, 1)->empty())
319 call assert_false(maparg('-', 'n', 0, 1)->empty())
320 call assert_false(maparg('+', 'n', 0, 1)->empty())
321 call assert_true(maparg('K', 'n', 0, 1).buffer->empty())
322 call assert_true(maparg('-', 'n', 0, 1).buffer->empty())
323 call assert_true(maparg('+', 'n', 0, 1).buffer->empty())
324
325 " Buffer 'foo' shall have the same mapping as before running the termdebug
326 " session
327 buffer foo
328 call assert_true(bufname() ==# 'foo')
Ken Takataffed1542024-05-21 17:14:56 +0200329 call assert_true(maparg('K', 'n', 0, 1).buffer)
330 call assert_true(maparg('-', 'n', 0, 1).buffer)
331 call assert_true(maparg('+', 'n', 0, 1).buffer)
332 call assert_equal(':echom "bK"<cr>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100333
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200334 nunmap K
335 nunmap +
336 nunmap -
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100337 %bw!
338endfunc
339
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200340function Test_termdebug_save_restore_variables()
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200341 " saved mousemodel
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200342 let &mousemodel=''
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200343
344 " saved keys
345 nnoremap K :echo "hello world!"<cr>
346 let expected_map_K = maparg('K', 'n', 0 , 1)
347 nnoremap + :echo "hello plus!"<cr>
348 let expected_map_plus = maparg('+', 'n', 0 , 1)
349 let expected_map_minus = {}
350
351 " saved &columns
352 let expected_columns = &columns
353
354 " We want termdebug to overwrite 'K' map but not '+' map.
355 let g:termdebug_config = {}
356 let g:termdebug_config['map_K'] = 1
357
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200358 Termdebug
359 call WaitForAssert({-> assert_equal(3, winnr('$'))})
360 call WaitForAssert({-> assert_match(&mousemodel, 'popup_setpos')})
361 wincmd t
362 quit!
363 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200364
365 call assert_true(empty(&mousemodel))
366
367 call assert_true(empty(expected_map_minus))
368 call assert_equal(expected_map_K.rhs, maparg('K', 'n', 0, 1).rhs)
369 call assert_equal(expected_map_plus.rhs, maparg('+', 'n', 0, 1).rhs)
370
371 call assert_equal(expected_columns, &columns)
372
373 nunmap K
374 nunmap +
375 unlet g:termdebug_config
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200376endfunction
377
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200378function Test_termdebug_sanity_check()
379 " Test if user has filename/folders with wrong names
380 let g:termdebug_config = {}
381 let s:dict = {'disasm_window': 'Termdebug-asm-listing', 'use_prompt': 'gdb', 'variables_window': 'Termdebug-variables-listing'}
382
383 for key in keys(s:dict)
384 let s:filename = s:dict[key]
385 let g:termdebug_config[key] = 1
386 let s:error_message = "You have a file/folder named '" .. s:filename .. "'"
387
388 " Write dummy file with bad name
389 call writefile(['This', 'is', 'a', 'test'], s:filename)
390 Termdebug
391 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
392 call WaitForAssert({-> assert_equal(1, winnr('$'))})
393
394 call delete(s:filename)
395 call remove(g:termdebug_config, key)
396 endfor
397
398 unlet g:termdebug_config
399endfunction
400
401function Test_termdebug_double_termdebug_instances()
402 let s:error_message = 'Terminal debugger already running, cannot run two'
403 Termdebug
404 call WaitForAssert({-> assert_equal(3, winnr('$'))})
405 Termdebug
406 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
407 wincmd t
408 quit!
409 call WaitForAssert({-> assert_equal(1, winnr('$'))})
410 :%bw!
411endfunction
Ubaldo Tiberi62ccaa62024-05-21 23:33:03 +0200412
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200413" vim: shiftwidth=2 sts=2 expandtab