diff options
Diffstat (limited to 'test/reline/helper.rb')
-rw-r--r-- | test/reline/helper.rb | 125 |
1 files changed, 89 insertions, 36 deletions
diff --git a/test/reline/helper.rb b/test/reline/helper.rb index cd3783ddb8..26fe834482 100644 --- a/test/reline/helper.rb +++ b/test/reline/helper.rb @@ -5,33 +5,77 @@ ENV['TERM'] = 'xterm' # for some CI environments require 'reline' require 'test/unit' +begin + require 'rbconfig' +rescue LoadError +end + +begin + # This should exist and available in load path when this file is mirrored to ruby/ruby and running at there + if File.exist?(File.expand_path('../../tool/lib/envutil.rb', __dir__)) + require 'envutil' + end +rescue LoadError +end + module Reline class <<self - def test_mode - remove_const('IOGate') if const_defined?('IOGate') - const_set('IOGate', Reline::GeneralIO) - if ENV['RELINE_TEST_ENCODING'] - encoding = Encoding.find(ENV['RELINE_TEST_ENCODING']) - else - encoding = Encoding::UTF_8 - end - Reline::GeneralIO.reset(encoding: encoding) - send(:core).config.instance_variable_set(:@test_mode, true) - send(:core).config.reset + 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] + end + Reline::GeneralIO.reset(encoding: encoding) unless ansi + 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 - end -end -def start_pasting - Reline::GeneralIO.start_pasting -end + # Return a executable name to spawn Ruby process. In certain build configuration, + # "ruby" may not be available. + def test_rubybin + # When this test suite is running in ruby/ruby, prefer EnvUtil result over original implementation + if const_defined?(:EnvUtil) + return EnvUtil.rubybin + end -def finish_pasting - Reline::GeneralIO.finish_pasting + # The following is a simplified port of EnvUtil.rubybin in ruby/ruby + if ruby = ENV["RUBY"] + return ruby + end + ruby = "ruby" + exeext = RbConfig::CONFIG["EXEEXT"] + rubyexe = (ruby + exeext if exeext and !exeext.empty?) + if File.exist? ruby and File.executable? ruby and !File.directory? ruby + return File.expand_path(ruby) + end + if rubyexe and File.exist? rubyexe and File.executable? rubyexe + return File.expand_path(rubyexe) + end + if defined?(RbConfig.ruby) + RbConfig.ruby + else + "ruby" + end + end + end end class Reline::TestCase < Test::Unit::TestCase @@ -77,25 +121,33 @@ class Reline::TestCase < Test::Unit::TestCase end end - def assert_line(expected) - expected = convert_str(expected) - assert_equal(expected, @line_editor.line) + def input_raw_keys(input, convert = true) + input = convert_str(input) if convert + input.bytes.each do |b| + @line_editor.input_key(Reline::Key.new(b, b, false)) + end + end + + def assert_line_around_cursor(before, after) + before = convert_str(before) + after = convert_str(after) + line = @line_editor.current_line + byte_pointer = @line_editor.instance_variable_get(:@byte_pointer) + actual_before = line.byteslice(0, byte_pointer) + actual_after = line.byteslice(byte_pointer..) + assert_equal([before, after], [actual_before, actual_after]) end def assert_byte_pointer_size(expected) expected = convert_str(expected) byte_pointer = @line_editor.instance_variable_get(:@byte_pointer) + chunk = @line_editor.line.byteslice(0, byte_pointer) assert_equal( expected.bytesize, byte_pointer, - "<#{expected.inspect}> expected but was\n<#{@line_editor.line.byteslice(0, byte_pointer).inspect}>") - end - - def assert_cursor(expected) - assert_equal(expected, @line_editor.instance_variable_get(:@cursor)) - end - - def assert_cursor_max(expected) - assert_equal(expected, @line_editor.instance_variable_get(:@cursor_max)) + <<~EOM) + <#{expected.inspect} (#{expected.encoding.inspect})> expected but was + <#{chunk.inspect} (#{chunk.encoding.inspect})> in <Terminal #{Reline::GeneralIO.encoding.inspect}> + EOM end def assert_line_index(expected) @@ -103,12 +155,13 @@ class Reline::TestCase < Test::Unit::TestCase end def assert_whole_lines(expected) - previous_line_index = @line_editor.instance_variable_get(:@previous_line_index) - if previous_line_index - lines = @line_editor.whole_lines(index: previous_line_index) - else - lines = @line_editor.whole_lines + assert_equal(expected, @line_editor.whole_lines) + end + + def assert_key_binding(input, method_symbol, editing_modes = [:emacs, :vi_insert, :vi_command]) + editing_modes.each do |editing_mode| + @config.editing_mode = editing_mode + assert_equal(method_symbol, @config.editing_mode.default_key_bindings[input.bytes]) end - assert_equal(expected, lines) end end |