diff options
| author | Stan Lo <stan001212@gmail.com> | 2024-06-01 11:28:03 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-06-01 10:28:08 +0000 |
| commit | cda69b5910494a745d87b7932547341cb2fefe3a (patch) | |
| tree | 602a8ee62968466b46b3ee0a81429d619d8595c5 /test | |
| parent | 767aa0cdb6b82beaf3ef3a51d1004812f52a72b9 (diff) | |
[ruby/reline] Overhaul io gate structure
(https://github.com/ruby/reline/pull/666)
* Overhaul IO gate structure
1. Move IO related classes to `lib/reline/io/` directory.
2. Rename `GeneralIO` to `Dumb`.
3. Use IO classes as instances instead of classes.
* Update lib/reline/io/ansi.rb
Co-authored-by: tomoya ishida <tomoyapenguin@gmail.com>
---------
https://github.com/ruby/reline/commit/dc1518e1ac
Co-authored-by: tomoya ishida <tomoyapenguin@gmail.com>
Diffstat (limited to 'test')
| -rw-r--r-- | test/reline/helper.rb | 29 | ||||
| -rw-r--r-- | test/reline/test_ansi_with_terminfo.rb | 4 | ||||
| -rw-r--r-- | test/reline/test_ansi_without_terminfo.rb | 4 | ||||
| -rw-r--r-- | test/reline/test_config.rb | 4 | ||||
| -rw-r--r-- | test/reline/test_line_editor.rb | 10 | ||||
| -rw-r--r-- | test/reline/test_reline.rb | 4 |
6 files changed, 29 insertions, 26 deletions
diff --git a/test/reline/helper.rb b/test/reline/helper.rb index 26fe834482..a5f850e838 100644 --- a/test/reline/helper.rb +++ b/test/reline/helper.rb @@ -22,29 +22,36 @@ module Reline class <<self def test_mode(ansi: false) @original_iogate = IOGate - remove_const('IOGate') - const_set('IOGate', ansi ? Reline::ANSI : Reline::GeneralIO) + if ENV['RELINE_TEST_ENCODING'] encoding = Encoding.find(ENV['RELINE_TEST_ENCODING']) else encoding = Encoding::UTF_8 end - @original_get_screen_size = IOGate.method(:get_screen_size) - IOGate.singleton_class.remove_method(:get_screen_size) - def IOGate.get_screen_size - [24, 80] + + if ansi + new_io_gate = ANSI.new + # Setting ANSI gate's screen size through set_screen_size will also change the tester's stdin's screen size + # Let's avoid that side-effect by stubbing the get_screen_size method + new_io_gate.define_singleton_method(:get_screen_size) do + [24, 80] + end + new_io_gate.define_singleton_method(:encoding) do + encoding + end + else + new_io_gate = Dumb.new(encoding: encoding) end - Reline::GeneralIO.reset(encoding: encoding) unless ansi + + remove_const('IOGate') + const_set('IOGate', new_io_gate) core.config.instance_variable_set(:@test_mode, true) core.config.reset end def test_reset - IOGate.singleton_class.remove_method(:get_screen_size) - IOGate.define_singleton_method(:get_screen_size, @original_get_screen_size) remove_const('IOGate') const_set('IOGate', @original_iogate) - Reline::GeneralIO.reset Reline.instance_variable_set(:@core, nil) end @@ -146,7 +153,7 @@ class Reline::TestCase < Test::Unit::TestCase expected.bytesize, byte_pointer, <<~EOM) <#{expected.inspect} (#{expected.encoding.inspect})> expected but was - <#{chunk.inspect} (#{chunk.encoding.inspect})> in <Terminal #{Reline::GeneralIO.encoding.inspect}> + <#{chunk.inspect} (#{chunk.encoding.inspect})> in <Terminal #{Reline::Dumb.new.encoding.inspect}> EOM end diff --git a/test/reline/test_ansi_with_terminfo.rb b/test/reline/test_ansi_with_terminfo.rb index e1c56b9ee1..3adda10716 100644 --- a/test/reline/test_ansi_with_terminfo.rb +++ b/test/reline/test_ansi_with_terminfo.rb @@ -1,7 +1,7 @@ require_relative 'helper' -require 'reline/ansi' +require 'reline' -class Reline::ANSI::TestWithTerminfo < Reline::TestCase +class Reline::ANSI::WithTerminfoTest < Reline::TestCase def setup Reline.send(:test_mode, ansi: true) @config = Reline::Config.new diff --git a/test/reline/test_ansi_without_terminfo.rb b/test/reline/test_ansi_without_terminfo.rb index 3d153514f3..2db14cf0a0 100644 --- a/test/reline/test_ansi_without_terminfo.rb +++ b/test/reline/test_ansi_without_terminfo.rb @@ -1,7 +1,7 @@ require_relative 'helper' -require 'reline/ansi' +require 'reline' -class Reline::ANSI::TestWithoutTerminfo < Reline::TestCase +class Reline::ANSI::WithoutTerminfoTest < Reline::TestCase def setup Reline.send(:test_mode, ansi: true) @config = Reline::Config.new diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index 03e4178f32..8c4b513600 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -85,15 +85,13 @@ class Reline::Config::Test < Reline::TestCase def test_encoding_is_ascii @config.reset - Reline.core.io_gate.reset(encoding: Encoding::US_ASCII) + Reline.core.io_gate.instance_variable_set(:@encoding, Encoding::US_ASCII) @config = Reline::Config.new assert_equal true, @config.convert_meta end def test_encoding_is_not_ascii - @config.reset - Reline.core.io_gate.reset(encoding: Encoding::UTF_8) @config = Reline::Config.new assert_equal nil, @config.convert_meta diff --git a/test/reline/test_line_editor.rb b/test/reline/test_line_editor.rb index 7a38ecd596..1859da8199 100644 --- a/test/reline/test_line_editor.rb +++ b/test/reline/test_line_editor.rb @@ -4,14 +4,12 @@ require 'stringio' class Reline::LineEditor class RenderLineDifferentialTest < Reline::TestCase - module TestIO - RESET_COLOR = "\e[0m" - - def self.move_cursor_column(col) + class TestIO < Reline::IO + def move_cursor_column(col) @output << "[COL_#{col}]" end - def self.erase_after_cursor + def erase_after_cursor @output << '[ERASE]' end end @@ -24,7 +22,7 @@ class Reline::LineEditor @line_editor.instance_variable_set(:@screen_size, [24, 80]) @line_editor.instance_variable_set(:@output, @output) Reline.send(:remove_const, :IOGate) - Reline.const_set(:IOGate, TestIO) + Reline.const_set(:IOGate, TestIO.new) Reline::IOGate.instance_variable_set(:@output, @output) ensure $VERBOSE = verbose diff --git a/test/reline/test_reline.rb b/test/reline/test_reline.rb index a20a5c9f44..c664ab74b0 100644 --- a/test/reline/test_reline.rb +++ b/test/reline/test_reline.rb @@ -375,7 +375,7 @@ class Reline::Test < Reline::TestCase def test_dumb_terminal lib = File.expand_path("../../lib", __dir__) out = IO.popen([{"TERM"=>"dumb"}, Reline.test_rubybin, "-I#{lib}", "-rreline", "-e", "p Reline.core.io_gate"], &:read) - assert_equal("Reline::GeneralIO", out.chomp) + assert_match(/#<Reline::Dumb/, out.chomp) end def test_require_reline_should_not_trigger_winsize @@ -389,7 +389,7 @@ class Reline::Test < Reline::TestCase require("reline") && p(Reline.core.io_gate) RUBY out = IO.popen([{}, Reline.test_rubybin, "-I#{lib}", "-e", code], &:read) - assert_equal("Reline::ANSI", out.chomp) + assert_include(out.chomp, "Reline::ANSI") end def win? |
