summaryrefslogtreecommitdiff
path: root/spec/ruby/library/zlib/gzipreader/read_spec.rb
blob: b81954b5ce1ab0041402772d74d5fc8d4faf6c7a (plain)
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
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_error(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 be_nil
      gz.read(2**16).should be_nil
    end
  end
end