2003-11-18 12:23:07 +03:00
|
|
|
require 'test/unit'
|
|
|
|
require 'stringio'
|
2003-12-05 05:54:48 +03:00
|
|
|
dir = File.expand_path(__FILE__)
|
|
|
|
2.times {dir = File.dirname(dir)}
|
|
|
|
$:.replace([File.join(dir, "ruby")] | $:)
|
|
|
|
require 'ut_eof'
|
2003-11-18 12:23:07 +03:00
|
|
|
|
|
|
|
class TestStringIO < Test::Unit::TestCase
|
2003-12-05 05:54:48 +03:00
|
|
|
include TestEOF
|
|
|
|
def open_file(content)
|
|
|
|
f = StringIO.new(content)
|
|
|
|
yield f
|
2003-11-18 12:23:07 +03:00
|
|
|
end
|
2003-12-09 08:35:16 +03:00
|
|
|
alias open_file_rw open_file
|
2003-12-10 11:16:14 +03:00
|
|
|
|
|
|
|
include TestEOF::Seek
|
2004-09-07 09:32:26 +04:00
|
|
|
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_truncate
|
2004-09-07 09:32:26 +04:00
|
|
|
io = StringIO.new("")
|
|
|
|
io.puts "abc"
|
|
|
|
io.truncate(0)
|
|
|
|
io.puts "def"
|
2007-11-18 10:18:56 +03:00
|
|
|
assert_equal("\0\0\0\0def\n", io.string, "[ruby-dev:24190]")
|
2004-09-07 09:32:26 +04:00
|
|
|
end
|
|
|
|
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_seek_beyond_eof
|
2004-09-07 09:32:26 +04:00
|
|
|
io = StringIO.new
|
|
|
|
n = 100
|
|
|
|
io.seek(n)
|
|
|
|
io.print "last"
|
2007-11-18 10:18:56 +03:00
|
|
|
assert_equal("\0" * n + "last", io.string, "[ruby-dev:24194]")
|
2004-09-07 09:32:26 +04:00
|
|
|
end
|
2004-11-29 10:06:21 +03:00
|
|
|
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_overwrite
|
2004-11-29 10:06:21 +03:00
|
|
|
stringio = StringIO.new
|
|
|
|
responses = ['', 'just another ruby', 'hacker']
|
|
|
|
responses.each do |resp|
|
|
|
|
stringio.puts(resp)
|
|
|
|
stringio.rewind
|
|
|
|
end
|
2007-11-18 10:18:56 +03:00
|
|
|
assert_equal("hacker\nother ruby\n", stringio.string, "[ruby-core:3836]")
|
2004-11-29 10:06:21 +03:00
|
|
|
end
|
2003-11-18 12:23:07 +03:00
|
|
|
end
|