Fixed vim and zsh

This commit is contained in:
2018-04-05 13:06:54 +02:00
parent f9db886bd3
commit 0331f6518a
2009 changed files with 256303 additions and 0 deletions

View File

@ -0,0 +1,45 @@
#!/usr/bin/env zsh
source "${0:a:h}/test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
testInvokeOriginalWidgetDefined() {
stub_and_eval \
zle \
'return 1'
_zsh_autosuggest_invoke_original_widget 'self-insert'
assertEquals \
'1' \
"$?"
assertTrue \
'zle was not invoked' \
'stub_called zle'
restore zle
}
testInvokeOriginalWidgetUndefined() {
stub_and_eval \
zle \
'return 1'
_zsh_autosuggest_invoke_original_widget 'some-undefined-widget'
assertEquals \
'0' \
"$?"
assertFalse \
'zle was invoked' \
'stub_called zle'
restore zle
}
run_tests "$0"

View File

@ -0,0 +1,73 @@
#!/usr/bin/env zsh
source "${0:a:h}/test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
testHighlightDefaultStyle() {
assertEquals \
'fg=8' \
"$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
}
testHighlightApplyWithSuggestion() {
local 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 \
'highlight did not use correct style' \
"0 2 fg=1 2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
"$region_highlight"
assertEquals \
'higlight was not saved to be removed later' \
"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 \
'region_highlight was modified' \
'0 4 fg=1' \
"$region_highlight"
assertNull \
'last highlight region was not cleared' \
"$_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 \
'last highlight region was not removed' \
'0 1 fg=1 1 2 fg=1' \
"$region_highlight"
assertNull \
'last highlight variable was not cleared' \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
}
run_tests "$0"

View File

@ -0,0 +1,56 @@
#!/usr/bin/env zsh
source "${0:a:h}/../test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
testNoMatch() {
set_history <<-'EOF'
ls foo
ls bar
EOF
assertSuggestion \
'foo' \
''
assertSuggestion \
'ls q' \
''
}
testBasicMatches() {
set_history <<-'EOF'
ls foo
ls bar
EOF
assertSuggestion \
'ls f' \
'ls foo'
assertSuggestion \
'ls b' \
'ls bar'
}
testMostRecentMatch() {
set_history <<-'EOF'
ls foo
cd bar
ls baz
cd quux
EOF
assertSuggestion \
'ls' \
'ls baz'
assertSuggestion \
'cd' \
'cd quux'
}
run_tests "$0"

View File

@ -0,0 +1,74 @@
#!/usr/bin/env zsh
source "${0:a:h}/../test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
ZSH_AUTOSUGGEST_STRATEGY=match_prev_cmd
}
testNoMatch() {
set_history <<-'EOF'
ls foo
ls bar
EOF
assertSuggestion \
'foo' \
''
assertSuggestion \
'ls q' \
''
}
testBasicMatches() {
set_history <<-'EOF'
ls foo
ls bar
EOF
assertSuggestion \
'ls f' \
'ls foo'
assertSuggestion \
'ls b' \
'ls bar'
}
testMostRecentMatch() {
set_history <<-'EOF'
ls foo
cd bar
ls baz
cd quux
EOF
assertSuggestion \
'ls' \
'ls baz'
assertSuggestion \
'cd' \
'cd quux'
}
testMatchMostRecentAfterPreviousCmd() {
set_history <<-'EOF'
echo what
ls foo
ls bar
echo what
ls baz
ls quux
echo what
EOF
assertSuggestion \
'ls' \
'ls baz'
}
run_tests "$0"

View File

