Skip to content

Commit

Permalink
Test ext/readline and lib/reline by test/readline
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed May 14, 2019
1 parent 07e7ae9 commit c754e97
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 198 deletions.
16 changes: 16 additions & 0 deletions test/readline/helper.rb
@@ -0,0 +1,16 @@
begin
require "readline.so"
ReadlineSo = Readline
rescue LoadError
end
require "reline"

def use_ext_readline # Use ext/readline as Readline
Object.send(:remove_const, :Readline) if Object.const_defined?(:Readline)
Object.const_set(:Readline, ReadlineSo)
end

def use_lib_reline # Use lib/reline as Readline
Object.send(:remove_const, :Readline) if Object.const_defined?(:Readline)
Object.const_set(:Readline, Reline)
end
224 changes: 125 additions & 99 deletions test/readline/test_readline.rb
@@ -1,14 +1,10 @@
# frozen_string_literal: false
begin
require "readline"
rescue LoadError
else
require "test/unit"
require "tempfile"
require "timeout"
end
require_relative "helper"
require "test/unit"
require "tempfile"
require "timeout"

class TestReadline < Test::Unit::TestCase
module BasetestReadline
INPUTRC = "INPUTRC"
SAVED_ENV = %w[COLUMNS LINES]

Expand All @@ -30,90 +26,91 @@ def teardown
SAVED_ENV.each_with_index {|k, i| ENV[k] = @saved_env[i] }
end

if !/EditLine/n.match(Readline::VERSION)
def test_readline
with_temp_stdio do |stdin, stdout|
stdin.write("hello\n")
stdin.close
stdout.flush
line = replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", true)
}
assert_equal("hello", line)
assert_equal(true, line.tainted?)
stdout.rewind
assert_equal("> ", stdout.read(2))
assert_equal(1, Readline::HISTORY.length)
assert_equal("hello", Readline::HISTORY[0])
Thread.start {
$SAFE = 1
assert_raise(SecurityError) do
replace_stdio(stdin.path, stdout.path) do
Readline.readline("> ".taint)
end
def test_readline
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
with_temp_stdio do |stdin, stdout|
stdin.write("hello\n")
stdin.close
stdout.flush
line = replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", true)
}
assert_equal("hello", line)
assert_equal(true, line.tainted?)
stdout.rewind
assert_equal("> ", stdout.read(2))
assert_equal(1, Readline::HISTORY.length)
assert_equal("hello", Readline::HISTORY[0])
Thread.start {
$SAFE = 1
assert_raise(SecurityError) do
replace_stdio(stdin.path, stdout.path) do
Readline.readline("> ".taint)
end
}.join
ensure
$SAFE = 0
end
end
}.join
ensure
$SAFE = 0
end
end

# line_buffer
# point
def test_line_buffer__point
begin
Readline.line_buffer
Readline.point
rescue NotImplementedError
return
end
# line_buffer
# point
def test_line_buffer__point
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
skip "GNU Readline has special behaviors" if defined?(Reline) and Readline == Reline
begin
Readline.line_buffer
Readline.point
rescue NotImplementedError
return
end

with_temp_stdio do |stdin, stdout|
actual_text = nil
actual_line_buffer = nil
actual_point = nil
Readline.completion_proc = ->(text) {
actual_text = text
actual_point = Readline.point
actual_line_buffer = Readline.line_buffer
stdin.write(" finish\n")
stdin.flush
stdout.flush
return ["complete"]
}

stdin.write("first second\t")
stdin.flush
Readline.completion_append_character = " "
replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", false)
}
assert_equal("second", actual_text)
assert_equal("first second", actual_line_buffer)
assert_equal(12, actual_point)
assert_equal("first complete finish", Readline.line_buffer)
assert_equal(Encoding.find("locale"), Readline.line_buffer.encoding)
assert_equal(true, Readline.line_buffer.tainted?)
assert_equal(22, Readline.point)

stdin.rewind
stdout.rewind

stdin.write("first second\t")
with_temp_stdio do |stdin, stdout|
actual_text = nil
actual_line_buffer = nil
actual_point = nil
Readline.completion_proc = ->(text) {
actual_text = text
actual_point = Readline.point
actual_line_buffer = Readline.line_buffer
stdin.write(" finish\n")
stdin.flush
Readline.completion_append_character = nil
replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", false)
}
assert_equal("second", actual_text)
assert_equal("first second", actual_line_buffer)
assert_equal(12, actual_point)
assert_equal("first complete finish", Readline.line_buffer)
assert_equal(Encoding.find("locale"), Readline.line_buffer.encoding)
assert_equal(true, Readline.line_buffer.tainted?)
assert_equal(21, Readline.point)
end
end if !defined?(Reline) or Readline != Reline
stdout.flush
return ["complete"]
}

stdin.write("first second\t")
stdin.flush
Readline.completion_append_character = " "
replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", false)
}
assert_equal("second", actual_text)
assert_equal("first second", actual_line_buffer)
assert_equal(12, actual_point)
assert_equal("first complete finish", Readline.line_buffer)
assert_equal(Encoding.find("locale"), Readline.line_buffer.encoding)
assert_equal(true, Readline.line_buffer.tainted?)
assert_equal(22, Readline.point)

stdin.rewind
stdout.rewind

