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