From 9ec62a1a231bed7849346a21bd9e80c39dacb501 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 14 Mar 2016 17:38:37 -0600 Subject: [PATCH] Split out a separate test file for each widget --- test/widgets/accept_test.zsh | 83 +++++++++++ test/widgets/clear_test.zsh | 51 +++++++ test/widgets/modify_test.zsh | 41 ++++++ test/widgets/partial_accept_test.zsh | 61 ++++++++ test/widgets_test.zsh | 209 --------------------------- 5 files changed, 236 insertions(+), 209 deletions(-) create mode 100644 test/widgets/accept_test.zsh create mode 100644 test/widgets/clear_test.zsh create mode 100644 test/widgets/modify_test.zsh create mode 100644 test/widgets/partial_accept_test.zsh delete mode 100644 test/widgets_test.zsh diff --git a/test/widgets/accept_test.zsh b/test/widgets/accept_test.zsh new file mode 100644 index 0000000..571fb77 --- /dev/null +++ b/test/widgets/accept_test.zsh @@ -0,0 +1,83 @@ +#!/usr/bin/env zsh + +source "${0:a:h}/../test_helper.zsh" + +oneTimeSetUp() { + source_autosuggestions +} + +testCursorAtEnd() { + BUFFER='echo' + POSTDISPLAY=' hello' + CURSOR=4 + + stub _zsh_autosuggest_invoke_original_widget + + _zsh_autosuggest_accept 'original-widget' + + 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" +} + +testCursorNotAtEnd() { + BUFFER='echo' + POSTDISPLAY=' hello' + CURSOR=2 + + stub _zsh_autosuggest_invoke_original_widget + + _zsh_autosuggest_accept 'original-widget' + + assertTrue \ + 'original widget not invoked' \ + 'stub_called _zsh_autosuggest_invoke_original_widget' + + assertEquals \ + 'BUFFER was modified' \ + 'echo' \ + "$BUFFER" + + assertEquals \ + 'POSTDISPLAY was modified' \ + ' hello' \ + "$POSTDISPLAY" +} + +testWidget() { + 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]#*:} 'original-widget' + + 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' +} + +run_tests "$0" diff --git a/test/widgets/clear_test.zsh b/test/widgets/clear_test.zsh new file mode 100644 index 0000000..e9f3a54 --- /dev/null +++ b/test/widgets/clear_test.zsh @@ -0,0 +1,51 @@ +#!/usr/bin/env zsh + +source "${0:a:h}/../test_helper.zsh" + +oneTimeSetUp() { + source_autosuggestions +} + +testClear() { + BUFFER='ec' + POSTDISPLAY='ho hello' + + _zsh_autosuggest_clear 'original-widget' + + assertEquals \ + 'BUFFER was modified' \ + 'ec' \ + "$BUFFER" + + assertNull \ + 'POSTDISPLAY was not cleared' \ + "$POSTDISPLAY" +} + +testWidget() { + 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]#*:} 'original-widget' + + 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' +} + +run_tests "$0" diff --git a/test/widgets/modify_test.zsh b/test/widgets/modify_test.zsh new file mode 100644 index 0000000..d0646a2 --- /dev/null +++ b/test/widgets/modify_test.zsh @@ -0,0 +1,41 @@ +#!/usr/bin/env zsh + +source "${0:a:h}/../test_helper.zsh" + +oneTimeSetUp() { + source_autosuggestions +} + +testModify() { + BUFFER='' + POSTDISPLAY='' + + stub_and_eval \ + _zsh_autosuggest_invoke_original_widget \ + 'BUFFER+="e"' + + stub_and_echo \ + _zsh_autosuggest_suggestion \ + 'echo hello' + + _zsh_autosuggest_modify 'original-widget' + + 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 +} + +run_tests "$0" diff --git a/test/widgets/partial_accept_test.zsh b/test/widgets/partial_accept_test.zsh new file mode 100644 index 0000000..b137943 --- /dev/null +++ b/test/widgets/partial_accept_test.zsh @@ -0,0 +1,61 @@ +#!/usr/bin/env zsh + +source "${0:a:h}/../test_helper.zsh" + +oneTimeSetUp() { + source_autosuggestions +} + +testCursorMovesOutOfBuffer() { + 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 'original-widget' + + 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" +} + +testCursorStaysInBuffer() { + 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 'original-widget' + + 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" +} + +run_tests "$0" diff --git a/test/widgets_test.zsh b/test/widgets_test.zsh deleted file mode 100644 index b5f0f4b..0000000 --- a/test/widgets_test.zsh +++ /dev/null @@ -1,209 +0,0 @@ -#!/usr/bin/env zsh - -source "${0:a:h}/test_helper.zsh" - -oneTimeSetUp() { - source_autosuggestions -} - -testWidgetFunctionClear() { - BUFFER='ec' - POSTDISPLAY='ho hello' - - _zsh_autosuggest_clear 'original-widget' - - 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 'original-widget' - - 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 'original-widget' - - 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 'original-widget' - - 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 'original-widget' - - 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 'original-widget' - - 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]#*:} 'original-widget' - - 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]#*:} 'original-widget' - - 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' -} - -run_tests "$0"