blob: 0acdab1a508c1446ffe2607eb39232d5704aef70 [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()
63 let bin_name = 'XTD_basic'
64 let src_name = bin_name .. '.c'
65 call s:generate_files(bin_name)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020066
67 edit XTD_basic.c
68 Termdebug ./XTD_basic
Christian Brabandt2979cfc2024-07-24 21:37:39 +020069 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020070 call WaitForAssert({-> assert_equal(3, winnr('$'))})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020071 let gdb_buf = winbufnr(1)
72 wincmd b
73 Break 9
74 call term_wait(gdb_buf)
75 redraw!
76 call assert_equal([
77 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
78 \ 'priority': 110, 'group': 'TermDebug'}],
79 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)
80 Run
Christian Brabandt6c93c942023-08-27 21:48:29 +020081 call term_wait(gdb_buf, 400)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020082 redraw!
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020083 call WaitForAssert({-> assert_equal([
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020084 \ {'lnum': 9, 'id': 12, 'name': 'debugPC', 'priority': 110,
85 \ 'group': 'TermDebug'},
86 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
87 \ 'priority': 110, 'group': 'TermDebug'}],
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020088 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020089 Finish
90 call term_wait(gdb_buf)
91 redraw!
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020092 call WaitForAssert({-> assert_equal([
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020093 \ {'lnum': 9, 'id': 1014, 'name': 'debugBreakpoint1.0',
94 \ 'priority': 110, 'group': 'TermDebug'},
95 \ {'lnum': 20, 'id': 12, 'name': 'debugPC',
96 \ 'priority': 110, 'group': 'TermDebug'}],
Yegappan Lakshmanan85c3a5b2023-08-27 21:59:54 +020097 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +020098 Continue
Shane-XB-Qian2dd613f2023-11-12 23:53:39 +080099 call term_wait(gdb_buf)
100
101 let i = 2
102 while i <= 258
103 Break
104 call term_wait(gdb_buf)
105 if i == 2
106 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint2.0')[0].text, '02')})
107 endif
108 if i == 10
109 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint10.0')[0].text, '0A')})
110 endif
111 if i == 168
112 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint168.0')[0].text, 'A8')})
113 endif
114 if i == 255
115 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint255.0')[0].text, 'FF')})
116 endif
117 if i == 256
118 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint256.0')[0].text, 'F+')})
119 endif
120 if i == 258
121 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint258.0')[0].text, 'F+')})
122 endif
123 let i += 1
124 endwhile
shane.xb.qianca482022023-11-08 21:59:15 +0100125
126 let cn = 0
127 " 60 is approx spaceBuffer * 3
128 if winwidth(0) <= 78 + 60
129 Var
zeertzjqaef61792024-07-18 20:35:42 +0200130 call assert_equal(winnr('$'), winnr())
131 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['leaf', 1000], ['leaf', 1003 + cn]]], winlayout())
shane.xb.qianca482022023-11-08 21:59:15 +0100132 let cn += 1
133 bw!
134 Asm
zeertzjqaef61792024-07-18 20:35:42 +0200135 call assert_equal(winnr('$'), winnr())
136 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['leaf', 1000], ['leaf', 1003 + cn]]], winlayout())
shane.xb.qianca482022023-11-08 21:59:15 +0100137 let cn += 1
138 bw!
139 endif
140 set columns=160
shane.xb.qianfdbadea2023-11-12 09:42:12 +0100141 call term_wait(gdb_buf)
Christian Brabandt305127f2023-11-11 18:59:33 +0100142 let winw = winwidth(0)
shane.xb.qianca482022023-11-08 21:59:15 +0100143 Var
Christian Brabandt305127f2023-11-11 18:59:33 +0100144 if winwidth(0) < winw
zeertzjqaef61792024-07-18 20:35:42 +0200145 call assert_equal(winnr('$') - 1, winnr())
146 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['row', [['leaf', 1003 + cn], ['leaf', 1000]]]]], winlayout())
Christian Brabandt305127f2023-11-11 18:59:33 +0100147 let cn += 1
148 bw!
149 endif
150 let winw = winwidth(0)
shane.xb.qianca482022023-11-08 21:59:15 +0100151 Asm
Christian Brabandt305127f2023-11-11 18:59:33 +0100152 if winwidth(0) < winw
zeertzjqaef61792024-07-18 20:35:42 +0200153 call assert_equal(winnr('$') - 1, winnr())
154 call assert_equal(['col', [['leaf', 1002], ['leaf', 1001], ['row', [['leaf', 1003 + cn], ['leaf', 1000]]]]], winlayout())
Christian Brabandt305127f2023-11-11 18:59:33 +0100155 let cn += 1
156 bw!
157 endif
shane.xb.qianca482022023-11-08 21:59:15 +0100158 set columns&
shane.xb.qianfdbadea2023-11-12 09:42:12 +0100159 call term_wait(gdb_buf)
shane.xb.qianca482022023-11-08 21:59:15 +0100160
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200161 wincmd t
162 quit!
163 redraw!
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100164 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200165 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
166
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200167 for use_prompt in [v:true, v:false]
zeertzjqaef61792024-07-18 20:35:42 +0200168 let g:termdebug_config = {}
169 let g:termdebug_config['use_prompt'] = use_prompt
170 TermdebugCommand ./XTD_basic arg args
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200171 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
zeertzjqaef61792024-07-18 20:35:42 +0200172 call WaitForAssert({-> assert_equal(3, winnr('$'))})
173 wincmd t
174 quit!
175 redraw!
176 call WaitForAssert({-> assert_equal(1, winnr('$'))})
177 unlet g:termdebug_config
178 endfor
179
iam28th323dda12023-12-14 20:30:26 +0100180 call s:cleanup_files(bin_name)
181 %bw!
182endfunc
183
Ubaldo Tiberib5c15572024-11-19 21:10:24 +0100184func Test_termdebug_decimal_breakpoints()
185 let bin_name = 'XTD_decimal'
186 let src_name = bin_name .. '.c'
187 call s:generate_files(bin_name)
188
189 exe "edit " .. src_name
190
191 let g:termdebug_config = {}
192 let g:termdebug_config['sign_decimal'] = 1
193
194 exe "Termdebug " .. bin_name
195 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
196 call WaitForAssert({-> assert_equal(3, winnr('$'))})
197 let gdb_buf = winbufnr(1)
198 wincmd b
199 Break 9
200 call term_wait(gdb_buf)
201 redraw!
202
203 let i = 2
204 while i <= 258
205 Break
206 call term_wait(gdb_buf)
207 if i == 2
208 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint2.0')[0].text, '02')})
209 endif
210 if i == 10
211 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint10.0')[0].text, '10')})
212 endif
213 if i == 168
214 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint168.0')[0].text, '9+')})
215 endif
216 if i == 255
217 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint255.0')[0].text, '9+')})
218 endif
219 if i == 256
220 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint256.0')[0].text, '9+')})
221 endif
222 if i == 258
223 call WaitForAssert({-> assert_equal(sign_getdefined('debugBreakpoint258.0')[0].text, '9+')})
224 endif
225 let i += 1
226 endwhile
227
228 wincmd t
229 quit!
230 redraw!
231 call WaitForAssert({-> assert_equal(1, winnr('$'))})
232 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
233
234 call s:cleanup_files(bin_name)
235 %bw!
236
237 unlet g:termdebug_config
238endfunc
239
iam28th323dda12023-12-14 20:30:26 +0100240func Test_termdebug_tbreak()
241 let g:test_is_flaky = 1
242 let bin_name = 'XTD_tbreak'
243 let src_name = bin_name .. '.c'
244
245 eval s:generate_files(bin_name)
246
247 execute 'edit ' .. src_name
248 execute 'Termdebug ./' .. bin_name
249
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200250 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
iam28th323dda12023-12-14 20:30:26 +0100251 call WaitForAssert({-> assert_equal(3, winnr('$'))})
252 let gdb_buf = winbufnr(1)
253 wincmd b
254
255 let bp_line = 22 " 'return' statement in main
256 let temp_bp_line = 10 " 'if' statement in 'for' loop body
257 execute "Tbreak " .. temp_bp_line
258 execute "Break " .. bp_line
259
260 call term_wait(gdb_buf)
261 redraw!
262 " both temporary and normal breakpoint signs were displayed...
263 call assert_equal([
264 \ {'lnum': temp_bp_line, 'id': 1014, 'name': 'debugBreakpoint1.0',
265 \ 'priority': 110, 'group': 'TermDebug'},
266 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
267 \ 'priority': 110, 'group': 'TermDebug'}],
268 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)
269
270 Run
271 call term_wait(gdb_buf, 400)
272 redraw!
273 " debugPC sign is on the line where the temp. bp was set;
274 " temp. bp sign was removed after hit;
275 " normal bp sign is still present
276 call WaitForAssert({-> assert_equal([
277 \ {'lnum': temp_bp_line, 'id': 12, 'name': 'debugPC', 'priority': 110,
278 \ 'group': 'TermDebug'},
279 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
280 \ 'priority': 110, 'group': 'TermDebug'}],
281 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
282
283 Continue
284 call term_wait(gdb_buf)
285 redraw!
286 " debugPC is on the normal breakpoint,
287 " temp. bp on line 10 was only hit once
288 call WaitForAssert({-> assert_equal([
289 \ {'lnum': bp_line, 'id': 12, 'name': 'debugPC', 'priority': 110,
290 \ 'group': 'TermDebug'},
291 \ {'lnum': bp_line, 'id': 2014, 'name': 'debugBreakpoint2.0',
292 \ 'priority': 110, 'group': 'TermDebug'}],
293 \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)})
294
295 wincmd t
296 quit!
297 redraw!
298 call WaitForAssert({-> assert_equal(1, winnr('$'))})
299 call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs)
300
301 eval s:cleanup_files(bin_name)
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200302 %bw!
303endfunc
304
Peter Wolf8f1d0982024-10-27 21:51:14 +0100305func Test_termdebug_evaluate()
306 let bin_name = 'XTD_evaluate'
307 let src_name = bin_name .. '.c'
308 call s:generate_files(bin_name)
309
310 edit XTD_evaluate.c
311 Termdebug ./XTD_evaluate
312 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
313 call WaitForAssert({-> assert_equal(3, winnr('$'))})
314 let gdb_buf = winbufnr(1)
315 wincmd b
316
317 " return stmt in main
318 Break 22
319 call term_wait(gdb_buf)
320 Run
321 call term_wait(gdb_buf, 400)
322 redraw!
323
324 " Evaluate an expression
325 Evaluate n
326 call term_wait(gdb_buf)
327 call assert_equal(execute('1messages')->trim(), '"n": 7')
328 Evaluate argc
329 call term_wait(gdb_buf)
330 call assert_equal(execute('1messages')->trim(), '"argc": 1')
331 Evaluate isprime(n)
332 call term_wait(gdb_buf)
333 call assert_equal(execute('1messages')->trim(), '"isprime(n)": 1')
334
335 wincmd t
336 quit!
337 redraw!
338 call s:cleanup_files(bin_name)
339 %bw!
340endfunc
341
342func Test_termdebug_evaluate_in_popup()
343 CheckScreendump
344 let bin_name = 'XTD_evaluate_in_popup'
345 let src_name = bin_name .. '.c'
346 let code =<< trim END
347 struct Point {
348 int x;
349 int y;
350 };
351
352 int main(int argc, char* argv[]) {
353 struct Point p = {argc, 2};
354 struct Point* p_ptr = &p;
355 return 0;
356 }
357 END
358 call writefile(code, src_name, 'D')
359 call system($'{g:GCC} -g -o {bin_name} {src_name}')
360
361 let lines =<< trim END
362 edit XTD_evaluate_in_popup.c
363 packadd termdebug
364 let g:termdebug_config = {}
365 let g:termdebug_config['evaluate_in_popup'] = v:true
366 Termdebug ./XTD_evaluate_in_popup
367 wincmd b
368 Break 9
369 Run
370 END
371
372 call writefile(lines, 'Xscript', 'D')
373 let buf = RunVimInTerminal('-S Xscript', {})
374 call TermWait(buf, 400)
375
376 call term_sendkeys(buf, ":Evaluate p\<CR>")
377 call TermWait(buf, 400)
378 call VerifyScreenDump(buf, 'Test_termdebug_evaluate_in_popup_01', {})
379
380 call term_sendkeys(buf, ":Evaluate p_ptr\<CR>")
381 call TermWait(buf, 400)
382 call VerifyScreenDump(buf, 'Test_termdebug_evaluate_in_popup_02', {})
383
384 " Cleanup
385 call term_sendkeys(buf, ":Gdb")
386 call term_sendkeys(buf, ":quit!\<CR>")
387 call term_sendkeys(buf, ":qa!\<CR>")
388 call StopVimInTerminal(buf)
389 call delete(bin_name)
390 %bw!
391endfunc
392
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100393func Test_termdebug_mapping()
394 %bw!
Ken Takataffed1542024-05-21 17:14:56 +0200395 call assert_true(maparg('K', 'n', 0, 1)->empty())
396 call assert_true(maparg('-', 'n', 0, 1)->empty())
397 call assert_true(maparg('+', 'n', 0, 1)->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100398 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200399 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100400 call WaitForAssert({-> assert_equal(3, winnr('$'))})
401 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200402 call assert_false(maparg('K', 'n', 0, 1)->empty())
403 call assert_false(maparg('-', 'n', 0, 1)->empty())
404 call assert_false(maparg('+', 'n', 0, 1)->empty())
405 call assert_false(maparg('K', 'n', 0, 1).buffer)
406 call assert_false(maparg('-', 'n', 0, 1).buffer)
407 call assert_false(maparg('+', 'n', 0, 1).buffer)
408 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100409 wincmd t
410 quit!
411 redraw!
412 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ken Takataffed1542024-05-21 17:14:56 +0200413 call assert_true(maparg('K', 'n', 0, 1)->empty())
414 call assert_true(maparg('-', 'n', 0, 1)->empty())
415 call assert_true(maparg('+', 'n', 0, 1)->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100416
417 %bw!
418 nnoremap K :echom "K"<cr>
419 nnoremap - :echom "-"<cr>
420 nnoremap + :echom "+"<cr>
421 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200422 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100423 call WaitForAssert({-> assert_equal(3, winnr('$'))})
424 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200425 call assert_false(maparg('K', 'n', 0, 1)->empty())
426 call assert_false(maparg('-', 'n', 0, 1)->empty())
427 call assert_false(maparg('+', 'n', 0, 1)->empty())
428 call assert_false(maparg('K', 'n', 0, 1).buffer)
429 call assert_false(maparg('-', 'n', 0, 1).buffer)
430 call assert_false(maparg('+', 'n', 0, 1).buffer)
431 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100432 wincmd t
433 quit!
434 redraw!
435 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ken Takataffed1542024-05-21 17:14:56 +0200436 call assert_false(maparg('K', 'n', 0, 1)->empty())
437 call assert_false(maparg('-', 'n', 0, 1)->empty())
438 call assert_false(maparg('+', 'n', 0, 1)->empty())
439 call assert_false(maparg('K', 'n', 0, 1).buffer)
440 call assert_false(maparg('-', 'n', 0, 1).buffer)
441 call assert_false(maparg('+', 'n', 0, 1).buffer)
442 call assert_equal(':echom "K"<cr>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100443
444 %bw!
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200445
446 " -- Test that local-buffer mappings are restored in the correct buffers --
447 " local mappings for foo
448 file foo
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100449 nnoremap <buffer> K :echom "bK"<cr>
450 nnoremap <buffer> - :echom "b-"<cr>
451 nnoremap <buffer> + :echom "b+"<cr>
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200452
453 " no mappings for 'bar'
454 enew
455 file bar
456
457 " Start termdebug from foo
458 buffer foo
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100459 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200460 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100461 call WaitForAssert({-> assert_equal(3, winnr('$'))})
462 wincmd b
Ken Takataffed1542024-05-21 17:14:56 +0200463 call assert_true(maparg('K', 'n', 0, 1).buffer)
464 call assert_true(maparg('-', 'n', 0, 1).buffer)
465 call assert_true(maparg('+', 'n', 0, 1).buffer)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100466 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':echom "bK"<cr>')
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200467
468 Source
469 buffer bar
470 call assert_false(maparg('K', 'n', 0, 1)->empty())
471 call assert_false(maparg('-', 'n', 0, 1)->empty())
472 call assert_false(maparg('+', 'n', 0, 1)->empty())
473 call assert_true(maparg('K', 'n', 0, 1).buffer->empty())
474 call assert_true(maparg('-', 'n', 0, 1).buffer->empty())
475 call assert_true(maparg('+', 'n', 0, 1).buffer->empty())
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100476 wincmd t
477 quit!
478 redraw!
479 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ubaldo Tiberi46f28232024-06-19 19:50:32 +0200480
481 " Termdebug session ended. Buffer 'bar' shall have no mappings
482 call assert_true(bufname() ==# 'bar')
483 call assert_false(maparg('K', 'n', 0, 1)->empty())
484 call assert_false(maparg('-', 'n', 0, 1)->empty())
485 call assert_false(maparg('+', 'n', 0, 1)->empty())
486 call assert_true(maparg('K', 'n', 0, 1).buffer->empty())
487 call assert_true(maparg('-', 'n', 0, 1).buffer->empty())
488 call assert_true(maparg('+', 'n', 0, 1).buffer->empty())
489
490 " Buffer 'foo' shall have the same mapping as before running the termdebug
491 " session
492 buffer foo
493 call assert_true(bufname() ==# 'foo')
Ken Takataffed1542024-05-21 17:14:56 +0200494 call assert_true(maparg('K', 'n', 0, 1).buffer)
495 call assert_true(maparg('-', 'n', 0, 1).buffer)
496 call assert_true(maparg('+', 'n', 0, 1).buffer)
497 call assert_equal(':echom "bK"<cr>', maparg('K', 'n', 0, 1).rhs)
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100498
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200499 nunmap K
500 nunmap +
501 nunmap -
shane.xb.qian7fbbd7f2023-11-08 21:44:48 +0100502 %bw!
503endfunc
504
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200505function Test_termdebug_save_restore_variables()
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200506 " saved mousemodel
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200507 let &mousemodel=''
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200508
509 " saved keys
510 nnoremap K :echo "hello world!"<cr>
511 let expected_map_K = maparg('K', 'n', 0 , 1)
512 nnoremap + :echo "hello plus!"<cr>
513 let expected_map_plus = maparg('+', 'n', 0 , 1)
514 let expected_map_minus = {}
515
516 " saved &columns
517 let expected_columns = &columns
518
519 " We want termdebug to overwrite 'K' map but not '+' map.
520 let g:termdebug_config = {}
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200521 let g:termdebug_config['map_K'] = v:true
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200522
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200523 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200524 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200525 call WaitForAssert({-> assert_equal(3, winnr('$'))})
526 call WaitForAssert({-> assert_match(&mousemodel, 'popup_setpos')})
527 wincmd t
528 quit!
529 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Ubaldo Tiberia48637c2024-06-18 20:18:20 +0200530
531 call assert_true(empty(&mousemodel))
532
533 call assert_true(empty(expected_map_minus))
534 call assert_equal(expected_map_K.rhs, maparg('K', 'n', 0, 1).rhs)
535 call assert_equal(expected_map_plus.rhs, maparg('+', 'n', 0, 1).rhs)
536
537 call assert_equal(expected_columns, &columns)
538
539 nunmap K
540 nunmap +
541 unlet g:termdebug_config
Ubaldo Tiberief8eab82024-06-13 19:23:07 +0200542endfunction
543
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200544function Test_termdebug_sanity_check()
545 " Test if user has filename/folders with wrong names
546 let g:termdebug_config = {}
547 let s:dict = {'disasm_window': 'Termdebug-asm-listing', 'use_prompt': 'gdb', 'variables_window': 'Termdebug-variables-listing'}
548
549 for key in keys(s:dict)
550 let s:filename = s:dict[key]
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200551 let g:termdebug_config[key] = v:true
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200552 let s:error_message = "You have a file/folder named '" .. s:filename .. "'"
553
554 " Write dummy file with bad name
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200555 call writefile(['This', 'is', 'a', 'test'], s:filename, 'D')
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200556 Termdebug
557 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
558 call WaitForAssert({-> assert_equal(1, winnr('$'))})
559
560 call delete(s:filename)
561 call remove(g:termdebug_config, key)
562 endfor
563
564 unlet g:termdebug_config
565endfunction
566
567function Test_termdebug_double_termdebug_instances()
568 let s:error_message = 'Terminal debugger already running, cannot run two'
569 Termdebug
Christian Brabandt2979cfc2024-07-24 21:37:39 +0200570 call WaitForAssert({-> assert_true(get(g:, "termdebug_is_running", v:false))})
Ubaldo Tiberif7f8f0b2024-06-20 22:17:34 +0200571 call WaitForAssert({-> assert_equal(3, winnr('$'))})
572 Termdebug
573 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
574 wincmd t
575 quit!
576 call WaitForAssert({-> assert_equal(1, winnr('$'))})
577 :%bw!
578endfunction
Ubaldo Tiberi62ccaa62024-05-21 23:33:03 +0200579
Ubaldo Tiberia90b0b42024-07-20 12:00:44 +0200580function Test_termdebug_config_types()
581 " TODO Remove the deprecated features after 1 Jan 2025.
582 let g:termdebug_config = {}
583 let s:error_message = 'Deprecation Warning:'
584 call assert_true(maparg('K', 'n', 0, 1)->empty())
585
586 for key in ['disasm_window', 'variables_window', 'map_K']
587 for val in [0, 1, v:true, v:false]
588 let g:termdebug_config[key] = val
589 Termdebug
590
591 " Type check: warning is displayed
592 if typename(val) == 'number'
593 call WaitForAssert({-> assert_true(execute('messages') =~ s:error_message)})
594 endif
595
596 " Test on g:termdebug_config keys
597 if val && key != 'map_K'
598 call WaitForAssert({-> assert_equal(4, winnr('$'))})
599 call remove(g:termdebug_config, key)
600 else
601 call WaitForAssert({-> assert_equal(3, winnr('$'))})
602 endif
603
604 " Test on mapping
605 if key == 'map_K'
606 if val
607 call assert_equal(':Evaluate<CR>', maparg('K', 'n', 0, 1).rhs)
608 else
609 call assert_true(maparg('K', 'n', 0, 1)->empty())
610 endif
611 endif
612
613 " Shutoff termdebug
614 wincmd t
615 quit!
616 call WaitForAssert({-> assert_equal(1, winnr('$'))})
617 :%bw!
618
619 endfor
620 endfor
621
622 unlet g:termdebug_config
623endfunction
624
Ubaldo Tiberiae1c8b72024-11-19 22:32:30 +0100625function Test_termdebug_config_debug()
626 let s:error_message = '\[termdebug\] Termdebug already loaded'
627
628 " USER mode: No error message shall be displayed
629 packadd termdebug
630 call assert_true(execute('messages') !~ s:error_message)
631
632 " DEBUG mode: Error message shall now be displayed
633 let g:termdebug_config = {}
634 let g:termdebug_config['debug'] = 1
635 packadd termdebug
636 call assert_true(execute('messages') =~ s:error_message)
637
638 unlet g:termdebug_config
639 unlet g:termdebug_loaded
640 " Revert DEBUG mode, by reloading the plugin
641 source $VIMRUNTIME/pack/dist/opt/termdebug/plugin/termdebug.vim
642endfunction
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200643" vim: shiftwidth=2 sts=2 expandtab