2004-11-18 01:12:12 -05:00
|
|
|
begin
|
|
|
|
require "readline"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2005-05-24 04:48:41 -04:00
|
|
|
if defined?(Readline) && !/EditLine/n.match(Readline::VERSION)
|
2004-11-18 01:12:12 -05:00
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
require "tempfile"
|
|
|
|
|
|
|
|
class TestReadline < Test::Unit::TestCase
|
|
|
|
def test_readline
|
|
|
|
stdin = Tempfile.new("test_readline_stdin")
|
|
|
|
stdout = Tempfile.new("test_readline_stdout")
|
|
|
|
begin
|
|
|
|
stdin.write("hello\n")
|
2004-12-01 20:54:13 -05:00
|
|
|
stdin.close
|
|
|
|
stdout.close
|
2005-05-19 10:36:20 -04:00
|
|
|
line = replace_stdio(stdin.path, stdout.path) {
|
|
|
|
Readline.readline("> ", true)
|
|
|
|
}
|
2004-11-18 01:12:12 -05:00
|
|
|
assert_equal("hello", line)
|
|
|
|
assert_equal(true, line.tainted?)
|
2004-12-01 20:54:13 -05:00
|
|
|
stdout.open
|
|
|
|
assert_equal("> ", stdout.read(2))
|
2005-05-19 10:36:20 -04:00
|
|
|
assert_equal(1, Readline::HISTORY.length)
|
|
|
|
assert_equal("hello", Readline::HISTORY[0])
|
2004-11-18 01:12:12 -05:00
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.start {
|
|
|
|
$SAFE = 1
|
2004-12-01 20:54:13 -05:00
|
|
|
replace_stdio(stdin.path, stdout.path) do
|
|
|
|
Readline.readline("> ".taint)
|
|
|
|
end
|
2004-11-18 01:12:12 -05:00
|
|
|
}.join
|
|
|
|
end
|
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.start {
|
|
|
|
$SAFE = 4
|
2004-12-01 20:54:13 -05:00
|
|
|
replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") }
|
2004-11-18 01:12:12 -05:00
|
|
|
}.join
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
stdin.close(true)
|
|
|
|
stdout.close(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-18 20:38:38 -05:00
|
|
|
def test_completion_append_character
|
2005-05-19 10:36:20 -04:00
|
|
|
begin
|
|
|
|
Readline.completion_append_character = "x"
|
|
|
|
assert_equal("x", Readline.completion_append_character)
|
|
|
|
Readline.completion_append_character = "xyz"
|
|
|
|
assert_equal("x", Readline.completion_append_character)
|
|
|
|
Readline.completion_append_character = nil
|
|
|
|
assert_equal(nil, Readline.completion_append_character)
|
|
|
|
Readline.completion_append_character = ""
|
|
|
|
assert_equal(nil, Readline.completion_append_character)
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
2004-11-18 20:38:38 -05:00
|
|
|
end
|
|
|
|
|
2004-11-18 01:12:12 -05:00
|
|
|
private
|
|
|
|
|
2004-12-01 20:54:13 -05:00
|
|
|
def replace_stdio(stdin_path, stdout_path)
|
2004-11-18 01:12:12 -05:00
|
|
|
orig_stdin = STDIN.dup
|
|
|
|
orig_stdout = STDOUT.dup
|
2004-12-01 20:54:13 -05:00
|
|
|
STDIN.reopen(stdin_path, "r")
|
|
|
|
STDOUT.reopen(stdout_path, "w")
|
2004-11-18 01:12:12 -05:00
|
|
|
begin
|
2005-06-17 12:18:56 -04:00
|
|
|
Readline.input = STDIN
|
|
|
|
Readline.output = STDOUT
|
2004-11-18 01:12:12 -05:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
STDIN.reopen(orig_stdin)
|
|
|
|
STDOUT.reopen(orig_stdout)
|
2004-12-01 20:54:13 -05:00
|
|
|
orig_stdin.close
|
|
|
|
orig_stdout.close
|
2004-11-18 01:12:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|