From 11d2edffe68fdd52d3a8a6bad26f774c9dd27dfa Mon Sep 17 00:00:00 2001 From: nahi Date: Mon, 6 Oct 2003 14:03:58 +0000 Subject: [PATCH] * 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 --- ChangeLog | 12 ++++++++-- lib/csv.rb | 8 ++++--- test/csv/test_csv.rb | 56 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index ec8a59056c..d3a119175f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Oct 6 22:59:46 2003 NAKAMURA, Hiroshi + + * 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 * ext/socket/extconf.rb: check recvmsg even if sendmsg is exists. @@ -60,13 +68,13 @@ Sun Oct 5 14:37:39 2003 NAKAMURA, Hiroshi * 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 * 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 diff --git a/lib/csv.rb b/lib/csv.rb index ee686db0cf..10c86f6417 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -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 diff --git a/test/csv/test_csv.rb b/test/csv/test_csv.rb index 6e95a1ba85..96712209d1 100644 --- a/test/csv/test_csv.rb +++ b/test/csv/test_csv.rb @@ -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