/
backwards_file_reader_spec.rb
109 lines (96 loc) · 3.08 KB
/
backwards_file_reader_spec.rb
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
require 'spec_helper'
describe BackwardsFileReader do
let :testfile do
Tempfile.new("tkellem-test").tap do |tf|
tf.write <<-LINES
line1
line2
line3
LINES
tf.rewind
end
end
describe '.scan' do
it "should read all lines" do
results = mock('lines')
results.expects(:got).with("line3\n")
results.expects(:got).with("line2\n")
results.expects(:got).with("line1\n")
BackwardsFileReader.scan(testfile) { |line| results.got(line); true }
testfile.pos.should == 0
end
it "should abort if the block returns false" do
results = mock('lines')
results.expects(:got).with("line3\n")
results.expects(:got).with("line2\n")
BackwardsFileReader.scan(testfile) do |line|
results.got(line)
line != "line2\n"
end
testfile.pos.should == 6
end
end
describe "#readline" do
it "should read correctly with a buffer size smaller than one line" do
reader = BackwardsFileReader.new(testfile, 2)
reader.readline.should == "line3\n"
reader.readline.should == "line2\n"
reader.readline.should == "line1\n"
reader.readline.should == nil
end
it "should read correctly with a buffer size larger than one line" do
reader = BackwardsFileReader.new(testfile, 6)
reader.readline.should == "line3\n"
reader.readline.should == "line2\n"
reader.readline.should == "line1\n"
reader.readline.should == nil
end
end
describe "#sync" do
it "should reset the stream position to the current line" do
reader = BackwardsFileReader.new(testfile)
reader.readline
reader.sync.should == testfile
testfile.pos.should == testfile.size - 6
reader = BackwardsFileReader.new(testfile)
reader.readline
reader.readline
reader.sync.should == testfile
testfile.pos.should == testfile.size - 12
end
it "should reset correctly with a buffer size smaller than one line" do
reader = BackwardsFileReader.new(testfile, 2)
reader.readline
reader.sync.should == testfile
testfile.pos.should == testfile.size - 6
reader = BackwardsFileReader.new(testfile, 2)
reader.readline
reader.readline
reader.sync.should == testfile
testfile.pos.should == testfile.size - 12
end
it "should reset correctly with a buffer size larger than one line" do
reader = BackwardsFileReader.new(testfile, 7)
reader.readline
reader.sync.should == testfile
testfile.pos.should == testfile.size - 6
reader = BackwardsFileReader.new(testfile, 7)
reader.readline
reader.readline
reader.sync.should == testfile
testfile.pos.should == testfile.size - 12
end
end
it "should support resuming" do
reader = BackwardsFileReader.new(testfile, 2)
reader.readline.should == "line3\n"
reader.sync
testfile.pos.should == testfile.size - 6
reader.readline.should == "line2\n"
reader.sync
testfile.pos.should == testfile.size - 12
reader.readline.should == "line1\n"
reader.sync
testfile.pos.should == 0
end
end