From f6af5a1128754bdac07b8cf3c41a7397f113a130 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 4 Jul 2023 16:17:36 +0100 Subject: [PATCH] [ruby/irb] Refactor eval history (https://github.com/ruby/irb/pull/623) * Rename `ext/history.rb` to `ext/eval_history.rb` To confusion with `lib/irb/history.rb` * Add eval_history tests * Rename eval_history's History to EvalHistory to avoid confusion --- lib/irb.rb | 2 +- lib/irb/ext/{history.rb => eval_history.rb} | 8 +-- lib/irb/extend-command.rb | 2 +- test/irb/test_eval_history.rb | 68 +++++++++++++++++++++ 4 files changed, 74 insertions(+), 6 deletions(-) rename lib/irb/ext/{history.rb => eval_history.rb} (95%) create mode 100644 test/irb/test_eval_history.rb diff --git a/lib/irb.rb b/lib/irb.rb index 64c716fd4c5a60..1f86a0f386e356 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -154,7 +154,7 @@ # # IRB.conf[:EVAL_HISTORY] = # -# See IRB::Context#eval_history= and History class. The history of command +# See IRB::Context#eval_history= and EvalHistory class. The history of command # results is not permanently saved in any file. # # == Customizing the IRB Prompt diff --git a/lib/irb/ext/history.rb b/lib/irb/ext/eval_history.rb similarity index 95% rename from lib/irb/ext/history.rb rename to lib/irb/ext/eval_history.rb index 5243504efa14e6..1a04178b4080db 100644 --- a/lib/irb/ext/history.rb +++ b/lib/irb/ext/eval_history.rb @@ -40,14 +40,14 @@ def set_last_value(value) # # If +no+ is +nil+, execution result history isn't used (default). # - # History values are available via __ variable, see - # IRB::History. + # EvalHistory values are available via __ variable, see + # IRB::EvalHistory. def eval_history=(no) if no if defined?(@eval_history) && @eval_history @eval_history_values.size(no) else - @eval_history_values = History.new(no) + @eval_history_values = EvalHistory.new(no) IRB.conf[:__TMP__EHV__] = @eval_history_values @workspace.evaluate("__ = IRB.conf[:__TMP__EHV__]") IRB.conf.delete(:__TMP_EHV__) @@ -89,7 +89,7 @@ def eval_history=(no) # __[1] # # => 10 # - class History + class EvalHistory def initialize(size = 16) # :nodoc: @size = size diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb index 7238f1fd1c42de..514293a438c549 100644 --- a/lib/irb/extend-command.rb +++ b/lib/irb/extend-command.rb @@ -316,7 +316,7 @@ module ContextExtender CE = ContextExtender # :nodoc: @EXTEND_COMMANDS = [ - [:eval_history=, "ext/history.rb"], + [:eval_history=, "ext/eval_history.rb"], [:use_tracer=, "ext/tracer.rb"], [:use_loader=, "ext/use-loader.rb"], ] diff --git a/test/irb/test_eval_history.rb b/test/irb/test_eval_history.rb new file mode 100644 index 00000000000000..e81e65f7f40479 --- /dev/null +++ b/test/irb/test_eval_history.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true +require "irb" + +require_relative "helper" + +module TestIRB + class EvalHistoryTest < TestCase + def setup + save_encodings + IRB.instance_variable_get(:@CONF).clear + end + + def teardown + restore_encodings + end + + def execute_lines(*lines, conf: {}, main: self, irb_path: nil) + IRB.init_config(nil) + IRB.conf[:VERBOSE] = false + IRB.conf[:PROMPT_MODE] = :SIMPLE + IRB.conf.merge!(conf) + input = TestInputMethod.new(lines) + irb = IRB::Irb.new(IRB::WorkSpace.new(main), input) + irb.context.return_format = "=> %s\n" + irb.context.irb_path = irb_path if irb_path + IRB.conf[:MAIN_CONTEXT] = irb.context + capture_output do + irb.eval_input + end + end + + def test_eval_history_is_diabled_by_default + out, err = execute_lines( + "a = 1", + "__" + ) + + assert_empty(err) + assert_match(/undefined local variable or method `__'/, out) + end + + def test_eval_history_can_be_retrieved_with_double_underscore + out, err = execute_lines( + "a = 1", + "__", + conf: { EVAL_HISTORY: 5 } + ) + + assert_empty(err) + assert_match("=> 1\n" + "=> 1 1\n", out) + end + + def test_eval_history_respects_given_limit + out, err = execute_lines( + "'foo'\n", + "'bar'\n", + "'baz'\n", + "'xyz'\n", + "__", + conf: { EVAL_HISTORY: 4 } + ) + + assert_empty(err) + # Because eval_history injects `__` into the history AND decide to ignore it, we only get - 1 results + assert_match("2 \"bar\"\n" + "3 \"baz\"\n" + "4 \"xyz\"\n", out) + end + end +end