2004-11-18 09:12:12 +03:00
|
|
|
begin
|
|
|
|
require "readline"
|
2008-08-11 11:58:02 +04:00
|
|
|
=begin
|
|
|
|
class << Readline
|
|
|
|
[
|
|
|
|
"vi_editing_mode",
|
|
|
|
"emacs_editing_mode",
|
|
|
|
"completion_append_character=",
|
|
|
|
"completion_append_character",
|
|
|
|
"basic_word_break_characters=",
|
|
|
|
"basic_word_break_characters",
|
|
|
|
"completer_word_break_characters=",
|
|
|
|
"completer_word_break_characters",
|
|
|
|
"basic_quote_characters=",
|
|
|
|
"basic_quote_characters",
|
|
|
|
"completer_quote_characters=",
|
|
|
|
"completer_quote_characters",
|
|
|
|
"filename_quote_characters=",
|
|
|
|
"filename_quote_characters",
|
|
|
|
].each do |method_name|
|
|
|
|
define_method(method_name.to_sym) do |*args|
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
=end
|
2004-11-18 09:12:12 +03:00
|
|
|
rescue LoadError
|
2008-08-11 11:58:02 +04:00
|
|
|
else
|
|
|
|
require "test/unit"
|
|
|
|
require "tempfile"
|
2004-11-18 09:12:12 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestReadline < Test::Unit::TestCase
|
2008-08-11 11:58:02 +04:00
|
|
|
def teardown
|
|
|
|
Readline.instance_variable_set("@completion_proc", nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_level_4
|
|
|
|
method_args =
|
|
|
|
[
|
|
|
|
["readline"],
|
|
|
|
["input=", $stdin],
|
|
|
|
["output=", $stdout],
|
|
|
|
["completion_proc=", proc {}],
|
|
|
|
["completion_proc"],
|
|
|
|
["completion_case_fold=", true],
|
|
|
|
["completion_case_fold"],
|
|
|
|
["vi_editing_mode"],
|
2008-08-12 10:17:48 +04:00
|
|
|
["vi_editing_mode?"],
|
2008-08-11 11:58:02 +04:00
|
|
|
["emacs_editing_mode"],
|
2008-08-12 10:17:48 +04:00
|
|
|
["emacs_editing_mode?"],
|
2008-08-11 11:58:02 +04:00
|
|
|
["completion_append_character=", "s"],
|
|
|
|
["completion_append_character"],
|
|
|
|
["basic_word_break_characters=", "s"],
|
|
|
|
["basic_word_break_characters"],
|
|
|
|
["completer_word_break_characters=", "s"],
|
|
|
|
["completer_word_break_characters"],
|
|
|
|
["basic_quote_characters=", "\\"],
|
|
|
|
["basic_quote_characters"],
|
|
|
|
["completer_quote_characters=", "\\"],
|
|
|
|
["completer_quote_characters"],
|
|
|
|
["filename_quote_characters=", "\\"],
|
|
|
|
["filename_quote_characters"],
|
|
|
|
]
|
|
|
|
method_args.each do |method_name, *args|
|
2008-09-24 21:44:39 +04:00
|
|
|
assert_raise(SecurityError, NotImplementedError,
|
2008-08-11 11:58:02 +04:00
|
|
|
"method=<#{method_name}>") do
|
|
|
|
Thread.start {
|
|
|
|
$SAFE = 4
|
|
|
|
Readline.send(method_name.to_sym, *args)
|
|
|
|
assert(true)
|
|
|
|
}.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-18 09:12:12 +03:00
|
|
|
def test_readline
|
|
|
|
stdin = Tempfile.new("test_readline_stdin")
|
|
|
|
stdout = Tempfile.new("test_readline_stdout")
|
|
|
|
begin
|
|
|
|
stdin.write("hello\n")
|
2004-12-02 04:54:13 +03:00
|
|
|
stdin.close
|
|
|
|
stdout.close
|
2005-05-19 18:36:20 +04:00
|
|
|
line = replace_stdio(stdin.path, stdout.path) {
|
|
|
|
Readline.readline("> ", true)
|
|
|
|
}
|
2004-11-18 09:12:12 +03:00
|
|
|
assert_equal("hello", line)
|
|
|
|
assert_equal(true, line.tainted?)
|
2004-12-02 04:54:13 +03:00
|
|
|
stdout.open
|
|
|
|
assert_equal("> ", stdout.read(2))
|
2005-05-19 18:36:20 +04:00
|
|
|
assert_equal(1, Readline::HISTORY.length)
|
|
|
|
assert_equal("hello", Readline::HISTORY[0])
|
2008-09-24 21:44:39 +04:00
|
|
|
assert_raise(SecurityError) do
|
2004-11-18 09:12:12 +03:00
|
|
|
Thread.start {
|
|
|
|
$SAFE = 1
|
2004-12-02 04:54:13 +03:00
|
|
|
replace_stdio(stdin.path, stdout.path) do
|
|
|
|
Readline.readline("> ".taint)
|
|
|
|
end
|
2004-11-18 09:12:12 +03:00
|
|
|
}.join
|
|
|
|
end
|
2008-09-24 21:44:39 +04:00
|
|
|
assert_raise(SecurityError) do
|
2004-11-18 09:12:12 +03:00
|
|
|
Thread.start {
|
|
|
|
$SAFE = 4
|
2004-12-02 04:54:13 +03:00
|
|
|
replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") }
|
2004-11-18 09:12:12 +03:00
|
|
|
}.join
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
stdin.close(true)
|
|
|
|
stdout.close(true)
|
|
|
|
end
|
2008-08-11 11:58:02 +04:00
|
|
|
end if !/EditLine/n.match(Readline::VERSION)
|
|
|
|
|
|
|
|
def test_input=
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Readline.input = "This is not a file."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_output=
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Readline.output = "This is not a file."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_completion_proc
|
|
|
|
expected = proc { |input| input }
|
|
|
|
Readline.completion_proc = expected
|
|
|
|
assert_equal(expected, Readline.completion_proc)
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
Readline.completion_proc = "This does not have call method."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_completion_case_fold
|
|
|
|
expected = [true, false, "string", {"a" => "b"}]
|
|
|
|
expected.each do |e|
|
|
|
|
Readline.completion_case_fold = e
|
|
|
|
assert_equal(e, Readline.completion_case_fold)
|
|
|
|
end
|
2004-11-18 09:12:12 +03:00
|
|
|
end
|
|
|
|
|
2008-08-11 12:31:45 +04:00
|
|
|
# vi_editing_mode
|
|
|
|
# emacs_editing_mode
|
|
|
|
def test_editing_mode
|
|
|
|
begin
|
|
|
|
assert_equal(false, Readline.vi_editing_mode?)
|
|
|
|
assert_equal(true, Readline.emacs_editing_mode?)
|
|
|
|
|
|
|
|
assert_equal(nil, Readline.vi_editing_mode)
|
|
|
|
assert_equal(true, Readline.vi_editing_mode?)
|
|
|
|
assert_equal(false, Readline.emacs_editing_mode?)
|
|
|
|
assert_equal(nil, Readline.vi_editing_mode)
|
|
|
|
assert_equal(true, Readline.vi_editing_mode?)
|
|
|
|
assert_equal(false, Readline.emacs_editing_mode?)
|
|
|
|
|
|
|
|
assert_equal(nil, Readline.emacs_editing_mode)
|
|
|
|
assert_equal(false, Readline.vi_editing_mode?)
|
|
|
|
assert_equal(true, Readline.emacs_editing_mode?)
|
|
|
|
assert_equal(nil, Readline.emacs_editing_mode)
|
|
|
|
assert_equal(false, Readline.vi_editing_mode?)
|
|
|
|
assert_equal(true, Readline.emacs_editing_mode?)
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-19 04:38:38 +03:00
|
|
|
def test_completion_append_character
|
2005-05-19 18:36:20 +04:00
|
|
|
begin
|
2008-12-21 07:51:43 +03:00
|
|
|
enc = get_default_internal_encoding
|
2008-12-12 19:13:11 +03:00
|
|
|
data_expected = [
|
|
|
|
["x", "x"],
|
|
|
|
["xyx", "x"],
|
|
|
|
[" ", " "],
|
|
|
|
["\t", "\t"],
|
|
|
|
]
|
|
|
|
data_expected.each do |(data, expected)|
|
|
|
|
Readline.completion_append_character = data
|
|
|
|
assert_equal(expected, Readline.completion_append_character)
|
|
|
|
assert_equal(enc, Readline.completion_append_character.encoding)
|
|
|
|
end
|
2005-05-19 18:36:20 +04:00
|
|
|
Readline.completion_append_character = ""
|
|
|
|
assert_equal(nil, Readline.completion_append_character)
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
2004-11-19 04:38:38 +03:00
|
|
|
end
|
|
|
|
|
2008-08-11 11:58:02 +04:00
|
|
|
# basic_word_break_characters
|
|
|
|
# completer_word_break_characters
|
|
|
|
# basic_quote_characters
|
|
|
|
# completer_quote_characters
|
|
|
|
# filename_quote_characters
|
|
|
|
def test_some_characters_methods
|
|
|
|
method_names = [
|
|
|
|
"basic_word_break_characters",
|
|
|
|
"completer_word_break_characters",
|
|
|
|
"basic_quote_characters",
|
|
|
|
"completer_quote_characters",
|
|
|
|
"filename_quote_characters",
|
|
|
|
]
|
|
|
|
method_names.each do |method_name|
|
|
|
|
begin
|
|
|
|
begin
|
2008-12-21 07:51:43 +03:00
|
|
|
enc = get_default_internal_encoding
|
2008-08-11 11:58:02 +04:00
|
|
|
saved = Readline.send(method_name.to_sym)
|
|
|
|
expecteds = [" ", " .,|\t", ""]
|
|
|
|
expecteds.each do |e|
|
|
|
|
Readline.send((method_name + "=").to_sym, e)
|
2008-12-21 07:51:43 +03:00
|
|
|
res = Readline.send(method_name.to_sym)
|
|
|
|
assert_equal(e, res)
|
|
|
|
assert_equal(enc, res.encoding)
|
2008-08-11 11:58:02 +04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Readline.send((method_name + "=").to_sym, saved) if saved
|
|
|
|
end
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-18 09:12:12 +03:00
|
|
|
private
|
|
|
|
|
2004-12-02 04:54:13 +03:00
|
|
|
def replace_stdio(stdin_path, stdout_path)
|
2005-09-22 12:33:05 +04:00
|
|
|
open(stdin_path, "r"){|stdin|
|
|
|
|
open(stdout_path, "w"){|stdout|
|
|
|
|
orig_stdin = STDIN.dup
|
|
|
|
orig_stdout = STDOUT.dup
|
|
|
|
STDIN.reopen(stdin)
|
|
|
|
STDOUT.reopen(stdout)
|
|
|
|
begin
|
|
|
|
Readline.input = STDIN
|
|
|
|
Readline.output = STDOUT
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
STDIN.reopen(orig_stdin)
|
|
|
|
STDOUT.reopen(orig_stdout)
|
|
|
|
orig_stdin.close
|
|
|
|
orig_stdout.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2004-11-18 09:12:12 +03:00
|
|
|
end
|
2008-12-21 07:51:43 +03:00
|
|
|
|
|
|
|
def get_default_internal_encoding
|
|
|
|
return Encoding.default_internal || Encoding.find("locale")
|
|
|
|
end
|
2008-08-11 11:58:02 +04:00
|
|
|
end if defined?(::Readline)
|