summaryrefslogtreecommitdiff
path: root/test/stringio/test_stringio.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/stringio/test_stringio.rb')
-rw-r--r--test/stringio/test_stringio.rb118
1 files changed, 108 insertions, 10 deletions
diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb
index 3fcb20e1fa..9031650581 100644
--- a/test/stringio/test_stringio.rb
+++ b/test/stringio/test_stringio.rb
@@ -14,14 +14,19 @@ class TestStringIO < Test::Unit::TestCase
include TestEOF::Seek
+ def test_version
+ assert_kind_of(String, StringIO::VERSION)
+ end
+
def test_initialize
assert_kind_of StringIO, StringIO.new
assert_kind_of StringIO, StringIO.new('str')
assert_kind_of StringIO, StringIO.new('str', 'r+')
+ assert_kind_of StringIO, StringIO.new(nil)
assert_raise(ArgumentError) { StringIO.new('', 'x') }
assert_raise(ArgumentError) { StringIO.new('', 'rx') }
assert_raise(ArgumentError) { StringIO.new('', 'rbt') }
- assert_raise(TypeError) { StringIO.new(nil) }
+ assert_raise(TypeError) { StringIO.new(Object) }
o = Object.new
def o.to_str
@@ -36,14 +41,21 @@ class TestStringIO < Test::Unit::TestCase
assert_kind_of StringIO, StringIO.new(o)
end
+ def test_null
+ io = StringIO.new(nil)
+ assert_nil io.gets
+ io.puts "abc"
+ assert_nil io.string
+ end
+
def test_truncate
io = StringIO.new("")
io.puts "abc"
- io.truncate(0)
+ assert_equal(0, io.truncate(0))
io.puts "def"
assert_equal("\0\0\0\0def\n", io.string, "[ruby-dev:24190]")
assert_raise(Errno::EINVAL) { io.truncate(-1) }
- io.truncate(10)
+ assert_equal(0, io.truncate(10))
assert_equal("\0\0\0\0def\n\0\0", io.string)
end
@@ -84,6 +96,14 @@ class TestStringIO < Test::Unit::TestCase
assert_string("", Encoding::UTF_8, StringIO.new("foo").gets(0))
end
+ def test_gets_utf_16
+ stringio = StringIO.new("line1\nline2\nline3\n".encode("utf-16le"))
+ assert_equal("line1\n".encode("utf-16le"), stringio.gets)
+ assert_equal("line2\n".encode("utf-16le"), stringio.gets)
+ assert_equal("line3\n".encode("utf-16le"), stringio.gets)
+ assert_nil(stringio.gets)
+ end
+
def test_gets_chomp
assert_equal(nil, StringIO.new("").gets(chomp: true))
assert_equal("", StringIO.new("\n").gets(chomp: true))
@@ -99,6 +119,8 @@ class TestStringIO < Test::Unit::TestCase
assert_equal("def\n", stringio.gets("", chomp: true))
assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true))
+
+ assert_equal("", StringIO.new("ab").gets("ab", chomp: true))
end
def test_gets_chomp_eol
@@ -223,7 +245,7 @@ class TestStringIO < Test::Unit::TestCase
def test_write_integer_overflow
f = StringIO.new
- f.pos = RbConfig::LIMITS["LONG_MAX"]
+ f.pos = StringIO::MAX_LENGTH
assert_raise(ArgumentError) {
f.write("pos + len overflows")
}
@@ -678,6 +700,18 @@ class TestStringIO < Test::Unit::TestCase
s.force_encoding(Encoding::US_ASCII)
assert_same(s, f.read(nil, s))
assert_string("", Encoding::UTF_8, s, bug13806)
+
+ bug20418 = '[Bug #20418] ™€®'.b
+ f = StringIO.new(bug20418)
+ s = ""
+ assert_equal(Encoding::UTF_8, s.encoding, bug20418)
+ f.read(4, s)
+ assert_equal(Encoding::UTF_8, s.encoding, bug20418)
+
+ f.rewind
+ s = ""
+ f.read(nil, s)
+ assert_equal(Encoding::ASCII_8BIT, s.encoding, bug20418)
end
def test_readpartial
@@ -689,8 +723,8 @@ class TestStringIO < Test::Unit::TestCase
assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size))
f.rewind
# not empty buffer
- s = '0123456789'
- assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size, s))
+ s = '0123456789'.b
+ assert_equal("\u3042\u3044".b, f.readpartial(f.size, s))
end
def test_read_nonblock
@@ -714,8 +748,8 @@ class TestStringIO < Test::Unit::TestCase
assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
f.rewind
# not empty buffer
- s = '0123456789'
- assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size, s))
+ s = '0123456789'.b
+ assert_equal("\u3042\u3044".b, f.read_nonblock(f.size, s))
end
def test_sysread
@@ -727,6 +761,32 @@ class TestStringIO < Test::Unit::TestCase
assert_equal Encoding::ASCII_8BIT, f.sysread(3).encoding
end
+ def test_pread
+ f = StringIO.new("pread")
+ f.read
+
+ assert_equal "pre".b, f.pread(3, 0)
+ assert_equal "read".b, f.pread(4, 1)
+ assert_equal Encoding::ASCII_8BIT, f.pread(4, 1).encoding
+
+ buf = "".b
+ f.pread(3, 0, buf)
+ assert_equal "pre".b, buf
+ f.pread(4, 1, buf)
+ assert_equal "read".b, buf
+
+ assert_raise(EOFError) { f.pread(1, 5) }
+ assert_raise(ArgumentError) { f.pread(-1, 0) }
+ assert_raise(Errno::EINVAL) { f.pread(3, -1) }
+
+ assert_equal "".b, StringIO.new("").pread(0, 0)
+ assert_equal "".b, StringIO.new("").pread(0, -10)
+
+ buf = "stale".b
+ assert_equal "stale".b, StringIO.new("").pread(0, 0, buf)
+ assert_equal "stale".b, buf
+ end
+
def test_size
f = StringIO.new("1234")
assert_equal(4, f.size)
@@ -859,8 +919,9 @@ class TestStringIO < Test::Unit::TestCase
end
def test_overflow
- return if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"]
- limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10
+ intptr_max = RbConfig::LIMITS["INTPTR_MAX"]
+ return if intptr_max > StringIO::MAX_LENGTH
+ limit = intptr_max - 0x10
assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
begin;
limit = #{limit}
@@ -913,6 +974,43 @@ class TestStringIO < Test::Unit::TestCase
$VERBOSE = verbose
end
+ def test_coderange_after_overwrite
+ s = StringIO.new("".b)
+
+ s.write("a=b&c=d")
+ s.rewind
+ assert_predicate(s.string, :ascii_only?)
+ s.write "\u{431 43e 433 443 441}"
+ assert_not_predicate(s.string, :ascii_only?)
+
+ s = StringIO.new("\u{3042}")
+ s.rewind
+ assert_not_predicate(s.string, :ascii_only?)
+ s.write('aaaa')
+ assert_predicate(s.string, :ascii_only?)
+ end
+
+ if eval(%{ "test".frozen? && !"test".equal?("test") }) # Ruby 3.4+ chilled strings
+ def test_chilled_string
+ chilled_string = eval(%{""})
+ io = StringIO.new(chilled_string)
+ assert_warning(/literal string will be frozen/) { io << "test" }
+ assert_equal("test", io.string)
+ assert_same(chilled_string, io.string)
+ end
+
+ def test_chilled_string_string_set
+ io = StringIO.new
+ chilled_string = eval(%{""})
+ io.string = chilled_string
+ assert_warning(/literal string will be frozen/) { io << "test" }
+ assert_equal("test", io.string)
+ assert_same(chilled_string, io.string)
+ end
+ end
+
+ private
+
def assert_string(content, encoding, str, mesg = nil)
assert_equal([content, encoding], [str, str.encoding], mesg)
end