1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/csv/test_csv.rb: close opened files for CSV::IOBuf explicitly. opened

file cannot be removed under win32 box.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4497 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nahi 2003-09-04 16:06:41 +00:00
parent e168b64b03
commit 822a72b8ee
2 changed files with 182 additions and 147 deletions

View file

@ -1,3 +1,8 @@
Wed Sep 5 01:03:59 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
* test/csv/test_csv.rb: close opened files for CSV::IOBuf explicitly.
opened file cannot be removed under win32 box.
Thu Sep 4 23:59:40 2003 Nobuyoshi Nakada <nobu@ruby-lang.org> Thu Sep 4 23:59:40 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (tokadd_string): newlines have no special meanings in * parse.y (tokadd_string): newlines have no special meanings in

View file

@ -2,7 +2,7 @@ require 'test/unit'
require 'tempfile' require 'tempfile'
require 'fileutils' require 'fileutils'
require 'csv' require '../lib/csv'
class CSV class CSV
class StreamBuf class StreamBuf
@ -381,7 +381,8 @@ public
reader = CSV::Reader.create("abc") reader = CSV::Reader.create("abc")
assert_instance_of(CSV::StringReader, reader, "With a String") assert_instance_of(CSV::StringReader, reader, "With a String")
reader = CSV::Reader.create(File.open(@infile, "rb")) file = File.open(@infile, "rb")
reader = CSV::Reader.create(file)
assert_instance_of(CSV::IOReader, reader, 'With an IO') assert_instance_of(CSV::IOReader, reader, 'With an IO')
obj = Object.new obj = Object.new
@ -398,6 +399,7 @@ public
# fors other tests. # fors other tests.
reader = CSV::Reader.create(Tempfile.new("in.csv")) reader = CSV::Reader.create(Tempfile.new("in.csv"))
assert_instance_of(CSV::IOReader, reader, "With an pseudo IO.") assert_instance_of(CSV::IOReader, reader, "With an pseudo IO.")
file.close
end end
def test_Reader_s_parse def test_Reader_s_parse
@ -521,6 +523,7 @@ public
writer = CSV::BasicWriter.create(f) writer = CSV::BasicWriter.create(f)
writer.close writer.close
assert(!f.closed?) assert(!f.closed?)
f.close
f = File.open(@outfile, "w") f = File.open(@outfile, "w")
writer = CSV::BasicWriter.create(f) writer = CSV::BasicWriter.create(f)
@ -1093,10 +1096,12 @@ public
assert_equal([["foo"], ["bar"]], rows) assert_equal([["foo"], ["bar"]], rows)
rows = [] rows = []
CSV::Reader.parse(File.open(@bomfile).read) do |row| file = File.open(@bomfile)
CSV::Reader.parse(file.read) do |row|
rows << row.to_a rows << row.to_a
end end
assert_equal([["foo"], ["bar"]], rows) assert_equal([["foo"], ["bar"]], rows)
file.close
end end
@ -1122,7 +1127,15 @@ public
File.open(@outfile, "wb") do |f| File.open(@outfile, "wb") do |f|
f << (InputStreamPattern * m)[0, size] f << (InputStreamPattern * m)[0, size]
end end
CSV::IOBuf.new(File.open(@outfile, "rb")) file = File.open(@outfile, "rb")
buf = CSV::IOBuf.new(file)
if block_given?
yield(buf)
file.close
nil
else
buf
end
end end
def setBufSize(size) def setBufSize(size)
@ -1227,37 +1240,41 @@ public
end end
def test_StreamBuf_AREF # '[idx]' def test_StreamBuf_AREF # '[idx]'
s = setupInputStream(22, 1024) setupInputStream(22, 1024) do |s|
[0, 1, 9, 10, 19, 20, 21].each do |idx| [0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expChar(idx), s[idx], idx.to_s) assert_equal(expChar(idx), s[idx], idx.to_s)
end end
[22, 23].each do |idx| [22, 23].each do |idx|
assert_equal(nil, s[idx], idx.to_s) assert_equal(nil, s[idx], idx.to_s)
end end
assert_equal(nil, s[-1]) assert_equal(nil, s[-1])
s = setupInputStream(22, 1)
[0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expChar(idx), s[idx], idx.to_s)
end
[22, 23].each do |idx|
assert_equal(nil, s[idx], idx.to_s)
end end
s = setupInputStream(1024, 1) setupInputStream(22, 1) do |s|
[1023, 0].each do |idx| [0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expChar(idx), s[idx], idx.to_s) assert_equal(expChar(idx), s[idx], idx.to_s)
end end
[1024, 1025].each do |idx| [22, 23].each do |idx|
assert_equal(nil, s[idx], idx.to_s) assert_equal(nil, s[idx], idx.to_s)
end
end end
s = setupInputStream(1, 1) setupInputStream(1024, 1) do |s|
[0].each do |idx| [1023, 0].each do |idx|
assert_equal(expChar(idx), s[idx], idx.to_s) assert_equal(expChar(idx), s[idx], idx.to_s)
end
[1024, 1025].each do |idx|
assert_equal(nil, s[idx], idx.to_s)
end
end end
[1, 2].each do |idx|
assert_equal(nil, s[idx], idx.to_s) setupInputStream(1, 1) do |s|
[0].each do |idx|
assert_equal(expChar(idx), s[idx], idx.to_s)
end
[1, 2].each do |idx|
assert_equal(nil, s[idx], idx.to_s)
end
end end
end end
@ -1266,153 +1283,165 @@ public
assert_equal("", "abc"[3, 1]) assert_equal("", "abc"[3, 1])
assert_equal(nil, "abc"[4, 1]) assert_equal(nil, "abc"[4, 1])
s = setupInputStream(22, 1024) setupInputStream(22, 1024) do |s|
[0, 1, 9, 10, 19, 20, 21].each do |idx| [0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s)
end
assert_equal("", s[22, 1])
assert_equal(nil, s[23, 1])
end end
assert_equal("", s[22, 1])
assert_equal(nil, s[23, 1])
s = setupInputStream(22, 1) setupInputStream(22, 1) do |s|
[0, 1, 9, 10, 19, 20, 21].each do |idx| [0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s)
end
assert_equal("", s[22, 1])
assert_equal(nil, s[23, 1])
end end
assert_equal("", s[22, 1])
assert_equal(nil, s[23, 1])
s = setupInputStream(1024, 1) setupInputStream(1024, 1) do |s|
[1023, 0].each do |idx| [1023, 0].each do |idx|
assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s)
end
assert_equal("", s[1024, 1])
assert_equal(nil, s[1025, 1])
end end
assert_equal("", s[1024, 1])
assert_equal(nil, s[1025, 1])
s = setupInputStream(1, 1) setupInputStream(1, 1) do |s|
[0].each do |idx| [0].each do |idx|
assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s)
end
assert_equal("", s[1, 1])
assert_equal(nil, s[2, 1])
end end
assert_equal("", s[1, 1])
assert_equal(nil, s[2, 1])
s = setupInputStream(22, 11) setupInputStream(22, 11) do |s|
[0, 1, 10, 11, 20].each do |idx| [0, 1, 10, 11, 20].each do |idx|
assert_equal(expStr(idx, 2), s[idx, 2], idx.to_s) assert_equal(expStr(idx, 2), s[idx, 2], idx.to_s)
end
assert_equal(expStr(21, 1), s[21, 2])
assert_equal(expStr(0, 12), s[0, 12])
assert_equal(expStr(10, 12), s[10, 12])
assert_equal(expStr(10, 12), s[10, 13])
assert_equal(expStr(10, 12), s[10, 14])
assert_equal(expStr(10, 12), s[10, 1024])
assert_equal(nil, s[0, -1])
assert_equal(nil, s[21, -1])
assert_equal(nil, s[-1, 10])
assert_equal(nil, s[-1, -1])
end end
assert_equal(expStr(21, 1), s[21, 2])
assert_equal(expStr(0, 12), s[0, 12])
assert_equal(expStr(10, 12), s[10, 12])
assert_equal(expStr(10, 12), s[10, 13])
assert_equal(expStr(10, 12), s[10, 14])
assert_equal(expStr(10, 12), s[10, 1024])
assert_equal(nil, s[0, -1])
assert_equal(nil, s[21, -1])
assert_equal(nil, s[-1, 10])
assert_equal(nil, s[-1, -1])
end end
def test_StreamBuf_get def test_StreamBuf_get
s = setupInputStream(22, 1024) setupInputStream(22, 1024) do |s|
[0, 1, 9, 10, 19, 20, 21].each do |idx| [0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expChar(idx), s.get(idx), idx.to_s) assert_equal(expChar(idx), s.get(idx), idx.to_s)
end
[22, 23].each do |idx|
assert_equal(nil, s.get(idx), idx.to_s)
end
assert_equal(nil, s.get(-1))
end end
[22, 23].each do |idx|
assert_equal(nil, s.get(idx), idx.to_s)
end
assert_equal(nil, s.get(-1))
end end
def test_StreamBuf_get_n def test_StreamBuf_get_n
s = setupInputStream(22, 1024) setupInputStream(22, 1024) do |s|
[0, 1, 9, 10, 19, 20, 21].each do |idx| [0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expStr(idx, 1), s.get(idx, 1), idx.to_s) assert_equal(expStr(idx, 1), s.get(idx, 1), idx.to_s)
end end
assert_equal("", s.get(22, 1)) assert_equal("", s.get(22, 1))
assert_equal(nil, s.get(23, 1)) assert_equal(nil, s.get(23, 1))
assert_equal(nil, s.get(-1, 1)) assert_equal(nil, s.get(-1, 1))
assert_equal(nil, s.get(-1, -1)) assert_equal(nil, s.get(-1, -1))
end
end end
def test_StreamBuf_drop def test_StreamBuf_drop
s = setupInputStream(22, 1024) setupInputStream(22, 1024) do |s|
assert_equal(expChar(0), s[0]) assert_equal(expChar(0), s[0])
assert_equal(expChar(21), s[21]) assert_equal(expChar(21), s[21])
assert_equal(nil, s[22]) assert_equal(nil, s[22])
dropped = s.drop(-1) dropped = s.drop(-1)
assert_equal(0, dropped) assert_equal(0, dropped)
assert_equal(expChar(0), s[0]) assert_equal(expChar(0), s[0])
dropped = s.drop(0) dropped = s.drop(0)
assert_equal(0, dropped) assert_equal(0, dropped)
assert_equal(expChar(0), s[0]) assert_equal(expChar(0), s[0])
dropped = s.drop(1) dropped = s.drop(1)
assert_equal(1, dropped) assert_equal(1, dropped)
assert_equal(expChar(1), s[0]) assert_equal(expChar(1), s[0])
assert_equal(expChar(2), s[1]) assert_equal(expChar(2), s[1])
dropped = s.drop(1) dropped = s.drop(1)
assert_equal(1, dropped) assert_equal(1, dropped)
assert_equal(expChar(2), s[0]) assert_equal(expChar(2), s[0])
assert_equal(expChar(3), s[1]) assert_equal(expChar(3), s[1])
end
s = setupInputStream(4, 2) setupInputStream(4, 2) do |s|
dropped = s.drop(2) dropped = s.drop(2)
assert_equal(2, dropped) assert_equal(2, dropped)
assert_equal(expChar(2), s[0]) assert_equal(expChar(2), s[0])
assert_equal(expChar(3), s[1]) assert_equal(expChar(3), s[1])
dropped = s.drop(1) dropped = s.drop(1)
assert_equal(1, dropped) assert_equal(1, dropped)
assert_equal(expChar(3), s[0]) assert_equal(expChar(3), s[0])
assert_equal(nil, s[1]) assert_equal(nil, s[1])
dropped = s.drop(1) dropped = s.drop(1)
assert_equal(1, dropped) assert_equal(1, dropped)
assert_equal(nil, s[0]) assert_equal(nil, s[0])
assert_equal(nil, s[1]) assert_equal(nil, s[1])
dropped = s.drop(0) dropped = s.drop(0)
assert_equal(0, dropped) assert_equal(0, dropped)
assert_equal(nil, s[0]) assert_equal(nil, s[0])
assert_equal(nil, s[1]) assert_equal(nil, s[1])
end
s = setupInputStream(6, 3) setupInputStream(6, 3) do |s|
dropped = s.drop(2) dropped = s.drop(2)
assert_equal(2, dropped) assert_equal(2, dropped)
dropped = s.drop(2) dropped = s.drop(2)
assert_equal(2, dropped) assert_equal(2, dropped)
assert_equal(expChar(4), s[0]) assert_equal(expChar(4), s[0])
assert_equal(expChar(5), s[1]) assert_equal(expChar(5), s[1])
dropped = s.drop(3) dropped = s.drop(3)
assert_equal(2, dropped) assert_equal(2, dropped)
assert_equal(nil, s[0]) assert_equal(nil, s[0])
assert_equal(nil, s[1]) assert_equal(nil, s[1])
end
end end
def test_StreamBuf_is_eos? def test_StreamBuf_is_eos?
s = setupInputStream(3, 1024) setupInputStream(3, 1024) do |s|
assert(!s.is_eos?) assert(!s.is_eos?)
s.drop(1) s.drop(1)
assert(!s.is_eos?) assert(!s.is_eos?)
s.drop(1) s.drop(1)
assert(!s.is_eos?) assert(!s.is_eos?)
s.drop(1) s.drop(1)
assert(s.is_eos?) assert(s.is_eos?)
s.drop(1) s.drop(1)
assert(s.is_eos?) assert(s.is_eos?)
end
s = setupInputStream(3, 2) setupInputStream(3, 2) do |s|
assert(!s.is_eos?) assert(!s.is_eos?)
s.drop(1) s.drop(1)
assert(!s.is_eos?) assert(!s.is_eos?)
s.drop(1) s.drop(1)
assert(!s.is_eos?) assert(!s.is_eos?)
s.drop(1) s.drop(1)
assert(s.is_eos?) assert(s.is_eos?)
s.drop(1) s.drop(1)
assert(s.is_eos?) assert(s.is_eos?)
end
end end
def test_StreamBuf_s_new def test_StreamBuf_s_new
@ -1431,6 +1460,7 @@ public
iobuf = CSV::IOBuf.new(f) iobuf = CSV::IOBuf.new(f)
iobuf.close iobuf.close
assert(true) # iobuf.close does not raise any exception. assert(true) # iobuf.close does not raise any exception.
f.close
end end
def test_IOBuf_s_new def test_IOBuf_s_new