aboutsummaryrefslogtreecommitdiffstats
path: root/test/reline/helper.rb
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2019-04-27 14:53:09 +0900
committeraycabta <aycabta@gmail.com>2019-04-30 11:44:20 +0900
commit17350c7e5534c8678097d70698fe08614a6c3997 (patch)
tree0f41959093014a97d50aeb06a052f5450b4cb6b1 /test/reline/helper.rb
parenteb45ba61160dbae412407f232fe9b3252eb99362 (diff)
downloadruby-17350c7e5534c8678097d70698fe08614a6c3997.tar.gz
Add Reline as a fallback library for Readline
* lib/reine.rb, lib/reline/*: Reline is a readline stdlib compatible library. * lib/readline.rb: Readline uses a fallback to Reline when ext/readline doesn't exist. * tool/sync_default_gems.rb: add ruby/reline as a default gem. * appveyor.yml: add "set RELINE_TEST_ENCODING=Windows-31J" for test suit of Reline, and add "--exclude readline" to "nmake test-all" on Visual Studio builds because of strange behavior. * spec/ruby/library/readline/spec_helper.rb: skip Reline as with RbReadline.
Diffstat (limited to 'test/reline/helper.rb')
-rw-r--r--test/reline/helper.rb73
1 files changed, 73 insertions, 0 deletions
diff --git a/test/reline/helper.rb b/test/reline/helper.rb
new file mode 100644
index 0000000000..2dced82a12
--- /dev/null
+++ b/test/reline/helper.rb
@@ -0,0 +1,73 @@
+$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
+require 'reline'
+require 'test/unit'
+
+RELINE_TEST_ENCODING ||=
+ if ENV['RELINE_TEST_ENCODING']
+ Encoding.find(ENV['RELINE_TEST_ENCODING'])
+ else
+ Encoding.default_external
+ end
+
+class Reline::TestCase < Test::Unit::TestCase
+ puts "Test encoding is #{RELINE_TEST_ENCODING}"
+
+ private def convert_str(input, options = {}, normalized = nil)
+ return nil if input.nil?
+ input.chars.map { |c|
+ if Reline::Unicode::EscapedChars.include?(c.ord)
+ c
+ else
+ c.encode(@line_editor.instance_variable_get(:@encoding), Encoding::UTF_8, options)
+ end
+ }.join
+ rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
+ input.unicode_normalize!(:nfc)
+ if normalized
+ options[:undef] = :replace
+ options[:replace] = '?'
+ end
+ normalized = true
+ retry
+ end
+
+ def input_keys(input, convert = true)
+ input = convert_str(input) if convert
+ input.chars.each do |c|
+ if c.bytesize == 1
+ eighth_bit = 0b10000000
+ byte = c.bytes.first
+ if byte.allbits?(eighth_bit)
+ @line_editor.input_key("\e".ord)
+ byte ^= eighth_bit
+ end
+ @line_editor.input_key(byte)
+ else
+ c.bytes.each do |b|
+ @line_editor.input_key(b)
+ end
+ end
+ end
+ end
+
+ def assert_line(expected)
+ expected = convert_str(expected)
+ assert_equal(expected, @line_editor.line)
+ end
+
+ def assert_byte_pointer_size(expected)
+ expected = convert_str(expected)
+ byte_pointer = @line_editor.instance_variable_get(:@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))
+ end
+end