Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RELINE_TEST_ENCODING #743

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,13 @@ def editing_mode
target = target.downcase if @config.completion_ignore_case
list.select do |item|
next unless item

unless Encoding.compatible?(target.encoding, item.encoding)
# Crash with Encoding::CompatibilityError is required by readline-ext/test/readline/test_readline.rb
# TODO: fix the test
raise Encoding::CompatibilityError, "#{target.encoding.name} is not compatible with #{item.encoding.name}"
# Workaround for Readline test
if defined?(::Readline) && ::Readline == ::Reline
raise Encoding::CompatibilityError, "incompatible character encodings: #{target.encoding} and #{item.encoding}"
end

next true
end

if @config.completion_ignore_case
Expand Down
27 changes: 17 additions & 10 deletions test/reline/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
end

module Reline
class <<self
class << self
def test_mode(ansi: false)
@original_iogate = IOGate

if ENV['RELINE_TEST_ENCODING']
encoding = Encoding.find(ENV['RELINE_TEST_ENCODING'])
if defined?(RELINE_TEST_ENCODING)
encoding = RELINE_TEST_ENCODING
else
encoding = Encoding::UTF_8
end
Expand Down Expand Up @@ -88,21 +88,24 @@ def test_rubybin
class Reline::TestCase < Test::Unit::TestCase
private def convert_str(input, options = {}, normalized = nil)
return nil if input.nil?
input.chars.map { |c|
input = input.chars.map { |c|
if Reline::Unicode::EscapedChars.include?(c.ord)
c
else
c.encode(@line_editor.encoding, Encoding::UTF_8, **options)
end
}.join
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
input = input.unicode_normalize(:nfc)
if normalized
options[:undef] = :replace
options[:replace] = '?'
if unicode?(input.encoding)
input = input.unicode_normalize(:nfc)
if normalized
options[:undef] = :replace
options[:replace] = '?'
end
normalized = true
retry
end
normalized = true
retry
input
end

def input_key_by_symbol(input)
Expand Down Expand Up @@ -176,4 +179,8 @@ def assert_key_binding(input, method_symbol, editing_modes = [:emacs, :vi_insert
assert_equal(method_symbol, @config.editing_mode.get(input.bytes))
end
end

private def unicode?(encoding)
[Encoding::UTF_8, Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(encoding)
end
end
7 changes: 4 additions & 3 deletions test/reline/test_key_actor_emacs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ def test_ed_argument_digit_by_meta_num
end

def test_halfwidth_kana_width_dakuten
omit "This test is for UTF-8 but the locale is #{Reline.core.encoding}" if Reline.core.encoding != Encoding::UTF_8
input_raw_keys('ガギゲゴ')
assert_line_around_cursor('ガギゲゴ', '')
input_keys("\C-b\C-b", false)
Expand Down Expand Up @@ -1519,7 +1520,7 @@ def test_vi_editing_mode
def test_undo
input_keys("\C-_", false)
assert_line_around_cursor('', '')
input_keys("aあb\C-h\C-h\C-h", false)
input_keys("aあb\C-h\C-h\C-h".encode(@encoding), false)
assert_line_around_cursor('', '')
input_keys("\C-_", false)
assert_line_around_cursor('a', '')
Expand All @@ -1540,7 +1541,7 @@ def test_undo_with_cursor_position
assert_line_around_cursor('a', 'c')
input_keys("\C-_", false)
assert_line_around_cursor('ab', 'c')
input_keys("あいう\C-b\C-h", false)
input_keys("あいう\C-b\C-h".encode(@encoding), false)
assert_line_around_cursor('abあ', 'うc')
input_keys("\C-_", false)
assert_line_around_cursor('abあい', 'うc')
Expand Down Expand Up @@ -1585,7 +1586,7 @@ def test_undo_with_many_times
end

def test_redo
input_keys("aあb", false)
input_keys("aあb".encode(@encoding), false)
assert_line_around_cursor('aあb', '')
input_keys("\M-\C-_", false)
assert_line_around_cursor('aあb', '')
Expand Down