|
| 1 | +"====================================================================== |
| 2 | +" |
| 3 | +" test_dialog_auto.vim - automated (non-interactive) test for dialog |
| 4 | +" |
| 5 | +" Usage (recommended — ex silent mode with rtp pre-set): |
| 6 | +" vim -u NONE -N -i NONE -n --not-a-term -es |
| 7 | +" -c "set rtp+=c:/Share/vim" |
| 8 | +" -c "source c:/Share/vim/tools/test/test_dialog_auto.vim" |
| 9 | +" echo Exit code: %ERRORLEVEL% |
| 10 | +" type c:\Share\vim\test_dialog_result.log |
| 11 | +" |
| 12 | +" Also works via -S when <sfile> resolves correctly: |
| 13 | +" vim -u NONE -N -i NONE -n --not-a-term -es |
| 14 | +" -S tools/test/test_dialog_auto.vim |
| 15 | +" |
| 16 | +"====================================================================== |
| 17 | + |
| 18 | +" vim: set ts=4 sw=4 tw=78 noet : |
| 19 | + |
| 20 | + |
| 21 | +" ── 0. load dependencies ────────────────────────────────── |
| 22 | +" Try <sfile> first (works with -S), fall back to rtp scan. |
| 23 | +let s:home = fnamemodify(resolve(expand('<sfile>:p')), ':h:h:h') |
| 24 | +if !isdirectory(s:home . '/autoload/quickui') |
| 25 | + let s:home = '' |
| 26 | + for s:p in split(&rtp, ',') |
| 27 | + if isdirectory(s:p . '/autoload/quickui') |
| 28 | + let s:home = s:p |
| 29 | + break |
| 30 | + endif |
| 31 | + endfor |
| 32 | +endif |
| 33 | +if s:home != '' |
| 34 | + exec 'set rtp+=' . fnameescape(s:home) |
| 35 | +endif |
| 36 | +let s:logfile = (s:home != '' ? s:home : '.') . '/test_dialog_result.log' |
| 37 | + |
| 38 | + |
| 39 | +" ── 1. test helpers ─────────────────────────────────────── |
| 40 | +let s:errors = [] |
| 41 | +let s:passed = 0 |
| 42 | + |
| 43 | +function! s:assert_equal(expected, actual, msg) abort |
| 44 | + if a:expected != a:actual |
| 45 | + call add(s:errors, a:msg . ': expected ' . |
| 46 | + \ string(a:expected) . ', got ' . string(a:actual)) |
| 47 | + else |
| 48 | + let s:passed += 1 |
| 49 | + endif |
| 50 | +endfunc |
| 51 | + |
| 52 | + |
| 53 | +" ── 2. test: empty items ───────────────────────────────── |
| 54 | +let r = quickui#dialog#open([], {}) |
| 55 | +call s:assert_equal('', r.button, 'empty: button') |
| 56 | +call s:assert_equal(-1, r.button_index, 'empty: index') |
| 57 | + |
| 58 | + |
| 59 | +" ── 3. test: ESC cancel preserves values ───────────────── |
| 60 | +call feedkeys("\<ESC>", 't') |
| 61 | +let r = quickui#dialog#open([ |
| 62 | + \ {'type': 'input', 'name': 'name', 'prompt': 'Name:', |
| 63 | + \ 'value': 'test_value'}, |
| 64 | + \ {'type': 'check', 'name': 'flag', 'text': 'Enable', 'value': 1}, |
| 65 | + \ {'type': 'button', 'name': 'confirm', |
| 66 | + \ 'items': [' &OK ', ' &Cancel ']}, |
| 67 | + \ ], {'title': 'Test', 'w': 40}) |
| 68 | +call s:assert_equal('', r.button, 'ESC: button') |
| 69 | +call s:assert_equal(-1, r.button_index, 'ESC: index') |
| 70 | +call s:assert_equal('test_value', r.name, 'ESC: preserves input') |
| 71 | +call s:assert_equal(1, r.flag, 'ESC: preserves check') |
| 72 | + |
| 73 | + |
| 74 | +" ── 4. test: type in input then Enter confirms ─────────── |
| 75 | +call feedkeys("hello\<CR>", 't') |
| 76 | +let r = quickui#dialog#open([ |
| 77 | + \ {'type': 'input', 'name': 'x', 'prompt': 'X:'}, |
| 78 | + \ {'type': 'button', 'name': 'ok', |
| 79 | + \ 'items': [' &OK ', ' &Cancel ']}, |
| 80 | + \ ], {'title': 'Test', 'w': 40}) |
| 81 | +call s:assert_equal('', r.button, 'input+Enter: button') |
| 82 | +call s:assert_equal(0, r.button_index, 'input+Enter: index') |
| 83 | +call s:assert_equal('hello', r.x, 'input+Enter: value') |
| 84 | + |
| 85 | + |
| 86 | +" ── 5. test: type in input, Tab to button, Enter ───────── |
| 87 | +call feedkeys("world\<Tab>\<CR>", 't') |
| 88 | +let r = quickui#dialog#open([ |
| 89 | + \ {'type': 'input', 'name': 'x', 'prompt': 'X:'}, |
| 90 | + \ {'type': 'button', 'name': 'ok', |
| 91 | + \ 'items': [' &OK ', ' &Cancel ']}, |
| 92 | + \ ], {'title': 'Test', 'w': 40}) |
| 93 | +call s:assert_equal('ok', r.button, 'Tab+Enter: button') |
| 94 | +call s:assert_equal(0, r.button_index, 'Tab+Enter: index') |
| 95 | +call s:assert_equal('world', r.x, 'Tab+Enter: value') |
| 96 | + |
| 97 | + |
| 98 | +" ── 6. test: radio Right navigation ────────────────────── |
| 99 | +" Right moves cursor, Space commits selection |
| 100 | +call feedkeys("\<Right>\<Right>\<Space>\<CR>", 't') |
| 101 | +let r = quickui#dialog#open([ |
| 102 | + \ {'type': 'radio', 'name': 'r', |
| 103 | + \ 'items': ['A', 'B', 'C'], 'value': 0}, |
| 104 | + \ ], {'title': 'Test', 'w': 40}) |
| 105 | +call s:assert_equal('', r.button, 'radio: button') |
| 106 | +call s:assert_equal(2, r.r, 'radio: value after Right x2 + Space') |
| 107 | + |
| 108 | + |
| 109 | +" ── 7. test: check Space toggle ────────────────────────── |
| 110 | +call feedkeys("\<Space>\<CR>", 't') |
| 111 | +let r = quickui#dialog#open([ |
| 112 | + \ {'type': 'check', 'name': 'c', 'text': 'Flag', 'value': 0}, |
| 113 | + \ ], {'title': 'Test', 'w': 40}) |
| 114 | +call s:assert_equal(1, r.c, 'check: toggled on') |
| 115 | + |
| 116 | + |
| 117 | +" ── 8. test: button hotkey from non-input control ──────── |
| 118 | +" Initial focus on check (first focusable), press 'c' for &Cancel |
| 119 | +call feedkeys("c", 't') |
| 120 | +let r = quickui#dialog#open([ |
| 121 | + \ {'type': 'check', 'name': 'f', 'text': 'Flag', 'value': 0}, |
| 122 | + \ {'type': 'button', 'name': 'ok', |
| 123 | + \ 'items': [' &OK ', ' &Cancel ']}, |
| 124 | + \ ], {'title': 'Test', 'w': 40}) |
| 125 | +call s:assert_equal('ok', r.button, 'hotkey: button') |
| 126 | +call s:assert_equal(1, r.button_index, 'hotkey: Cancel index') |
| 127 | + |
| 128 | + |
| 129 | +" ── 9. test: Enter from check confirms ─────────────────── |
| 130 | +call feedkeys("\<CR>", 't') |
| 131 | +let r = quickui#dialog#open([ |
| 132 | + \ {'type': 'check', 'name': 'c', 'text': 'Flag', 'value': 0}, |
| 133 | + \ {'type': 'button', 'name': 'ok', |
| 134 | + \ 'items': [' &OK ']}, |
| 135 | + \ ], {'title': 'Test', 'w': 40}) |
| 136 | +call s:assert_equal('', r.button, 'check+Enter: button') |
| 137 | +call s:assert_equal(0, r.button_index, 'check+Enter: index') |
| 138 | + |
| 139 | + |
| 140 | +" ── 10. test: label only (no focusable controls) ───────── |
| 141 | +call feedkeys("\<ESC>", 't') |
| 142 | +let r = quickui#dialog#open([ |
| 143 | + \ {'type': 'label', 'text': 'Just a label'}, |
| 144 | + \ ], {'title': 'Test', 'w': 40}) |
| 145 | +call s:assert_equal('', r.button, 'label only: button') |
| 146 | +call s:assert_equal(-1, r.button_index, 'label only: index') |
| 147 | + |
| 148 | + |
| 149 | +" ── 11. test: prompt alignment ──────────────────────────── |
| 150 | +" Two inputs with different prompt lengths should align |
| 151 | +call feedkeys("\<ESC>", 't') |
| 152 | +let r = quickui#dialog#open([ |
| 153 | + \ {'type': 'input', 'name': 'a', 'prompt': 'Name:'}, |
| 154 | + \ {'type': 'input', 'name': 'b', 'prompt': 'Email Address:'}, |
| 155 | + \ ], {'title': 'Test', 'w': 50}) |
| 156 | +call s:assert_equal('', r.button, 'align: button') |
| 157 | +call s:assert_equal('', r.a, 'align: a default') |
| 158 | +call s:assert_equal('', r.b, 'align: b default') |
| 159 | + |
| 160 | + |
| 161 | +" ── 12. test: no button control — Enter from radio ─────── |
| 162 | +call feedkeys("\<CR>", 't') |
| 163 | +let r = quickui#dialog#open([ |
| 164 | + \ {'type': 'radio', 'name': 'r', |
| 165 | + \ 'items': ['X', 'Y'], 'value': 1}, |
| 166 | + \ ], {'title': 'Test', 'w': 40}) |
| 167 | +call s:assert_equal('', r.button, 'no-btn: button') |
| 168 | +call s:assert_equal(0, r.button_index, 'no-btn: index') |
| 169 | +call s:assert_equal(1, r.r, 'no-btn: radio value unchanged') |
| 170 | + |
| 171 | + |
| 172 | +" ── 13. test: prompt alignment inflates width for check ─── |
| 173 | +" A check with short prompt + wide text should not overflow when |
| 174 | +" aligned to an input with a long prompt. |
| 175 | +call feedkeys("\<ESC>", 't') |
| 176 | +let r = quickui#dialog#open([ |
| 177 | + \ {'type': 'input', 'name': 'a', 'prompt': 'Very Long Prompt:', |
| 178 | + \ 'value': ''}, |
| 179 | + \ {'type': 'check', 'name': 'b', 'prompt': 'P:', |
| 180 | + \ 'text': 'A checkbox label text here', 'value': 0}, |
| 181 | + \ ], {'title': 'Test'}) |
| 182 | +" The dialog should have opened and closed without error. |
| 183 | +" Verify that the width is at least prompt_width(aligned) + 4 + text_width. |
| 184 | +" 'Very Long Prompt:' display width = 17, aligned prompt_width = 17+2 = 19 |
| 185 | +" check text 'A checkbox label text here' display width = 26 |
| 186 | +" minimum width = 19 + 4 + 26 = 49 |
| 187 | +" If the old code ran, w might be as small as 40 (min_w), causing overflow. |
| 188 | +call s:assert_equal('', r.button, 'align-inflate: button') |
| 189 | +call s:assert_equal(0, r.b, 'align-inflate: check value') |
| 190 | +call s:assert_equal('', r.a, 'align-inflate: input value') |
| 191 | + |
| 192 | + |
| 193 | +" ── report results ──────────────────────────────────────── |
| 194 | +let total = s:passed + len(s:errors) |
| 195 | +if len(s:errors) == 0 |
| 196 | + call writefile(['ALL PASSED (' . total . ' assertions)'], s:logfile) |
| 197 | + qa! |
| 198 | +else |
| 199 | + let report = ['FAILED: ' . len(s:errors) . '/' . total] + s:errors |
| 200 | + call writefile(report, s:logfile) |
| 201 | + cq 1 |
| 202 | +endif |
0 commit comments