Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/io/binmode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
@io.binmode
@io.internal_encoding.should == nil
end

it "disables newline conversion for reading" do
data = "line1\r\nline2\r\n"

@io = new_io(@name, "wb")
@io.write(data)
@io.close

@io = new_io(@name, "rt")
@io.set_encoding("utf-8:ISO-8859-1", newline: :universal)
@io.binmode
@io.read.should == data
end
end

describe "IO#binmode?" do
Expand Down
16 changes: 16 additions & 0 deletions core/io/getbyte_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
it "raises an IOError on closed stream" do
-> { IOSpecs.closed_io.getbyte }.should.raise(IOError)
end

it "reads after ungetc without character conversion" do
@io.set_encoding("utf-8")
c = @io.getc
@io.ungetc(c)
@io.getbyte.should == 86
end

it "raises an exception after ungetc with character conversion" do
@io.set_encoding("utf-8:utf-16be")
c = @io.getc
@io.ungetc(c)
-> do
@io.getbyte
end.should.raise(IOError, "byte oriented read for character buffered IO")
end
end

describe "IO#getbyte" do
Expand Down
21 changes: 21 additions & 0 deletions core/io/internal_encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@
@io = new_io @name, "#{@object}:binary"
@io.internal_encoding.should == nil
end

it "returns nil when the external encoding is BINARY and internal encoding is set" do
@io = new_io @name, "#{@object}:binary:ibm437"
@io.internal_encoding.should == nil
end

ruby_version_is ""..."3.3" do
it "returns the value set by #set_encoding when the external encoding is BINARY" do
@io = new_io @name, @object
@io.set_encoding(Encoding::BINARY, Encoding::IBM437)
@io.internal_encoding.should.equal?(Encoding::IBM437)
end
end

