Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 1 | " Test for the termdebug plugin |
| 2 | |
Yegappan Lakshmanan | 85c3a5b | 2023-08-27 21:59:54 +0200 | [diff] [blame] | 3 | source shared.vim |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 4 | source check.vim |
| 5 | |
| 6 | CheckUnix |
| 7 | CheckFeature terminal |
| 8 | CheckExecutable gdb |
| 9 | CheckExecutable gcc |
| 10 | |
| 11 | let g:GDB = exepath('gdb') |
| 12 | if g:GDB->empty() |
Christian Brabandt | f253443 | 2023-08-27 19:59:28 +0200 | [diff] [blame] | 13 | throw 'Skipped: gdb is not found in $PATH' |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 14 | endif |
| 15 | |
| 16 | let g:GCC = exepath('gcc') |
| 17 | if g:GCC->empty() |
Christian Brabandt | f253443 | 2023-08-27 19:59:28 +0200 | [diff] [blame] | 18 | throw 'Skipped: gcc is not found in $PATH' |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 19 | endif |
| 20 | |
iam28th | 323dda1 | 2023-12-14 20:30:26 +0100 | [diff] [blame^] | 21 | function s:generate_files(bin_name) |
| 22 | let src_name = a:bin_name .. '.c' |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 23 | 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 |
iam28th | 323dda1 | 2023-12-14 20:30:26 +0100 | [diff] [blame^] | 48 | call writefile(lines, src_name) |
| 49 | call system($'{g:GCC} -g -o {a:bin_name} {src_name}') |
| 50 | endfunction |
| 51 | |
| 52 | function s:cleanup_files(bin_name) |
| 53 | call delete(a:bin_name) |
| 54 | call delete(a:bin_name .. '.c') |
| 55 | endfunction |
| 56 | |
| 57 | packadd termdebug |
| 58 | |
| 59 | func Test_termdebug_basic() |
| 60 | let bin_name = 'XTD_basic' |
| 61 | let src_name = bin_name .. '.c' |
| 62 | call s:generate_files(bin_name) |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 63 | |
| 64 | edit XTD_basic.c |
| 65 | Termdebug ./XTD_basic |
Yegappan Lakshmanan | 85c3a5b | 2023-08-27 21:59:54 +0200 | [diff] [blame] | 66 | call WaitForAssert({-> assert_equal(3, winnr('$'))}) |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 67 | 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 Brabandt | 6c93c94 | 2023-08-27 21:48:29 +0200 | [diff] [blame] | 77 | call term_wait(gdb_buf, 400) |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 78 | redraw! |
Yegappan Lakshmanan | 85c3a5b | 2023-08-27 21:59:54 +0200 | [diff] [blame] | 79 | call WaitForAssert({-> assert_equal([ |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 80 | \ {'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 Lakshmanan | 85c3a5b | 2023-08-27 21:59:54 +0200 | [diff] [blame] | 84 | \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)}) |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 85 | Finish |
| 86 | call term_wait(gdb_buf) |
| 87 | redraw! |
Yegappan Lakshmanan | 85c3a5b | 2023-08-27 21:59:54 +0200 | [diff] [blame] | 88 | call WaitForAssert({-> assert_equal([ |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 89 | \ {'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 Lakshmanan | 85c3a5b | 2023-08-27 21:59:54 +0200 | [diff] [blame] | 93 | \ sign_getplaced('', #{group: 'TermDebug'})[0].signs)}) |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 94 | Continue |
Shane-XB-Qian | 2dd613f | 2023-11-12 23:53:39 +0800 | [diff] [blame] | 95 | 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.qian | ca48202 | 2023-11-08 21:59:15 +0100 | [diff] [blame] | 121 | |
| 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.qian | fdbadea | 2023-11-12 09:42:12 +0100 | [diff] [blame] | 137 | call term_wait(gdb_buf) |
Christian Brabandt | 305127f | 2023-11-11 18:59:33 +0100 | [diff] [blame] | 138 | let winw = winwidth(0) |
shane.xb.qian | ca48202 | 2023-11-08 21:59:15 +0100 | [diff] [blame] | 139 | Var |
Christian Brabandt | 305127f | 2023-11-11 18:59:33 +0100 | [diff] [blame] | 140 | 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.qian | ca48202 | 2023-11-08 21:59:15 +0100 | [diff] [blame] | 147 | Asm |
Christian Brabandt | 305127f | 2023-11-11 18:59:33 +0100 | [diff] [blame] | 148 | 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.qian | ca48202 | 2023-11-08 21:59:15 +0100 | [diff] [blame] | 154 | set columns& |
shane.xb.qian | fdbadea | 2023-11-12 09:42:12 +0100 | [diff] [blame] | 155 | call term_wait(gdb_buf) |
shane.xb.qian | ca48202 | 2023-11-08 21:59:15 +0100 | [diff] [blame] | 156 | |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 157 | wincmd t |
| 158 | quit! |
| 159 | redraw! |
shane.xb.qian | 7fbbd7f | 2023-11-08 21:44:48 +0100 | [diff] [blame] | 160 | call WaitForAssert({-> assert_equal(1, winnr('$'))}) |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 161 | call assert_equal([], sign_getplaced('', #{group: 'TermDebug'})[0].signs) |
| 162 | |
iam28th | 323dda1 | 2023-12-14 20:30:26 +0100 | [diff] [blame^] | 163 | call s:cleanup_files(bin_name) |
| 164 | %bw! |
| 165 | endfunc |
| 166 | |
| 167 | func 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 Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 228 | %bw! |
| 229 | endfunc |
| 230 | |
shane.xb.qian | 7fbbd7f | 2023-11-08 21:44:48 +0100 | [diff] [blame] | 231 | func 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! |
| 301 | endfunc |
| 302 | |
Yegappan Lakshmanan | 58f39d8 | 2023-08-27 11:14:44 +0200 | [diff] [blame] | 303 | " vim: shiftwidth=2 sts=2 expandtab |