@ -0,0 +1,102 @@
#!/usr/bin/env zsh
source "${0:a:h}/test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
assertBackslashSuggestion() {
set_history <<-'EOF'
echo "hello\nworld"
EOF
assertSuggestion \
'echo "hello\' \
'echo "hello\nworld"'
}
assertDoubleBackslashSuggestion() {
set_history <<-'EOF'
echo "\\"
EOF
assertSuggestion \
'echo "\\' \
'echo "\\"'
}
assertTildeSuggestion() {
set_history <<-'EOF'
cd ~/something
EOF
assertSuggestion \
'cd' \
'cd ~/something'
assertSuggestion \
'cd ~' \
'cd ~/something'
assertSuggestion \
'cd ~/s' \
'cd ~/something'
}
assertTildeSuggestionWithExtendedGlob() {
setopt local_options extended_glob
assertTildeSuggestion
}
assertParenthesesSuggestion() {
set_history <<-'EOF'
echo "$(ls foo)"
EOF
assertSuggestion \
'echo "$(' \
'echo "$(ls foo)"'
}
assertSquareBracketsSuggestion() {
set_history <<-'EOF'
echo "$history[123]"
EOF
assertSuggestion \
'echo "$history[' \
'echo "$history[123]"'
}
assertHashSuggestion() {
set_history <<-'EOF'
echo "#yolo"
EOF
assertSuggestion \
'echo "#' \
'echo "#yolo"'
}
testSpecialCharsForAllStrategies() {
local strategies
strategies=(
"default"
"match_prev_cmd"
)
for s in $strategies; do
ZSH_AUTOSUGGEST_STRATEGY="$s"
assertBackslashSuggestion
assertDoubleBackslashSuggestion
assertTildeSuggestion
assertTildeSuggestionWithExtendedGlob
assertParenthesesSuggestion
assertSquareBracketsSuggestion
done
}
run_tests "$0"

View File

@ -0,0 +1,46 @@
#!/usr/bin/env zsh
source "${0:a:h}/test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
testEscapeCommand() {
assertEquals \
'Did not escape single backslash' \
'\\' \
"$(_zsh_autosuggest_escape_command '\')"
assertEquals \
'Did not escape two backslashes' \
'\\\\' \
"$(_zsh_autosuggest_escape_command '\\')"
assertEquals \
'Did not escape parentheses' \
'\(\)' \
"$(_zsh_autosuggest_escape_command '()')"
assertEquals \
'Did not escape square brackets' \
'\[\]' \
"$(_zsh_autosuggest_escape_command '[]')"
assertEquals \
'Did not escape pipe' \
'\|' \
"$(_zsh_autosuggest_escape_command '|')"
assertEquals \
'Did not escape star' \
'\*' \
"$(_zsh_autosuggest_escape_command '*')"
assertEquals \
'Did not escape question mark' \
'\?' \
"$(_zsh_autosuggest_escape_command '?')"
}
run_tests "$0"

View File

@ -0,0 +1,60 @@
DIR="${0:a:h}"
ROOT_DIR="$DIR/.."
VENDOR_DIR="$ROOT_DIR/vendor"
# Use stub.sh for stubbing/mocking
source "$VENDOR_DIR/stub.sh/stub.sh"
#--------------------------------------------------------------------#
# Helper Functions #
#--------------------------------------------------------------------#
# Source the autosuggestions plugin file
source_autosuggestions() {
source "$ROOT_DIR/zsh-autosuggestions.zsh"
}
# Set history list from stdin
set_history() {
# Make a tmp file in shunit's tmp dir
local tmp=$(mktemp "$SHUNIT_TMPDIR/hist.XXX")
# Write from stdin to the tmp file
> "$tmp"
# Write an extra line to simulate history active mode
# See https://github.com/zsh-users/zsh/blob/ca3bc0d95d7deab4f5381f12b15047de748c0814/Src/hist.c#L69-L82
echo >> "$tmp"
# Clear history and re-read from the tmp file
fc -P; fc -p; fc -R "$tmp"
rm "$tmp"
}
# Should be called at the bottom of every test suite file
# Pass in the name of the test script ($0) for shunit
run_tests() {
local test_script="$1"
shift
# Required for shunit to work with zsh
setopt localoptions shwordsplit
SHUNIT_PARENT="$test_script"
source "$VENDOR_DIR/shunit2/2.1.6/src/shunit2"
}
#--------------------------------------------------------------------#
# Custom Assertions #
#--------------------------------------------------------------------#
assertSuggestion() {
local prefix="$1"
local expected_suggestion="$2"
assertEquals \
"Did not get correct suggestion for prefix:<$prefix> using strategy <$ZSH_AUTOSUGGEST_STRATEGY>" \
"$expected_suggestion" \
"$(_zsh_autosuggest_suggestion "$prefix")"
}

