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

Refactor utf-8 strings and invalid strings in test code #800

Merged
merged 5 commits into from
Jan 5, 2025
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
1 change: 0 additions & 1 deletion lib/reline/unicode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Reline::Unicode
0x1F => '^_', # C-_ C-7
0x7F => '^?', # C-? C-8
}
EscapedChars = EscapedPairs.keys.map(&:chr)

NON_PRINTING_START = "\1"
NON_PRINTING_END = "\2"
Expand Down
42 changes: 8 additions & 34 deletions test/reline/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,44 +86,22 @@ def test_rubybin
end

class Reline::TestCase < Test::Unit::TestCase
private def convert_str(input, options = {}, normalized = nil)
return nil if input.nil?
input = input.chars.map { |c|
if Reline::Unicode::EscapedChars.include?(c.ord)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reline::Unicode::EscapedChars is no longer use. Could you remove it?

EscapedChars = EscapedPairs.keys.map(&:chr)

~/ghq/github.com/ruby/reline test_rm_invalid_encoding
❯ git grep EscapedChars
lib/reline/unicode.rb:  EscapedChars = EscapedPairs.keys.map(&:chr)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 👍
I also added two more commits that removes \M-[char] from test_within_pipe.rb and from test_rendering.rb

c
else
c.encode(@line_editor.encoding, Encoding::UTF_8, **options)
end
}.join
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
if unicode?(input.encoding)
tompng marked this conversation as resolved.
Show resolved Hide resolved
input = input.unicode_normalize(:nfc)
if normalized
options[:undef] = :replace
options[:replace] = '?'
end
normalized = true
retry
end
input
private def convert_str(input)
input.encode(@line_editor.encoding, Encoding::UTF_8)
end

def omit_unless_utf8
omit "This test is for UTF-8 but the locale is #{Reline.core.encoding}" if Reline.core.encoding != Encoding::UTF_8
end

def input_key_by_symbol(method_symbol, char: nil, csi: false)
char ||= csi ? "\e[A" : "\C-a"
@line_editor.input_key(Reline::Key.new(char, method_symbol, false))
end

def input_keys(input, convert = true)
# Reline does not support convert-meta, but test data includes \M-char. It should be converted to ESC+char.
# Note that mixing unicode chars and \M-char is not recommended. "\M-C\M-\C-A" is a single unicode character.
input = input.chars.map do |c|
c.valid_encoding? ? c : "\e#{(c.bytes[0] & 0x7f).chr}"
end.join
input_raw_keys(input, convert)
end
def input_keys(input)
input = convert_str(input)

def input_raw_keys(input, convert = true)
input = convert_str(input) if convert
key_stroke = Reline::KeyStroke.new(@config, @encoding)
input_bytes = input.bytes
until input_bytes.empty?
Expand Down Expand Up @@ -177,8 +155,4 @@ 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
Loading
Loading