Skip to content

Commit

Permalink
Refactor utf-8 strings and invalid strings in test code (#800)
Browse files Browse the repository at this point in the history
* Remove invalid encoding string "\M-[char]" from test code, remove unused code/arg/options

* Omit unicode unnoralized input test in non-utf8 testcase

* Remove helper method and constant no longer used in testcode

* Change key binding test to use realistic bytes instead of invalid byte sequence

* Remove invalid byte sequence input from rendering test

yamatanooroti handles invalid byte sequence input "\M-[char]" and converts it to "\e[char]"
We don't need to use these invalid byte sequence and rely on the hack implemented in yamatanooroti
  • Loading branch information
tompng authored Jan 5, 2025
1 parent c5d5c44 commit f09e7b1
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 372 deletions.
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)
c
else
c.encode(@line_editor.encoding, Encoding::UTF_8, **options)
end
}.join
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
if unicode?(input.encoding)
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

0 comments on commit f09e7b1

Please sign in to comment.