Skip to content

Commit fb68990

Browse files
authored
Merge pull request #1093 from herwinw/io_pwrite
Extend specs of IO#pwrite
2 parents 0c8e2d1 + d3a3f56 commit fb68990

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

core/io/pwrite_spec.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,42 @@
2828
@file.pread(6, 0).should == "foobar"
2929
end
3030

31+
it "calls #to_s on the object to be written" do
32+
object = mock("to_s")
33+
object.should_receive(:to_s).and_return("foo")
34+
@file.pwrite(object, 0)
35+
@file.pread(3, 0).should == "foo"
36+
end
37+
38+
it "calls #to_int on the offset" do
39+
offset = mock("to_int")
40+
offset.should_receive(:to_int).and_return(2)
41+
@file.pwrite("foo", offset)
42+
@file.pread(3, 2).should == "foo"
43+
end
44+
3145
it "raises IOError when file is not open in write mode" do
3246
File.open(@fname, "r") do |file|
33-
-> { file.pwrite("foo", 1) }.should raise_error(IOError)
47+
-> { file.pwrite("foo", 1) }.should raise_error(IOError, "not opened for writing")
3448
end
3549
end
3650

3751
it "raises IOError when file is closed" do
3852
file = File.open(@fname, "w+")
3953
file.close
40-
-> { file.pwrite("foo", 1) }.should raise_error(IOError)
54+
-> { file.pwrite("foo", 1) }.should raise_error(IOError, "closed stream")
55+
end
56+
57+
it "raises a NoMethodError if object does not respond to #to_s" do
58+
-> {
59+
@file.pwrite(BasicObject.new, 0)
60+
}.should raise_error(NoMethodError, /undefined method `to_s'/)
61+
end
62+
63+
it "raises a TypeError if the offset cannot be converted to an Integer" do
64+
-> {
65+
@file.pwrite("foo", Object.new)
66+
}.should raise_error(TypeError, "no implicit conversion of Object into Integer")
4167
end
4268
end
4369
end

0 commit comments

Comments
 (0)