zsh-autosuggestions/script/test.zsh

375 lines
8 KiB
Bash
Raw Permalink Normal View History

2016-02-05 23:14:08 +01:00
#!/usr/bin/env zsh
SCRIPT_DIR=$(dirname "$0")
TEST_DIR=$SCRIPT_DIR/../test
DIST_DIR=$SCRIPT_DIR/../
2016-02-05 23:14:08 +01:00
2016-02-13 20:58:21 +01:00
# Use stub.sh for stubbing/mocking
source $TEST_DIR/stub-1.0.2.sh
source $DIST_DIR/zsh-autosuggestions.zsh
2016-02-05 23:14:08 +01:00
2016-02-13 20:58:21 +01:00
#--------------------------------------------------------------------#
# Highlighting #
#--------------------------------------------------------------------#
testHighlightDefaultStyle() {
2016-02-05 23:14:08 +01:00
assertEquals \
"fg=8" \
"$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
}
testHighlightApplyWithSuggestion() {
orig_style=ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=4"
BUFFER="ec"
POSTDISPLAY="ho hello"
region_highlight=("0 2 fg=1")
_zsh_autosuggest_highlight_apply
assertEquals \
2016-02-13 20:58:21 +01:00
"highlight did not use correct style" \
2016-02-05 23:14:08 +01:00
"0 2 fg=1 2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
"$region_highlight"
assertEquals \
2016-02-13 20:58:21 +01:00
"higlight was not saved to be removed later" \
2016-02-05 23:14:08 +01:00
"2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=orig_style
}
testHighlightApplyWithoutSuggestion() {
BUFFER="echo hello"
POSTDISPLAY=""
region_highlight=("0 4 fg=1")
_zsh_autosuggest_highlight_apply
assertEquals \
2016-02-13 20:58:21 +01:00
"region_highlight was modified" \
2016-02-05 23:14:08 +01:00
"0 4 fg=1" \
"$region_highlight"
assertNull \
2016-02-13 20:58:21 +01:00
"last highlight region was not cleared" \
2016-02-05 23:14:08 +01:00
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
}
testHighlightReset() {
BUFFER="ec"
POSTDISPLAY="ho hello"
region_highlight=("0 1 fg=1" "2 10 fg=8" "1 2 fg=1")
_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="2 10 fg=8"
_zsh_autosuggest_highlight_reset
assertEquals \
2016-02-13 20:58:21 +01:00
"last highlight region was not removed" \
2016-02-05 23:14:08 +01:00
"0 1 fg=1 1 2 fg=1" \
"$region_highlight"
assertNull \
2016-02-13 20:58:21 +01:00
"last highlight variable was not cleared" \
2016-02-05 23:14:08 +01:00
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
}
2016-02-13 20:59:09 +01:00
#--------------------------------------------------------------------#
# Widgets #
#--------------------------------------------------------------------#
testWidgetFunctionClear() {
BUFFER="ec"
POSTDISPLAY="ho hello"
2016-02-15 16:31:00 +01:00
_zsh_autosuggest_clear "original-widget"
2016-02-13 20:59:09 +01:00
assertEquals \
"BUFFER was modified" \
"ec" \
"$BUFFER"
assertNull \
"POSTDISPLAY was not cleared" \
"$POSTDISPLAY"
}
testWidgetFunctionModify() {
BUFFER=""
POSTDISPLAY=""
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
2016-02-13 20:59:09 +01:00
'BUFFER+="e"'
stub_and_echo \
_zsh_autosuggest_suggestion \
"echo hello"
2016-02-15 16:31:00 +01:00
_zsh_autosuggest_modify "original-widget"
2016-02-13 20:59:09 +01:00
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was not modified" \
"e" \
"$BUFFER"
assertEquals \
"POSTDISPLAY does not contain suggestion" \
"cho hello" \
"$POSTDISPLAY"
restore _zsh_autosuggest_invoke_original_widget
restore _zsh_autosuggest_suggestion
}
testWidgetFunctionAcceptCursorAtEnd() {
BUFFER="echo"
POSTDISPLAY=" hello"
CURSOR=4
stub _zsh_autosuggest_invoke_original_widget
2016-02-15 16:31:00 +01:00
_zsh_autosuggest_accept "original-widget"
2016-02-13 20:59:09 +01:00
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was not modified" \
"echo hello" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was not cleared" \
"" \
"$POSTDISPLAY"
}
testWidgetFunctionAcceptCursorNotAtEnd() {
BUFFER="echo"
POSTDISPLAY=" hello"
CURSOR=2
stub _zsh_autosuggest_invoke_original_widget
2016-02-15 16:31:00 +01:00
_zsh_autosuggest_accept "original-widget"
2016-02-13 20:59:09 +01:00
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was modified" \
"echo" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was modified" \
" hello" \
"$POSTDISPLAY"
}
2016-02-23 18:33:40 +01:00
testWidgetFunctionExecute() {
BUFFER="ec"
POSTDISPLAY="ho hello"
stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_execute
assertTrue \
"accept-line not invoked" \
"stub_called_with _zsh_autosuggest_invoke_original_widget 'accept-line'"
assertEquals \
"BUFFER was not modified" \
"echo hello" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was not cleared" \
"" \
"$POSTDISPLAY"
}
2016-02-13 20:59:09 +01:00
testWidgetFunctionPartialAcceptCursorMovesOutOfBuffer() {
BUFFER="ec"
POSTDISPLAY="ho hello"
CURSOR=1
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
2016-02-15 16:31:00 +01:00
_zsh_autosuggest_partial_accept "original-widget"
2016-02-13 20:59:09 +01:00
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was not modified correctly" \
"echo " \
"$BUFFER"
assertEquals \
"POSTDISPLAY was not modified correctly" \
"hello" \
"$POSTDISPLAY"
}
testWidgetFunctionPartialAcceptCursorStaysInBuffer() {
BUFFER="echo hello"
POSTDISPLAY=" world"
CURSOR=1
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
2016-02-15 16:31:00 +01:00
_zsh_autosuggest_partial_accept "original-widget"
2016-02-13 20:59:09 +01:00
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was modified" \
"echo hello" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was modified" \
" world" \
"$POSTDISPLAY"
}
testWidgetAccept() {
stub _zsh_autosuggest_highlight_reset
stub _zsh_autosuggest_accept
stub _zsh_autosuggest_highlight_apply
# Call the function pointed to by the widget since we can't call
# the widget itself when zle is not active
2016-02-15 16:31:00 +01:00
${widgets[autosuggest-accept]#*:} "original-widget"
2016-02-13 20:59:09 +01:00
assertTrue \
"autosuggest-accept widget does not exist" \
"zle -l autosuggest-accept"
assertTrue \
"highlight_reset was not called" \
"stub_called _zsh_autosuggest_highlight_reset"
assertTrue \
"widget function was not called" \
"stub_called _zsh_autosuggest_accept"
assertTrue \
"highlight_apply was not called" \
"stub_called _zsh_autosuggest_highlight_apply"
}
testWidgetClear() {
stub _zsh_autosuggest_highlight_reset
stub _zsh_autosuggest_clear
stub _zsh_autosuggest_highlight_apply
# Call the function pointed to by the widget since we can't call
# the widget itself when zle is not active
2016-02-15 16:31:00 +01:00
${widgets[autosuggest-clear]#*:} "original-widget"
2016-02-13 20:59:09 +01:00
assertTrue \
"autosuggest-clear widget does not exist" \
"zle -l autosuggest-clear"
assertTrue \
"highlight_reset was not called" \
"stub_called _zsh_autosuggest_highlight_reset"
assertTrue \
"widget function was not called" \
"stub_called _zsh_autosuggest_clear"
assertTrue \
2016-02-23 18:33:40 +01:00
"highlight_apply was not called" \
"stub_called _zsh_autosuggest_highlight_apply"
}
testWidgetExecute() {
stub _zsh_autosuggest_highlight_reset
stub _zsh_autosuggest_execute
stub _zsh_autosuggest_highlight_apply
# Call the function pointed to by the widget since we can't call
# the widget itself when zle is not active
${widgets[autosuggest-execute]#*:} "original-widget"
assertTrue \
"autosuggest-execute widget does not exist" \
"zle -l autosuggest-execute"
assertTrue \
"highlight_reset was not called" \
"stub_called _zsh_autosuggest_highlight_reset"
assertTrue \
"widget function was not called" \
"stub_called _zsh_autosuggest_execute"
assertTrue \
2016-02-13 20:59:09 +01:00
"highlight_apply was not called" \
"stub_called _zsh_autosuggest_highlight_apply"
}
testEscapeCommandPrefix() {
assertEquals \
"Did not escape single backslash" \
"\\\\" \
2016-03-05 03:22:19 +01:00
"$(_zsh_autosuggest_escape_command "\\")"
assertEquals \
"Did not escape two backslashes" \
"\\\\\\\\" \
2016-03-05 03:22:19 +01:00
"$(_zsh_autosuggest_escape_command "\\\\")"
assertEquals \
"Did not escape parentheses" \
"\\(\\)" \
2016-03-05 03:22:19 +01:00
"$(_zsh_autosuggest_escape_command "()")"
assertEquals \
"Did not escape square brackets" \
"\\[\\]" \
2016-03-05 03:22:19 +01:00
"$(_zsh_autosuggest_escape_command "[]")"
assertEquals \
"Did not escape pipe" \
"\\|" \
2016-03-05 03:22:19 +01:00
"$(_zsh_autosuggest_escape_command "|")"
assertEquals \
"Did not escape star" \
"\\*" \
2016-03-05 03:22:19 +01:00
"$(_zsh_autosuggest_escape_command "*")"
assertEquals \
"Did not escape question mark" \
"\\?" \
2016-03-05 03:22:19 +01:00
"$(_zsh_autosuggest_escape_command "?")"
}
2016-02-05 23:14:08 +01:00
# For zsh compatibility
setopt shwordsplit
SHUNIT_PARENT=$0
source $TEST_DIR/shunit2-2.1.6/src/shunit2