From f1aef4a3091abce0ac36c29ebbcb568860843fce Mon Sep 17 00:00:00 2001 From: tompng Date: Wed, 30 Aug 2023 18:46:40 +0900 Subject: [PATCH] Fix prompt test --- test/irb/test_context.rb | 8 ++++---- test/irb/test_irb.rb | 38 +++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index 34bceea54..d42879b1f 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -614,21 +614,21 @@ def test_eval_input_with_long_exception def test_prompt_main_escape main = Struct.new(:to_s).new("main\a\t\r\n") irb = IRB::Irb.new(IRB::WorkSpace.new(main), TestInputMethod.new) - assert_equal("irb(main )>", irb.prompt('irb(%m)>', nil, 1, 1)) + assert_equal("irb(main )>", irb.format_prompt('irb(%m)>', nil, 1, 1)) end def test_prompt_main_inspect_escape main = Struct.new(:inspect).new("main\\n\nmain") irb = IRB::Irb.new(IRB::WorkSpace.new(main), TestInputMethod.new) - assert_equal("irb(main\\n main)>", irb.prompt('irb(%M)>', nil, 1, 1)) + assert_equal("irb(main\\n main)>", irb.format_prompt('irb(%M)>', nil, 1, 1)) end def test_prompt_main_truncate main = Struct.new(:to_s).new("a" * 100) def main.inspect; to_s.inspect; end irb = IRB::Irb.new(IRB::WorkSpace.new(main), TestInputMethod.new) - assert_equal('irb(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa...)>', irb.prompt('irb(%m)>', nil, 1, 1)) - assert_equal('irb("aaaaaaaaaaaaaaaaaaaaaaaaaaaa...)>', irb.prompt('irb(%M)>', nil, 1, 1)) + assert_equal('irb(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa...)>', irb.format_prompt('irb(%m)>', nil, 1, 1)) + assert_equal('irb("aaaaaaaaaaaaaaaaaaaaaaaaaaaa...)>', irb.format_prompt('irb(%M)>', nil, 1, 1)) end def test_lineno diff --git a/test/irb/test_irb.rb b/test/irb/test_irb.rb index 4870f35f3..84cdc002b 100644 --- a/test/irb/test_irb.rb +++ b/test/irb/test_irb.rb @@ -75,7 +75,6 @@ class MockIO_AutoIndent def initialize(*params) @params = params - @calculated_indent end def auto_indent(&block) @@ -84,14 +83,14 @@ def auto_indent(&block) end class MockIO_DynamicPrompt + attr_reader :prompt_list + def initialize(params, &assertion) @params = params - @assertion = assertion end def dynamic_prompt(&block) - result = block.call(@params) - @assertion.call(result) + @prompt_list = block.call(@params) end end @@ -710,24 +709,25 @@ def test_dynamic_prompt_with_blank_line def assert_dynamic_prompt(input_with_prompt) expected_prompt_list, lines = input_with_prompt.transpose - dynamic_prompt_executed = false - io = MockIO_DynamicPrompt.new(lines) do |prompt_list| - error_message = <<~EOM - Expected dynamic prompt: - #{expected_prompt_list.join("\n")} - - Actual dynamic prompt: - #{prompt_list.join("\n")} - EOM - dynamic_prompt_executed = true - assert_equal(expected_prompt_list, prompt_list, error_message) - end - @irb.context.io = io - @irb.scanner.set_prompt do |ltype, indent, continue, line_no| + def @irb.prompt(opens, continue, line_offset) + ltype = @scanner.ltype_from_open_tokens(opens) + indent = @scanner.calc_indent_level(opens) + continue = opens.any? || continue + line_no = @line_no + line_offset '%03d:%01d:%1s:%s ' % [line_no, indent, ltype, continue ? '*' : '>'] end + io = MockIO_DynamicPrompt.new(lines) + @irb.context.io = io @irb.configure_io - assert dynamic_prompt_executed, "dynamic_prompt's assertions were not executed." + + error_message = <<~EOM + Expected dynamic prompt: + #{expected_prompt_list.join("\n")} + + Actual dynamic prompt: + #{io.prompt_list.join("\n")} + EOM + assert_equal(expected_prompt_list, io.prompt_list, error_message) end end