Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend specs for IO#pread #1079

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions core/io/pread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,88 @@
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 "ignores the current offset" do
@file.pos = 3
@file.pread(4, 0).should == "1234"
end

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

it "ignores the offset for maxlen = 0, even if it is out of file bounds" do
@file.pread(0, 400).should == ""
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved

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 "converts maxlen to Integer using #to_int" do
maxlen = mock('maxlen')
maxlen.should_receive(:to_int).and_return(4)
@file.pread(maxlen, 0).should == "1234"
end

it "converts offset to Integer using #to_int" do
offset = mock('offset')
offset.should_receive(:to_int).and_return(0)
@file.pread(4, offset).should == "1234"
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved

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

it "raises TypeError if maxlen is not an Integer and cannot be coerced into Integer" do
maxlen = Object.new
-> { @file.pread(maxlen, 0) }.should raise_error(TypeError, 'no implicit conversion of Object into Integer')
end

it "raises TypeError if offset is not an Integer and cannot be coerced into Integer" do
offset = Object.new
-> { @file.pread(4, offset) }.should raise_error(TypeError, 'no implicit conversion of Object into Integer')
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved

it "raises ArgumentError for negative values of maxlen" do
-> { @file.pread(-4, 0) }.should raise_error(ArgumentError, 'negative string size (or size too big)')
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved

it "raised Errno::EINVAL for negative values of offset" do
-> { @file.pread(4, -1) }.should raise_error(Errno::EINVAL, /Invalid argument/)
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved

it "raises TypeError if the buffer is not a String and cannot be coerced into String" do
buffer = Object.new
-> { @file.pread(4, 0, buffer) }.should raise_error(TypeError, 'no implicit conversion of Object into String')
end

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