diff options
Diffstat (limited to 'test/stringio/test_stringio.rb')
-rw-r--r-- | test/stringio/test_stringio.rb | 185 |
1 files changed, 158 insertions, 27 deletions
diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index e0b4504b54..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)) @@ -92,13 +112,15 @@ class TestStringIO < Test::Unit::TestCase assert_equal("a", StringIO.new("a").gets(chomp: true)) assert_equal("a", StringIO.new("a\nb").gets(chomp: true)) assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true)) - assert_equal("abc\n\ndef", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true)) - assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets("", chomp: true)) + assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true)) + assert_equal("abc", StringIO.new("abc\n\ndef\n").gets("", chomp: true)) stringio = StringIO.new("abc\n\ndef\n") - assert_equal("abc\n", stringio.gets("", chomp: true)) - assert_equal("def", stringio.gets("", chomp: true)) + assert_equal("abc", stringio.gets("", chomp: true)) + 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 @@ -109,11 +131,11 @@ class TestStringIO < Test::Unit::TestCase assert_equal("a", StringIO.new("a").gets(chomp: true)) assert_equal("a", StringIO.new("a\r\nb").gets(chomp: true)) assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true)) - assert_equal("abc\r\n\r\ndef", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true)) - assert_equal("abc\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true)) + assert_equal("abc\r\n\r\ndef\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true)) + assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true)) stringio = StringIO.new("abc\r\n\r\ndef\r\n") - assert_equal("abc\r\n", stringio.gets("", chomp: true)) - assert_equal("def", stringio.gets("", chomp: true)) + assert_equal("abc", stringio.gets("", chomp: true)) + assert_equal("def\r\n", stringio.gets("", chomp: true)) end def test_readlines @@ -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") } @@ -256,6 +278,12 @@ class TestStringIO < Test::Unit::TestCase f.set_encoding(Encoding::ASCII_8BIT) } assert_equal("foo\x83".b, f.gets) + + f = StringIO.new() + f.set_encoding("ISO-8859-16:ISO-8859-1") + assert_equal(Encoding::ISO_8859_16, f.external_encoding) + assert_equal(Encoding::ISO_8859_16, f.string.encoding) + assert_nil(f.internal_encoding) end def test_mode_error @@ -584,20 +612,30 @@ class TestStringIO < Test::Unit::TestCase end end + def test_each_string_sep + f = StringIO.new('a||b||c') + assert_equal(["a||", "b||", "c"], f.each("||").to_a) + f.rewind + assert_equal(["a", "b", "c"], f.each("||", chomp: true).to_a) + end + def test_each f = StringIO.new("foo\nbar\nbaz\n") assert_equal(["foo\n", "bar\n", "baz\n"], f.each.to_a) f.rewind assert_equal(["foo", "bar", "baz"], f.each(chomp: true).to_a) - f = StringIO.new("foo\nbar\n\nbaz\n") - assert_equal(["foo\nbar\n\n", "baz\n"], f.each("").to_a) + f = StringIO.new("foo\nbar\n\n\nbaz\n") + assert_equal(["foo\nbar\n\n\n", "baz\n"], f.each("").to_a) f.rewind - assert_equal(["foo\nbar\n", "baz"], f.each("", chomp: true).to_a) + assert_equal(["foo\nbar", "baz\n"], f.each("", chomp: true).to_a) - f = StringIO.new("foo\r\nbar\r\n\r\nbaz\r\n") - assert_equal(["foo\r\nbar\r\n\r\n", "baz\r\n"], f.each("").to_a) + f = StringIO.new("foo\r\nbar\r\n\r\n\r\nbaz\r\n") + assert_equal(["foo\r\nbar\r\n\r\n\r\n", "baz\r\n"], f.each("").to_a) f.rewind - assert_equal(["foo\r\nbar\r\n", "baz"], f.each("", chomp: true).to_a) + assert_equal(["foo\r\nbar", "baz\r\n"], f.each("", chomp: true).to_a) + + f = StringIO.new("abc\n\ndef\n") + assert_equal(["ab", "c\n", "\nd", "ef", "\n"], f.each(nil, 2, chomp: true).to_a) end def test_putc @@ -662,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 @@ -673,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 @@ -698,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 @@ -711,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) @@ -757,6 +833,15 @@ class TestStringIO < Test::Unit::TestCase assert_equal("b""\0""a", s.string) end + def test_ungetc_fill + count = 100 + s = StringIO.new + s.print 'a' * count + s.ungetc('b' * (count * 5)) + assert_equal((count * 5), s.string.size) + assert_match(/\Ab+\z/, s.string) + end + def test_ungetbyte_pos b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011' s = StringIO.new( b ) @@ -782,6 +867,15 @@ class TestStringIO < Test::Unit::TestCase assert_equal("b""\0""a", s.string) end + def test_ungetbyte_fill + count = 100 + s = StringIO.new + s.print 'a' * count + s.ungetbyte('b' * (count * 5)) + assert_equal((count * 5), s.string.size) + assert_match(/\Ab+\z/, s.string) + end + def test_frozen s = StringIO.new s.freeze @@ -825,18 +919,18 @@ class TestStringIO < Test::Unit::TestCase end def test_overflow - omit 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} ary = [] - while true + begin x = "a"*0x100000 break if [x].pack("p").unpack("i!")[0] < 0 ary << x - omit if ary.size > 100 - end + end while ary.size <= 100 s = StringIO.new(x) s.gets("xxx", limit) assert_equal(0x100000, s.pos) @@ -880,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 |