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

* lib/csv.rb (IOReader, BasicWriter): call binmode when a given IO

respond_to?(:binmode).  record separator was wrong when you gave text mode IO
  to Reader.parse and Writer.generate.

* test/csv/test_csv.rb: add tests for above change.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4708 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nahi 2003-10-06 14:03:58 +00:00
parent 26d111540e
commit 11d2edffe6
3 changed files with 62 additions and 14 deletions

View file

@ -1,3 +1,11 @@
Mon Oct 6 22:59:46 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
* lib/csv.rb (IOReader, BasicWriter): call binmode when a given IO
respond_to?(:binmode). record separator was wrong when you gave
text mode IO to Reader.parse and Writer.generate.
* test/csv/test_csv.rb: add tests for above change.
Sun Oct 5 23:27:09 2003 Tanaka Akira <akr@m17n.org>
* ext/socket/extconf.rb: check recvmsg even if sendmsg is exists.
@ -60,13 +68,13 @@ Sun Oct 5 14:37:39 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
* test/soap/marshal/test_marshal.rb: ditto.
* test/soap/calc/test_calc_cgi.rb: add Config::CONFIG["EXEECT"] to
* test/soap/calc/test_calc_cgi.rb: add Config::CONFIG["EXEEXT"] to
RUBYBIN.
Sun Oct 5 13:47:22 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
* test/ruby/test_beginendblock.rb, test/ruby/beginmainend.rb: add tests
about scope, order and allowd syntax.
about scope, order and allowed syntax.
Sun Oct 5 11:54:29 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>

View file

@ -463,6 +463,7 @@ public
#
def initialize(io, col_sep = ?,, row_sep = nil)
@io = io
@io.binmode if @io.respond_to?(:binmode)
@col_sep = col_sep
@row_sep = row_sep
@dev = CSV::IOBuf.new(@io)
@ -551,8 +552,8 @@ public
# Create instance. To add CSV data to generate CSV string, see
# CSV::Writer#<< or CSV::Writer#add_row.
#
def Writer.create(str_or_readable, col_sep = ?,, row_sep = nil)
BasicWriter.new(str_or_readable, col_sep, row_sep)
def Writer.create(str_or_writable, col_sep = ?,, row_sep = nil)
BasicWriter.new(str_or_writable, col_sep, row_sep)
end
# SYNOPSIS
@ -675,6 +676,7 @@ public
@col_sep = col_sep
@row_sep = row_sep
@dev = str_or_writable
@dev.binmode if @dev.respond_to?(:binmode)
@close_on_terminate = false
end
@ -1036,7 +1038,7 @@ private
when :DT_COLSEP
out_dev << col_sep.chr
when :DT_ROWSEP
out_dev << (row_sep || "\r\n")
out_dev << (row_sep ? row_sep.chr : "\r\n")
end
end
end

View file

@ -409,6 +409,22 @@ public
file.close
end
def test_IOReader_s_create_binmode
file = File.open(@outfile, "wb")
file << "\"\r\n\",\"\r\",\"\n\"\r1,2,3"
file.close
file = File.open(@outfile, "r") # not "rb"
begin
reader = CSV::IOReader.new(file, ?,, ?\r)
assert_equal(["\r\n", "\r", "\n"], reader.shift.to_a)
assert_equal(["1", "2", "3"], reader.shift.to_a)
reader.close
ensure
file.close
end
end
def test_Reader_s_parse
ret = CSV::Reader.parse("a,b,c") { |row|
assert_instance_of(CSV::Row, row, "Block parameter")
@ -486,7 +502,9 @@ public
writer << [nil, 'e', 'f'] << [nil, nil, '']
end
str = file.open.read
file.open
file.binmode
str = file.read
assert_equal("a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal')
file = Tempfile.new("out2.csv")
@ -496,7 +514,9 @@ public
writer << [d(nil, true), d('e'), d('f')] << [d(nil, true), d(nil, true), d('')]
end
str = file.open.read
file.open
file.binmode
str = file.read
assert_equal("a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal')
end
@ -513,7 +533,9 @@ public
[d('a', true), d('b', true), d('', false)]
)
end
str = file.open.read
file.open
file.binmode
str = file.read
assert_equal("a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal')
end
@ -533,12 +555,28 @@ public
f.close
f = File.open(@outfile, "w")
writer = CSV::BasicWriter.create(f)
writer = CSV::BasicWriter.new(f)
writer.close_on_terminate
writer.close
assert(f.closed?)
end
def test_BasicWriter_s_create_binmode
file = File.open(@outfile, "w") # not "wb"
begin
writer = CSV::BasicWriter.new(file, ?,, ?\r)
writer << ["\r\n", "\r", "\n"]
writer << ["1", "2", "3"]
writer.close
ensure
file.close
end
file = File.open(@outfile, "rb")
str = file.read
file.close
assert_equal("\"\r\n\",\"\r\",\"\n\"\r1,2,3\r", str)
end
#### CSV unit test
@ -582,7 +620,7 @@ public
end
# Illegal format.
File.open(@outfile, "w") do |f|
File.open(@outfile, "wb") do |f|
f << "a,b\r\na,b,\"c\"\ra"
end
assert_raises(CSV::IllegalFormatError) do
@ -590,7 +628,7 @@ public
end
end
File.open(@outfile, "w") do |f|
File.open(@outfile, "wb") do |f|
f << "a,b\r\na,b\""
end
assert_raises(CSV::IllegalFormatError) do
@ -1192,7 +1230,7 @@ public
rows = []
file = File.open(@bomfile)
CSV::Reader.parse(file.read) do |row|
CSV::Reader.parse(file) do |row|
rows << row.to_a
end
assert_equal([["foo"], ["bar"]], rows)
@ -1215,7 +1253,7 @@ public
rows = []
file = File.open(@macfile)
CSV::Reader.parse(file.read, ?,, ?\r) do |row|
CSV::Reader.parse(file, ?,, ?\r) do |row|
rows << row.to_a
end
assert_equal([["Avenches", "aus Umgebung"], ["Bad Hersfeld", "Ausgrabung"]], rows)
@ -1224,7 +1262,7 @@ public
rows = []
file = File.open(@macfile)
assert_raises(CSV::IllegalFormatError) do
CSV::Reader.parse(file.read, ?,) do |row|
CSV::Reader.parse(file, ?,) do |row|
rows << row.to_a
end
end