Skip to content

Commit

Permalink
Add helper method to get instance variable value of Reline::Config
Browse files Browse the repository at this point in the history
  • Loading branch information
ima1zumi committed Sep 2, 2024
1 parent 40f124f commit 9f35dd2
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions test/reline/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ def teardown
ENV['INPUTRC'] = @inputrc_backup
end

def get_config_variable(variable)
@config.instance_variable_get(variable)
end

def additional_key_bindings(keymap_label)
@config.instance_variable_get(:@additional_key_bindings)[keymap_label].instance_variable_get(:@key_bindings)
get_config_variable(:@additional_key_bindings)[keymap_label].instance_variable_get(:@key_bindings)
end

def registered_key_bindings(keys)
Expand All @@ -38,15 +42,15 @@ def test_read_lines
set show-mode-in-prompt on
LINES

assert_equal true, @config.instance_variable_get(:@show_mode_in_prompt)
assert_equal true, get_config_variable(:@show_mode_in_prompt)
end

def test_read_lines_with_variable
@config.read_lines(<<~LINES.lines)
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
Expand All @@ -55,7 +59,7 @@ def test_string_value
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
Expand All @@ -64,7 +68,7 @@ def test_string_value_with_brackets
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
Expand All @@ -73,7 +77,7 @@ def test_string_value_with_brackets_and_quotes
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
Expand All @@ -82,7 +86,7 @@ def test_string_value_with_parens
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
Expand All @@ -91,7 +95,7 @@ def test_string_value_with_parens_and_quotes
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_encoding_is_ascii
Expand Down Expand Up @@ -169,7 +173,7 @@ def test_include
$include included_partial
LINES

assert_equal true, @config.instance_variable_get(:@show_mode_in_prompt)
assert_equal true, get_config_variable(:@show_mode_in_prompt)
end

def test_include_expand_path
Expand All @@ -184,7 +188,7 @@ def test_include_expand_path
$include ~/included_partial
LINES

assert_equal true, @config.instance_variable_get(:@show_mode_in_prompt)
assert_equal true, get_config_variable(:@show_mode_in_prompt)
ensure
ENV['HOME'] = home_backup
end
Expand All @@ -198,7 +202,7 @@ def test_if
$endif
LINES

assert_equal '(cmd)', @config.instance_variable_get(:@vi_cmd_mode_string)
assert_equal '(cmd)', get_config_variable(:@vi_cmd_mode_string)
end

def test_if_with_false
Expand All @@ -210,7 +214,7 @@ def test_if_with_false
$endif
LINES

assert_equal '[cmd]', @config.instance_variable_get(:@vi_cmd_mode_string)
assert_equal '[cmd]', get_config_variable(:@vi_cmd_mode_string)
end

def test_if_with_indent
Expand All @@ -224,7 +228,7 @@ def test_if_with_indent
$endif
LINES

assert_equal '(cmd)', @config.instance_variable_get(:@vi_cmd_mode_string)
assert_equal '(cmd)', get_config_variable(:@vi_cmd_mode_string)
end
end

Expand Down Expand Up @@ -446,7 +450,7 @@ def test_history_size
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
Expand Down Expand Up @@ -477,7 +481,7 @@ def test_inputrc_raw_value
set vi-ins-mode-string aaa aaa
set vi-cmd-mode-string bbb ccc # comment
LINES
assert_equal :vi_insert, @config.instance_variable_get(:@editing_mode_label)
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
Expand Down Expand Up @@ -611,41 +615,41 @@ def test_reset_variables
vi_command: Reline::KeyActor::Base.new(Reline::KeyActor::VI_COMMAND_MAPPING)
}

assert_equal additional_key_bindings.keys, @config.instance_variable_get(:@additional_key_bindings).keys
assert_equal additional_key_bindings.keys, get_config_variable(:@additional_key_bindings).keys

additional_key_bindings.each do |editing_mode, key_actor|
assert_equal key_actor.instance_variable_get(:@key_bindings), @config.instance_variable_get(:@additional_key_bindings)[editing_mode].instance_variable_get(:@key_bindings)
assert_equal key_actor.instance_variable_get(:@mapping), @config.instance_variable_get(:@additional_key_bindings)[editing_mode].instance_variable_get(:@mapping)
assert_equal key_actor.instance_variable_get(:@matching_bytes), @config.instance_variable_get(:@additional_key_bindings)[editing_mode].instance_variable_get(:@matching_bytes)
assert_equal key_actor.instance_variable_get(:@key_bindings), get_config_variable(:@additional_key_bindings)[editing_mode].instance_variable_get(:@key_bindings)
assert_equal key_actor.instance_variable_get(:@mapping), get_config_variable(:@additional_key_bindings)[editing_mode].instance_variable_get(:@mapping)
assert_equal key_actor.instance_variable_get(:@matching_bytes), get_config_variable(:@additional_key_bindings)[editing_mode].instance_variable_get(:@matching_bytes)
end

