嘗試使用
我不明白為什么這不起作用,而其他測驗中的以下內容起作用。
[[ "${output}" == "[30mHello World.(B[m How are you?" ]] # also with ESC in IntelliJ
編輯:
我假設底層代碼可以作業,因為輸出是正確的。message()但是,這里來自源檔案 1的基礎代碼:
# arguments and stuff here
if is_false "${no_icons:-}" && is_present "${icon:-}"; then
output_string=" [${icon}] "
unset spaces
fi
output_string="$output_string${spaces:-}${title:-}\n"
text_red "${output_string:-}"
if is_blank "${message:-}"; then
return
fi
echo "$message" | while read -r line; do
# Indirect color function call # Trim whitespaces from line beginning
text_red " $(echo -e "${line}" | sed -e 's/^[[:space:]]*//')\n"
done
這里來自源檔案 2 的著色函式:
function _print_foreground_color() {
printf "%b" "$(tput setaf "${2:-}" 2>/dev/null)" "${1:-}" "$(tput sgr0 2>/dev/null)"
}
function text_red() {
_print_foreground_color "${1:-}" 1
}
我的問題:
- 我究竟做錯了什么?
- 使用 Bats Core 測驗格式的最佳方法是什么?
任何幫助將非常感激!
uj5u.com熱心網友回復:
您可以使用printf "%q"來獲取要測驗的值:
#!/usr/bin/env bash
function _print_foreground_color() {
printf "%b" "$(tput setaf "${2:-}" 2>/dev/null)" "${1:-}" "$(tput sgr0 2>/dev/null)"
}
function text_red() {
_print_foreground_color "${1:-}" 1
}
text_red alert; echo
# Output :
# alert in red
printf "%q\n" "$(text_red alert)"
# Output :
# $'\E[31malert\E(B\E[m'
if test "$(text_red alert)" = $'\E[31malert\E(B\E[m'; then
echo "Compared successfully"
fi
# Output :
# Compared successfully
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/481717.html
