dev tools: Allow specifying preamble code when generating test cases.

This commit is contained in:
Daniel Shahaf 2020-01-15 18:50:21 +00:00
parent 6b0671b63e
commit 1c6a6d92b0

View File

@ -32,16 +32,17 @@ emulate -LR zsh
setopt localoptions extendedglob setopt localoptions extendedglob
# Argument parsing. # Argument parsing.
if (( $# != 3 )) || [[ $1 == -* ]]; then if (( $# * $# - 7 * $# + 12 )) || [[ $1 == -* ]]; then
print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME" print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME [PREAMBLE]"
print -r -- >&2 "" print -r -- >&2 ""
print -r -- >&2 "Generate highlighters/HIGHLIGHTER/test-data/BASENAME.zsh based on the" print -r -- >&2 "Generate highlighters/HIGHLIGHTER/test-data/BASENAME.zsh based on the"
print -r -- >&2 "current highlighting of BUFFER." print -r -- >&2 "current highlighting of BUFFER, using the setup code PREAMBLE."
exit 1 exit 1
fi fi
buffer=$1 buffer=$1
ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 ) ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 )
fname=${0:A:h:h}/highlighters/$2/test-data/${3%.zsh}.zsh fname=${0:A:h:h}/highlighters/$2/test-data/${3%.zsh}.zsh
preamble=${4:-""}
# Load the main script. # Load the main script.
. ${0:A:h:h}/zsh-syntax-highlighting.zsh . ${0:A:h:h}/zsh-syntax-highlighting.zsh
@ -61,9 +62,14 @@ fi
<$0 sed -n -e '1,/^$/p' | sed -e "s/2[0-9][0-9][0-9]/${year}/" > $fname <$0 sed -n -e '1,/^$/p' | sed -e "s/2[0-9][0-9][0-9]/${year}/" > $fname
# Assumes stdout is line-buffered # Assumes stdout is line-buffered
git add -- $fname git add -- $fname
exec > >(tee -a $fname)
# Preamble
if [[ -n $preamble ]]; then
print -rl -- "$preamble" ""
fi
# Buffer # Buffer
exec > >(tee -a $fname)
print -n 'BUFFER=' print -n 'BUFFER='
if [[ $buffer != (#s)[$'\t -~']#(#e) ]]; then if [[ $buffer != (#s)[$'\t -~']#(#e) ]]; then
print -r -- ${(qqqq)buffer} print -r -- ${(qqqq)buffer}
@ -82,8 +88,17 @@ print 'expected_region_highlight=('
PREBUFFER="" PREBUFFER=""
BUFFER="$buffer" BUFFER="$buffer"
region_highlight=() region_highlight=()
# TODO: use run_test() from tests/test-highlighting.zsh (to get a tempdir) eval $(
_zsh_highlight exec 3>&1 >/dev/null
typeset -r __tests_tmpdir="$(mktemp -d)"
{
# Use a subshell to ensure $__tests_tmpdir, which is to be rm -rf'd, won't be modified.
(cd -- "$__tests_tmpdir" && eval $preamble && _zsh_highlight && typeset -p region_highlight >&3)
: # workaround zsh bug workers/45305 with respect to the $(…) subshell we're in
} always {
rm -rf -- ${__tests_tmpdir}
}
)
for ((i=1; i<=${#region_highlight}; i++)); do for ((i=1; i<=${#region_highlight}; i++)); do
local -a highlight_zone; highlight_zone=( ${(z)region_highlight[$i]} ) local -a highlight_zone; highlight_zone=( ${(z)region_highlight[$i]} )