Fix default suggestion strategy and add testing

This commit is contained in:
Geza Lore 2016-03-01 21:57:10 +00:00 committed by Eric Freese
parent 83f78d0760
commit 976acc708c
4 changed files with 89 additions and 3 deletions

View File

@ -24,6 +24,9 @@ ALL_TARGETS := \
$(PLUGIN_TARGET) \ $(PLUGIN_TARGET) \
$(OH_MY_ZSH_LINK_TARGET) $(OH_MY_ZSH_LINK_TARGET)
TEST_FILES := \
$(SCRIPT_DIR)/test*.zsh
all: $(ALL_TARGETS) all: $(ALL_TARGETS)
$(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES) $(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES)
@ -39,4 +42,7 @@ clean:
.PHONY: test .PHONY: test
test: all test: all
$(SCRIPT_DIR)/test.zsh @for test_file in $(TEST_FILES); do \
echo "\nRunning $$test_file"; \
$$test_file; \
done

View File

@ -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

View File

@ -10,7 +10,7 @@ _zsh_autosuggest_strategy_default() {
local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")" local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")"
# Get the hist number of the most recent history item that matches # 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 the history entry
echo -E "${history[$histkey]}" echo -E "${history[$histkey]}"

View File

@ -341,7 +341,7 @@ _zsh_autosuggest_strategy_default() {
local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")" local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")"
# Get the hist number of the most recent history item that matches # 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 the history entry
echo -E "${history[$histkey]}" echo -E "${history[$histkey]}"