From 976acc708c396827d1dc978b8e1a81fb8301f638 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Tue, 1 Mar 2016 21:57:10 +0000 Subject: [PATCH] Fix default suggestion strategy and add testing --- Makefile | 8 +++- script/test-strategy-default.zsh | 80 ++++++++++++++++++++++++++++++++ src/strategies/default.zsh | 2 +- zsh-autosuggestions.zsh | 2 +- 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100755 script/test-strategy-default.zsh diff --git a/Makefile b/Makefile index 564fc95..d3d8ec6 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,9 @@ ALL_TARGETS := \ $(PLUGIN_TARGET) \ $(OH_MY_ZSH_LINK_TARGET) +TEST_FILES := \ + $(SCRIPT_DIR)/test*.zsh + all: $(ALL_TARGETS) $(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES) @@ -39,4 +42,7 @@ clean: .PHONY: test test: all - $(SCRIPT_DIR)/test.zsh + @for test_file in $(TEST_FILES); do \ + echo "\nRunning $$test_file"; \ + $$test_file; \ + done diff --git a/script/test-strategy-default.zsh b/script/test-strategy-default.zsh new file mode 100755 index 0000000..9dfbb89 --- /dev/null +++ b/script/test-strategy-default.zsh @@ -0,0 +1,80 @@ +#!/usr/bin/env zsh + +SCRIPT_DIR=$(dirname "$0") +TEST_DIR=$SCRIPT_DIR/../test +DIST_DIR=$SCRIPT_DIR/../ + +source $TEST_DIR/stub-1.0.2.sh + +source $DIST_DIR/zsh-autosuggestions.zsh + +#--------------------------------------------------------------------# +# Default Suggestions Strategy # +#--------------------------------------------------------------------# + +TMPHIST_FILE=/tmp/zsh-autosuggestions-test-tmp-hist + +# Use stub.sh for stubbing/mocking +HISTSIZE=0 # Clear history +HISTSIZE=100 + +cat > $TMPHIST_FILE <<-EOH + one + two + three + four + five + six + seven + eight + nine + ten + eleven +EOH +echo >> $TMPHIST_FILE + +fc -R $TMPHIST_FILE + +rm $TMPHIST_FILE + +ZSH_AUTOSUGGEST_STRATEGY=default + +testNoMatch() { + assertEquals \ + "Did not pick correct suggestion for prefix 'garbage'" \ + "" \ + "$(_zsh_autosuggest_suggestion garbage)" +} + +testMatch() { + assertEquals \ + "Did not pick correct suggestion for prefix 'o'" \ + "one" \ + "$(_zsh_autosuggest_suggestion o)" + + assertEquals \ + "Did not pick correct suggestion for prefix 't'" \ + "ten" \ + "$(_zsh_autosuggest_suggestion t)" + + assertEquals \ + "Did not pick correct suggestion for prefix 'tw'" \ + "two" \ + "$(_zsh_autosuggest_suggestion tw)" + + assertEquals \ + "Did not pick correct suggestion for prefix 'f'" \ + "five" \ + "$(_zsh_autosuggest_suggestion f)" + + assertEquals \ + "Did not pick correct suggestion for prefix 'fo'" \ + "four" \ + "$(_zsh_autosuggest_suggestion fo)" +} + +setopt shwordsplit +SHUNIT_PARENT=$0 + +source $TEST_DIR/shunit2-2.1.6/src/shunit2 + diff --git a/src/strategies/default.zsh b/src/strategies/default.zsh index feee14e..86b12f8 100644 --- a/src/strategies/default.zsh +++ b/src/strategies/default.zsh @@ -10,7 +10,7 @@ _zsh_autosuggest_strategy_default() { local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")" # Get the hist number of the most recent history item that matches - local histkey="${${(k)history[(R)$prefix*]}[1]}" + local histkey="${${(@k)history[(R)$prefix*]}[1]}" # Echo the history entry echo -E "${history[$histkey]}" diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index a3b0d51..a55c8ff 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -341,7 +341,7 @@ _zsh_autosuggest_strategy_default() { local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")" # Get the hist number of the most recent history item that matches - local histkey="${${(k)history[(R)$prefix*]}[1]}" + local histkey="${${(@k)history[(R)$prefix*]}[1]}" # Echo the history entry echo -E "${history[$histkey]}"