1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/readline/test_readline_history.rb
shugo 1f828497d1 * safe.c (rb_set_safe_level, safe_setter): raise an ArgumentError
when $SAFE is set to 4.  $SAFE=4 is now obsolete.
  [ruby-core:55222] [Feature #8468]

* object.c (rb_obj_untrusted, rb_obj_untrust, rb_obj_trust):
  Kernel#untrusted?, untrust, and trust are now deprecated.
  Their behavior is same as tainted?, taint, and untaint,
  respectively.

* include/ruby/ruby.h (OBJ_UNTRUSTED, OBJ_UNTRUST): OBJ_UNTRUSTED()
  and OBJ_UNTRUST() are aliases of OBJ_TAINTED() and OBJ_TAINT(),
  respectively.

* array.c, class.c, debug.c, dir.c, encoding.c, error.c, eval.c,
  ext/curses/curses.c, ext/dbm/dbm.c, ext/dl/cfunc.c,
  ext/dl/cptr.c, ext/dl/dl.c, ext/etc/etc.c, ext/fiddle/fiddle.c,
  ext/fiddle/pointer.c, ext/gdbm/gdbm.c, ext/readline/readline.c,
  ext/sdbm/init.c, ext/socket/ancdata.c, ext/socket/basicsocket.c,
  ext/socket/socket.c, ext/socket/udpsocket.c,
  ext/stringio/stringio.c, ext/syslog/syslog.c, ext/tk/tcltklib.c,
  ext/win32ole/win32ole.c, file.c, gc.c, hash.c, io.c, iseq.c,
  load.c, marshal.c, object.c, proc.c, process.c, random.c, re.c,
  safe.c, string.c, thread.c, transcode.c, variable.c,
  vm_insnhelper.c, vm_method.c, vm_trace.c: remove code for
  $SAFE=4.

* test/dl/test_dl2.rb, test/erb/test_erb.rb,
  test/readline/test_readline.rb,
  test/readline/test_readline_history.rb, test/ruby/test_alias.rb,
  test/ruby/test_array.rb, test/ruby/test_dir.rb,
  test/ruby/test_encoding.rb, test/ruby/test_env.rb,
  test/ruby/test_eval.rb, test/ruby/test_exception.rb,
  test/ruby/test_file_exhaustive.rb, test/ruby/test_hash.rb,
  test/ruby/test_io.rb, test/ruby/test_method.rb,
  test/ruby/test_module.rb, test/ruby/test_object.rb,
  test/ruby/test_pack.rb, test/ruby/test_rand.rb,
  test/ruby/test_regexp.rb, test/ruby/test_settracefunc.rb,
  test/ruby/test_struct.rb, test/ruby/test_thread.rb,
  test/ruby/test_time.rb: remove tests for $SAFE=4.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41259 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-06-12 14:20:51 +00:00

292 lines
6.5 KiB
Ruby

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_to_s
expected = "HISTORY"
assert_equal(expected, HISTORY.to_s)
end
def test_get
lines = push_history(5)
lines.each_with_index do |s, i|
assert_external_string_equal(s, HISTORY[i])
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
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
push_history(5)
5.times do |i|
expected = "set: #{i}"
HISTORY[i] = expected
assert_external_string_equal(expected, HISTORY[i])
end
rescue NotImplementedError
end
end
def test_set__out_of_range
assert_raise(IndexError, NotImplementedError, "index=<0>") do
HISTORY[0] = "set: 0"
end
push_history(5)
invalid_indexes = [5, 6, 100, -6, -7, -100]
invalid_indexes.each do |i|
assert_raise(IndexError, NotImplementedError, "index=<#{i}>") do
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|
s = i.to_s
assert_equal(HISTORY, HISTORY.push(s))
assert_external_string_equal(s, HISTORY[i])
end
assert_equal(5, HISTORY.length)
end
def test_push__operator
5.times do |i|
s = i.to_s
assert_equal(HISTORY, HISTORY << s)
assert_external_string_equal(s, HISTORY[i])
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|
assert_external_string_equal(i.to_s, HISTORY[i])
end
assert_equal(5, HISTORY.length)
assert_equal(HISTORY, HISTORY.push("5", "6", "7", "8", "9"))
(5..9).each do |i|
assert_external_string_equal(i.to_s, HISTORY[i])
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|
assert_external_string_equal(lines[-i], HISTORY.pop)
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|
assert_external_string_equal(lines[i], HISTORY.shift)
assert_equal(lines.length - (i + 1), HISTORY.length)
end
assert_equal(nil, HISTORY.shift)
rescue NotImplementedError
end
end
def test_each
e = HISTORY.each do |s|
assert(false) # not reachable
end
assert_equal(HISTORY, e)
lines = push_history(5)
i = 0
e = HISTORY.each do |s|
assert_external_string_equal(HISTORY[i], s)
assert_external_string_equal(lines[i], s)
i += 1
end
assert_equal(HISTORY, e)
end
def test_each__enumerator
e = HISTORY.each
assert_instance_of(Enumerator, e)
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|
assert_external_string_equal(lines[i], HISTORY.delete_at(0))
end
assert(HISTORY.empty?)
lines = push_history(5)
(1..5).each do |i|
assert_external_string_equal(lines[lines.length - i], HISTORY.delete_at(-1))
end
assert(HISTORY.empty?)
lines = push_history(5)
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))
assert(HISTORY.empty?)
rescue NotImplementedError
end
end
def test_delete_at__out_of_range
assert_raise(IndexError, NotImplementedError, "index=<0>") do
HISTORY.delete_at(0)
end
push_history(5)
invalid_indexes = [5, 6, 100, -6, -7, -100]
invalid_indexes.each do |i|
assert_raise(IndexError, NotImplementedError, "index=<#{i}>") do
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|
assert_raise(RangeError, NotImplementedError, "index=<#{i}>") do
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
def assert_external_string_equal(expected, actual)
assert_equal(expected, actual)
assert_equal(get_default_internal_encoding, actual.encoding)
end
def get_default_internal_encoding
return Encoding.default_internal || Encoding.find("locale")
end
end if defined?(::Readline) && defined?(::Readline::HISTORY) &&
(
begin
Readline::HISTORY.clear
rescue NotImplementedError
false
end
)