From e55c693d1a1e68e57c1e77cd56e65d227c173a9f Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Mon, 5 Oct 2026 14:46:14 +0100 Subject: [PATCH] Test for 5688. --- regress/cmd-parse-condition.sh | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 regress/cmd-parse-condition.sh diff --git a/regress/cmd-parse-condition.sh b/regress/cmd-parse-condition.sh new file mode 100644 index 000000000..c5487cc2f --- /dev/null +++ b/regress/cmd-parse-condition.sh @@ -0,0 +1,75 @@ +#!/bin/sh + +# Assignments and commands must use the first matching conditional branch. + +PATH=/bin:/usr/bin +TERM=screen + +[ -z "$TEST_TMUX" ] && TEST_TMUX=$(readlink -f ../tmux) +TMUX="$TEST_TMUX -LtestA$$ -f/dev/null" +CONF=$(mktemp) +trap '$TMUX kill-server 2>/dev/null; rm -f "$CONF"' 0 +trap 'exit 1' 1 2 15 +$TMUX new-session -d || exit 1 + +branch() +{ + printf '%%%s %s%s' "$1" "$2" "$sep" + printf '%sPARSE_BRANCH=%s%sset -g @branch %s%s' \ + "$prefix" "$3" "$sep" "$3" "$sep" +} + +# Cover no, one and two elif branches, with and without else, all condition +# values, and nesting inside both an active and an inactive outer branch. +for mode in normal hidden inline; do + prefix= + sep=' +' + [ "$mode" = hidden ] && prefix='%hidden ' + [ "$mode" = inline ] && sep=' ' + for elifs in 0 1 2; do + bits=0 + while [ "$bits" -lt "$((1 << (elifs + 1)))" ]; do + for otherwise in 0 1; do + for outer in 0 1; do + expected=unchanged + { + printf '%s\n' 'PARSE_BRANCH=unchanged' \ + 'set -g @branch unchanged' + printf '%%if %s\n' "$outer" + n=0 + while [ "$n" -le "$elifs" ]; do + condition=$(((bits >> n) & 1)) + directive=elif + [ "$n" -eq 0 ] && directive=if + branch "$directive" "$condition" "$n" + if [ "$condition" -eq 1 ] && + [ "$expected" = unchanged ]; then + expected=$n + fi + n=$((n + 1)) + done + if [ "$otherwise" -eq 1 ]; then + branch else '' else + [ "$expected" = unchanged ] && expected=else + fi + printf '%s\n' '%endif' '%endif' \ + 'set -g @value "$PARSE_BRANCH"' + } >"$CONF" + [ "$outer" -eq 0 ] && expected=unchanged + $TMUX source-file "$CONF" || exit 1 + out=$($TMUX display-message -p '#{@value}:#{@branch}') + if [ "$out" != "$expected:$expected" ]; then + echo "$mode, $elifs elifs, bits=$bits, else=$otherwise, outer=$outer" + echo "Expected $expected:$expected, got $out" + cat "$CONF" + exit 1 + fi + done + done + bits=$((bits + 1)) + done + done +done + +exit 0