stdin.write("first second\t")
stdin.flush
Readline.completion_append_character = nil
replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", false)
}
assert_equal("second", actual_text)
assert_equal("first second", actual_line_buffer)
assert_equal(12, actual_point)
assert_equal("first complete finish", Readline.line_buffer)
assert_equal(Encoding.find("locale"), Readline.line_buffer.encoding)
assert_equal(true, Readline.line_buffer.tainted?)
assert_equal(21, Readline.point)
end
end

def test_input=
Expand Down Expand Up @@ -147,6 +144,7 @@ def test_completion_case_fold
end

def test_completion_proc_empty_result
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
with_temp_stdio do |stdin, stdout|
stdin.write("first\t")
stdin.flush
Expand All @@ -165,7 +163,7 @@ def test_completion_proc_empty_result
rescue NotimplementedError
end
end
end if !/EditLine/n.match(Readline::VERSION)
end

def test_get_screen_size
begin
Expand Down Expand Up @@ -225,6 +223,7 @@ def test_completion_append_character
end

def test_completion_encoding
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
bug5941 = '[Bug #5941]'
append_character = Readline.completion_append_character
Readline.completion_append_character = ""
Expand Down Expand Up @@ -264,9 +263,10 @@ def test_completion_encoding
with_pipe {|r, w| w << "\t"}
end
ensure
return if /EditLine/n.match(Readline::VERSION)
Readline.completion_case_fold = completion_case_fold
Readline.completion_append_character = append_character
end if !/EditLine/n.match(Readline::VERSION)
end

# basic_word_break_characters
# completer_word_break_characters
Expand Down Expand Up @@ -325,6 +325,7 @@ def test_pre_input_hook
end

def test_point
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
assert_equal(0, Readline.point)
Readline.insert_text('12345')
assert_equal(5, Readline.point)
Expand All @@ -336,9 +337,10 @@ def test_point

assert_equal('1234abc5', Readline.line_buffer)
rescue NotImplementedError
end if !/EditLine/n.match(Readline::VERSION)
end

def test_insert_text
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
str = "test_insert_text"
assert_equal(0, Readline.point)
assert_equal(Readline, Readline.insert_text(str))
Expand Down Expand Up @@ -366,9 +368,10 @@ def test_insert_text
Readline.delete_text
assert_equal("", Readline.line_buffer)
rescue NotImplementedError
end if !/EditLine/n.match(Readline::VERSION)
end

def test_delete_text
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
str = "test_insert_text"
assert_equal(0, Readline.point)
assert_equal(Readline, Readline.insert_text(str))
Expand All @@ -385,9 +388,10 @@ def test_delete_text
assert_equal("", Readline.line_buffer)
end
rescue NotImplementedError
end if !/EditLine/n.match(Readline::VERSION)
end

def test_modify_text_in_pre_input_hook
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
with_temp_stdio {|stdin, stdout|
begin
stdin.write("world\n")
Expand All @@ -413,9 +417,10 @@ def test_modify_text_in_pre_input_hook
end
end
}
end if !/EditLine|\A4\.3\z/n.match(Readline::VERSION)
end

def test_input_metachar
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
skip("Won't pass on mingw w/readline 7.0.005 [ruby-core:45682]") if mingw?
bug6601 = '[ruby-core:45682]'
Readline::HISTORY << "hello"
Expand All @@ -427,11 +432,13 @@ def test_input_metachar
assert_equal("hello", line, bug6601)
ensure
wo&.close
return if /EditLine/n.match(Readline::VERSION)
Readline.delete_text
Readline::HISTORY.clear
end if !/EditLine/n.match(Readline::VERSION)
end

def test_input_metachar_multibyte
skip "Skip Editline" if /EditLine/n.match(Readline::VERSION)
unless Encoding.find("locale") == Encoding::UTF_8
return if assert_under_utf8
skip 'this test needs UTF-8 locale'
Expand All @@ -455,11 +462,13 @@ def test_input_metachar_multibyte
end
end
ensure
return if /EditLine/n.match(Readline::VERSION)
Readline.delete_text
Readline::HISTORY.clear
end if !/EditLine/n.match(Readline::VERSION)
end

def test_refresh_line
skip "Only when refresh_line exists" unless Readline.respond_to?(:refresh_line)
bug6232 = '[ruby-core:43957] [Bug #6232] refresh_line after set_screen_size'
with_temp_stdio do |stdin, stdout|
replace_stdio(stdin.path, stdout.path) do
Expand All @@ -469,7 +478,7 @@ def test_refresh_line
end;
end
end
end if Readline.respond_to?(:refresh_line)
end

def test_setting_quoting_detection_proc
return unless Readline.respond_to?(:quoting_detection_proc=)
Expand Down Expand Up @@ -692,5 +701,22 @@ def assert_under_utf8
SRC
return true
end
end if defined?(::Readline) && !(/mswin|mingw/ =~ RUBY_PLATFORM && defined?(Reline) && Readline == Reline)
# skip on Windows now when using reline because it causes hang of whole tests
end

class TestReadline < Test::Unit::TestCase
include BasetestReadline

def setup
use_ext_readline
super
end
end if defined?(ReadlineSo)

class TestRelineAsReadline < Test::Unit::TestCase
include BasetestReadline

def setup
use_lib_reline
super
end
end

0 comments on commit c754e97

Please sign in to comment.