Skip to content

Commit

Permalink
Extend specs for IO#pread
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Sep 11, 2023
1 parent ee83ee6 commit 1bb216e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions core/io/pread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,73 @@
buffer.should == "567"
end

it "shrinks the buffer in case of less bytes read" do
buffer = "foo"
@file.pread(1, 0, buffer)
buffer.should == "1"
end

it "grows the buffer in case of more bytes read" do
buffer = "foo"
@file.pread(5, 0, buffer)
buffer.should == "12345"
end

it "does not advance the file pointer" do
@file.pread(4, 0).should == "1234"
@file.read.should == "1234567890"
end

it "returns an empty string for maxlen = 0, regardless of the offset" do
@file.pread(0, 4).should == ""
@file.pread(0, 400).should == ""
end

it "does not reset the buffer when reading with maxlen = 0" do
buffer = "foo"
@file.pread(0, 4, buffer)
buffer.should == "foo"

@file.pread(0, 400, buffer)
buffer.should == "foo"
end

it "ignores the current offset" do
@file.pos = 3
@file.pread(4, 0).should == "1234"
end

it "converts maxlen and offset to Integers using #to_int" do
maxlen = mock('maxlen')
maxlen.should_receive(:to_int).and_return(4)
offset = mock('offset')
offset.should_receive(:to_int).and_return(0)
@file.pread(maxlen, offset).should == "1234"
end

it "converts a buffer to String using to_str (but you cannot use this value)" do
buffer = mock('buffer')
buffer.should_receive(:to_str).at_least(1).and_return("foo")
@file.pread(0, 4, buffer).should == "foo"
buffer.should_not.is_a?(String)
buffer.to_str.should == "foo"
end

it "expects Integer values for maxlen and offset" do
-> { @file.pread("maxlen", 0) }.should raise_error(TypeError)
-> { @file.pread(4, "offset") }.should raise_error(TypeError)
end

it "does not support negative values for maxlen and offset" do
-> { @file.pread(-4, 0) }.should raise_error(ArgumentError)
-> { @file.pread(4, -1) }.should raise_error(Errno::EINVAL)
end

it "expects a String as buffer" do
buffer = Object.new
-> { @file.pread(4, 0, buffer) }.should raise_error(TypeError)
end

it "raises EOFError if end-of-file is reached" do
-> { @file.pread(1, 10) }.should raise_error(EOFError)
end
Expand Down

0 comments on commit 1bb216e

Please sign in to comment.