blob: 68bffd4b28b83ea5f6b0a727ae5f8307a7d00e34 [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!
233 call assert_equal(maparg('K', 'n', 0, 1)->empty(), 1)
234 call assert_equal(maparg('-', 'n', 0, 1)->empty(), 1)
235 call assert_equal(maparg('+', 'n', 0, 1)->empty(), 1)
236 Termdebug
237 call WaitForAssert({-> assert_equal(3, winnr('$'))})
238 wincmd b
239 call assert_equal(maparg('K', 'n', 0, 1)->empty(), 0)
240 call assert_equal(maparg('-', 'n', 0, 1)->empty(), 0)
241 call assert_equal(maparg('+', 'n', 0, 1)->empty(), 0)
242 call assert_equal(maparg('K', 'n', 0, 1).buffer, 0)
243 call assert_equal(maparg('-', 'n', 0, 1).buffer, 0)
244 call assert_equal(maparg('+', 'n', 0, 1).buffer, 0)
245 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':Evaluate<CR>')
246 wincmd t
247 quit!
248 redraw!
249 call WaitForAssert({-> assert_equal(1, winnr('$'))})
250 call assert_equal(maparg('K', 'n', 0, 1)->empty(), 1)
251 call assert_equal(maparg('-', 'n', 0, 1)->empty(), 1)
252 call assert_equal(maparg('+', 'n', 0, 1)->empty(), 1)
253
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
261 call assert_equal(maparg('K', 'n', 0, 1)->empty(), 0)
262 call assert_equal(maparg('-', 'n', 0, 1)->empty(), 0)
263 call assert_equal(maparg('+', 'n', 0, 1)->empty(), 0)
264 call assert_equal(maparg('K', 'n', 0, 1).buffer, 0)
265 call assert_equal(maparg('-', 'n', 0, 1).buffer, 0)
266 call assert_equal(maparg('+', 'n', 0, 1).buffer, 0)
267 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':Evaluate<CR>')
268 wincmd t
269 quit!
270 redraw!
271 call WaitForAssert({-> assert_equal(1, winnr('$'))})
272 call assert_equal(maparg('K', 'n', 0, 1)->empty(), 0)
273 call assert_equal(maparg('-', 'n', 0, 1)->empty(), 0)
274 call assert_equal(maparg('+', 'n', 0, 1)->empty(), 0)
275 call assert_equal(maparg('K', 'n', 0, 1).buffer, 0)
276 call assert_equal(maparg('-', 'n', 0, 1).buffer, 0)
277 call assert_equal(maparg('+', 'n', 0, 1).buffer, 0)
278 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':echom "K"<cr>')
279
280 %bw!
281 nnoremap <buffer> K :echom "bK"<cr>
282 nnoremap <buffer> - :echom "b-"<cr>
283 nnoremap <buffer> + :echom "b+"<cr>
284 Termdebug
285 call WaitForAssert({-> assert_equal(3, winnr('$'))})
286 wincmd b
287 call assert_equal(maparg('K', 'n', 0, 1).buffer, 1)
288 call assert_equal(maparg('-', 'n', 0, 1).buffer, 1)
289 call assert_equal(maparg('+', 'n', 0, 1).buffer, 1)
290 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':echom "bK"<cr>')
291 wincmd t
292 quit!
293 redraw!
294 call WaitForAssert({-> assert_equal(1, winnr('$'))})
295 call assert_equal(maparg('K', 'n', 0, 1).buffer, 1)
296 call assert_equal(maparg('-', 'n', 0, 1).buffer, 1)
297 call assert_equal(maparg('+', 'n', 0, 1).buffer, 1)
298 call assert_equal(maparg('K', 'n', 0, 1).rhs, ':echom "bK"<cr>')
299
300 %bw!
301endfunc
302
Yegappan Lakshmanan58f39d82023-08-27 11:14:44 +0200303" vim: shiftwidth=2 sts=2 expandtab