2018-10-27 06:48:40 -04:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
require_relative '../../spec_helper'
|
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
platform_is_not :windows do
|
|
|
|
describe "IO#pwrite" do
|
|
|
|
before :each do
|
|
|
|
@fname = tmp("io_pwrite.txt")
|
|
|
|
@file = File.open(@fname, "w+")
|
|
|
|
end
|
2018-10-27 06:48:40 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
after :each do
|
|
|
|
@file.close
|
|
|
|
rm_r @fname
|
|
|
|
end
|
2018-10-27 06:48:40 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "returns the number of bytes written" do
|
|
|
|
@file.pwrite("foo", 0).should == 3
|
|
|
|
end
|
2018-10-27 06:48:40 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "accepts a string and an offset" do
|
|
|
|
@file.pwrite("foo", 2)
|
|
|
|
@file.pread(3, 2).should == "foo"
|
|
|
|
end
|
2018-10-27 06:48:40 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "does not advance the pointer in the file" do
|
|
|
|
@file.pwrite("bar", 3)
|
|
|
|
@file.write("foo")
|
|
|
|
@file.pread(6, 0).should == "foobar"
|
|
|
|
end
|
2018-10-27 06:48:40 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "raises IOError when file is not open in write mode" do
|
|
|
|
File.open(@fname, "r") do |file|
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { file.pwrite("foo", 1) }.should raise_error(IOError)
|
2018-10-27 06:48:40 -04:00
|
|
|
end
|
|
|
|
end
|
2020-02-08 05:43:27 -05:00
|
|
|
|
|
|
|
it "raises IOError when file is closed" do
|
|
|
|
file = File.open(@fname, "w+")
|
|
|
|
file.close
|
|
|
|
-> { file.pwrite("foo", 1) }.should raise_error(IOError)
|
|
|
|
end
|
2018-10-27 06:48:40 -04:00
|
|
|
end
|
|
|
|
end
|