Skip to content

Commit

Permalink
Merge pull request #1093 from herwinw/io_pwrite
Browse files Browse the repository at this point in the history
Extend specs of IO#pwrite
  • Loading branch information
andrykonchin authored Oct 28, 2023
2 parents 0c8e2d1 + d3a3f56 commit fb68990
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions core/io/pwrite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,42 @@
@file.pread(6, 0).should == "foobar"
end

it "calls #to_s on the object to be written" do
object = mock("to_s")
object.should_receive(:to_s).and_return("foo")
@file.pwrite(object, 0)
@file.pread(3, 0).should == "foo"
end

it "calls #to_int on the offset" do
offset = mock("to_int")
offset.should_receive(:to_int).and_return(2)
@file.pwrite("foo", offset)
@file.pread(3, 2).should == "foo"
end

it "raises IOError when file is not open in write mode" do
File.open(@fname, "r") do |file|
-> { file.pwrite("foo", 1) }.should raise_error(IOError)
-> { file.pwrite("foo", 1) }.should raise_error(IOError, "not opened for writing")
end
end

it "raises IOError when file is closed" do
file = File.open(@fname, "w+")
file.close
-> { file.pwrite("foo", 1) }.should raise_error(IOError)
-> { file.pwrite("foo", 1) }.should raise_error(IOError, "closed stream")
end

it "raises a NoMethodError if object does not respond to #to_s" do
-> {
@file.pwrite(BasicObject.new, 0)
}.should raise_error(NoMethodError, /undefined method `to_s'/)
end

it "raises a TypeError if the offset cannot be converted to an Integer" do
-> {
@file.pwrite("foo", Object.new)
}.should raise_error(TypeError, "no implicit conversion of Object into Integer")
end
end
end

0 comments on commit fb68990

Please sign in to comment.