assert_equal oneshot_key_bindings.instance_variable_get(:@key_bindings), @config.instance_variable_get(:@oneshot_key_bindings).instance_variable_get(:@key_bindings)
assert_equal oneshot_key_bindings.instance_variable_get(:@mapping), @config.instance_variable_get(:@oneshot_key_bindings).instance_variable_get(:@mapping)
assert_equal oneshot_key_bindings.instance_variable_get(:@matching_bytes), @config.instance_variable_get(:@oneshot_key_bindings).instance_variable_get(:@matching_bytes)
assert_equal oneshot_key_bindings.instance_variable_get(:@key_bindings), get_config_variable(:@oneshot_key_bindings).instance_variable_get(:@key_bindings)
assert_equal oneshot_key_bindings.instance_variable_get(:@mapping), get_config_variable(:@oneshot_key_bindings).instance_variable_get(:@mapping)
assert_equal oneshot_key_bindings.instance_variable_get(:@matching_bytes), get_config_variable(:@oneshot_key_bindings).instance_variable_get(:@matching_bytes)

assert_equal default_key_bindings.keys, @config.instance_variable_get(:@additional_key_bindings).keys
assert_equal default_key_bindings.keys, get_config_variable(:@additional_key_bindings).keys

default_key_bindings.each do |editing_mode, key_actor|
assert_equal key_actor.instance_variable_get(:@key_bindings), @config.instance_variable_get(:@default_key_bindings)[editing_mode].instance_variable_get(:@key_bindings)
assert_equal key_actor.instance_variable_get(:@mapping), @config.instance_variable_get(:@default_key_bindings)[editing_mode].instance_variable_get(:@mapping)
assert_equal key_actor.instance_variable_get(:@matching_bytes), @config.instance_variable_get(:@default_key_bindings)[editing_mode].instance_variable_get(:@matching_bytes)
assert_equal key_actor.instance_variable_get(:@key_bindings), get_config_variable(:@default_key_bindings)[editing_mode].instance_variable_get(:@key_bindings)
assert_equal key_actor.instance_variable_get(:@mapping), get_config_variable(:@default_key_bindings)[editing_mode].instance_variable_get(:@mapping)
assert_equal key_actor.instance_variable_get(:@matching_bytes), get_config_variable(:@default_key_bindings)[editing_mode].instance_variable_get(:@matching_bytes)
end

assert_equal :emacs, @config.instance_variable_get(:@editing_mode_label)
assert_equal :emacs, @config.instance_variable_get(:@keymap_label)
assert_equal [], @config.instance_variable_get(:@keymap_prefix)
assert_equal '(ins)', @config.instance_variable_get(:@vi_ins_mode_string)
assert_equal '(cmd)', @config.instance_variable_get(:@vi_cmd_mode_string)
assert_equal '@', @config.instance_variable_get(:@emacs_mode_string)
assert_equal (-1), @config.instance_variable_get(:@history_size)
assert_equal 500, @config.instance_variable_get(:@keyseq_timeout)
assert_equal false, @config.instance_variable_get(:@test_mode)
assert_equal false, @config.instance_variable_get(:@autocompletion)
assert_equal false, @config.instance_variable_get(:@convert_meta)
assert_equal false, @config.instance_variable_get(:@loaded)
assert_equal true, @config.instance_variable_get(:@enable_bracketed_paste)
assert_equal false, @config.instance_variable_get(:@show_mode_in_prompt)
assert_equal nil, @config.instance_variable_get(:@inputrc_path)
assert_equal :emacs, get_config_variable(:@editing_mode_label)
assert_equal :emacs, get_config_variable(:@keymap_label)
assert_equal [], get_config_variable(:@keymap_prefix)
assert_equal '(ins)', get_config_variable(:@vi_ins_mode_string)
assert_equal '(cmd)', get_config_variable(:@vi_cmd_mode_string)
assert_equal '@', get_config_variable(:@emacs_mode_string)
assert_equal (-1), get_config_variable(:@history_size)
assert_equal 500, get_config_variable(:@keyseq_timeout)
assert_equal false, get_config_variable(:@test_mode)
assert_equal false, get_config_variable(:@autocompletion)
assert_equal false, get_config_variable(:@convert_meta)
assert_equal false, get_config_variable(:@loaded)
assert_equal true, get_config_variable(:@enable_bracketed_paste)
assert_equal false, get_config_variable(:@show_mode_in_prompt)
assert_equal nil, get_config_variable(:@inputrc_path)

# NOTE: Check for reset leaks
assert_equal 18, @config.instance_variables.size
Expand Down

0 comments on commit 9f35dd2

Please sign in to comment.