summaryrefslogtreecommitdiff
path: root/test/reline/test_history.rb
blob: 189f2db86d583e556a46f03ecac612617d10430e (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
require_relative 'helper'
require "reline/history"

class Reline::History::Test < Reline::TestCase
  def setup
    Reline.send(:test_mode)
  end

  def test_ancestors
    assert_equal(Reline::History.ancestors.include?(Array), true)
  end

  def test_to_s
    history = history_new
    expected = "HISTORY"
    assert_equal(expected, history.to_s)
  end

  def test_get
    history, lines = lines = history_new_and_push_history(5)
    lines.each_with_index do |s, i|
      assert_external_string_equal(s, history[i])
    end
  end

  def test_get__negative
    history, lines = lines = history_new_and_push_history(5)
    (1..5).each do |i|
      assert_equal(lines[-i], history[-i])
    end
  end

  def test_get__out_of_range
    history, _ = history_new_and_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
      history, _ = history_new_and_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
    history = history_new
    assert_raise(IndexError, NotImplementedError, "index=<0>") do
      history[0] = "set: 0"
    end

    history, _ = history_new_and_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
    history = history_new
    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
    history = history_new
    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
    history = history_new
    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
    history = history_new
    begin
      assert_equal(nil, history.pop)

      history, lines = lines = history_new_and_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
    history = history_new
    begin
      assert_equal(nil, history.shift)

      history, lines = lines = history_new_and_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
    history = history_new
    e = history.each do |s|
      assert(false) # not reachable
    end
    assert_equal(history, e)
    history, lines = lines = history_new_and_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
    history = history_new
    e = history.each
    assert_instance_of(Enumerator, e)
  end

  def test_length
    history = history_new
    assert_equal(0, history.length)
    push_history(history, 1)
    assert_equal(1, history.length)
    push_history(history, 4)
    assert_equal(5, history.length)
    history.clear
    assert_equal(0, history.length)
  end

  def test_empty_p
    history = history_new
    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
      history, lines = lines = history_new_and_push_history(5)
      (0..4).each do |i|
        assert_external_string_equal(lines[i], history.delete_at(0))
      end
      assert(history.empty?)

      history, lines = lines = history_new_and_push_history(5)
      (1..5).each do |i|
        assert_external_string_equal(lines[lines.length - i], history.delete_at(-1))
      end
      assert(history.empty?)

      history, lines = lines = history_new_and_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
    history = history_new
    assert_raise(IndexError, NotImplementedError, "index=<0>") do
      history.delete_at(0)
    end

    history, _ = history_new_and_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 history_new(history_size: 10)
    Reline::History.new(Struct.new(:history_size).new(history_size))
  end

  def push_history(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 history, lines
  end

  def history_new_and_push_history(num)
    history = history_new(history_size: 100)
    return push_history(history, num)
  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
    if RUBY_PLATFORM =~ /mswin|mingw/
      Encoding.default_internal || Encoding::UTF_8
    else
      Encoding.default_internal || Encoding.find("locale")
    end
  end
end