File: history_spec.rb

package info (click to toggle)
ruby-tty-reader 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 804 kB
  • sloc: ruby: 1,759; sh: 4; makefile: 4
file content (202 lines) | stat: -rw-r--r-- 5,035 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

RSpec.describe TTY::Reader::History do
  it "has no lines" do
    history = described_class.new
    expect(history.size).to eq(0)
  end

  it "doesn't navigate through empty buffer" do
    history = described_class.new
    expect(history.next?).to eq(false)
    expect(history.previous?).to eq(false)
  end

  it "allows to cycle through non-empty buffer" do
    history = described_class.new(3, cycle: true)
    history << "line"
    expect(history.next?).to eq(true)
    expect(history.previous?).to eq(true)
  end

  it "defaults maximum size" do
    history = described_class.new
    expect(history.max_size).to eq(512)
  end

  it "presents string representation" do
    history = described_class.new
    expect(history.to_s).to eq("[]")
  end

  it "adds items to history without overflowing" do
    history = described_class.new(3)
    history << "line #1"
    history << "line #2"
    history << "line #3"
    history << "line #4"

    expect(history.to_a).to eq(["line #2", "line #3", "line #4"])
    expect(history.index).to eq(2)
  end

  it "iterates over lines" do
    history = described_class.new
    history << "line #1"
    history << "line #2"
    history << "line #3"

    expect { |block|
      history.each(&block)
    }.to yield_successive_args("line #1", "line #2", "line #3")
  end

  it "returns enumerator when iterating without a block" do
    history = described_class.new

    expect(history.each).to be_an(Enumerator)
  end

  it "excludes empty lines by default" do
    history = described_class.new
    history << ""
    history << "   "

    expect(history.to_a).to eq(["   "])
  end

  it "excludes lines matching pattern" do
    exclude = ->(line) { /line #[23]/.match(line) }
    history = described_class.new(exclude: exclude)
    history << "line #1"
    history << "line #2"
    history << "line #3"

    expect(history.to_a).to eq(["line #1"])
    expect(history.index).to eq(0)
  end

  it "allows duplicates" do
    history = described_class.new
    history << "line #1"
    history << "line #1"
    history << "line #1"

    expect(history.to_a).to eq(["line #1", "line #1", "line #1"])
  end

  it "prevents duplicates" do
    history = described_class.new(duplicates: false)
    history << "line #1"
    history << "line #1"
    history << "line #1"

    expect(history.to_a).to eq(["line #1"])
  end

  it "navigates through history buffer without cycling" do
    history = described_class.new(3)
    history << "line #1"
    history << "line #2"
    history << "line #3"

    expect(history.index).to eq(2)
    history.previous
    history.previous
    expect(history.index).to eq(0)
    history.previous
    expect(history.index).to eq(0)
    history.next
    history.next
    expect(history.index).to eq(2)
    history.next
    expect(history.next?).to eq(false)
    expect(history.index).to eq(2)
  end

  it "navigates through history buffer with cycling" do
    history = described_class.new(3, cycle: true)
    history << "line #1"
    history << "line #2"
    history << "line #3"

    expect(history.index).to eq(2)
    history.previous
    history.previous
    expect(history.index).to eq(0)
    history.previous
    expect(history.index).to eq(2)
    expect(history.next?).to eq(true)
    history.next
    history.next
    expect(history.index).to eq(1)
    history.next
    expect(history.index).to eq(2)
  end

  it "checks if navigation is possible" do
    history = described_class.new(3)

    expect(history.index).to eq(nil)
    expect(history.previous?).to eq(false)
    expect(history.next?).to eq(false)

    history << "line #1"
    history << "line #2"
    expect(history.index).to eq(1)
    expect(history.previous?).to eq(true)
    expect(history.next?).to eq(false)

    history.previous
    expect(history.index).to eq(0)
    expect(history.previous?).to eq(true)
    expect(history.next?).to eq(true)

    history.previous
    expect(history.index).to eq(0)
    expect(history.previous?).to eq(true)
    expect(history.next?).to eq(true)
  end

  it "gets line based on index" do
    history = described_class.new(3, cycle: true)
    history << "line #1"
    history << "line #2"
    history << "line #3"

    expect(history[-1]).to eq("line #3")
    expect(history[1]).to eq("line #2")
    expect {
      history[11]
    }.to raise_error(IndexError, "invalid index")
  end

  it "retrieves current line" do
    history = described_class.new(3, cycle: true)
    expect(history.get).to eq(nil)

    history << "line #1"
    history << "line #2"
    history << "line #3"

    expect(history.get).to eq("line #3")
    history.previous
    history.previous
    expect(history.get).to eq("line #1")
    history.next
    expect(history.get).to eq("line #2")
  end

  it "clears all lines" do
    history = described_class.new(3)

    history << "line #1"
    history << "line #2"
    history << "line #3"

    expect(history.size).to eq(3)
    history.clear
    expect(history.size).to eq(0)
    expect(history.index).to eq(0)
  end
end