diff options
Diffstat (limited to 'spec/ruby/library/zlib/gzipreader/read_spec.rb')
| -rw-r--r-- | spec/ruby/library/zlib/gzipreader/read_spec.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/spec/ruby/library/zlib/gzipreader/read_spec.rb b/spec/ruby/library/zlib/gzipreader/read_spec.rb new file mode 100644 index 0000000000..b07d433bdd --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/read_spec.rb @@ -0,0 +1,66 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#read" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + end + + it "with no arguments reads the entire content of a gzip file" do + gz = Zlib::GzipReader.new @io + gz.read.should == @data + end + + it "with nil length argument reads the entire content of a gzip file" do + gz = Zlib::GzipReader.new @io + gz.read(nil).should == @data + end + + it "reads the contents up to a certain size" do + gz = Zlib::GzipReader.new @io + gz.read(5).should == @data[0...5] + gz.read(5).should == @data[5...10] + end + + it "does not accept a negative length to read" do + gz = Zlib::GzipReader.new @io + -> { + gz.read(-1) + }.should.raise(ArgumentError) + end + + it "returns an empty string if a 0 length is given" do + gz = Zlib::GzipReader.new @io + gz.read(0).should == "" + end + + it "respects :external_encoding option" do + gz = Zlib::GzipReader.new(@io, external_encoding: 'UTF-8') + gz.read.encoding.should == Encoding::UTF_8 + + @io.rewind + gz = Zlib::GzipReader.new(@io, external_encoding: 'UTF-16LE') + gz.read.encoding.should == Encoding::UTF_16LE + end + + describe "at the end of data" do + it "returns empty string if length parameter is not specified or 0" do + gz = Zlib::GzipReader.new @io + gz.read # read till the end + gz.read(0).should == "" + gz.read().should == "" + gz.read(nil).should == "" + end + + it "returns nil if length parameter is positive" do + gz = Zlib::GzipReader.new @io + gz.read # read till the end + gz.read(1).should == nil + gz.read(2**16).should == nil + end + end +end |