View File

@ -0,0 +1,161 @@
#!/usr/bin/env zsh
source "${0:a:h}/../test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
setUp() {
BUFFER=''
POSTDISPLAY=''
CURSOR=0
KEYMAP='main'
}
tearDown() {
restore _zsh_autosuggest_invoke_original_widget
}
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"
}
testViCursorAtEnd() {
BUFFER='echo'
POSTDISPLAY=' hello'
CURSOR=3
KEYMAP='vicmd'
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"
}
testViCursorNotAtEnd() {
BUFFER='echo'
POSTDISPLAY=' hello'
CURSOR=2
KEYMAP='vicmd'
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"
}
testRetval() {
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'return 1'
_zsh_autosuggest_widget_accept 'original-widget'
assertEquals \
'Did not return correct value from original widget' \
'1' \
"$?"
}
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'
restore _zsh_autosuggest_highlight_reset
restore _zsh_autosuggest_accept
restore _zsh_autosuggest_highlight_apply
}
run_tests "$0"

View File

@ -0,0 +1,77 @@
#!/usr/bin/env zsh
source "${0:a:h}/../test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
setUp() {
BUFFER=''
POSTDISPLAY=''
}
tearDown() {
restore _zsh_autosuggest_invoke_original_widget
}
testClear() {
BUFFER='ec'
POSTDISPLAY='ho hello'
_zsh_autosuggest_clear 'original-widget'
assertEquals \
'BUFFER was modified' \
'ec' \
"$BUFFER"
assertNull \
'POSTDISPLAY was not cleared' \
"$POSTDISPLAY"
}
testRetval() {
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'return 1'
_zsh_autosuggest_widget_clear 'original-widget'
assertEquals \
'Did not return correct value from original widget' \
'1' \
"$?"
}
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'
restore _zsh_autosuggest_highlight_reset
restore _zsh_autosuggest_clear
restore _zsh_autosuggest_highlight_apply
}
run_tests "$0"

View File

@ -0,0 +1,26 @@
#!/usr/bin/env zsh
source "${0:a:h}/../test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
tearDown() {
restore _zsh_autosuggest_invoke_original_widget
}
testRetval() {
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'return 1'
_zsh_autosuggest_widget_execute 'original-widget'
assertEquals \
'Did not return correct value from original widget' \
'1' \
"$?"
}
run_tests "$0"

View File

@ -0,0 +1,88 @@
#!/usr/bin/env zsh
source "${0:a:h}/../test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
setUp() {
BUFFER=''
POSTDISPLAY=''
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=''
}
tearDown() {
restore _zsh_autosuggest_invoke_original_widget
restore _zsh_autosuggest_suggestion
}
testModify() {
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"
}
testModifyBufferTooLarge() {
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE='20'
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'BUFFER+="012345678901234567890"'
stub_and_echo \
_zsh_autosuggest_suggestion \
'012345678901234567890123456789'
_zsh_autosuggest_modify 'original-widget'
assertTrue \
'original widget not invoked' \
'stub_called _zsh_autosuggest_invoke_original_widget'
assertEquals \
'BUFFER was not modified' \
'012345678901234567890' \
"$BUFFER"
assertEquals \
'POSTDISPLAY does not contain suggestion' \
'' \
"$POSTDISPLAY"
}
testRetval() {
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'return 1'
_zsh_autosuggest_widget_modify 'original-widget'
assertEquals \
'Did not return correct value from original widget' \
'1' \
"$?"
}
run_tests "$0"

View File

@ -0,0 +1,84 @@
#!/usr/bin/env zsh
source "${0:a:h}/../test_helper.zsh"
oneTimeSetUp() {
source_autosuggestions
}
setUp() {
BUFFER=''
POSTDISPLAY=''
CURSOR=0
}
tearDown() {
restore _zsh_autosuggest_invoke_original_widget
}
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"
}
testRetval() {
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'return 1'
_zsh_autosuggest_widget_partial_accept 'original-widget'
assertEquals \
'Did not return correct value from original widget' \
'1' \
"$?"
}
run_tests "$0"