Skip to content

Commit

Permalink
Fix prompt test
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Oct 11, 2023
1 parent 213c5be commit f1aef4a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions test/irb/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 19 additions & 19 deletions test/irb/test_irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class MockIO_AutoIndent

def initialize(*params)
@params = params
@calculated_indent
end

def auto_indent(&block)
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit f1aef4a

Please sign in to comment.