diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/irb/test_context.rb | 2 | ||||
-rw-r--r-- | test/irb/test_pager.rb | 66 | ||||
-rw-r--r-- | test/irb/yamatanooroti/test_rendering.rb | 18 |
3 files changed, 84 insertions, 2 deletions
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index b02d8dbe09..c44c8e0573 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -361,7 +361,7 @@ module TestIRB irb.eval_input end assert_empty err - assert_equal("=> #{value_first_line[0..(input.winsize.last - 9)]}...\n=> \n#{value}\n", out) + assert_equal("=> \n#{value_first_line[0, input.winsize.last]}...\n=> \n#{value}\n", out) irb.context.evaluate_expression('A.remove_method(:inspect)', 0) input.reset diff --git a/test/irb/test_pager.rb b/test/irb/test_pager.rb new file mode 100644 index 0000000000..5842519e62 --- /dev/null +++ b/test/irb/test_pager.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: false +require 'irb/pager' + +require_relative 'helper' + +module TestIRB + class PagerTest < TestCase + def test_take_first_page + assert_equal ['a' * 40, true], IRB::Pager.take_first_page(10, 4) {|io| io.puts 'a' * 41; raise 'should not reach here' } + assert_equal ['a' * 39, false], IRB::Pager.take_first_page(10, 4) {|io| io.write 'a' * 39 } + assert_equal ['a' * 39 + 'b', false], IRB::Pager.take_first_page(10, 4) {|io| io.write 'a' * 39 + 'b' } + assert_equal ['a' * 39 + 'b', true], IRB::Pager.take_first_page(10, 4) {|io| io.write 'a' * 39 + 'bc' } + assert_equal ["a\nb\nc\nd\n", false], IRB::Pager.take_first_page(10, 4) {|io| io.write "a\nb\nc\nd\n" } + assert_equal ["a\nb\nc\nd\n", true], IRB::Pager.take_first_page(10, 4) {|io| io.write "a\nb\nc\nd\ne" } + assert_equal ['a' * 15 + "\n" + 'b' * 20, true], IRB::Pager.take_first_page(10, 4) {|io| io.puts 'a' * 15; io.puts 'b' * 30 } + assert_equal ["\e[31mA\e[0m" * 10 + 'x' * 30, true], IRB::Pager.take_first_page(10, 4) {|io| io.puts "\e[31mA\e[0m" * 10 + 'x' * 31; } + end + end + + class PageOverflowIOTest < TestCase + def test_overflow + actual_events = [] + overflow_callback = ->(lines) do + actual_events << [:callback_called, lines] + end + out = IRB::Pager::PageOverflowIO.new(10, 4, overflow_callback) + out.puts 'a' * 15 + out.write 'b' * 15 + + actual_events << :before_write + out.write 'c' * 1000 + actual_events << :after_write + + out.puts 'd' * 1000 + out.write 'e' * 1000 + + expected_events = [ + :before_write, + [:callback_called, ['a' * 10, 'a' * 5 + "\n", 'b' * 10, 'b' * 5 + 'c' * 5]], + :after_write, + ] + assert_equal expected_events, actual_events + + expected_whole_content = 'a' * 15 + "\n" + 'b' * 15 + 'c' * 1000 + 'd' * 1000 + "\n" + 'e' * 1000 + assert_equal expected_whole_content, out.string + end + + def test_callback_delay + actual_events = [] + overflow_callback = ->(lines) do + actual_events << [:callback_called, lines] + end + out = IRB::Pager::PageOverflowIO.new(10, 4, overflow_callback, delay: 0.2) + out.write 'a' * 1000 + assert_equal ['a' * 10] * 4, out.first_page_lines + out.write 'b' + actual_events << :before_delay + sleep 0.2 + out.write 'c' + actual_events << :after_delay + out.write 'd' + assert_equal 'a' * 1000 + 'bcd', out.string + assert_equal [:before_delay, [:callback_called, ['a' * 10] * 4], :after_delay], actual_events + end + end +end diff --git a/test/irb/yamatanooroti/test_rendering.rb b/test/irb/yamatanooroti/test_rendering.rb index 212ab0cf81..c1bc81241c 100644 --- a/test/irb/yamatanooroti/test_rendering.rb +++ b/test/irb/yamatanooroti/test_rendering.rb @@ -385,12 +385,28 @@ class IRB::RenderingTest < Yamatanooroti::TestCase write("'a' * 80 * 11\n") write("'foo' + 'bar'\n") # eval something to make sure IRB resumes - assert_screen(/(a{80}\n){8}/) + assert_screen(/"a{79}\n(a{80}\n){7}/) # because pager is invoked, foobar will not be evaluated assert_screen(/\A(?!foobar)/) close end + def test_pretty_print_preview_with_slow_inspect + write_irbrc <<~'LINES' + require "irb/pager" + LINES + start_terminal(10, 80, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: /irb\(main\)/) + write("o1 = Object.new; def o1.inspect; 'INSPECT'; end\n") + write("o2 = Object.new; def o2.inspect; sleep 0.1; 'SLOW'; end\n") + # preview should be shown even if pretty_print is not completed. + write("[o1] * 20 + [o2] * 100\n") + assert_screen(/=>\n\[INSPECT,\n( INSPECT,\n){6}Preparing full inspection value\.\.\./) + write("\C-c") # abort pretty_print + write("'foo' + 'bar'\n") # eval something to make sure IRB resumes + assert_screen(/foobar/) + close + end + def test_long_evaluation_output_is_preserved_after_paging write_irbrc <<~'LINES' require "irb/pager" |