2008-08-01 11:04:37 -04:00
|
|
|
begin
|
|
|
|
require "readline"
|
|
|
|
=begin
|
|
|
|
class << Readline::HISTORY
|
|
|
|
def []=(index, str)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def pop
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def shift
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_at(index)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
=end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
class << Readline::HISTORY
|
|
|
|
def clear
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
=end
|
|
|
|
rescue LoadError
|
|
|
|
else
|
|
|
|
require "test/unit"
|
|
|
|
end
|
|
|
|
|
|
|
|
class Readline::TestHistory < Test::Unit::TestCase
|
|
|
|
include Readline
|
|
|
|
|
|
|
|
def setup
|
|
|
|
HISTORY.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_level_4
|
|
|
|
method_args =
|
|
|
|
[
|
|
|
|
["[]", [0]],
|
|
|
|
["[]=", [0, "s"]],
|
|
|
|
["\<\<", ["s"]],
|
|
|
|
["push", ["s"]],
|
|
|
|
["pop", []],
|
|
|
|
["shift", []],
|
|
|
|
["length", []],
|
|
|
|
["delete_at", [0]],
|
|
|
|
["clear", []],
|
|
|
|
]
|
|
|
|
method_args.each do |method_name, args|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(SecurityError, NotImplementedError,
|
2008-08-01 11:04:37 -04:00
|
|
|
"method=<#{method_name}>") do
|
|
|
|
Thread.start {
|
|
|
|
$SAFE = 4
|
|
|
|
HISTORY.send(method_name.to_sym, *args)
|
|
|
|
assert(true)
|
|
|
|
}.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(SecurityError, NotImplementedError,
|
2008-08-01 11:04:37 -04:00
|
|
|
"method=<each>") do
|
|
|
|
Thread.start {
|
|
|
|
$SAFE = 4
|
|
|
|
HISTORY.each { |s|
|
|
|
|
assert(true)
|
|
|
|
}
|
|
|
|
}.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
2008-12-21 00:49:18 -05:00
|
|
|
expected = "HISTORY"
|
|
|
|
assert_equal(expected, HISTORY.to_s)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_get
|
|
|
|
lines = push_history(5)
|
|
|
|
lines.each_with_index do |s, i|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(s, HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get__negative
|
|
|
|
lines = push_history(5)
|
|
|
|
(1..5).each do |i|
|
|
|
|
assert_equal(lines[-i], HISTORY[-i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get__out_of_range
|
|
|
|
lines = push_history(5)
|
|
|
|
invalid_indexes = [5, 6, 100, -6, -7, -100]
|
|
|
|
invalid_indexes.each do |i|
|
|
|
|
assert_raise(IndexError, "i=<#{i}>") do
|
|
|
|
HISTORY[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
invalid_indexes = [100_000_000_000_000_000_000,
|
|
|
|
-100_000_000_000_000_000_000]
|
|
|
|
invalid_indexes.each do |i|
|
|
|
|
assert_raise(RangeError, "i=<#{i}>") do
|
|
|
|
HISTORY[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set
|
|
|
|
begin
|
|
|
|
lines = push_history(5)
|
|
|
|
5.times do |i|
|
|
|
|
expected = "set: #{i}"
|
2008-12-24 11:11:01 -05:00
|
|
|
HISTORY[i] = expected
|
|
|
|
assert_external_string_equal(expected, HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set__out_of_range
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(IndexError, NotImplementedError, "index=<0>") do
|
2008-08-01 11:04:37 -04:00
|
|
|
HISTORY[0] = "set: 0"
|
|
|
|
end
|
|
|
|
|
|
|
|
lines = push_history(5)
|
|
|
|
invalid_indexes = [5, 6, 100, -6, -7, -100]
|
|
|
|
invalid_indexes.each do |i|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(IndexError, NotImplementedError, "index=<#{i}>") do
|
2008-08-01 11:04:37 -04:00
|
|
|
HISTORY[i] = "set: #{i}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
invalid_indexes = [100_000_000_000_000_000_000,
|
|
|
|
-100_000_000_000_000_000_000]
|
|
|
|
invalid_indexes.each do |i|
|
|
|
|
assert_raise(RangeError, NotImplementedError, "index=<#{i}>") do
|
|
|
|
HISTORY[i] = "set: #{i}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_push
|
|
|
|
5.times do |i|
|
2008-12-21 00:49:18 -05:00
|
|
|
s = i.to_s
|
|
|
|
assert_equal(HISTORY, HISTORY.push(s))
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(s, HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
assert_equal(5, HISTORY.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_push__operator
|
|
|
|
5.times do |i|
|
2008-12-21 00:49:18 -05:00
|
|
|
s = i.to_s
|
|
|
|
assert_equal(HISTORY, HISTORY << s)
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(s, HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
assert_equal(5, HISTORY.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_push__plural
|
|
|
|
assert_equal(HISTORY, HISTORY.push("0", "1", "2", "3", "4"))
|
|
|
|
(0..4).each do |i|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(i.to_s, HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
assert_equal(5, HISTORY.length)
|
|
|
|
|
|
|
|
assert_equal(HISTORY, HISTORY.push("5", "6", "7", "8", "9"))
|
|
|
|
(5..9).each do |i|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(i.to_s, HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
assert_equal(10, HISTORY.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pop
|
|
|
|
begin
|
|
|
|
assert_equal(nil, HISTORY.pop)
|
|
|
|
|
|
|
|
lines = push_history(5)
|
|
|
|
(1..5).each do |i|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(lines[-i], HISTORY.pop)
|
2008-08-01 11:04:37 -04:00
|
|
|
assert_equal(lines.length - i, HISTORY.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(nil, HISTORY.pop)
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift
|
|
|
|
begin
|
|
|
|
assert_equal(nil, HISTORY.shift)
|
|
|
|
|
|
|
|
lines = push_history(5)
|
|
|
|
(0..4).each do |i|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(lines[i], HISTORY.shift)
|
2008-08-01 11:04:37 -04:00
|
|
|
assert_equal(lines.length - (i + 1), HISTORY.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(nil, HISTORY.shift)
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
2008-09-22 13:13:25 -04:00
|
|
|
e = HISTORY.each do |s|
|
2008-08-01 11:04:37 -04:00
|
|
|
assert(false) # not reachable
|
|
|
|
end
|
2008-09-22 13:13:25 -04:00
|
|
|
assert_equal(HISTORY, e)
|
2008-08-01 11:04:37 -04:00
|
|
|
lines = push_history(5)
|
|
|
|
i = 0
|
2008-09-22 13:13:25 -04:00
|
|
|
e = HISTORY.each do |s|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(HISTORY[i], s)
|
|
|
|
assert_external_string_equal(lines[i], s)
|
2008-08-01 11:04:37 -04:00
|
|
|
i += 1
|
|
|
|
end
|
2008-09-22 13:13:25 -04:00
|
|
|
assert_equal(HISTORY, e)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each__enumerator
|
|
|
|
e = HISTORY.each
|
2008-08-13 04:03:30 -04:00
|
|
|
assert_instance_of(Enumerator, e)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_length
|
|
|
|
assert_equal(0, HISTORY.length)
|
|
|
|
push_history(1)
|
|
|
|
assert_equal(1, HISTORY.length)
|
|
|
|
push_history(4)
|
|
|
|
assert_equal(5, HISTORY.length)
|
|
|
|
HISTORY.clear
|
|
|
|
assert_equal(0, HISTORY.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_p
|
|
|
|
2.times do
|
|
|
|
assert(HISTORY.empty?)
|
|
|
|
HISTORY.push("s")
|
|
|
|
assert_equal(false, HISTORY.empty?)
|
|
|
|
HISTORY.clear
|
|
|
|
assert(HISTORY.empty?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_at
|
|
|
|
begin
|
|
|
|
lines = push_history(5)
|
|
|
|
(0..4).each do |i|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(lines[i], HISTORY.delete_at(0))
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
assert(HISTORY.empty?)
|
|
|
|
|
|
|
|
lines = push_history(5)
|
|
|
|
(1..5).each do |i|
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(lines[lines.length - i], HISTORY.delete_at(-1))
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
assert(HISTORY.empty?)
|
|
|
|
|
|
|
|
lines = push_history(5)
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(lines[0], HISTORY.delete_at(0))
|
|
|
|
assert_external_string_equal(lines[4], HISTORY.delete_at(3))
|
|
|
|
assert_external_string_equal(lines[1], HISTORY.delete_at(0))
|
|
|
|
assert_external_string_equal(lines[3], HISTORY.delete_at(1))
|
|
|
|
assert_external_string_equal(lines[2], HISTORY.delete_at(0))
|
2008-08-01 11:04:37 -04:00
|
|
|
assert(HISTORY.empty?)
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_at__out_of_range
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(IndexError, NotImplementedError, "index=<0>") do
|
2008-08-01 11:04:37 -04:00
|
|
|
HISTORY.delete_at(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
lines = push_history(5)
|
|
|
|
invalid_indexes = [5, 6, 100, -6, -7, -100]
|
|
|
|
invalid_indexes.each do |i|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(IndexError, NotImplementedError, "index=<#{i}>") do
|
2008-08-01 11:04:37 -04:00
|
|
|
HISTORY.delete_at(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
invalid_indexes = [100_000_000_000_000_000_000,
|
|
|
|
-100_000_000_000_000_000_000]
|
|
|
|
invalid_indexes.each do |i|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(RangeError, NotImplementedError, "index=<#{i}>") do
|
2008-08-01 11:04:37 -04:00
|
|
|
HISTORY.delete_at(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def push_history(num)
|
|
|
|
lines = []
|
|
|
|
num.times do |i|
|
|
|
|
s = "a"
|
|
|
|
i.times do
|
|
|
|
s = s.succ
|
|
|
|
end
|
|
|
|
lines.push("#{i + 1}:#{s}")
|
|
|
|
end
|
|
|
|
HISTORY.push(*lines)
|
|
|
|
return lines
|
|
|
|
end
|
2008-12-21 00:49:18 -05:00
|
|
|
|
2008-12-24 11:11:01 -05:00
|
|
|
def assert_external_string_equal(expected, actual)
|
2008-12-21 00:49:18 -05:00
|
|
|
assert_equal(expected, actual)
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_equal(get_default_internal_encoding, actual.encoding)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_default_internal_encoding
|
|
|
|
return Encoding.default_internal || Encoding.find("locale")
|
2008-12-21 00:49:18 -05:00
|
|
|
end
|
2008-08-01 11:04:37 -04:00
|
|
|
end if defined?(::Readline) && defined?(::Readline::HISTORY) &&
|
|
|
|
(
|
|
|
|
begin
|
|
|
|
Readline::HISTORY.clear
|
|
|
|
rescue NotImplementedError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
)
|