ruby_version_is "3.3" do
it "ignores the value set by #set_encoding when the external encoding is BINARY and returns nil" do
@io = new_io @name, @object
@io.set_encoding(Encoding::BINARY, Encoding::IBM437)
@io.internal_encoding.should == nil
end
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions core/io/pwrite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@
}.should.raise(NoMethodError, /undefined method [`']to_s'/)
end

platform_is_not :windows do
it "raises a Errno::EINVAL if the offset is invalid" do
-> {
@file.pwrite("foo", -1)
}.should.raise(Errno::EINVAL)
end
end

it "raises a TypeError if the offset cannot be converted to an Integer" do
-> {
@file.pwrite("foo", Object.new)
Expand Down
14 changes: 12 additions & 2 deletions core/io/read_nonblock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@
@read.read_nonblock(3).should == "bar"
end

it "raises an exception after ungetc with data in the buffer and character conversion enabled" do
it "raises an exception after ungetc with character conversion enabled" do
@write.write("foobar")
@read.set_encoding(
'utf-8', universal_newline: true
)
c = @read.getc
@read.ungetc(c)
-> { @read.read_nonblock(3).should == "foo" }.should.raise(IOError)
-> do
@read.read_nonblock(3)
end.should.raise(IOError, "byte oriented read for character buffered IO")
end

it "returns less data if that is all that is available" do
Expand Down Expand Up @@ -137,6 +139,14 @@
-> { @read.read_nonblock(5) }.should.raise(EOFError)
end

ruby_bug "#18421", ""..."3.0.4" do
it "clears and returns the given buffer if the length argument is 0" do
buffer = String.new("existing content")
@read.read_nonblock(0, buffer).should == buffer
buffer.should == ""
end
end

it "preserves the encoding of the given buffer" do
buffer = ''.encode(Encoding::ISO_8859_1)
@write.write("abc")
Expand Down
38 changes: 38 additions & 0 deletions core/io/read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,12 @@
@io.read.encoding.should.equal?(Encoding::EUC_JP)
end

it "reads after ungetc" do
c = @io.getc
@io.ungetc(c)
@io.read(2).should == [164, 162].pack('C*').force_encoding(Encoding::BINARY)
end

it_behaves_like :io_read_size_internal_encoding, nil
end

Expand All @@ -680,6 +686,14 @@
@io = IOSpecs.io_fixture "read_euc_jp.txt", "r:euc-jp:utf-8"
end

it "raises an exception after ungetc" do
c = @io.getc
@io.ungetc(c)
-> do
@io.read(2)
end.should.raise(IOError, "byte oriented read for character buffered IO")
end

it_behaves_like :io_read_internal_encoding, nil
it_behaves_like :io_read_size_internal_encoding, nil
end
Expand All @@ -689,6 +703,14 @@
@io = IOSpecs.io_fixture "read_euc_jp.txt", mode: "r:euc-jp:utf-8"
end

it "raises an exception after ungetc" do
c = @io.getc
@io.ungetc(c)
-> do
@io.read(2)
end.should.raise(IOError, "byte oriented read for character buffered IO")
end

it_behaves_like :io_read_internal_encoding, nil
it_behaves_like :io_read_size_internal_encoding, nil
end
Expand All @@ -701,6 +723,14 @@
@io = IOSpecs.io_fixture "read_euc_jp.txt", options
end

it "raises an exception after ungetc" do
c = @io.getc
@io.ungetc(c)
-> do
@io.read(2)
end.should.raise(IOError, "byte oriented read for character buffered IO")
end

it_behaves_like :io_read_internal_encoding, nil
it_behaves_like :io_read_size_internal_encoding, nil
end
Expand All @@ -711,6 +741,14 @@
@io = IOSpecs.io_fixture "read_euc_jp.txt", options
end

it "raises an exception after ungetc" do
c = @io.getc
@io.ungetc(c)
-> do
@io.read(2)
end.should.raise(IOError, "byte oriented read for character buffered IO")
end

it_behaves_like :io_read_internal_encoding, nil
it_behaves_like :io_read_size_internal_encoding, nil
end
Expand Down
16 changes: 16 additions & 0 deletions core/io/readbyte_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@
@io.readbyte
end.should.raise EOFError
end

it "reads after ungetc without character conversion" do
@io.set_encoding("utf-8")
c = @io.getc
@io.ungetc(c)
@io.readbyte.should == ?r.getbyte(0)
end

it "raises an exception after ungetc with character conversion" do
@io.set_encoding("utf-8:utf-16be")
c = @io.getc
@io.ungetc(c)
-> do
@io.readbyte
end.should.raise(IOError, "byte oriented read for character buffered IO")
end
end
10 changes: 10 additions & 0 deletions core/io/readpartial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
@rd.readpartial(2).should == "b"
end

it "raises an exception after ungetc with character conversion enabled" do
@wr.write("foobar")
@rd.set_encoding('utf-8', 'ascii')
c = @rd.getc
@rd.ungetc(c)
-> do
@rd.readpartial(3)
end.should.raise(IOError, "byte oriented read for character buffered IO")
end

it "discards the existing buffer content upon successful read" do
buffer = +"existing content"
@wr.write("hello world")
Expand Down
51 changes: 51 additions & 0 deletions core/io/reopen_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
@io.close
-> { @io.reopen(STDOUT) }.should.raise(IOError)
end

it "raises ArgumentError when too many arguments are given" do
-> { @io.reopen(@other_name, "r", "excess argument") }.should.raise(ArgumentError)
end
end

describe "IO#reopen with a String" do
Expand Down Expand Up @@ -170,6 +174,53 @@
@io.reopen(@other_name)
File.should.exist?(@other_name)
end

it "opens the file in read mode if the IO is read-only" do
touch(@name) { |f| f.write "original data" }
touch(@other_name) { |f| f.write "new data" }
@io = new_io @name, "r"

@io.reopen(@other_name)
-> { @io.write("overwrite content") }.should.raise(IOError)
@io.read.should == "new data"
end

it "opens the file in write mode if the IO is write-only" do
touch(@name) { |f| f.write "original data" }
touch(@other_name) { |f| f.write "new data" }
@io = new_io @name, "w"

@io.reopen(@other_name)
-> { @io.read }.should.raise(IOError)
@io.write("overwrite content").should == 17
@io.close
File.read(@other_name).should == "overwrite content"
end

it "opens the file in read-write mode if the IO is read-write" do
touch(@name) { |f| f.write "original data" }
touch(@other_name) { |f| f.write "new data" }
@io = new_io @name, "r+"

@io.reopen(@other_name)
@io.read.should == "new data"
@io.rewind
@io.write("overwrite content").should == 17
@io.close
File.read(@other_name).should == "overwrite content"
end

it "opens the file in append mode if the IO appends" do
touch(@name) { |f| f.write "original data" }
touch(@other_name) { |f| f.write "new data" }
@io = new_io @name, "a"

@io.reopen(@other_name)
-> { @io.read }.should.raise(IOError)
@io.write("overwrite content").should == 17
@io.close
File.read(@other_name).should == "new dataoverwrite content"
end
end

describe "IO#reopen with a String" do
Expand Down
13 changes: 13 additions & 0 deletions core/io/seek_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@
value[-1].should == @io.read[0]
end

ruby_bug "#20919", "" ... "3.4" do
it "clears the character buffer" do
@io.ungetc("a")
@io.seek(1, IO::SEEK_SET)
@io.getc.should == "o"

@io.set_encoding(Encoding::UTF_8, Encoding::UTF_16LE)
@io.ungetc("a".encode(Encoding::UTF_16LE))
@io.seek(1, IO::SEEK_SET)
@io.getc.should == "o".encode(Encoding::UTF_16LE)
end
end

platform_is :darwin do
it "supports seek offsets greater than 2^32" do
begin
Expand Down
Loading