Skip to content

Commit

Permalink
Add specs for non default argument types in IO#pwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Oct 14, 2023
1 parent 59bdcb4 commit 82e1948
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions core/io/pwrite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
@file.pread(6, 0).should == "foobar"
end

it "calls #to_s on the object to be written" do
data = mock("to_s")
data.should_receive(:to_s).and_return("foo")
@file.pwrite(data, 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)
Expand All @@ -39,5 +53,17 @@
file.close
-> { file.pwrite("foo", 1) }.should raise_error(IOError)
end

it "raises a NoMethodError if #to_s cannot be called on the object to be written" 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 82e1948

Please sign in to comment.