summaryrefslogtreecommitdiff
path: root/jni/ruby/test/readline/test_readline_history.rb
blob: 4bcd7b3b252be4525dcdaaab4fd0b237c5be7a1b (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
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
   )