blob: d2e046ff8253ce65a149ae055061b5b665541460 [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
Peter Wolf8f1d0982024-10-27 21:51:14 +01004source screendump.vim
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +02005source check.vim
6
7CheckUnix
8CheckFeature terminal
9CheckExecutable gdb
10CheckExecutable gcc
11
12let g:GDB = exepath('gdb')
13if g:GDB->empty()
Christian Brabandtf2534432023-08-27 19:59:28 +020014 throw 'Skipped: gdb is not found in $PATH'
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020015endif
16
17let g:GCC = exepath('gcc')
18if g:GCC->empty()
Christian Brabandtf2534432023-08-27 19:59:28 +020019 throw 'Skipped: gcc is not found in $PATH'
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020020endif
21
iam28th323dda12023-12-14 20:30:26 +010022function s:generate_files(bin_name)
23 let src_name = a:bin_name .. '.c'
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020024 let lines =<< trim END
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 int isprime(int n)
29 {
30 if (n <= 1)
31 return 0;
32
33 for (int i = 2; i <= n / 2; i++)
34 if (n % i == 0)
35 return 0;
36
37 return 1;
38 }
39
40 int main(int argc, char *argv[])
41 {
42 int n = 7;
43
44 printf("%d is %s prime\n", n, isprime(n) ? "a" : "not a");
45
46 return 0;
47 }
48 END
iam28th323dda12023-12-14 20:30:26 +010049 call writefile(lines, src_name)
50 call system($'{g:GCC} -g -o {a:bin_name} {src_name}')
51endfunction
52
53function s:cleanup_files(bin_name)
54 call delete(a:bin_name)
55 call delete(a:bin_name .. '.c')
56endfunction
57
58packadd termdebug
59
Ubaldo Tiberiae1c8b72024-11-19 22:32:30 +010060" should be the first test to run, since it validates the window layout with
61" win ids
iam28th323dda12023-12-14 20:30:26 +010062func Test_termdebug_basic()
Christian Brabandtebb08d52025-01-11 15:43:12 +010063 let g:test_is_flaky = 1
iam28th323dda12023-12-14 20:30:26 +010064 let bin_name = 'XTD_basic'
65 let src_name = bin_name .. '.c'
66 call s:generate_files(bin_name)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020067
68 edit XTD_basic.c
69 Termdebug ./XTD_basic
Christian Brabandt2979cfc2024-07-24 21:37:39 +020070 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020071 call WaitForAssert({-> assert_equal(3, winnr('$'))})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020072 let gdb_buf = winbufnr(1)
73 wincmd b
74 Break 9
75 call term_wait(gdb_buf)
76 redraw!
77 call assert_equal([
78 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
79 \ 'priority': 110, 'group': 'TermDebug'}],
80 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)
81 Run
Christian Brabandt6c93c942023-08-27 21:48:29 +020082 call term_wait(gdb_buf, 400)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020083 redraw!
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020084 call WaitForAssert({-> assert_equal([
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020085 \ {'lnum': 9, 'id': 12, 'name': 'debugPC', 'priority': 110,
86 \ 'group': 'TermDebug'},
87 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
88 \ 'priority': 110, 'group': 'TermDebug'}],
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020089 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020090 Finish
91 call term_wait(gdb_buf)
92 redraw!
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020093 call WaitForAssert({-> assert_equal([
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020094 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
95 \ 'priority': 110, 'group': 'TermDebug'},
96 \ {'lnum': 20, 'id': 12, 'name': 'debugPC',
97 \ 'priority': 110, 'group': 'TermDebug'}],
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020098 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020099 Continue
Shane-XB-Qian2dd613f2023-11-12 23:53:39 +0800100 call term_wait(gdb_buf)
101
102 let i = 2
103 while i <= 258
104 Break
105 call term_wait(gdb_buf)
106 if i == 2
107 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint2.0')[0].text, '02')})
108 endif
109 if i == 10
110 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint10.0')[0].text, '0A')})
111 endif
112 if i == 168
113 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint168.0')[0].text, 'A8')})
114 endif
115 if i == 255
116 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint255.0')[0].text, 'FF')})
117 endif
118 if i == 256
119 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint256.0')[0].text, 'F+')})
120 endif
121 if i == 258
122 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint258.0')[0].text, 'F+')})
123 endif
124 let i += 1
125 endwhile
shane.xb.qianca482022023-11-08 21:59:15 +0100126
127 let cn = 0
128 " 60 is approx spaceBuffer * 3
129 if winwidth(0) <= 78 + 60
130 Var
zeertzjqaef61792024-07-18 20:35:42 +0200131 call assert_equal(winnr('$'), winnr())
132 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['leaf', 1000], ['leaf', 1003 + cn]]], winlayout())
shane.xb.qianca482022023-11-08 21:59:15 +0100133 let cn += 1
134 bw!
135 Asm
zeertzjqaef61792024-07-18 20:35:42 +0200136 call assert_equal(winnr('$'), winnr())
137 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['leaf', 1000], ['leaf', 1003 + cn]]], winlayout())
shane.xb.qianca482022023-11-08 21:59:15 +0100138 let cn += 1
139 bw!
140 endif
141 set columns=160
shane.xb.qianfdbadea2023-11-12 09:42:12 +0100142 call term_wait(gdb_buf)
Christian Brabandt305127f2023-11-11 18:59:33 +0100143 let winw = winwidth(0)
shane.xb.qianca482022023-11-08 21:59:15 +0100144 Var
Christian Brabandt305127f2023-11-11 18:59:33 +0100145 if winwidth(0) < winw
zeertzjqaef61792024-07-18 20:35:42 +0200146 call assert_equal(winnr('$') - 1, winnr())
147 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['row', [['leaf', 1003 + cn], ['leaf', 1000]]]]], winlayout())
Christian Brabandt305127f2023-11-11 18:59:33 +0100148 let cn += 1
149 bw!
150 endif
151 let winw = winwidth(0)
shane.xb.qianca482022023-11-08 21:59:15 +0100152 Asm
Christian Brabandt305127f2023-11-11 18:59:33 +0100153 if winwidth(0) < winw
zeertzjqaef61792024-07-18 20:35:42 +0200154 call assert_equal(winnr('$') - 1, winnr())
155 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['row', [['leaf', 1003 + cn], ['leaf', 1000]]]]], winlayout())
Christian Brabandt305127f2023-11-11 18:59:33 +0100156 let cn += 1
157 bw!
158 endif
shane.xb.qianca482022023-11-08 21:59:15 +0100159 set columns&
shane.xb.qianfdbadea2023-11-12 09:42:12 +0100160 call term_wait(gdb_buf)
shane.xb.qianca482022023-11-08 21:59:15 +0100161
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200162 wincmd t
163 quit!
164 redraw!
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100165 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200166 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
167
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200168 for use_prompt in [v:true, v:false]
zeertzjqaef61792024-07-18 20:35:42 +0200169 let g:termdebug_config = {}
170 let g:termdebug_config['use_prompt'] = use_prompt
171 TermdebugCommand ./XTD_basic arg args
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200172 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
zeertzjqaef61792024-07-18 20:35:42 +0200173 call WaitForAssert({-> assert_equal(3, winnr('$'))})
174 wincmd t
175 quit!
176 redraw!
177 call WaitForAssert({-> assert_equal(1, winnr('$'))})
178 unlet g:termdebug_config
179 endfor
180
iam28th323dda12023-12-14 20:30:26 +0100181 call s:cleanup_files(bin_name)
182 %bw!
183endfunc
184
Ubaldo Tiberib5c15572024-11-19 21:10:24 +0100185func Test_termdebug_decimal_breakpoints()
186 let bin_name = 'XTD_decimal'
187 let src_name = bin_name .. '.c'
188 call s:generate_files(bin_name)
189
190 exe "edit " .. src_name
191
192 let g:termdebug_config = {}
193 let g:termdebug_config['sign_decimal'] = 1
194
195 exe "Termdebug " .. bin_name
196 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
197 call WaitForAssert({-> assert_equal(3, winnr('$'))})
198 let gdb_buf = winbufnr(1)
199 wincmd b
200 Break 9
201 call term_wait(gdb_buf)
202 redraw!
203
204 let i = 2
205 while i <= 258
206 Break
207 call term_wait(gdb_buf)
208 if i == 2
209 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint2.0')[0].text, '02')})
210 endif
211 if i == 10
212 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint10.0')[0].text, '10')})
213 endif
214 if i == 168
215 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint168.0')[0].text, '9+')})
216 endif
217 if i == 255
218 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint255.0')[0].text, '9+')})
219 endif
220 if i == 256
221 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint256.0')[0].text, '9+')})
222 endif
223 if i == 258
224 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint258.0')[0].text, '9+')})
225 endif
226 let i += 1
227 endwhile
228
229 wincmd t
230 quit!
231 redraw!
232 call WaitForAssert({-> assert_equal(1, winnr('$'))})
233 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
234
235 call s:cleanup_files(bin_name)
236 %bw!
237
238 unlet g:termdebug_config
239endfunc
240
iam28th323dda12023-12-14 20:30:26 +0100241func Test_termdebug_tbreak()
242 let g:test_is_flaky = 1
243 let bin_name = 'XTD_tbreak'
244 let src_name = bin_name .. '.c'
245
246 eval s:generate_files(bin_name)
247
248 execute 'edit ' .. src_name
249 execute 'Termdebug ./' .. bin_name
250
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200251 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
iam28th323dda12023-12-14 20:30:26 +0100252 call WaitForAssert({-> assert_equal(3, winnr('$'))})
253 let gdb_buf = winbufnr(1)
254 wincmd b
255
256 let bp_line = 22 " 'return' statement in main
257 let temp_bp_line = 10 " 'if' statement in 'for' loop body
258 execute "Tbreak " .. temp_bp_line
259 execute "Break " .. bp_line
260
261 call term_wait(gdb_buf)
262 redraw!
263 " both temporary and normal breakpoint signs were displayed...
264 call assert_equal([
265 \ {'lnum': temp_bp_line, 'id': 1014, 'name': 'debugBreakpoint1.0',
266 \ 'priority': 110, 'group': 'TermDebug'},
267 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
268 \ 'priority': 110, 'group': 'TermDebug'}],
269 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)
270
271 Run
272 call term_wait(gdb_buf, 400)
273 redraw!
274 " debugPC sign is on the line where the temp. bp was set;
275 " temp. bp sign was removed after hit;
276 " normal bp sign is still present
277 call WaitForAssert({-> assert_equal([
278 \ {'lnum': temp_bp_line, 'id': 12, 'name': 'debugPC', 'priority': 110,
279 \ 'group': 'TermDebug'},
280 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
281 \ 'priority': 110, 'group': 'TermDebug'}],
282 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
283
284 Continue
285 call term_wait(gdb_buf)
286 redraw!
287 " debugPC is on the normal breakpoint,
288 " temp. bp on line 10 was only hit once
289 call WaitForAssert({-> assert_equal([
290 \ {'lnum': bp_line, 'id': 12, 'name': 'debugPC', 'priority': 110,
291 \ 'group': 'TermDebug'},
292 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
293 \ 'priority': 110, 'group': 'TermDebug'}],
294 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
295
296 wincmd t
297 quit!
298 redraw!
299 call WaitForAssert({-> assert_equal(1, winnr('$'))})
300 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
301
302 eval s:cleanup_files(bin_name)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200303 %bw!
304endfunc
305
Peter Wolf8f1d0982024-10-27 21:51:14 +0100306func Test_termdebug_evaluate()
307 let bin_name = 'XTD_evaluate'
308 let src_name = bin_name .. '.c'
309 call s:generate_files(bin_name)
310
311 edit XTD_evaluate.c
312 Termdebug ./XTD_evaluate
313 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
314 call WaitForAssert({-> assert_equal(3, winnr('$'))})
315 let gdb_buf = winbufnr(1)
316 wincmd b
317
318 " return stmt in main
319 Break 22
320 call term_wait(gdb_buf)
321 Run
322 call term_wait(gdb_buf, 400)
323 redraw!
324
325 " Evaluate an expression
326 Evaluate n
327 call term_wait(gdb_buf)
328 call assert_equal(execute('1messages')->trim(), '"n": 7')
329 Evaluate argc
330 call term_wait(gdb_buf)
331 call assert_equal(execute('1messages')->trim(), '"argc": 1')
332 Evaluate isprime(n)
333 call term_wait(gdb_buf)
334 call assert_equal(execute('1messages')->trim(), '"isprime(n)": 1')
335
336 wincmd t
337 quit!
338 redraw!
339 call s:cleanup_files(bin_name)
340 %bw!
341endfunc
342
343func Test_termdebug_evaluate_in_popup()
344 CheckScreendump
345 let bin_name = 'XTD_evaluate_in_popup'
346 let src_name = bin_name .. '.c'
347 let code =<< trim END
348 struct Point {
349 int x;
350 int y;
351 };
352
353 int main(int argc, char* argv[]) {
354 struct Point p = {argc, 2};
355 struct Point* p_ptr = &p;
356 return 0;
357 }
358 END
359 call writefile(code, src_name, 'D')
360 call system($'{g:GCC} -g -o {bin_name} {src_name}')
361
362 let lines =<< trim END
363 edit XTD_evaluate_in_popup.c
364 packadd termdebug
365 let g:termdebug_config = {}
366 let g:termdebug_config['evaluate_in_popup'] = v:true
367 Termdebug ./XTD_evaluate_in_popup
368 wincmd b
369 Break 9
370 Run
371 END
372
373 call writefile(lines, 'Xscript', 'D')
374 let buf = RunVimInTerminal('-S Xscript', {})
375 call TermWait(buf, 400)
376
377 call term_sendkeys(buf, ":Evaluate p\<CR>")
378 call TermWait(buf, 400)
379 call VerifyScreenDump(buf, 'Test_termdebug_evaluate_in_popup_01', {})
380
381 call term_sendkeys(buf, ":Evaluate p_ptr\<CR>")
382 call TermWait(buf, 400)
383 call VerifyScreenDump(buf, 'Test_termdebug_evaluate_in_popup_02', {})
384
385 " Cleanup
386 call term_sendkeys(buf, ":Gdb")
387 call term_sendkeys(buf, ":quit!\<CR>")
388 call term_sendkeys(buf, ":qa!\<CR>")
389 call StopVimInTerminal(buf)
390 call delete(bin_name)
391 %bw!
392endfunc
393
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100394func Test_termdebug_mapping()
395 %bw!
Ken Takataffed1542024-05-21 17:14:56 +0200396 call assert_true(maparg('K', 'n', 0, 1)->empty())
397 call assert_true(maparg('-', 'n', 0, 1)->empty())
398 call assert_true(maparg('+', 'n', 0, 1)->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100399 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200400 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100401 call WaitForAssert({-> assert_equal(3, winnr('$'))})
402 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200403 call assert_false(maparg('K', 'n', 0, 1)->empty())
404 call assert_false(maparg('-', 'n', 0, 1)->empty())
405 call assert_false(maparg('+', 'n', 0, 1)->empty())
406 call assert_false(maparg('K', 'n', 0, 1).buffer)
407 call assert_false(maparg('-', 'n', 0, 1).buffer)
408 call assert_false(maparg('+', 'n', 0, 1).buffer)
409 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100410 wincmd t
411 quit!
412 redraw!
413 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ken Takataffed1542024-05-21 17:14:56 +0200414 call assert_true(maparg('K', 'n', 0, 1)->empty())
415 call assert_true(maparg('-', 'n', 0, 1)->empty())
416 call assert_true(maparg('+', 'n', 0, 1)->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100417
418 %bw!
419 nnoremap K :echom "K"<cr>
420 nnoremap - :echom "-"<cr>
421 nnoremap + :echom "+"<cr>
422 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200423 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100424 call WaitForAssert({-> assert_equal(3, winnr('$'))})
425 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200426 call assert_false(maparg('K', 'n', 0, 1)->empty())
427 call assert_false(maparg('-', 'n', 0, 1)->empty())
428 call assert_false(maparg('+', 'n', 0, 1)->empty())
429 call assert_false(maparg('K', 'n', 0, 1).buffer)
430 call assert_false(maparg('-', 'n', 0, 1).buffer)
431 call assert_false(maparg('+', 'n', 0, 1).buffer)
432 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100433 wincmd t
434 quit!
435 redraw!
436 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ken Takataffed1542024-05-21 17:14:56 +0200437 call assert_false(maparg('K', 'n', 0, 1)->empty())
438 call assert_false(maparg('-', 'n', 0, 1)->empty())
439 call assert_false(maparg('+', 'n', 0, 1)->empty())
440 call assert_false(maparg('K', 'n', 0, 1).buffer)
441 call assert_false(maparg('-', 'n', 0, 1).buffer)
442 call assert_false(maparg('+', 'n', 0, 1).buffer)
443 call assert_equal(':echom "K"<cr>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100444
445 %bw!
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200446
447 " -- Test that local-buffer mappings are restored in the correct buffers --
448 " local mappings for foo
449 file foo
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100450 nnoremap <buffer> K :echom "bK"<cr>
451 nnoremap <buffer> - :echom "b-"<cr>
452 nnoremap <buffer> + :echom "b+"<cr>
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200453
454 " no mappings for 'bar'
455 enew
456 file bar
457
458 " Start termdebug from foo
459 buffer foo
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100460 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200461 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100462 call WaitForAssert({-> assert_equal(3, winnr('$'))})
463 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200464 call assert_true(maparg('K', 'n', 0, 1).buffer)
465 call assert_true(maparg('-', 'n', 0, 1).buffer)
466 call assert_true(maparg('+', 'n', 0, 1).buffer)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100467 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':echom "bK"<cr>')
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200468
469 Source
470 buffer bar
471 call assert_false(maparg('K', 'n', 0, 1)->empty())
472 call assert_false(maparg('-', 'n', 0, 1)->empty())
473 call assert_false(maparg('+', 'n', 0, 1)->empty())
474 call assert_true(maparg('K', 'n', 0, 1).buffer->empty())
475 call assert_true(maparg('-', 'n', 0, 1).buffer->empty())
476 call assert_true(maparg('+', 'n', 0, 1).buffer->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100477 wincmd t
478 quit!
479 redraw!
480 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200481
482 " Termdebug session ended. Buffer 'bar' shall have no mappings
483 call assert_true(bufname() ==# 'bar')
484 call assert_false(maparg('K', 'n', 0, 1)->empty())
485 call assert_false(maparg('-', 'n', 0, 1)->empty())
486 call assert_false(maparg('+', 'n', 0, 1)->empty())
487 call assert_true(maparg('K', 'n', 0, 1).buffer->empty())
488 call assert_true(maparg('-', 'n', 0, 1).buffer->empty())
489 call assert_true(maparg('+', 'n', 0, 1).buffer->empty())
490
491 " Buffer 'foo' shall have the same mapping as before running the termdebug
492 " session
493 buffer foo
494 call assert_true(bufname() ==# 'foo')
Ken Takataffed1542024-05-21 17:14:56 +0200495 call assert_true(maparg('K', 'n', 0, 1).buffer)
496 call assert_true(maparg('-', 'n', 0, 1).buffer)
497 call assert_true(maparg('+', 'n', 0, 1).buffer)
498 call assert_equal(':echom "bK"<cr>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100499
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200500 nunmap K
501 nunmap +
502 nunmap -
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100503 %bw!
504endfunc
505
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200506function Test_termdebug_save_restore_variables()
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200507 " saved mousemodel
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200508 let &mousemodel=''
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200509
510 " saved keys
511 nnoremap K :echo "hello world!"<cr>
512 let expected_map_K = maparg('K', 'n', 0 , 1)
513 nnoremap + :echo "hello plus!"<cr>
514 let expected_map_plus = maparg('+', 'n', 0 , 1)
515 let expected_map_minus = {}
516
517 " saved &columns
518 let expected_columns = &columns
519
520 " We want termdebug to overwrite 'K' map but not '+' map.
521 let g:termdebug_config = {}
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200522 let g:termdebug_config['map_K'] = v:true
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200523
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200524 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200525 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200526 call WaitForAssert({-> assert_equal(3, winnr('$'))})
527 call WaitForAssert({-> assert_match(&mousemodel, 'popup_setpos')})
528 wincmd t
529 quit!
530 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200531
532 call assert_true(empty(&mousemodel))
533
534 call assert_true(empty(expected_map_minus))
535 call assert_equal(expected_map_K.rhs, maparg('K', 'n', 0, 1).rhs)
536 call assert_equal(expected_map_plus.rhs, maparg('+', 'n', 0, 1).rhs)
537
538 call assert_equal(expected_columns, &columns)
539
540 nunmap K
541 nunmap +
542 unlet g:termdebug_config
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200543endfunction
544
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200545function Test_termdebug_sanity_check()
546 " Test if user has filename/folders with wrong names
547 let g:termdebug_config = {}
548 let s:dict = {'disasm_window': 'Termdebug-asm-listing', 'use_prompt': 'gdb', 'variables_window': 'Termdebug-variables-listing'}
549
550 for key in keys(s:dict)
551 let s:filename = s:dict[key]
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200552 let g:termdebug_config[key] = v:true
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200553 let s:error_message = "You have a file/folder named '" .. s:filename .. "'"
554
555 " Write dummy file with bad name
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200556 call writefile(['This', 'is', 'a', 'test'], s:filename, 'D')
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200557 Termdebug
558 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
559 call WaitForAssert({-> assert_equal(1, winnr('$'))})
560
561 call delete(s:filename)
562 call remove(g:termdebug_config, key)
563 endfor
564
565 unlet g:termdebug_config
566endfunction
567
568function Test_termdebug_double_termdebug_instances()
569 let s:error_message = 'Terminal debugger already running, cannot run two'
570 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200571 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200572 call WaitForAssert({-> assert_equal(3, winnr('$'))})
573 Termdebug
574 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
575 wincmd t
576 quit!
577 call WaitForAssert({-> assert_equal(1, winnr('$'))})
578 :%bw!
579endfunction
Ubaldo Tiberi62ccaa62024-05-21 23:33:03 +0200580
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200581function Test_termdebug_config_types()
582 " TODO Remove the deprecated features after 1 Jan 2025.
Christian Brabandtebb08d52025-01-11 15:43:12 +0100583 let g:test_is_flaky = 1
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200584 let g:termdebug_config = {}
585 let s:error_message = 'Deprecation Warning:'
586 call assert_true(maparg('K', 'n', 0, 1)->empty())
587
588 for key in ['disasm_window', 'variables_window', 'map_K']
589 for val in [0, 1, v:true, v:false]
590 let g:termdebug_config[key] = val
591 Termdebug
592
593 " Type check: warning is displayed
594 if typename(val) == 'number'
595 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
596 endif
597
598 " Test on g:termdebug_config keys
599 if val && key != 'map_K'
600 call WaitForAssert({-> assert_equal(4, winnr('$'))})
601 call remove(g:termdebug_config, key)
602 else
603 call WaitForAssert({-> assert_equal(3, winnr('$'))})
604 endif
605
606 " Test on mapping
607 if key == 'map_K'
608 if val
609 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
610 else
611 call assert_true(maparg('K', 'n', 0, 1)->empty())
612 endif
613 endif
614
615 " Shutoff termdebug
616 wincmd t
617 quit!
618 call WaitForAssert({-> assert_equal(1, winnr('$'))})
619 :%bw!
620
621 endfor
622 endfor
623
624 unlet g:termdebug_config
625endfunction
626
Ubaldo Tiberiae1c8b72024-11-19 22:32:30 +0100627function Test_termdebug_config_debug()
628 let s:error_message = '\[termdebug\] Termdebug already loaded'
629
630 " USER mode: No error message shall be displayed
631 packadd termdebug
632 call assert_true(execute('messages') !~ s:error_message)
633
634 " DEBUG mode: Error message shall now be displayed
635 let g:termdebug_config = {}
636 let g:termdebug_config['debug'] = 1
637 packadd termdebug
638 call assert_true(execute('messages') =~ s:error_message)
639
640 unlet g:termdebug_config
641 unlet g:termdebug_loaded
642 " Revert DEBUG mode, by reloading the plugin
643 source $VIMRUNTIME/pack/dist/opt/termdebug/plugin/termdebug.vim
644endfunction
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200645" vim: shiftwidth=2 sts=2 expandtab