* test/zlib/test_zlib.rb (*): should close files associated with zlib.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30201 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
usa 2010-12-14 03:57:26 +00:00
Родитель 842110e6a6
Коммит b33cb3c017
2 изменённых файлов: 155 добавлений и 129 удалений

Просмотреть файл

@ -1,3 +1,7 @@
Tue Dec 14 12:55:46 2010 NAKAMURA Usaku <usa@ruby-lang.org>
* test/zlib/test_zlib.rb (*): should close files associated with zlib.
Tue Dec 14 11:30:17 2010 NAKAMURA Usaku <usa@ruby-lang.org>
* test/ruby/test_argf.rb (test_inplace_rename_impossible): unlink

Просмотреть файл

@ -233,7 +233,7 @@ if defined? Zlib
class TestZlibGzipFile < Test::Unit::TestCase
def test_to_io
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_to_io")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
@ -242,19 +242,20 @@ if defined? Zlib
end
def test_crc
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_crc")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
f.read
assert_equal(0x8c736521, f.crc)
end
end
def test_mtime
tim = Time.now
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_mtime")
t.close
Zlib::GzipWriter.open(t.path) do |gz|
gz.mtime = -1
@ -264,30 +265,33 @@ if defined? Zlib
assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
end
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal(tim.to_i, f.mtime.to_i)
end
end
def test_level
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_level")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
end
end
def test_os_code
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_os_code")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal(Zlib::OS_CODE, f.os_code)
end
end
def test_orig_name
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_orig_name")
t.close
Zlib::GzipWriter.open(t.path) do |gz|
gz.orig_name = "foobarbazqux\0quux"
@ -296,12 +300,13 @@ if defined? Zlib
assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
end
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal("foobarbazqux", f.orig_name)
end
end
def test_comment
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_comment")
t.close
Zlib::GzipWriter.open(t.path) do |gz|
gz.comment = "foobarbazqux\0quux"
@ -310,52 +315,55 @@ if defined? Zlib
assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
end
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal("foobarbazqux", f.comment)
end
end
def test_lineno
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_lineno")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal([0, "foo\n"], [f.lineno, f.gets])
assert_equal([1, "bar\n"], [f.lineno, f.gets])
f.lineno = 1000
assert_equal([1000, "baz\n"], [f.lineno, f.gets])
assert_equal([1001, "qux\n"], [f.lineno, f.gets])
end
end
def test_closed_p
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_closed_p")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal(false, f.closed?)
f.read
assert_equal(false, f.closed?)
f.close
assert_equal(true, f.closed?)
end
end
def test_sync
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_sync")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
f.sync = true
assert_equal(true, f.sync)
f.read
f.sync = false
assert_equal(false, f.sync)
f.close
end
end
def test_pos
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_pos")
t.close
Zlib::GzipWriter.open(t.path) do |gz|
gz.print("foo")
@ -365,7 +373,7 @@ if defined? Zlib
end
def test_path
t = Tempfile.new("test_zlib_gzip_file")
t = Tempfile.new("test_zlib_gzip_file_path")
t.close
gz = Zlib::GzipWriter.open(t.path)
@ -374,10 +382,11 @@ if defined? Zlib
gz.close
assert_equal(t.path, gz.path)
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal(t.path, f.path)
f.close
assert_equal(t.path, f.path)
end
s = ""
sio = StringIO.new(s)
@ -387,9 +396,9 @@ if defined? Zlib
gz.close
sio = StringIO.new(s)
f = Zlib::GzipReader.new(sio)
Zlib::GzipReader.new(sio) do |f|
assert_raise(NoMethodError) { f.path }
f.close
end
end
end
@ -429,7 +438,7 @@ if defined? Zlib
end
def test_open
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_open")
t.close
e = assert_raise(Zlib::GzipFile::Error) {
Zlib::GzipReader.open(t.path)
@ -456,156 +465,166 @@ if defined? Zlib
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
f = Zlib::GzipReader.open(t.path)
begin
assert_equal("foo", f.read)
ensure
f.close
end
end
def test_rewind
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_rewind")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo", f.read)
f.rewind
assert_equal("foo", f.read)
f.close
end
end
def test_unused
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_unused")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo", f.read(3))
f.unused
assert_equal("bar", f.read)
f.unused
f.close
end
end
def test_read
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_read")
t.close
str = "\u3042\u3044\u3046"
Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
f = Zlib::GzipReader.open(t.path, encoding: "UTF-8")
Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
assert_raise(ArgumentError) { f.read(-1) }
assert_equal(str, f.read)
end
end
def test_readpartial
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_readpartial")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert("foo".start_with?(f.readpartial(3)))
end
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
s = ""
f.readpartial(3, s)
assert("foo".start_with?(s))
assert_raise(ArgumentError) { f.readpartial(-1) }
end
end
def test_getc
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_getc")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
"foobar".each_char {|c| assert_equal(c, f.getc) }
assert_nil(f.getc)
end
end
def test_getbyte
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_getbyte")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
"foobar".each_byte {|c| assert_equal(c, f.getbyte) }
assert_nil(f.getbyte)
end
end
def test_readchar
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_readchar")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
"foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
assert_raise(EOFError) { f.readchar }
end
end
def test_each_byte
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_each_byte")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
a = []
f.each_byte {|c| a << c }
assert_equal("foobar".each_byte.to_a, a)
end
end
def test_gets2
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_gets2")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo\n", f.gets)
assert_equal("bar\n", f.gets)
assert_equal("baz\n", f.gets)
assert_nil(f.gets)
f.close
end
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo\nbar\nbaz\n", f.gets(nil))
f.close
end
end
def test_gets
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_gets")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo\n", f.readline)
assert_equal("bar\n", f.readline)
assert_equal("baz\n", f.readline)
assert_raise(EOFError) { f.readline }
f.close
end
end
def test_each
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_each")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
a = ["foo\n", "bar\n", "baz\n"]
f.each {|l| assert_equal(a.shift, l) }
f.close
end
end
def test_readlines
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_readlines")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path)
Zlib::GzipReader.open(t.path) do |f|
assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
f.close
end
end
def test_reader_wrap
t = Tempfile.new("test_zlib_gzip_reader")
t = Tempfile.new("test_zlib_gzip_reader_wrap")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = open(t.path)
@ -625,21 +644,24 @@ if defined? Zlib
def test_open
assert_raise(ArgumentError) { Zlib::GzipWriter.open }
t = Tempfile.new("test_zlib_gzip_writer")
t = Tempfile.new("test_zlib_gzip_writer_open")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
f = Zlib::GzipWriter.open(t.path)
begin
f.print("bar")
ensure
f.close
end
assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
end
def test_write
t = Tempfile.new("test_zlib_gzip_writer")
t = Tempfile.new("test_zlib_gzip_writer_write")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
@ -651,7 +673,7 @@ if defined? Zlib
end
def test_putc
t = Tempfile.new("test_zlib_gzip_writer")
t = Tempfile.new("test_zlib_gzip_writer_putc")
t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
@ -660,7 +682,7 @@ if defined? Zlib
end
def test_writer_wrap
t = Tempfile.new("test_zlib_gzip_writer")
t = Tempfile.new("test_zlib_gzip_writer_wrap")
Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
t.close
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })