diff --git a/script/test.zsh b/script/test.zsh index 2d9b13a..23f2793 100755 --- a/script/test.zsh +++ b/script/test.zsh @@ -77,6 +77,210 @@ testHighlightReset() { "$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT" } +#--------------------------------------------------------------------# +# 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" +} + # For zsh compatibility setopt shwordsplit SHUNIT_PARENT=$0