diff --git a/spec/strategies/default_spec.rb b/spec/strategies/default_spec.rb new file mode 100644 index 0000000..36a1221 --- /dev/null +++ b/spec/strategies/default_spec.rb @@ -0,0 +1,132 @@ +describe 'default strategy' do + let(:session) { TerminalSession.new } + + before do + session.run_command('source zsh-autosuggestions.zsh') + session.run_command('fc -p') + session.clear + end + + after do + session.destroy + end + + context 'with some simple history entries' do + before do + session.run_command('ls foo') + session.run_command('ls bar') + + session.clear + end + + it 'suggests nothing if there is no match' do + session.send_string('ls q') + wait_for { session.content }.to eq('ls q') + end + + it 'suggests the most recent matching history item' do + session.send_string('ls') + wait_for { session.content }.to eq('ls bar') + end + end + + xcontext 'with a multiline hist entry' do + before do + session.send_string('echo "') + session.send_keys('enter') + session.send_string('"') + session.send_keys('enter') + + session.clear + end + + it do + session.send_keys('e') + wait_for { session.content }.to eq "echo \"\n\"" + end + end + + context 'with a hist entry with a backslash' do + before do + session.run_command('echo "hello\nworld"') + session.clear + end + + it do + session.send_string('echo "hello\\') + wait_for { session.content }.to eq('echo "hello\nworld"') + end + end + + context 'with a hist entry with double backslashes' do + before do + session.run_command('echo "\\\\"') + session.clear + end + + it do + session.send_string('echo "\\\\') + wait_for { session.content }.to eq('echo "\\\\"') + end + end + + context 'with a hist entry with a tilde' do + before do + session.run_command('ls ~/foo') + session.clear + end + + it do + session.send_string('ls ~') + wait_for { session.content }.to eq('ls ~/foo') + end + + context 'with extended_glob set' do + before do + session.run_command('setopt local_options extended_glob') + session.clear + end + + it do + session.send_string('ls ~') + wait_for { session.content }.to eq('ls ~/foo') + end + end + end + + context 'with a hist entry with parentheses' do + before do + session.run_command('echo "$(ls foo)"') + session.clear + end + + it do + session.send_string('echo "$(') + wait_for { session.content }.to eq('echo "$(ls foo)"') + end + end + + context 'with a hist entry with square brackets' do + before do + session.run_command('echo "$history[123]"') + session.clear + end + + it do + session.send_string('echo "$history[') + wait_for { session.content }.to eq('echo "$history[123]"') + end + end + + context 'with a hist entry with pound sign' do + before do + session.run_command('echo "#yolo"') + session.clear + end + + it do + session.send_string('echo "#') + wait_for { session.content }.to eq('echo "#yolo"') + end + end +end diff --git a/spec/strategies/match_prev_cmd_spec.rb b/spec/strategies/match_prev_cmd_spec.rb new file mode 100644 index 0000000..e038fc7 --- /dev/null +++ b/spec/strategies/match_prev_cmd_spec.rb @@ -0,0 +1,60 @@ +describe 'match_prev_cmd strategy' do + let(:session) { TerminalSession.new } + + before do + session.run_command('source zsh-autosuggestions.zsh') + session.run_command('ZSH_AUTOSUGGEST_STRATEGY=match_prev_cmd') + session.run_command('fc -p') + session.clear + end + + after do + session.destroy + end + + context 'with some history entries' do + before do + session.run_command('echo what') + session.run_command('ls foo') + session.run_command('echo what') + session.run_command('ls bar') + session.run_command('ls baz') + + session.clear + end + + it 'suggests nothing if prefix does not match' do + session.send_string('ls q') + wait_for { session.content }.to eq('ls q') + end + + it 'suggests the most recent matching history item' do + session.send_string('ls') + wait_for { session.content }.to eq('ls baz') + end + + it 'suggests the most recent after the previous command' do + session.run_command('echo what') + session.clear + + session.send_string('ls') + wait_for { session.content }.to eq('ls bar') + end + end + + context 'with a multiline hist entry' do + before do + session.send_string('echo "') + session.send_keys('enter') + session.send_string('"') + session.send_keys('enter') + + session.clear + end + + it do + session.send_keys('e') + wait_for { session.content }.to eq "echo \"\n\"" + end + end +end diff --git a/test/strategies/default_test.zsh b/test/strategies/default_test.zsh deleted file mode 100755 index f5200e6..0000000 --- a/test/strategies/default_test.zsh +++ /dev/null @@ -1,56 +0,0 @@ -#!/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" diff --git a/test/strategies/match_prev_cmd_test.zsh b/test/strategies/match_prev_cmd_test.zsh deleted file mode 100755 index bf3fc64..0000000 --- a/test/strategies/match_prev_cmd_test.zsh +++ /dev/null @@ -1,74 +0,0 @@ -#!/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" diff --git a/test/strategies_test.zsh b/test/strategies_test.zsh deleted file mode 100644 index 0a937f4..0000000 --- a/test/strategies_test.zsh +++ /dev/null @@ -1,102 +0,0 @@ -#!/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"