zsh-autosuggestions/script/test.zsh

289 lines
6 KiB
Bash
Raw 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"
_zsh_autosuggest_clear
assertEquals \
"BUFFER was modified" \
"ec" \
"$BUFFER"
assertNull \
"POSTDISPLAY was not cleared" \
"$POSTDISPLAY"
}
testWidgetFunctionModify() {
BUFFER=""
POSTDISPLAY=""
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'BUFFER+="e"'
stub_and_echo \
_zsh_autosuggest_suggestion \
"echo hello"
_zsh_autosuggest_modify
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
_zsh_autosuggest_accept
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
_zsh_autosuggest_accept
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was modified" \
"echo" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was modified" \
" hello" \
"$POSTDISPLAY"
}
testWidgetFunctionPartialAcceptCursorMovesOutOfBuffer() {
BUFFER="ec"
POSTDISPLAY="ho hello"
CURSOR=1
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
_zsh_autosuggest_partial_accept
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"'
_zsh_autosuggest_partial_accept
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
${widgets[autosuggest-accept]#*:}
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
${widgets[autosuggest-clear]#*:}
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 \
"highlight_apply was not called" \
"stub_called _zsh_autosuggest_highlight_apply"
}
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