diff options
Diffstat (limited to 'test/reline/test_config.rb')
-rw-r--r-- | test/reline/test_config.rb | 283 |
1 files changed, 253 insertions, 30 deletions
diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index b6d2ec7d7e..68a102a599 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -11,21 +11,38 @@ class Reline::Config::Test < Reline::TestCase Dir.mkdir(@tmpdir) end Dir.chdir(@tmpdir) + Reline.test_mode @config = Reline::Config.new + @inputrc_backup = ENV['INPUTRC'] end def teardown Dir.chdir(@pwd) FileUtils.rm_rf(@tmpdir) + Reline.test_reset @config.reset + ENV['INPUTRC'] = @inputrc_backup + end + + def get_config_variable(variable) + @config.instance_variable_get(variable) + end + + def additional_key_bindings(keymap_label) + get_config_variable(:@additional_key_bindings)[keymap_label].instance_variable_get(:@key_bindings) + end + + def registered_key_bindings(keys) + key_bindings = @config.key_bindings + keys.to_h { |key| [key, key_bindings.get(key)] } end def test_read_lines @config.read_lines(<<~LINES.lines) - set bell-style on + set show-mode-in-prompt on LINES - assert_equal :audible, @config.instance_variable_get(:@bell_style) + assert_equal true, get_config_variable(:@show_mode_in_prompt) end def test_read_lines_with_variable @@ -33,7 +50,7 @@ class Reline::Config::Test < Reline::TestCase set disable-completion on LINES - assert_equal true, @config.instance_variable_get(:@disable_completion) + assert_equal true, get_config_variable(:@disable_completion) end def test_string_value @@ -42,7 +59,7 @@ class Reline::Config::Test < Reline::TestCase set emacs-mode-string Emacs LINES - assert_equal 'Emacs', @config.instance_variable_get(:@emacs_mode_string) + assert_equal 'Emacs', get_config_variable(:@emacs_mode_string) end def test_string_value_with_brackets @@ -51,7 +68,7 @@ class Reline::Config::Test < Reline::TestCase set emacs-mode-string [Emacs] LINES - assert_equal '[Emacs]', @config.instance_variable_get(:@emacs_mode_string) + assert_equal '[Emacs]', get_config_variable(:@emacs_mode_string) end def test_string_value_with_brackets_and_quotes @@ -60,7 +77,7 @@ class Reline::Config::Test < Reline::TestCase set emacs-mode-string "[Emacs]" LINES - assert_equal '[Emacs]', @config.instance_variable_get(:@emacs_mode_string) + assert_equal '[Emacs]', get_config_variable(:@emacs_mode_string) end def test_string_value_with_parens @@ -69,7 +86,7 @@ class Reline::Config::Test < Reline::TestCase set emacs-mode-string (Emacs) LINES - assert_equal '(Emacs)', @config.instance_variable_get(:@emacs_mode_string) + assert_equal '(Emacs)', get_config_variable(:@emacs_mode_string) end def test_string_value_with_parens_and_quotes @@ -78,17 +95,34 @@ class Reline::Config::Test < Reline::TestCase set emacs-mode-string "(Emacs)" LINES - assert_equal '(Emacs)', @config.instance_variable_get(:@emacs_mode_string) + assert_equal '(Emacs)', get_config_variable(:@emacs_mode_string) end - def test_comment_line - @config.read_lines([" #a: error\n"]) - assert_not_include @config.key_bindings, nil + def test_encoding_is_ascii + @config.reset + Reline.core.io_gate.instance_variable_set(:@encoding, Encoding::US_ASCII) + @config = Reline::Config.new + + assert_equal true, @config.convert_meta + end + + def test_encoding_is_not_ascii + @config = Reline::Config.new + + assert_equal false, @config.convert_meta end def test_invalid_keystroke - @config.read_lines(["a: error\n"]) - assert_not_include @config.key_bindings, nil + @config.read_lines(<<~LINES.lines) + #"a": comment + a: error + "b": no-error + LINES + key_bindings = additional_key_bindings(:emacs) + assert_not_include key_bindings, 'a'.bytes + assert_not_include key_bindings, nil + assert_not_include key_bindings, [] + assert_include key_bindings, 'b'.bytes end def test_bind_key @@ -132,55 +166,104 @@ class Reline::Config::Test < Reline::TestCase def test_include File.open('included_partial', 'wt') do |f| f.write(<<~PARTIAL_LINES) - set bell-style on + set show-mode-in-prompt on PARTIAL_LINES end @config.read_lines(<<~LINES.lines) $include included_partial LINES - assert_equal :audible, @config.instance_variable_get(:@bell_style) + assert_equal true, get_config_variable(:@show_mode_in_prompt) + end + + def test_include_expand_path + home_backup = ENV['HOME'] + File.open('included_partial', 'wt') do |f| + f.write(<<~PARTIAL_LINES) + set show-mode-in-prompt on + PARTIAL_LINES + end + ENV['HOME'] = Dir.pwd + @config.read_lines(<<~LINES.lines) + $include ~/included_partial + LINES + + assert_equal true, get_config_variable(:@show_mode_in_prompt) + ensure + ENV['HOME'] = home_backup end def test_if @config.read_lines(<<~LINES.lines) $if Ruby - set bell-style audible + set vi-cmd-mode-string (cmd) $else - set bell-style visible + set vi-cmd-mode-string [cmd] $endif LINES - assert_equal :audible, @config.instance_variable_get(:@bell_style) + assert_equal '(cmd)', get_config_variable(:@vi_cmd_mode_string) end def test_if_with_false @config.read_lines(<<~LINES.lines) $if Python - set bell-style audible + set vi-cmd-mode-string (cmd) $else - set bell-style visible + set vi-cmd-mode-string [cmd] $endif LINES - assert_equal :visible, @config.instance_variable_get(:@bell_style) + assert_equal '[cmd]', get_config_variable(:@vi_cmd_mode_string) end def test_if_with_indent %w[Ruby Reline].each do |cond| @config.read_lines(<<~LINES.lines) - set bell-style none + set vi-cmd-mode-string {cmd} $if #{cond} - set bell-style audible + set vi-cmd-mode-string (cmd) $else - set bell-style visible + set vi-cmd-mode-string [cmd] $endif LINES - assert_equal :audible, @config.instance_variable_get(:@bell_style) + assert_equal '(cmd)', get_config_variable(:@vi_cmd_mode_string) end end + def test_nested_if_else + @config.read_lines(<<~LINES.lines) + $if Ruby + "\x1": "O" + $if NotRuby + "\x2": "X" + $else + "\x3": "O" + $if Ruby + "\x4": "O" + $else + "\x5": "X" + $endif + "\x6": "O" + $endif + "\x7": "O" + $else + "\x8": "X" + $if NotRuby + "\x9": "X" + $else + "\xA": "X" + $endif + "\xB": "X" + $endif + "\xC": "O" + LINES + keys = [0x1, 0x3, 0x4, 0x6, 0x7, 0xC] + key_bindings = keys.to_h { |k| [[k], ['O'.ord]] } + assert_equal(key_bindings, additional_key_bindings(:emacs)) + end + def test_unclosed_if e = assert_raise(Reline::Config::InvalidInputrc) do @config.read_lines(<<~LINES.lines, "INPUTRC") @@ -208,6 +291,78 @@ class Reline::Config::Test < Reline::TestCase assert_equal "INPUTRC:1: unmatched endif", e.message end + def test_if_with_mode + @config.read_lines(<<~LINES.lines) + $if mode=emacs + "\C-e": history-search-backward # comment + $else + "\C-f": history-search-forward + $endif + LINES + + assert_equal({[5] => :history_search_backward}, additional_key_bindings(:emacs)) + assert_equal({}, additional_key_bindings(:vi_insert)) + assert_equal({}, additional_key_bindings(:vi_command)) + end + + def test_else + @config.read_lines(<<~LINES.lines) + $if mode=vi + "\C-e": history-search-backward # comment + $else + "\C-f": history-search-forward + $endif + LINES + + assert_equal({[6] => :history_search_forward}, additional_key_bindings(:emacs)) + assert_equal({}, additional_key_bindings(:vi_insert)) + assert_equal({}, additional_key_bindings(:vi_command)) + end + + def test_if_with_invalid_mode + @config.read_lines(<<~LINES.lines) + $if mode=vim + "\C-e": history-search-backward + $else + "\C-f": history-search-forward # comment + $endif + LINES + + assert_equal({[6] => :history_search_forward}, additional_key_bindings(:emacs)) + assert_equal({}, additional_key_bindings(:vi_insert)) + assert_equal({}, additional_key_bindings(:vi_command)) + end + + def test_mode_label_differs_from_keymap_label + @config.read_lines(<<~LINES.lines) + # Sets mode_label and keymap_label to vi + set editing-mode vi + # Change keymap_label to emacs. mode_label is still vi. + set keymap emacs + # condition=true because current mode_label is vi + $if mode=vi + # sets keybinding to current keymap_label=emacs + "\C-e": history-search-backward + $endif + LINES + assert_equal({[5] => :history_search_backward}, additional_key_bindings(:emacs)) + assert_equal({}, additional_key_bindings(:vi_insert)) + assert_equal({}, additional_key_bindings(:vi_command)) + end + + def test_if_without_else_condition + @config.read_lines(<<~LINES.lines) + set editing-mode vi + $if mode=vi + "\C-e": history-search-backward + $endif + LINES + + assert_equal({}, additional_key_bindings(:emacs)) + assert_equal({[5] => :history_search_backward}, additional_key_bindings(:vi_insert)) + assert_equal({}, additional_key_bindings(:vi_command)) + end + def test_default_key_bindings @config.add_default_key_binding('abcd'.bytes, 'EFGH'.bytes) @config.read_lines(<<~'LINES'.lines) @@ -216,7 +371,7 @@ class Reline::Config::Test < Reline::TestCase LINES expected = { 'abcd'.bytes => 'ABCD'.bytes, 'ijkl'.bytes => 'IJKL'.bytes } - assert_equal expected, @config.key_bindings + assert_equal expected, registered_key_bindings(expected.keys) end def test_additional_key_bindings @@ -226,7 +381,7 @@ class Reline::Config::Test < Reline::TestCase LINES expected = { 'ef'.bytes => 'EF'.bytes, 'gh'.bytes => 'GH'.bytes } - assert_equal expected, @config.key_bindings + assert_equal expected, registered_key_bindings(expected.keys) end def test_additional_key_bindings_with_nesting_and_comment_out @@ -238,7 +393,7 @@ class Reline::Config::Test < Reline::TestCase LINES expected = { 'ef'.bytes => 'EF'.bytes, 'gh'.bytes => 'GH'.bytes } - assert_equal expected, @config.key_bindings + assert_equal expected, registered_key_bindings(expected.keys) end def test_additional_key_bindings_for_other_keymap @@ -253,7 +408,41 @@ class Reline::Config::Test < Reline::TestCase LINES expected = { 'cd'.bytes => 'CD'.bytes } - assert_equal expected, @config.key_bindings + assert_equal expected, registered_key_bindings(expected.keys) + end + + def test_additional_key_bindings_for_auxiliary_emacs_keymaps + @config.read_lines(<<~'LINES'.lines) + set keymap emacs + "ab": "AB" + set keymap emacs-standard + "cd": "CD" + set keymap emacs-ctlx + "ef": "EF" + set keymap emacs-meta + "gh": "GH" + set editing-mode emacs # keymap changes to be emacs + LINES + + expected = { + 'ab'.bytes => 'AB'.bytes, + 'cd'.bytes => 'CD'.bytes, + "\C-xef".bytes => 'EF'.bytes, + "\egh".bytes => 'GH'.bytes, + } + assert_equal expected, registered_key_bindings(expected.keys) + end + + def test_key_bindings_with_reset + # @config.reset is called after each readline. + # inputrc file is read once, so key binding shouldn't be cleared by @config.reset + @config.add_default_key_binding('default'.bytes, 'DEFAULT'.bytes) + @config.read_lines(<<~'LINES'.lines) + "additional": "ADDITIONAL" + LINES + @config.reset + expected = { 'default'.bytes => 'DEFAULT'.bytes, 'additional'.bytes => 'ADDITIONAL'.bytes } + assert_equal expected, registered_key_bindings(expected.keys) end def test_history_size @@ -261,7 +450,7 @@ class Reline::Config::Test < Reline::TestCase set history-size 5000 LINES - assert_equal 5000, @config.instance_variable_get(:@history_size) + assert_equal 5000, get_config_variable(:@history_size) history = Reline::History.new(@config) history << "a\n" assert_equal 1, history.size @@ -286,6 +475,17 @@ class Reline::Config::Test < Reline::TestCase ENV['INPUTRC'] = inputrc_backup end + def test_inputrc_raw_value + @config.read_lines(<<~'LINES'.lines) + set editing-mode vi ignored-string + set vi-ins-mode-string aaa aaa + set vi-cmd-mode-string bbb ccc # comment + LINES + assert_equal :vi_insert, get_config_variable(:@editing_mode_label) + assert_equal 'aaa aaa', @config.vi_ins_mode_string + assert_equal 'bbb ccc # comment', @config.vi_cmd_mode_string + end + def test_inputrc_with_utf8 # This file is encoded by UTF-8 so this heredoc string is also UTF-8. @config.read_lines(<<~'LINES'.lines) @@ -311,6 +511,12 @@ class Reline::Config::Test < Reline::TestCase # do nothing end + def test_empty_inputrc + assert_nothing_raised do + @config.read_lines([]) + end + end + def test_xdg_config_home home_backup = ENV['HOME'] xdg_config_home_backup = ENV['XDG_CONFIG_HOME'] @@ -362,4 +568,21 @@ class Reline::Config::Test < Reline::TestCase ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup ENV['HOME'] = home_backup end + + def test_reload + inputrc = "#{@tmpdir}/inputrc" + ENV['INPUTRC'] = inputrc + + File.write(inputrc, "set emacs-mode-string !") + @config.read + assert_equal '!', @config.emacs_mode_string + + File.write(inputrc, "set emacs-mode-string ?") + @config.reload + assert_equal '?', @config.emacs_mode_string + + File.write(inputrc, "") + @config.reload + assert_equal '@', @config.emacs_mode_string + end end |