summaryrefslogtreecommitdiff
path: root/test/readline/test_readline_history.rb
blob: 39ad4fe85ac5abccea6a994c98c5ca86b995d54d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# frozen_string_literal: false
require_relative "helper"
require "test/unit"

module BasetestReadlineHistory
  def setup
    Readline::HISTORY.clear
  end

  def test_to_s
    expected = "HISTORY"
    assert_equal(expected, Readline::HISTORY.to_s)
  end

  def test_get
    lines = push_history(5)
    lines.each_with_index do |s, i|
      assert_external_string_equal(s, Readline::HISTORY[i])
    end
  end

  def test_get__negative
    lines = push_history(5)
    (1..5).each do |i|
      assert_equal(lines[-i], Readline::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
        Readline::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
        Readline::HISTORY[i]
      end
    end
  end

  def test_set
    begin
      push_history(5)
      5.times do |i|
        expected = "set: #{i}"
        Readline::HISTORY[i] = expected
        assert_external_string_equal(expected, Readline::HISTORY[i])
      end
    rescue NotImplementedError
    end
  end

  def test_set__out_of_range
    assert_raise(IndexError, NotImplementedError, "index=<0>") do
      Readline::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
        Readline::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
        Readline::HISTORY[i] = "set: #{i}"
      end
    end
  end

  def test_push
    5.times do |i|
      s = i.to_s
      assert_equal(Readline::HISTORY, Readline::HISTORY.push(s))
      assert_external_string_equal(s, Readline::HISTORY[i])
    end
    assert_equal(5, Readline::HISTORY.length)
  end

  def test_push__operator
    5.times do |i|
      s = i.to_s
      assert_equal(Readline::HISTORY, Readline::HISTORY << s)
      assert_external_string_equal(s, Readline::HISTORY[i])
    end
    assert_equal(5, Readline::HISTORY.length)
  end

  def test_push__plural
    assert_equal(Readline::HISTORY, Readline::HISTORY.push("0", "1", "2", "3", "4"))
    (0..4).each do |i|
      assert_external_string_equal(i.to_s, Readline::HISTORY[i])
    end
    assert_equal(5, Readline::HISTORY.length)

    assert_equal(Readline::HISTORY, Readline::HISTORY.push("5", "6", "7", "8", "9"))
    (5..9).each do |i|
      assert_external_string_equal(i.to_s, Readline::HISTORY[i])
    end
    assert_equal(10, Readline::HISTORY.length)
  end

  def test_pop
    begin
      assert_equal(nil, Readline::HISTORY.pop)

      lines = push_history(5)
      (1..5).each do |i|
        assert_external_string_equal(lines[-i], Readline::HISTORY.pop)
        assert_equal(lines.length - i, Readline::HISTORY.length)
      end

      assert_equal(nil, Readline::HISTORY.pop)
    rescue NotImplementedError
    end
  end

  def test_shift
    begin
      assert_equal(nil, Readline::HISTORY.shift)

      lines = push_history(5)
      (0..4).each do |i|
        assert_external_string_equal(lines[i], Readline::HISTORY.shift)
        assert_equal(lines.length - (i + 1), Readline::HISTORY.length)
      end

      assert_equal(nil, Readline::HISTORY.shift)
    rescue NotImplementedError
    end
  end

  def test_each
    e = Readline::HISTORY.each do |s|
      assert(false) # not reachable
    end
    assert_equal(Readline::HISTORY, e)
    lines = push_history(5)
    i = 0
    e = Readline::HISTORY.each do |s|
      assert_external_string_equal(Readline::HISTORY[i], s)
      assert_external_string_equal(lines[i], s)
      i += 1
    end
    assert_equal(Readline::HISTORY, e)
  end

  def test_each__enumerator
    e = Readline::HISTORY.each
    assert_instance_of(Enumerator, e)
  end

  def test_length
    assert_equal(0, Readline::HISTORY.length)
    push_history(1)
    assert_equal(1, Readline::HISTORY.length)
    push_history(4)
    assert_equal(5, Readline::HISTORY.length)
    Readline::HISTORY.clear
    assert_equal(0, Readline::HISTORY.length)
  end

  def test_empty_p
    2.times do
      assert(Readline::HISTORY.empty?)
      Readline::HISTORY.push("s")
      assert_equal(false, Readline::HISTORY.empty?)
      Readline::HISTORY.clear
      assert(Readline::HISTORY.empty?)
    end
  end

  def test_delete_at
    begin
      lines = push_history(5)
      (0..4).each do |i|
        assert_external_string_equal(lines[i], Readline::HISTORY.delete_at(0))
      end
      assert(Readline::HISTORY.empty?)

      lines = push_history(5)
      (1..5).each do |i|
        assert_external_string_equal(lines[lines.length - i], Readline::HISTORY.delete_at(-1))
      end
      assert(Readline::HISTORY.empty?)

      lines = push_history(5)
      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?)
    rescue NotImplementedError
    end
  end

  def test_delete_at__out_of_range
    assert_raise(IndexError, NotImplementedError, "index=<0>") do
      Readline::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
        Readline::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
        Readline::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
    Readline::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

class TestReadlineHistory < Test::Unit::TestCase
  include BasetestReadlineHistory

  def setup
    use_ext_readline
    super
  end
end if defined?(::ReadlineSo) && defined?(::ReadlineSo::HISTORY) &&
  ENV["TEST_READLINE_OR_RELINE"] != "Reline" &&
  (
   begin
     ReadlineSo::HISTORY.clear
   rescue NotImplementedError
     false
   end
   )

class TestRelineAsReadlineHistory < Test::Unit::TestCase
  include BasetestReadlineHistory

  def setup
    use_lib_reline
    super
  end

  def teardown
    finish_using_lib_reline
    super
  end

  def get_default_internal_encoding
    if RUBY_PLATFORM =~ /mswin|mingw/
      Encoding.default_internal || Encoding::UTF_8
    else
      Reline::IOGate.encoding
    end
  end
end if defined?(Reline) && ENV["TEST_READLINE_OR_RELINE"] != "Readline"