Skip to content

Commit

Permalink
Make the Writer specs more descriptive by giving them some doc strings.
Browse files Browse the repository at this point in the history
Also reverse the order on the expectations to make a bit more sense
  • Loading branch information
alexcrichton committed Oct 12, 2010
1 parent 450e842 commit 9271096
Showing 1 changed file with 46 additions and 50 deletions.
96 changes: 46 additions & 50 deletions spec/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ def to_s
end
end

before(:all) do
@file = "_10lines_"
before(:each) do
@file = File.expand_path('../_10lines_', __FILE__)
end

after(:all) do
File.unlink(@file)
after(:each) do
File.delete(@file) if File.exists?(@file)
end

it "should test_f_LSHIFT" do
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"]
Bzip2::Reader.foreach(@file) do |line|
expected.shift.should == line
end
[].should == expected
actual = []
Bzip2::Reader.foreach(@file){ |line| actual.push line }
actual.should == expected
end

it "should test_f_error" do
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"
Expand All @@ -39,18 +38,20 @@ def to_s
lambda { Bzip2::Reader.new(io) }.should raise_error(IOError)
end

it "should test_f_gets_para" do
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
file.print "foo\n" * 4096, "\n" * 4096,
"bar" * 4096, "\n" * 4096, "zot\n" * 1024
end

Bzip2::Reader.open(@file) do |file|
("foo\n"*4096+"\n").should == file.gets("")
("bar"*4096+"\n\n").should == file.gets("")
("zot\n"*1024).should == file.gets("")
file.gets('').should == "foo\n" * 4096 + "\n"
file.gets('').should == "bar" * 4096 + "\n\n"
file.gets('').should == "zot\n" * 1024
end
end

it "should test_f_print" do
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
Expand All @@ -66,68 +67,63 @@ def to_s
end

Bzip2::Reader.open(@file) do |file|
content = file.gets(nil)
"hello12wombat\n3,4:5,6:\n".should == content
file.gets(nil).should == "hello12wombat\n3,4:5,6:\n"
end
end

it "should test_f_putc" do
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|
'A'.bytes.first.should == file.getc
0.upto(255) { |ch| ch.should == file.getc }
file.getc.should == 'A'.bytes.first
0.upto(255) { |ch| file.getc.should == ch }
end
end

it "should test_f_puts" do
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|
"line 1\n".should == file.gets
"line 2\n".should == file.gets
"dummy\n".should == file.gets
"4\n".should == file.gets
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 "should test_f_write" do
it "writes data successfully to a file and returns the length of the data" do
Bzip2::Writer.open(@file, "w") do |file|
10.should == file.write('*' * 10)
5.should == file.write('!' * 5)
0.should == file.write('')
1.should == file.write(1)
3.should == file.write(2.30000)
1.should == file.write("\n")
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|
"**********!!!!!12.3\n".should == file.gets
file.gets.should == "**********!!!!!12.3\n"
end
end

it "should test_s_string" do
it "returns the compressed data when no constructor argument is specified" do
file = Bzip2::Writer.new
10.should == file.write('*' * 10)
5.should == file.write('!' * 5)
0.should == file.write('')
1.should == file.write(1)
3.should == file.write(2.30000)
1.should == file.write("\n")
line = Bzip2::bunzip2(file.flush)
"**********!!!!!12.3\n".should == line

line = Bzip2::bunzip2(Bzip2::bzip2("**********!!!!!12.3\n"))
"**********!!!!!12.3\n".should == line

test = "foo\n"*4096 + "\n"*4096 + "bar"*4096 + "\n"*4096 + "zot\n"*1024
line = Bzip2::bunzip2(Bzip2::bzip2(test))
test.should == line
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
end

0 comments on commit 9271096

Please sign in to comment.