2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2019-05-13 18:00:03 -04:00
|
|
|
require_relative "helper"
|
|
|
|
require "test/unit"
|
2008-08-01 11:04:37 -04:00
|
|
|
|
2019-05-17 13:32:25 -04:00
|
|
|
module BasetestReadlineHistory
|
2008-08-01 11:04:37 -04:00
|
|
|
def setup
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY.clear
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
2008-12-21 00:49:18 -05:00
|
|
|
expected = "HISTORY"
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(expected, Readline::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|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(s, Readline::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|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(lines[-i], Readline::HISTORY[-i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get__out_of_range
|
2012-06-18 22:58:43 -04:00
|
|
|
push_history(5)
|
2008-08-01 11:04:37 -04:00
|
|
|
invalid_indexes = [5, 6, 100, -6, -7, -100]
|
|
|
|
invalid_indexes.each do |i|
|
|
|
|
assert_raise(IndexError, "i=<#{i}>") do
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY[i]
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY[i]
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set
|
|
|
|
begin
|
2012-06-18 22:58:43 -04:00
|
|
|
push_history(5)
|
2008-08-01 11:04:37 -04:00
|
|
|
5.times do |i|
|
|
|
|
expected = "set: #{i}"
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY[i] = expected
|
|
|
|
assert_external_string_equal(expected, Readline::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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY[0] = "set: 0"
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-06-18 22:58:43 -04:00
|
|
|
push_history(5)
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY[i] = "set: #{i}"
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY[i] = "set: #{i}"
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_push
|
|
|
|
5.times do |i|
|
2008-12-21 00:49:18 -05:00
|
|
|
s = i.to_s
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(Readline::HISTORY, Readline::HISTORY.push(s))
|
|
|
|
assert_external_string_equal(s, Readline::HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(5, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_push__operator
|
|
|
|
5.times do |i|
|
2008-12-21 00:49:18 -05:00
|
|
|
s = i.to_s
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(Readline::HISTORY, Readline::HISTORY << s)
|
|
|
|
assert_external_string_equal(s, Readline::HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(5, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_push__plural
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(Readline::HISTORY, Readline::HISTORY.push("0", "1", "2", "3", "4"))
|
2008-08-01 11:04:37 -04:00
|
|
|
(0..4).each do |i|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(i.to_s, Readline::HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(5, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(Readline::HISTORY, Readline::HISTORY.push("5", "6", "7", "8", "9"))
|
2008-08-01 11:04:37 -04:00
|
|
|
(5..9).each do |i|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(i.to_s, Readline::HISTORY[i])
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(10, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pop
|
|
|
|
begin
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(nil, Readline::HISTORY.pop)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-08-01 11:04:37 -04:00
|
|
|
lines = push_history(5)
|
|
|
|
(1..5).each do |i|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(lines[-i], Readline::HISTORY.pop)
|
|
|
|
assert_equal(lines.length - i, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(nil, Readline::HISTORY.pop)
|
2008-08-01 11:04:37 -04:00
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift
|
|
|
|
begin
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(nil, Readline::HISTORY.shift)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-08-01 11:04:37 -04:00
|
|
|
lines = push_history(5)
|
|
|
|
(0..4).each do |i|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(lines[i], Readline::HISTORY.shift)
|
|
|
|
assert_equal(lines.length - (i + 1), Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(nil, Readline::HISTORY.shift)
|
2008-08-01 11:04:37 -04:00
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
2019-05-13 18:00:03 -04:00
|
|
|
e = Readline::HISTORY.each do |s|
|
2008-08-01 11:04:37 -04:00
|
|
|
assert(false) # not reachable
|
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(Readline::HISTORY, e)
|
2008-08-01 11:04:37 -04:00
|
|
|
lines = push_history(5)
|
|
|
|
i = 0
|
2019-05-13 18:00:03 -04:00
|
|
|
e = Readline::HISTORY.each do |s|
|
|
|
|
assert_external_string_equal(Readline::HISTORY[i], s)
|
2008-12-24 11:11:01 -05:00
|
|
|
assert_external_string_equal(lines[i], s)
|
2008-08-01 11:04:37 -04:00
|
|
|
i += 1
|
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(Readline::HISTORY, e)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each__enumerator
|
2019-05-13 18:00:03 -04:00
|
|
|
e = Readline::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
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(0, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
push_history(1)
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(1, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
push_history(4)
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_equal(5, Readline::HISTORY.length)
|
|
|
|
Readline::HISTORY.clear
|
|
|
|
assert_equal(0, Readline::HISTORY.length)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_p
|
|
|
|
2.times do
|
2019-05-13 18:00:03 -04:00
|
|
|
assert(Readline::HISTORY.empty?)
|
|
|
|
Readline::HISTORY.push("s")
|
|
|
|
assert_equal(false, Readline::HISTORY.empty?)
|
|
|
|
Readline::HISTORY.clear
|
|
|
|
assert(Readline::HISTORY.empty?)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_at
|
|
|
|
begin
|
|
|
|
lines = push_history(5)
|
|
|
|
(0..4).each do |i|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(lines[i], Readline::HISTORY.delete_at(0))
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert(Readline::HISTORY.empty?)
|
2008-08-01 11:04:37 -04:00
|
|
|
|
|
|
|
lines = push_history(5)
|
|
|
|
(1..5).each do |i|
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(lines[lines.length - i], Readline::HISTORY.delete_at(-1))
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2019-05-13 18:00:03 -04:00
|
|
|
assert(Readline::HISTORY.empty?)
|
2008-08-01 11:04:37 -04:00
|
|
|
|
|
|
|
lines = push_history(5)
|
2019-05-13 18:00:03 -04:00
|
|
|
assert_external_string_equal(lines[0], Readline::HISTORY.delete_at(0))
|
|
|
|
assert_external_string_equal(lines[4], Readline::HISTORY.delete_at(3))
|
|
|
|
assert_external_string_equal(lines[1], Readline::HISTORY.delete_at(0))
|
|
|
|
assert_external_string_equal(lines[3], Readline::HISTORY.delete_at(1))
|
|
|
|
assert_external_string_equal(lines[2], Readline::HISTORY.delete_at(0))
|
|
|
|
assert(Readline::HISTORY.empty?)
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY.delete_at(0)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-06-18 22:58:43 -04:00
|
|
|
push_history(5)
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY.delete_at(i)
|
2008-08-01 11:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY.delete_at(i)
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
Readline::HISTORY.push(*lines)
|
2008-08-01 11:04:37 -04:00
|
|
|
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
|
2019-05-13 18:00:03 -04:00
|
|
|
end
|
|
|
|
|
2019-05-17 13:32:25 -04:00
|
|
|
class TestReadlineHistory < Test::Unit::TestCase
|
|
|
|
include BasetestReadlineHistory
|
2019-05-13 18:00:03 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
use_ext_readline
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end if defined?(::ReadlineSo) && defined?(::ReadlineSo::HISTORY) &&
|
2008-08-01 11:04:37 -04:00
|
|
|
(
|
|
|
|
begin
|
2019-05-13 18:00:03 -04:00
|
|
|
ReadlineSo::HISTORY.clear
|
2008-08-01 11:04:37 -04:00
|
|
|
rescue NotImplementedError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
)
|
2019-05-13 18:00:03 -04:00
|
|
|
|
2019-05-17 13:32:25 -04:00
|
|
|
class TestRelineAsReadlineHistory < Test::Unit::TestCase
|
|
|
|
include BasetestReadlineHistory
|
2019-05-13 18:00:03 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
use_lib_reline
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|