summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/sysread_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/io/sysread_spec.rb')
-rw-r--r--spec/ruby/core/io/sysread_spec.rb55
1 files changed, 47 insertions, 8 deletions
diff --git a/spec/ruby/core/io/sysread_spec.rb b/spec/ruby/core/io/sysread_spec.rb
index 024200efea..2d58db2250 100644
--- a/spec/ruby/core/io/sysread_spec.rb
+++ b/spec/ruby/core/io/sysread_spec.rb
@@ -6,7 +6,7 @@ describe "IO#sysread on a file" do
@file_name = tmp("IO_sysread_file") + $$.to_s
File.open(@file_name, "w") do |f|
# write some stuff
- f.write("012345678901234567890123456789")
+ f.write("012345678901234567890123456789\nabcdef")
end
@file = File.open(@file_name, "r+")
end
@@ -21,25 +21,25 @@ describe "IO#sysread on a file" do
end
it "reads the specified number of bytes from the file to the buffer" do
- buf = "" # empty buffer
+ buf = +"" # empty buffer
@file.sysread(15, buf).should == buf
buf.should == "012345678901234"
@file.rewind
- buf = "ABCDE" # small buffer
+ buf = +"ABCDE" # small buffer
@file.sysread(15, buf).should == buf
buf.should == "012345678901234"
@file.rewind
- buf = "ABCDE" * 5 # large buffer
+ buf = +"ABCDE" * 5 # large buffer
@file.sysread(15, buf).should == buf
buf.should == "012345678901234"
end
it "coerces the second argument to string and uses it as a buffer" do
- buf = "ABCDE"
+ buf = +"ABCDE"
(obj = mock("buff")).should_receive(:to_str).any_number_of_times.and_return(buf)
@file.sysread(15, obj).should == buf
buf.should == "012345678901234"
@@ -50,6 +50,11 @@ describe "IO#sysread on a file" do
@file.sysread(5).should == "56789"
end
+ it "raises an error when called after buffered reads" do
+ @file.readline
+ -> { @file.sysread(5) }.should.raise(IOError)
+ end
+
it "reads normally even when called immediately after a buffered IO#read" do
@file.read(15)
@file.sysread(5).should == "56789"
@@ -58,13 +63,13 @@ describe "IO#sysread on a file" do
it "does not raise error if called after IO#read followed by IO#write" do
@file.read(5)
@file.write("abcde")
- -> { @file.sysread(5) }.should_not raise_error(IOError)
+ -> { @file.sysread(5) }.should_not.raise(IOError)
end
it "does not raise error if called after IO#read followed by IO#syswrite" do
@file.read(5)
@file.syswrite("abcde")
- -> { @file.sysread(5) }.should_not raise_error(IOError)
+ -> { @file.sysread(5) }.should_not.raise(IOError)
end
it "reads updated content after the flushed buffered IO#write" do
@@ -77,7 +82,37 @@ describe "IO#sysread on a file" do
end
it "raises IOError on closed stream" do
- -> { IOSpecs.closed_io.sysread(5) }.should raise_error(IOError)
+ -> { IOSpecs.closed_io.sysread(5) }.should.raise(IOError)
+ end
+
+ it "immediately returns an empty string if the length argument is 0" do
+ @file.sysread(0).should == ""
+ end
+
+ it "immediately returns the given buffer if the length argument is 0" do
+ buffer = +"existing content"
+ @file.sysread(0, buffer).should == buffer
+ buffer.should == "existing content"
+ end
+
+ it "discards the existing buffer content upon successful read" do
+ buffer = +"existing content"
+ @file.sysread(11, buffer).should.equal?(buffer)
+ buffer.should == "01234567890"
+ end
+
+ it "discards the existing buffer content upon error" do
+ buffer = +"existing content"
+ @file.seek(0, :END)
+ -> { @file.sysread(1, buffer) }.should.raise(EOFError)
+ buffer.should.empty?
+ end
+
+ it "preserves the encoding of the given buffer" do
+ buffer = ''.encode(Encoding::ISO_8859_1)
+ string = @file.sysread(10, buffer)
+
+ buffer.encoding.should == Encoding::ISO_8859_1
end
end
@@ -95,4 +130,8 @@ describe "IO#sysread" do
@write.syswrite "ab"
@read.sysread(3).should == "ab"
end
+
+ it "raises ArgumentError when length is less than 0" do
+ -> { @read.sysread(-1) }.should.raise(ArgumentError)
+ end
end