| 1 |
############################################################################## |
| 2 |
# Modules Revision 3.0 |
| 3 |
# Providing a flexible user environment |
| 4 |
# |
| 5 |
# File: completion.00-init/%M% |
| 6 |
# Revision: %I% |
| 7 |
# First Edition: 2026/08/07 |
| 8 |
# Last Mod.: %U%, %G% |
| 9 |
# |
| 10 |
# Authors: Xavier Delaruelle, [email protected] |
| 11 |
# |
| 12 |
# Description: Test procedures |
| 13 |
# Command: |
| 14 |
# Sub-Command: |
| 15 |
# |
| 16 |
# Comment: %C{ |
| 17 |
# Defines shell-agnostic test procedures shared by every |
| 18 |
# shell's completion tests: a shell-specific |
| 19 |
# completion_<shell>_list procedure is expected to |
| 20 |
# record the cmdline it was passed in |
| 21 |
# completion_last_cmdline, which the assert procedures |
| 22 |
# below use to build their test label |
| 23 |
# }C% |
| 24 |
# |
| 25 |
############################################################################## |
| 26 |
|
| 27 |
# prompt used as a distinctive sentinel by every shell-specific driver, to |
| 28 |
# reliably tell apart the shell's own output from the test session's |
| 29 |
set completion_prompt {MODULESTEST> } |
| 30 |
|
| 31 |
# escape a string so it is safe to embed in a Tcl regexp as a literal |
| 32 |
proc completion_re_escape {str} { |
| 33 |
return [regsub -all {[][$.+?*\\()&|^{}]} $str {\\&}] |
| 34 |
} |
| 35 |
|
| 36 |
# assert every word in 'needed' is present in the 'got' candidate list; the |
| 37 |
# test label is built from the cmdline last passed to completion_<shell>_list |
| 38 |
# plus the words looked for |
| 39 |
proc completion_assert_contains {got needed} { |
| 40 |
global completion_last_cmdline |
| 41 |
|
| 42 |
set label "'$completion_last_cmdline' completion contains $needed" |
| 43 |
set missing {} |
| 44 |
foreach word $needed { |
| 45 |
if {$word ni $got} { |
| 46 |
lappend missing $word |
| 47 |
} |
| 48 |
} |
| 49 |
if {[llength $missing]} { |
| 50 |
fail "$label (missing: $missing) (got: $got)" |
| 51 |
} else { |
| 52 |
pass $label |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
# assert no word in 'unwanted' is present in the 'got' candidate list; the |
| 57 |
# test label is built from the cmdline last passed to completion_<shell>_list |
| 58 |
# plus the words that must not appear |
| 59 |
proc completion_assert_not_contains {got unwanted} { |
| 60 |
global completion_last_cmdline |
| 61 |
|
| 62 |
set label "'$completion_last_cmdline' completion excludes $unwanted" |
| 63 |
set found {} |
| 64 |
foreach word $unwanted { |
| 65 |
if {$word in $got} { |
| 66 |
lappend found $word |
| 67 |
} |
| 68 |
} |
| 69 |
if {[llength $found]} { |
| 70 |
fail "$label (unexpected: $found) (got: $got)" |
| 71 |
} else { |
| 72 |
pass $label |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
# assert every word in 'got' matching 'pattern' (a Tcl 'string match' glob) |
| 77 |
# is a member of 'known'; used to check a candidate list holds no more than |
| 78 |
# the fetched, expected words of a given kind (eg no option flag beyond the |
| 79 |
# ones 'module help' documents) without having to also enumerate every other |
| 80 |
# kind of candidate that may legitimately appear (module names, collection |
| 81 |
# names, ...). The test label is built from the cmdline last passed to |
| 82 |
# completion_<shell>_list plus the pattern looked for |
| 83 |
proc completion_assert_only_known {got pattern known} { |
| 84 |
global completion_last_cmdline |
| 85 |
|
| 86 |
set label "'$completion_last_cmdline' completion has no unknown '$pattern' word" |
| 87 |
set extra {} |
| 88 |
foreach word $got { |
| 89 |
if {[string match $pattern $word] && $word ni $known} { |
| 90 |
lappend extra $word |
| 91 |
} |
| 92 |
} |
| 93 |
if {[llength $extra]} { |
| 94 |
fail "$label (unknown: $extra) (known: $known)" |
| 95 |
} else { |
| 96 |
pass $label |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
# assert at least one word in 'candidates' is present in the 'got' candidate |
| 101 |
# list; used where the exact expected word is not known in advance (eg |
| 102 |
# which of several plausible module names a fixture happens to offer), only |
| 103 |
# that completion offers a word of that kind at all. The test label is |
| 104 |
# built from the cmdline last passed to completion_<shell>_list plus the |
| 105 |
# candidate words looked for |
| 106 |
proc completion_assert_any {got candidates} { |
| 107 |
global completion_last_cmdline |
| 108 |
|
| 109 |
set label "'$completion_last_cmdline' completion contains any of $candidates" |
| 110 |
foreach word $candidates { |
| 111 |
if {$word in $got} { |
| 112 |
pass $label |
| 113 |
return |
| 114 |
} |
| 115 |
} |
| 116 |
fail "$label (got: $got)" |
| 117 |
} |
| 118 |
|
| 119 |
# assert 'got' is exactly 'expected'; the test label is built from the |
| 120 |
# cmdline last passed to completion_<shell>_list/_inline plus the expected |
| 121 |
# value |
| 122 |
proc completion_assert_eq {got expected} { |
| 123 |
global completion_last_cmdline |
| 124 |
|
| 125 |
set label "'$completion_last_cmdline' completion inserts '$expected'" |
| 126 |
if {$got ne $expected} { |
| 127 |
fail "$label (got: '$got')" |
| 128 |
} else { |
| 129 |
pass $label |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
# assert a marker file was not created (used to prove no shell code |
| 134 |
# injected through a candidate word got executed); the test label is built |
| 135 |
# from the cmdline last passed to completion_<shell>_list plus the marker |
| 136 |
# path |
| 137 |
proc completion_assert_no_exec {markerpath} { |
| 138 |
global completion_last_cmdline |
| 139 |
|
| 140 |
set label "'$completion_last_cmdline' completion does not execute injected code ($markerpath)" |
| 141 |
if {[file exists $markerpath]} { |
| 142 |
fail "$label (marker file was created: injected code ran)" |
| 143 |
file delete -force $markerpath |
| 144 |
} else { |
| 145 |
pass $label |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |