/
writer_spec.rb
134 lines (115 loc) · 3.62 KB
/
writer_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
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
# encoding: UTF-8
require 'spec_helper'
describe Bzip2::Writer do
let(:file){ File.expand_path('../_10lines_', __FILE__) }
class Dummy
def to_s
"dummy"
end
end
after(:each) do
File.delete(file) if File.exists?(file)
end
it "performs like IO#<< when using the #<< method" do
Bzip2::Writer.open(file, "w") do |file|
file << 1 << "\n" << Dummy.new << "\n" << "cat\n"
end
expected = [ "1\n", "dummy\n", "cat\n"]
actual = []
Bzip2::Reader.foreach(file){ |line| actual.push line }
actual.should == expected
end
it "doesn't immediately flush the data when written to" do
io = File.new(file, "w")
bz2 = Bzip2::Writer.new(io)
bz2 << 1 << "\n" << Dummy.new << "\n" << "cat\n"
bz = Bzip2::Reader.new(file)
lambda { bz.gets }.should raise_error(Bzip2::Error)
bz = Bzip2::Reader.open(file)
lambda { bz.gets }.should raise_error(Bzip2::Error)
io.close
lambda { Bzip2::Reader.new(io) }.should raise_error(IOError)
end
it "behaves the same as IO#print when using #print" do
Bzip2::Writer.open(file) do |file|
file.print "foo\n" * 4096, "\n" * 4096,
"bar" * 4096, "\n" * 4096, "zot\n" * 1024
end
Bzip2::Reader.open(file) do |file|
file.gets('').should == "foo\n" * 4096 + "\n"
file.gets('').should == "bar" * 4096 + "\n\n"
file.gets('').should == "zot\n" * 1024
end
end
it "respects specific global variables like IO#print does via #print" do
Bzip2::Writer.open(file) do |file|
file.print "hello"
file.print 1,2
$_ = "wombat\n"
file.print
$\ = ":"
$, = ","
file.print 3, 4
file.print 5, 6
$\ = nil
file.print "\n"
$, = nil
end
Bzip2::Reader.open(file) do |file|
file.gets(nil).should == "hello12wombat\n3,4:5,6:\n"
end
end
it "only writes one byte via the #putc method" do
Bzip2::Writer.open(file, "wb") do |file|
file.putc "A"
0.upto(255) { |ch| file.putc ch }
end
Bzip2::Reader.open(file, "rb") do |file|
file.getc.should == 'A'.bytes.first
0.upto(255) { |ch| file.getc.should == ch }
end
end
it "behaves the same as IO#puts when using #puts" do
Bzip2::Writer.open(file, "w") do |file|
file.puts "line 1", "line 2"
file.puts [ Dummy.new, 4 ]
end
Bzip2::Reader.open(file) do |file|
file.gets.should == "line 1\n"
file.gets.should == "line 2\n"
file.gets.should == "dummy\n"
file.gets.should == "4\n"
end
end
it "writes data successfully to a file and returns the length of the data" do
Bzip2::Writer.open(file, "w") do |file|
file.write('*' * 10).should == 10
file.write('!' * 5).should == 5
file.write('').should == 0
file.write(1).should == 1
file.write(2.30000).should == 3
file.write("\n").should == 1
end
Bzip2::Reader.open(file) do |file|
file.gets.should == "**********!!!!!12.3\n"
end
end
it "returns the compressed data when no constructor argument is specified" do
file = Bzip2::Writer.new
file << ('*' * 10) << ('!' * 5) << '' << 1 << 2.3000 << "\n"
Bzip2::bunzip2(file.flush).should == "**********!!!!!12.3\n"
end
it "compresses data via the #bzip2 shortcut" do
data = ["**********!!!!!12.3\n"]
data << "foo\n"*4096 + "\n"*4096 + "bar"*4096 + "\n"*4096 + "zot\n"*1024
data.each do |test|
Bzip2::bunzip2(Bzip2::bzip2(test)).should == test
end
end
it "correctly reports when a writer is closed" do
writer = Bzip2::Writer.open(file, 'w')
writer.should_not be_closed
writer.close
writer.should be_closed
end
end