File: read_exactly_spec.rb

package info (click to toggle)
ruby-archive-zip 0.11.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 772 kB
  • sloc: ruby: 5,052; makefile: 3
file content (52 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (2)
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
# encoding: UTF-8

require 'minitest/autorun'

require 'archive/support/ioextensions.rb'
require 'archive/support/binary_stringio'

describe "IOExtensions.read_exactly" do
  it "reads and returns length bytes from a given IO object" do
    io = BinaryStringIO.new('This is test data')
    IOExtensions.read_exactly(io, 4).must_equal 'This'
    IOExtensions.read_exactly(io, 13).must_equal ' is test data'
  end

  it "raises an error when too little data is available" do
    io = BinaryStringIO.new('This is test data')
    lambda do
      IOExtensions.read_exactly(io, 18)
    end.must_raise EOFError
  end

  it "takes an optional buffer argument and fills it" do
    io = BinaryStringIO.new('This is test data')
    buffer = ''
    IOExtensions.read_exactly(io, 4, buffer)
    buffer.must_equal 'This'
    buffer = ''
    IOExtensions.read_exactly(io, 13, buffer)
    buffer.must_equal ' is test data'
  end

  it "empties the optional buffer before filling it" do
    io = BinaryStringIO.new('This is test data')
    buffer = ''
    IOExtensions.read_exactly(io, 4, buffer)
    buffer.must_equal 'This'
    IOExtensions.read_exactly(io, 13, buffer)
    buffer.must_equal ' is test data'
  end

  it "can read 0 bytes" do
    io = BinaryStringIO.new('This is test data')
    IOExtensions.read_exactly(io, 0).must_equal ''
  end

  it "retries partial reads" do
    io = MiniTest::Mock.new
    io.expect(:read, 'hello', [10])
    io.expect(:read, 'hello', [5])
    IOExtensions.read_exactly(io, 10).must_equal 'hellohello'
  end
end