From 1c9d6dd646d508a3b4de0e84424283242708fe77 Mon Sep 17 00:00:00 2001 From: nahi Date: Thu, 27 May 2004 14:39:11 +0000 Subject: * lib/logger.rb: leading 0 padding of timestamp usec part. * lib/csv.rb (CSV.parse): [CAUTION] behavior changed. in the past, CSV.parse accepts a filename to be read-opened (it was just a shortcut of CSV.open(filename, 'r')). now CSV.parse accepts a string or a stream to be parsed e.g. CSV.parse("1,2\n3,r") #=> [['1', '2'], ['3', '4']] * lib/csv.rb: CSV::Row and CSV::Cell are deprecated. these classes are removed in the future. in the new csv.rb, row is represented as just an Array. since CSV::Row was a subclass of Array, it won't hurt almost all programs except one which depended CSV::Row#match. and a cell is represented as just a String or nil(NULL). this change will cause widespread destruction. CSV.open("foo.csv", "r") do |row| row.each do |cell| if cell.is_null # using Cell#is_null p "(NULL)" else p cell.data # using Cell#data end end end must be just; CSV.open("foo.csv", "r") do |row| row.each do |cell| if cell.nil? p "(NULL)" else p cell end end end * lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior change. CSV.open, CSV.parse, and CSV,generate now do not force opened file binmode. formerly it set binmode explicitly. with CSV.open, binmode of opened file depends the given mode parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open file with "r" and "w". setting mode properly is user's responsibility now. * lib/csv.rb: accepts String as a fs (field separator/column separator) and rs (record separator/row separator) * lib/csv.rb (CSV.read, CSV.readlines): added. works as IO.read and IO.readlines in CSV format. * lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach now does not handle "| cmd" as a path different from IO.foreach. needed? * test/csv/test_csv.rb: updated. * test/ruby/test_float.rb: added test_strtod to test Float("0"). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@6424 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/csv/test_csv.rb | 677 ++++++++++++++++++++++++++------------------------- 1 file changed, 352 insertions(+), 325 deletions(-) (limited to 'test/csv') diff --git a/test/csv/test_csv.rb b/test/csv/test_csv.rb index 7092470915..1db540921b 100644 --- a/test/csv/test_csv.rb +++ b/test/csv/test_csv.rb @@ -1,5 +1,4 @@ -require 'test/unit/testsuite' -require 'test/unit/testcase' +require 'test/unit' require 'tempfile' require 'fileutils' @@ -15,174 +14,20 @@ end module CSVTestSupport - def d(data, is_null = false) - CSV::Cell.new(data.to_s, is_null) - end -end - - -class TestCSVCell < Test::Unit::TestCase - @@colData = ['', nil, true, false, 'foo', '!' * 1000] - - def test_Cell_EQUAL # '==' - d1 = CSV::Cell.new('d', false) - d2 = CSV::Cell.new('d', false) - d3 = CSV::Cell.new('d', true) - d4 = CSV::Cell.new('d', true) - assert(d1 == d2, "Normal case.") - assert(d1 != d3, "RHS is null.") - assert(d4 != d1, "LHS is null.") - assert(d3 != d4, "Either is null.") - end - - def test_Cell_match - d1 = CSV::Cell.new('d', false) - d2 = CSV::Cell.new('d', false) - d3 = CSV::Cell.new('d', true) - d4 = CSV::Cell.new('d', true) - assert(d1.match(d2), "Normal case.") - assert(!d1.match(d3), "RHS is null.") - assert(!d4.match(d1), "LHS is null.") - assert(d3.match(d4), "Either is null.") - end - - def test_Cell_data - d = CSV::Cell.new() - @@colData.each do |v| - d.data = v - assert_equal(d.data, v, "Case: #{ v }.") - end - end - - def test_Cell_data= - d = CSV::Cell.new() - @@colData.each do |v| - d.data = v - assert_equal(d.data, v, "Case: #{ v }.") - end - end - - def test_Cell_is_null - d = CSV::Cell.new() - d.is_null = true - assert_equal(d.is_null, true, "Case: true.") - d.is_null = false - assert_equal(d.is_null, false, "Case: false.") - end - - def test_Cell_is_null= - d = CSV::Cell.new() - d.is_null = true - assert_equal(d.is_null, true, "Case: true.") - d.is_null = false - assert_equal(d.is_null, false, "Case: false.") - end - - def test_Cell_s_new - d1 = CSV::Cell.new() - assert_equal(d1.data, '', "Default: data.") - assert_equal(d1.is_null, true, "Default: is_null.") - - @@colData.each do |v| - d = CSV::Cell.new(v) - assert_equal(d.data, v, "Data: #{ v }.") - end - - d2 = CSV::Cell.new(nil, true) - assert_equal(d2.is_null, true, "Data: true.") - d3 = CSV::Cell.new(nil, false) - assert_equal(d3.is_null, false, "Data: false.") - end - - def test_to_str - d = CSV::Cell.new("foo", false) - assert_equal("foo", d.to_str) - assert(/foo/ =~ d) - d = CSV::Cell.new("foo", true) - begin - d.to_str - assert(false) - rescue - # NoMethodError or NameError - assert(true) - end - end - - def test_to_s - d = CSV::Cell.new("foo", false) - assert_equal("foo", d.to_s) - assert_equal("foo", "#{d}") - d = CSV::Cell.new("foo", true) - assert_equal("", d.to_s) - assert_equal("", "#{d}") - end -end - - -class TestCSVRow < Test::Unit::TestCase - include CSVTestSupport - - def test_Row_s_match - c1 = CSV::Row[d(1), d(2), d(3)] - c2 = CSV::Row[d(1, false), d(2, false), d(3, false)] - assert(c1.match(c2), "Normal case.") - - c1 = CSV::Row[d(1), d('foo', true), d(3)] - c2 = CSV::Row[d(1, false), d('bar', true), d(3, false)] - assert(c1.match(c2), "Either is null.") - - c1 = CSV::Row[d(1), d('foo', true), d(3)] - c2 = CSV::Row[d(1, false), d('bar', false), d(3, false)] - assert(!c1.match(c2), "LHS is null.") - - c1 = CSV::Row[d(1), d('foo'), d(3)] - c2 = CSV::Row[d(1, false), d('bar', true), d(3, false)] - assert(!c1.match(c2), "RHS is null.") - - c1 = CSV::Row[d(1), d('', true), d(3)] - c2 = CSV::Row[d(1, false), d('', true), d(3, false)] - assert(c1.match(c2), "Either is null(empty data).") - - c1 = CSV::Row[d(1), d('', true), d(3)] - c2 = CSV::Row[d(1, false), d('', false), d(3, false)] - assert(!c1.match(c2), "LHS is null(empty data).") - - c1 = CSV::Row[d(1), d(''), d(3)] - c2 = CSV::Row[d(1, false), d('', true), d(3, false)] - assert(!c1.match(c2), "RHS is null(empty data).") - - c1 = CSV::Row[] - c2 = CSV::Row[] - assert(c1.match(c2)) - - c1 = CSV::Row[] - c2 = CSV::Row[d(1)] - assert(!c1.match(c2)) - end - - def test_Row_to_a - r = CSV::Row[d(1), d(2), d(3)] - assert_equal(['1', '2', '3'], r.to_a, 'Normal case') - - r = CSV::Row[d(1)] - assert_equal(['1'], r.to_a, '1 item') - - r = CSV::Row[d(nil, true), d(2), d(3)] - assert_equal([nil, '2', '3'], r.to_a, 'Null in data') - - r = CSV::Row[d(nil, true), d(nil, true), d(nil, true)] - assert_equal([nil, nil, nil], r.to_a, 'Nulls') - - r = CSV::Row[d(nil, true)] - assert_equal([nil], r.to_a, '1 Null') - - r = CSV::Row[] - assert_equal([], r.to_a, 'Empty') + def d(data) + data end end class TestCSV < Test::Unit::TestCase + file = Tempfile.new("crlf") + file << "\n" + file.open + file.binmode + RSEP = file.read + file.close + include CSVTestSupport class << self @@ -221,17 +66,17 @@ class TestCSV < Test::Unit::TestCase } @@fullCSVData = { - [d('', true)] => '', + [d(nil)] => '', [d('')] => '""', - [d('', true), d('', true)] => ',', - [d('', true), d('', true), d('', true)] => ',,', + [d(nil), d(nil)] => ',', + [d(nil), d(nil), d(nil)] => ',,', [d('foo')] => 'foo', [d('foo'), d('bar')] => 'foo,bar', [d('foo'), d('"bar"'), d('baz')] => 'foo,"""bar""",baz', [d('foo'), d('foo,bar'), d('baz')] => 'foo,"foo,bar",baz', [d('foo'), d('""'), d('baz')] => 'foo,"""""",baz', [d('foo'), d(''), d('baz')] => 'foo,"",baz', - [d('foo'), d('', true), d('baz')] => 'foo,,baz', + [d('foo'), d(nil), d('baz')] => 'foo,,baz', [d('foo'), d("\r"), d('baz')] => "foo,\"\r\",baz", [d('foo'), d("\n"), d('baz')] => "foo,\"\n\",baz", [d('foo'), d("\r\n"), d('baz')] => "foo,\"\r\n\",baz", @@ -259,7 +104,7 @@ class TestCSV < Test::Unit::TestCase end def sepConv(srcStr, srcSep, destSep, row_sep = nil) - rows = CSV::Row.new + rows = [] cols, idx = CSV.parse_row(srcStr, 0, rows, srcSep, row_sep) destStr = '' cols = CSV.generate_row(rows, rows.size, destStr, destSep, row_sep) @@ -278,13 +123,13 @@ public @bomfile = File.join(@tmpdir, "bom.csv") @macfile = File.join(@tmpdir, "mac.csv") - CSV.open(@infile, "w") do |writer| + CSV.open(@infile, "wb") do |writer| @@fullCSVDataArray.each do |row| writer.add_row(row) end end - CSV.open(@infiletsv, "w", ?\t) do |writer| + CSV.open(@infiletsv, "wb", ?\t) do |writer| @@fullCSVDataArray.each do |row| writer.add_row(row) end @@ -317,11 +162,11 @@ public first = true ret = reader.each { |row| if first - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) first = false end expected = expectedArray.shift - assert(row.match(expected)) + assert_equal(expected, row) } assert_nil(ret, "Return is nil") assert(expectedArray.empty?) @@ -352,10 +197,10 @@ public @@fullCSVDataArray.each do |expected| actual = reader.shift if first - assert_instance_of(CSV::Row, actual) + assert_instance_of(Array, actual) first = false end - assert(actual.match(expected)) + assert_equal(expected, actual) checked += 1 end assert(checked == @@fullCSVDataArray.size) @@ -445,7 +290,7 @@ public file << "\"\r\n\",\"\r\",\"\n\"\r1,2,3" file.close - file = File.open(@outfile, "r") # not "rb" + file = File.open(@outfile, "rb") begin reader = CSV::IOReader.new(file, ?,, ?\r) assert_equal(["\r\n", "\r", "\n"], reader.shift.to_a) @@ -454,23 +299,34 @@ public ensure file.close end + + file = File.open(@outfile, "r") # not "rb" + begin + lfincell = (RSEP == "\n" ? "\r\n" : "\n") + reader = CSV::IOReader.new(file, ?,, ?\r) + assert_equal([lfincell, "\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") + assert_instance_of(Array, row, "Block parameter") } assert_nil(ret, "Return is nil") ret = CSV::Reader.parse("a;b;c", ?;) { |row| - assert_instance_of(CSV::Row, row, "Block parameter") + assert_instance_of(Array, row, "Block parameter") } file = Tempfile.new("in.csv") file << "a,b,c" file.open ret = CSV::Reader.parse(file) { |row| - assert_instance_of(CSV::Row, row, "Block parameter") + assert_instance_of(Array, row, "Block parameter") } assert_nil(ret, "Return is nil") @@ -478,7 +334,7 @@ public file << "a,b,c" file.open ret = CSV::Reader.parse(file, ?,) { |row| - assert_instance_of(CSV::Row, row, "Block parameter") + assert_instance_of(Array, row, "Block parameter") } # Illegal format. @@ -536,38 +392,38 @@ public file.open file.binmode str = file.read - assert_equal("a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal') + assert_equal("a,b,c#{RSEP},e,f#{RSEP},,\"\"#{RSEP}", str, 'Normal') file = Tempfile.new("out2.csv") CSV::Writer.generate(file) do |writer| ret = writer << [d('a'), d('b'), d('c')] assert_instance_of(CSV::BasicWriter, ret, 'Return is self') - writer << [d(nil, true), d('e'), d('f')] << [d(nil, true), d(nil, true), d('')] + writer << [d(nil), d('e'), d('f')] << [d(nil), d(nil), d('')] end file.open file.binmode str = file.read - assert_equal("a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal') + assert_equal("a,b,c#{RSEP},e,f#{RSEP},,\"\"#{RSEP}", str, 'Normal') end def test_Writer_add_row file = Tempfile.new("out.csv") CSV::Writer.generate(file) do |writer| ret = writer.add_row( - [d('a', false), d('b', false), d('c', false)]) + [d('a'), d('b'), d('c')]) assert_instance_of(CSV::BasicWriter, ret, 'Return is self') writer.add_row( - [d('dummy', true), d('e', false), d('f', false)] + [d(nil), d('e'), d('f')] ).add_row( - [d('a', true), d('b', true), d('', false)] + [d(nil), d(nil), d('')] ) end file.open file.binmode str = file.read - assert_equal("a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal') + assert_equal("a,b,c#{RSEP},e,f#{RSEP},,\"\"#{RSEP}", str, 'Normal') end def test_Writer_close @@ -606,7 +462,7 @@ public file = File.open(@outfile, "rb") str = file.read file.close - assert_equal("\"\r\n\",\"\r\",\"\n\"\r1,2,3\r", str) + assert_equal("\"\r#{RSEP}\",\"\r\",\"#{RSEP}\"\r1,2,3\r", str) end #### CSV unit test @@ -633,12 +489,12 @@ public reader.close CSV.open(@infile, "r") do |row| - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) break end CSV.open(@infiletsv, "r", ?\t) do |row| - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) break end @@ -673,39 +529,40 @@ public end def test_s_parse - reader = CSV.parse(@infile) - assert_instance_of(CSV::IOReader, reader) - reader.close + result = CSV.parse(File.read(@infile)) + assert_instance_of(Array, result) + assert_instance_of(Array, result[0]) - reader = CSV.parse(@infile) - assert_instance_of(CSV::IOReader, reader) - reader.close + result = CSV.parse(File.read(@infile)) + assert_instance_of(Array, result) + assert_instance_of(Array, result[0]) - reader = CSV.parse(@infile, ?;) - assert_instance_of(CSV::IOReader, reader) - reader.close + assert_equal([], CSV.parse("")) + assert_equal([[nil]], CSV.parse("\n")) - CSV.parse(@infile) do |row| - assert_instance_of(CSV::Row, row) + CSV.parse(File.read(@infile)) do |row| + assert_instance_of(Array, row) break end - CSV.parse(@infiletsv, ?\t) do |row| - assert_instance_of(CSV::Row, row) + CSV.parse(File.read(@infiletsv), ?\t) do |row| + assert_instance_of(Array, row) break end - assert_raises(Errno::ENOENT) do - CSV.parse("NoSuchFileOrDirectory") + CSV.parse("") do |row| + assert(false) end - assert_raises(Errno::ENOENT) do - CSV.parse("NoSuchFileOrDirectory", ?;) + count = 0 + CSV.parse("\n") do |row| + assert_equal([nil], row) + count += 1 end + assert_equal(1, count) - CSV.parse(@emptyfile) do |row| - assert_fail("Must not reach here") - end + assert_equal([["a|b-c|d"]], CSV.parse("a|b-c|d")) + assert_equal([["a", "b"], ["c", "d"]], CSV.parse("a|b-c|d", "|", "-")) end def test_s_open_writer @@ -776,30 +633,36 @@ public @@simpleCSVData.each do |col, str| buf = CSV.generate_line(col, ?;) - assert_equal(str + "\r\n", ssv2csv(buf)) + assert_equal(str + "\n", ssv2csv(buf)) end @@simpleCSVData.each do |col, str| buf = CSV.generate_line(col, ?\t) - assert_equal(str + "\r\n", tsv2csv(buf)) + assert_equal(str + "\n", tsv2csv(buf)) end + + str = CSV.generate_line(['a', 'b'], nil, ?|) + assert_equal('a,b', str) + + str = CSV.generate_line(['a', 'b'], nil, "a") + assert_equal('"a",b', str) end def test_s_generate_row buf = '' cols = CSV.generate_row([], 0, buf) assert_equal(0, cols) - assert_equal("\r\n", buf, "Extra boundary check.") + assert_equal("\n", buf, "Extra boundary check.") buf = '' cols = CSV.generate_row([], 0, buf, ?;) assert_equal(0, cols) - assert_equal("\r\n", buf, "Extra boundary check.") + assert_equal("\n", buf, "Extra boundary check.") buf = '' cols = CSV.generate_row([], 0, buf, ?\t) assert_equal(0, cols) - assert_equal("\r\n", buf, "Extra boundary check.") + assert_equal("\n", buf, "Extra boundary check.") buf = '' cols = CSV.generate_row([], 0, buf, ?\t, ?|) @@ -807,64 +670,64 @@ public assert_equal("|", buf, "Extra boundary check.") buf = '' - cols = CSV.generate_row([d(1)], 2, buf) + cols = CSV.generate_row([d('1')], 2, buf) assert_equal('1,', buf) buf = '' - cols = CSV.generate_row([d(1)], 2, buf, ?;) + cols = CSV.generate_row([d('1')], 2, buf, ?;) assert_equal('1;', buf) buf = '' - cols = CSV.generate_row([d(1)], 2, buf, ?\t) + cols = CSV.generate_row([d('1')], 2, buf, ?\t) assert_equal("1\t", buf) buf = '' - cols = CSV.generate_row([d(1)], 2, buf, ?\t, ?|) + cols = CSV.generate_row([d('1')], 2, buf, ?\t, ?|) assert_equal("1\t", buf) buf = '' - cols = CSV.generate_row([d(1), d(2)], 1, buf) - assert_equal("1\r\n", buf) + cols = CSV.generate_row([d('1'), d('2')], 1, buf) + assert_equal("1\n", buf) buf = '' - cols = CSV.generate_row([d(1), d(2)], 1, buf, ?;) - assert_equal("1\r\n", buf) + cols = CSV.generate_row([d('1'), d('2')], 1, buf, ?;) + assert_equal("1\n", buf) buf = '' - cols = CSV.generate_row([d(1), d(2)], 1, buf, ?\t) - assert_equal("1\r\n", buf) + cols = CSV.generate_row([d('1'), d('2')], 1, buf, ?\t) + assert_equal("1\n", buf) buf = '' - cols = CSV.generate_row([d(1), d(2)], 1, buf, ?\t, ?\n) + cols = CSV.generate_row([d('1'), d('2')], 1, buf, ?\t, ?\n) assert_equal("1\n", buf) buf = '' - cols = CSV.generate_row([d(1), d(2)], 1, buf, ?\t, ?\r) + cols = CSV.generate_row([d('1'), d('2')], 1, buf, ?\t, ?\r) assert_equal("1\r", buf) buf = '' - cols = CSV.generate_row([d(1), d(2)], 1, buf, ?\t, ?|) + cols = CSV.generate_row([d('1'), d('2')], 1, buf, ?\t, ?|) assert_equal("1|", buf) @@fullCSVData.each do |col, str| buf = '' cols = CSV.generate_row(col, col.size, buf) assert_equal(col.size, cols) - assert_equal(str + "\r\n", buf) + assert_equal(str + "\n", buf) end @@fullCSVData.each do |col, str| buf = '' cols = CSV.generate_row(col, col.size, buf, ?;) assert_equal(col.size, cols) - assert_equal(str + "\r\n", ssv2csv(buf)) + assert_equal(str + "\n", ssv2csv(buf)) end @@fullCSVData.each do |col, str| buf = '' cols = CSV.generate_row(col, col.size, buf, ?\t) assert_equal(col.size, cols) - assert_equal(str + "\r\n", tsv2csv(buf)) + assert_equal(str + "\n", tsv2csv(buf)) end # row separator @@ -889,7 +752,7 @@ public colsToBe = 0 @@fullCSVData.each do |col, str| cols += CSV.generate_row(col, col.size, buf) - toBe << str << "\r\n" + toBe << str << "\n" colsToBe += col.size end assert_equal(colsToBe, cols) @@ -902,8 +765,8 @@ public @@fullCSVData.each do |col, str| lineBuf = '' cols += CSV.generate_row(col, col.size, lineBuf, ?;) - buf << ssv2csv(lineBuf) << "\r\n" - toBe << ssv2csv(lineBuf) << "\r\n" + buf << ssv2csv(lineBuf) << "\n" + toBe << ssv2csv(lineBuf) << "\n" colsToBe += col.size end assert_equal(colsToBe, cols) @@ -916,8 +779,8 @@ public @@fullCSVData.each do |col, str| lineBuf = '' cols += CSV.generate_row(col, col.size, lineBuf, ?\t) - buf << tsv2csv(lineBuf) << "\r\n" - toBe << tsv2csv(lineBuf) << "\r\n" + buf << tsv2csv(lineBuf) << "\n" + toBe << tsv2csv(lineBuf) << "\n" colsToBe += col.size end assert_equal(colsToBe, cols) @@ -941,7 +804,7 @@ public def test_s_parse_line @@simpleCSVData.each do |col, str| row = CSV.parse_line(str) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(col.size, row.size) assert_equal(col, row) end @@ -949,214 +812,233 @@ public @@simpleCSVData.each do |col, str| str = csv2ssv(str) row = CSV.parse_line(str, ?;) - assert_instance_of(CSV::Row, row) - assert_equal(col.size, row.size) - assert_equal(col, row) + assert_instance_of(Array, row) + assert_equal(col.size, row.size, str.inspect) + assert_equal(col, row, str.inspect) end @@simpleCSVData.each do |col, str| str = csv2tsv(str) row = CSV.parse_line(str, ?\t) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(col.size, row.size) assert_equal(col, row) end + assert_equal(['a', 'b', 'c'], CSV.parse_line("a,b,c", nil, nil)) + assert_equal(['a', nil], CSV.parse_line("a,b,c", nil, ?b)) + assert_equal(['a', 'b', nil], CSV.parse_line("a,b,c", nil, "c")) + assert_equal([nil], CSV.parse_line("")) + assert_equal([nil], CSV.parse_line("\n")) + assert_equal([""], CSV.parse_line("\"\"\n")) + # Illegal format. - buf = CSV::Row.new + buf = [] row = CSV.parse_line("a,b,\"c\"\ra") - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) - buf = CSV::Row.new + buf = Array.new row = CSV.parse_line("a;b;\"c\"\ra", ?;) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) - buf = CSV::Row.new + buf = Array.new row = CSV.parse_line("a\tb\t\"c\"\ra", ?\t) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("a,b\"") - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("a;b\"", ?;) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("a\tb\"", ?\t) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("\"a,b\"\r,") - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("\"a;b\"\r;", ?;) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("\"a\tb\"\r\t", ?\t) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("\"a,b\"\r\"") - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("\"a;b\"\r\"", ?;) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) row = CSV.parse_line("\"a\tb\"\r\"", ?\t) - assert_instance_of(CSV::Row, row) + assert_instance_of(Array, row) assert_equal(0, row.size) end def test_s_parse_row @@fullCSVData.each do |col, str| - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row(str + "\r\n", 0, buf) assert_equal(cols, buf.size, "Reported size.") assert_equal(col.size, buf.size, "Size.") - assert(buf.match(col)) + assert_equal(col, buf, str.inspect) - buf = CSV::Row.new - cols, idx = CSV.parse_row(str + "\n", 0, buf) + buf = Array.new + cols, idx = CSV.parse_row(str + "\n", 0, buf, ?,, ?\n) assert_equal(cols, buf.size, "Reported size.") assert_equal(col.size, buf.size, "Size.") - assert(buf.match(col)) + assert_equal(col, buf, str.inspect) # separator: | - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row(str + "|", 0, buf, ?,) - assert(!buf.match(col)) - buf = CSV::Row.new + assert_not_equal(col, buf) + buf = Array.new cols, idx = CSV.parse_row(str + "|", 0, buf, ?,, ?|) assert_equal(cols, buf.size, "Reported size.") assert_equal(col.size, buf.size, "Size.") - assert(buf.match(col)) + assert_equal(col, buf, str.inspect) end @@fullCSVData.each do |col, str| str = csv2ssv(str) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row(str + "\r\n", 0, buf, ?;) assert_equal(cols, buf.size, "Reported size.") assert_equal(col.size, buf.size, "Size.") - assert(buf.match(col)) + assert_equal(col, buf, str) end @@fullCSVData.each do |col, str| str = csv2tsv(str) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row(str + "\r\n", 0, buf, ?\t) assert_equal(cols, buf.size, "Reported size.") assert_equal(col.size, buf.size, "Size.") - assert(buf.match(col)) + assert_equal(col, buf, str) end @@fullCSVData.each do |col, str| str = csv2tsv(str, ?|) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row(str + "|", 0, buf, ?\t, ?|) assert_equal(cols, buf.size, "Reported size.") assert_equal(col.size, buf.size, "Size.") - assert(buf.match(col), str) + assert_equal(col, buf, str) end - buf = CSV::Row.new + buf = [] + CSV.parse_row("a,b,c", 0, buf, nil, nil) + assert_equal(['a', 'b', 'c'], buf) + + buf = [] + CSV.parse_row("a,b,c", 0, buf, nil, ?b) + assert_equal(['a', nil], buf) + + buf = [] + CSV.parse_row("a,b,c", 0, buf, nil, "c") + assert_equal(['a', 'b', nil], buf) + + buf = Array.new cols, idx = CSV.parse_row("a,b,\"c\r\"", 0, buf) assert_equal(["a", "b", "c\r"], buf.to_a) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a;b;\"c\r\"", 0, buf, ?;) assert_equal(["a", "b", "c\r"], buf.to_a) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a\tb\t\"c\r\"", 0, buf, ?\t) assert_equal(["a", "b", "c\r"], buf.to_a) - buf = CSV::Row.new - cols, idx = CSV.parse_row("a,b,c\n", 0, buf) + buf = Array.new + cols, idx = CSV.parse_row("a,b,c\n", 0, buf, ?,, ?\n) assert_equal(["a", "b", "c"], buf.to_a) - buf = CSV::Row.new - cols, idx = CSV.parse_row("a\tb\tc\n", 0, buf, ?\t) + buf = Array.new + cols, idx = CSV.parse_row("a\tb\tc\n", 0, buf, ?\t, ?\n) assert_equal(["a", "b", "c"], buf.to_a) # Illegal format. - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a,b,c\"", 0, buf) assert_equal(0, cols, "Illegal format; unbalanced double-quote.") - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a;b;c\"", 0, buf, ?;) assert_equal(0, cols, "Illegal format; unbalanced double-quote.") - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a,b,\"c\"\ra", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a,b,\"c\"\ra", 0, buf, ?;) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a,b\"", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a;b\"", 0, buf, ?;) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("\"a,b\"\r,", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a\r,", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a\r", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a\rbc", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a\r\"\"", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("a\r\rabc,", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("\"a;b\"\r;", 0, buf, ?;) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("\"a,b\"\r\"", 0, buf) assert_equal(0, cols) assert_equal(0, idx) - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row("\"a;b\"\r\"", 0, buf, ?;) assert_equal(0, cols) assert_equal(0, idx) @@ -1168,11 +1050,11 @@ public # String "" is not allowed. next end - buf = CSV::Row.new + buf = Array.new cols, idx = CSV.parse_row(str, 0, buf) assert_equal(col.size, cols, "Reported size.") assert_equal(col.size, buf.size, "Size.") - assert(buf.match(col)) + assert_equal(col, buf) end end @@ -1185,7 +1067,7 @@ public end idx = 0 cols = 0 - parsed = CSV::Row.new + parsed = Array.new parsedCols = 0 begin cols, idx = CSV.parse_row(buf, idx, parsed) @@ -1193,7 +1075,7 @@ public end while cols > 0 assert_equal(toBe.size, parsedCols) assert_equal(toBe.size, parsed.size) - assert(parsed.match(toBe)) + assert_equal(toBe, parsed) buf = '' toBe = [] @@ -1203,15 +1085,15 @@ public end idx = 0 cols = 0 - parsed = CSV::Row.new + parsed = Array.new parsedCols = 0 begin - cols, idx = CSV.parse_row(buf, idx, parsed) + cols, idx = CSV.parse_row(buf, idx, parsed, ?,, ?\n) parsedCols += cols end while cols > 0 assert_equal(toBe.size, parsedCols) assert_equal(toBe.size, parsed.size) - assert(parsed.match(toBe)) + assert_equal(toBe, parsed) buf = '' toBe = [] @@ -1223,15 +1105,15 @@ public end idx = 0 cols = 0 - parsed = CSV::Row.new + parsed = Array.new parsedCols = 0 begin - cols, idx = CSV.parse_row(buf, idx, parsed) + cols, idx = CSV.parse_row(buf, idx, parsed, ?,, ?\n) parsedCols += cols end while cols > 0 assert_equal(toBe.size, parsedCols) assert_equal(toBe.size, parsed.size) - assert(parsed.match(toBe)) + assert_equal(toBe, parsed) buf = '' toBe = [] @@ -1241,7 +1123,7 @@ public end idx = 0 cols = 0 - parsed = CSV::Row.new + parsed = [] parsedCols = 0 begin cols, idx = CSV.parse_row(buf, idx, parsed, ?,, ?|) @@ -1249,7 +1131,7 @@ public end while cols > 0 assert_equal(toBe.size, parsedCols) assert_equal(toBe.size, parsed.size) - assert(parsed.match(toBe)) + assert_equal(toBe, parsed) end def test_utf8 @@ -1278,26 +1160,34 @@ public rows = [] assert_raises(CSV::IllegalFormatError) do CSV.open(@macfile, "r") do |row| - rows << row.to_a + rows << row.to_a end + assert_equal([["Avenches", "aus Umgebung\r\"Bad Hersfeld", "Ausgrabung"]], rows) end rows = [] file = File.open(@macfile) - CSV::Reader.parse(file, ?,, ?\r) do |row| - rows << row.to_a + begin + CSV::Reader.parse(file, ?,, ?\r) do |row| + rows << row.to_a + end + assert_equal([["Avenches", "aus Umgebung"], ["Bad Hersfeld", "Ausgrabung"]], rows) + ensure + file.close end - assert_equal([["Avenches", "aus Umgebung"], ["Bad Hersfeld", "Ausgrabung"]], rows) - file.close rows = [] file = File.open(@macfile) - assert_raises(CSV::IllegalFormatError) do - CSV::Reader.parse(file, ?,) do |row| - rows << row.to_a + begin + assert_raises(CSV::IllegalFormatError) do + CSV::Reader.parse(file, ?,) do |row| + rows << row.to_a + end + assert_equal([["Avenches", "aus Umgebung\r\"Bad Hersfeld", "Ausgrabung"]], rows) end + ensure + file.close end - file.close end @@ -1678,8 +1568,8 @@ public # def test_s_parseAndCreate colSize = 8 - csvStr = "foo,!!!foo!!!,!foo,bar!,!!!!!!,!!,,!\r!,!\r\n!\r\nNaHi,!!!Na!!!,!Na,Hi!,!\r.\n!,!\r\n\n!,!!!!,!\n!,!\r\n!".gsub!('!', '"') - csvStrTerminated = csvStr + "\r\n" + csvStr = "foo,!!!foo!!!,!foo,bar!,!!!!!!,!!,,!\r!,!\r\n!\nNaHi,!!!Na!!!,!Na,Hi!,!\r.\n!,!\r\n\n!,!!!!,!\n!,!\r\n!".gsub!('!', '"') + csvStrTerminated = csvStr + "\n" myStr = csvStr.dup res1 = []; res2 = [] @@ -1708,19 +1598,156 @@ public buf = '' CSV::Writer.generate(buf) do |writer| parsed.each do |row| - writer << row.collect { |e| e.is_null ? nil : e.data } + writer << row end end assert_equal(csvStrTerminated, buf) end -end + def test_writer_fs_rs_generate + buf = '' + CSV::Writer.generate(buf, ",,") do |writer| + writer << [] + end + assert_equal("\n", buf) + + buf = '' + CSV::Writer.generate(buf, ",,") do |writer| + writer << [] << [] + end + assert_equal("\n\n", buf) + + buf = '' + CSV::Writer.generate(buf, ",,") do |writer| + writer << [1] + end + assert_equal("1\n", buf) + + buf = '' + CSV::Writer.generate(buf, ",,") do |writer| + writer << [1, 2, 3] + writer << [4, ",,", 5] + end + assert_equal("1,,2,,3\n4,,\",,\",,5\n", buf) + + buf = '' + CSV::Writer.generate(buf, ",,:", ",,;") do |writer| + writer << [nil, nil, nil] + writer << [nil, ",,", nil] + end + assert_equal(",,:,,:,,;,,:,,,,:,,;", buf) + + buf = '' + CSV::Writer.generate(buf, "---") do |writer| + writer << [1, 2, 3] + writer << [4, "---\"---", 5] + end + assert_equal("1---2---3\n4---\"---\"\"---\"---5\n", buf) + + buf = '' + CSV::Writer.generate(buf, nil) do |writer| + writer << [1, 2, 3] + writer << [4, ",\",", 5] + end + assert_equal("1,2,3\n4,\",\"\",\",5\n", buf) + end + + def test_writer_fs_rs_parse + reader = CSV::Reader.create('a||b--c||d', '||', '--') + assert_equal(['a', 'b'], reader.shift) + assert_equal(['c', 'd'], reader.shift) + + reader = CSV::Reader.create("a@|b@-c@|d", "@|", "@-") + assert_equal(['a', 'b'], reader.shift) + assert_equal(['c', 'd'], reader.shift) + + reader = CSV::Reader.create("ababfsababrs", "abfs", "abrs") + assert_equal(['ab', 'ab'], reader.shift) + + reader = CSV::Reader.create('"ab"abfsababrs', "abfs", "abrs") + assert_equal(['ab', 'ab'], reader.shift) + + reader = CSV::Reader.create('"ab"aabfsababrs', "abfs", "abrs") + assert_raises(CSV::IllegalFormatError) do + reader.shift + end + + # fs match while matching rs progress + reader = CSV::Reader.create("ab,ababrs", nil, "abrs") + assert_equal(['ab', 'ab'], reader.shift) + + reader = CSV::Reader.create(',ababrs', nil, "abrs") + assert_equal([nil, 'ab'], reader.shift) -if $0 == __FILE__ - suite = Test::Unit::TestSuite.new('CSV') - ObjectSpace.each_object(Class) do |klass| - suite << klass.suite if (Test::Unit::TestCase > klass) + reader = CSV::Reader.create('"",ababrs', nil, "abrs") + assert_equal(['', 'ab'], reader.shift) + + reader = CSV::Reader.create('ab,"ab"abrs', nil, "abrs") + assert_equal(['ab', 'ab'], reader.shift) + + reader = CSV::Reader.create('ab,"ab"aabrs', nil, "abrs") + assert_raises(CSV::IllegalFormatError) do + reader.shift + end + + # rs match while matching fs progress + reader = CSV::Reader.create("ab|abc", 'ab-', "ab|") + assert_equal([nil], reader.shift) + assert_equal(['abc'], reader.shift) + + reader = CSV::Reader.create("ab\ncdabcef", "abc", "\n") + assert_equal(['ab'], reader.shift) + assert_equal(['cd', "ef"], reader.shift) + + # EOF while fs/rs matching + reader = CSV::Reader.create("ab", 'ab-', "xyz") + assert_equal(['ab'], reader.shift) + + reader = CSV::Reader.create("ab", 'xyz', "ab|") + assert_equal(['ab'], reader.shift) + + reader = CSV::Reader.create("ab", 'ab-', "ab|") + assert_equal(['ab'], reader.shift) + + reader = CSV::Reader.create(",,:,,:,,;,,:,,,,:,,;", ",,:", ",,;") + assert_equal([nil, nil, nil], reader.shift) + assert_equal([nil, ",,", nil], reader.shift) + end + + def test_s_foreach + File.open(@outfile, "w") do |f| + f << "1,2,3\n4,5,6" + end + row = [] + CSV.foreach(@outfile) { |line| + row << line + } + assert_equal([['1', '2', '3'], ['4', '5', '6']], row) + + File.open(@outfile, "w") do |f| + f << "1,2,3\r4,5,6" + end + row = [] + CSV.foreach(@outfile, "\r") { |line| + row << line + } + assert_equal([['1', '2', '3'], ['4', '5', '6']], row) + end + + def test_s_readlines + File.open(@outfile, "w") do |f| + f << "1,2,3\n4,5,6" + end + assert_equal([["1", "2", "3"], ["4", "5", "6"]], CSV.readlines(@outfile)) + assert_equal([["1", "2", nil], [nil, "5", "6"]], CSV.readlines(@outfile, "3\n4")) + end + + def test_s_read + File.open(@outfile, "w") do |f| + f << "1,2,3\n4,5,6" + end + assert_equal([["1", "2", "3"], ["4", "5", "6"]], CSV.read(@outfile)) + assert_equal([["1", "2"]], CSV.read(@outfile, 3)) + assert_equal([[nil], ["4", nil]], CSV.read(@outfile, 3, 5)) end - require 'test/unit/ui/console/testrunner' - Test::Unit::UI::Console::TestRunner.run(suite).passed? end -- cgit v1.2.3