From 951200ac639980c1d1d8d836c4badfa889f0ccbb Mon Sep 17 00:00:00 2001 From: jeg2 Date: Mon, 24 Dec 2007 23:41:06 +0000 Subject: [PATCH] * lib/csv.rb, test/csv/test_csv.rb: Removed in preparation for FasterCSV code import. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14639 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +- lib/csv.rb | 972 ----------------------- test/csv/test_csv.rb | 1753 ------------------------------------------ 3 files changed, 7 insertions(+), 2727 deletions(-) delete mode 100644 lib/csv.rb delete mode 100644 test/csv/test_csv.rb diff --git a/ChangeLog b/ChangeLog index 5528ae4578..27cf79ed80 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,13 @@ +Tue Dec 25 08:37:43 2007 James Edward Gray II + + * lib/csv.rb, test/csv/test_csv.rb: Removed in preparation for FasterCSV + code import. + Tue Dec 25 08:27:43 2007 Eric Hodel - * lib/rubygems.rb: Fix test failures. + * lib/rubygems.rb: Fix test failures. - * test/rubygems/test_gem.rb: Fix test failure. + * test/rubygems/test_gem.rb: Fix test failure. Tue Dec 25 06:23:40 2007 Koichi Sasada diff --git a/lib/csv.rb b/lib/csv.rb deleted file mode 100644 index 536ed8af58..0000000000 --- a/lib/csv.rb +++ /dev/null @@ -1,972 +0,0 @@ -# CSV -- module for generating/parsing CSV data. -# Copyright (C) 2000-2004 NAKAMURA, Hiroshi . - -# $Id$ - -# This program is copyrighted free software by NAKAMURA, Hiroshi. You can -# redistribute it and/or modify it under the same terms of Ruby's license; -# either the dual license version in 2003, or any later version. - - -class CSV - class IllegalFormatError < RuntimeError; end - - # Open a CSV formatted file for reading or writing. - # - # For reading. - # - # EXAMPLE 1 - # CSV.open('csvfile.csv', 'r') do |row| - # p row - # end - # - # EXAMPLE 2 - # reader = CSV.open('csvfile.csv', 'r') - # row1 = reader.shift - # row2 = reader.shift - # if row2.empty? - # p 'row2 not find.' - # end - # reader.close - # - # ARGS - # filename: filename to parse. - # col_sep: Column separator. ?, by default. If you want to separate - # fields with semicolon, give ?; here. - # row_sep: Row separator. nil by default. nil means "\r\n or \n". If you - # want to separate records with \r, give ?\r here. - # - # RETURNS - # reader instance. To get parse result, see CSV::Reader#each. - # - # - # For writing. - # - # EXAMPLE 1 - # CSV.open('csvfile.csv', 'w') do |writer| - # writer << ['r1c1', 'r1c2'] - # writer << ['r2c1', 'r2c2'] - # writer << [nil, nil] - # end - # - # EXAMPLE 2 - # writer = CSV.open('csvfile.csv', 'w') - # writer << ['r1c1', 'r1c2'] << ['r2c1', 'r2c2'] << [nil, nil] - # writer.close - # - # ARGS - # filename: filename to generate. - # col_sep: Column separator. ?, by default. If you want to separate - # fields with semicolon, give ?; here. - # row_sep: Row separator. nil by default. nil means "\r\n or \n". If you - # want to separate records with \r, give ?\r here. - # - # RETURNS - # writer instance. See CSV::Writer#<< and CSV::Writer#add_row to know how - # to generate CSV string. - # - def CSV.open(path, mode, fs = nil, rs = nil, &block) - if mode == 'r' or mode == 'rb' - open_reader(path, mode, fs, rs, &block) - elsif mode == 'w' or mode == 'wb' - open_writer(path, mode, fs, rs, &block) - else - raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'") - end - end - - def CSV.foreach(path, rs = nil, &block) - open_reader(path, 'r', ',', rs, &block) - end - - def CSV.read(path, length = nil, offset = nil) - CSV.parse(IO.read(path, length, offset)) - end - - def CSV.readlines(path, rs = nil) - reader = open_reader(path, 'r', ',', rs) - begin - reader.collect { |row| row } - ensure - reader.close - end - end - - def CSV.generate(path, fs = nil, rs = nil, &block) - open_writer(path, 'w', fs, rs, &block) - end - - # Parse lines from given string or stream. Return rows as an Array of Arrays. - def CSV.parse(str_or_readable, fs = nil, rs = nil, &block) - if block - CSV::Reader.parse(str_or_readable, fs, rs) do |row| - yield(row) - end - nil - else - CSV::Reader.create(str_or_readable, fs, rs).collect { |row| row } - end - end - - # Parse a line from given string. Bear in mind it parses ONE LINE. Rest of - # the string is ignored for example "a,b\r\nc,d" => ['a', 'b'] and the - # second line 'c,d' is ignored. - # - # If you don't know whether a target string to parse is exactly 1 line or - # not, use CSV.parse_row instead of this method. - def CSV.parse_line(src, fs = nil, rs = nil) - fs ||= ',' - if fs.is_a?(Fixnum) - fs = fs.chr - end - if !rs.nil? and rs.is_a?(Fixnum) - rs = rs.chr - end - idx = 0 - res_type = :DT_COLSEP - row = [] - begin - while res_type == :DT_COLSEP - res_type, idx, cell = parse_body(src, idx, fs, rs) - row << cell - end - rescue IllegalFormatError - return [] - end - row - end - - # Create a line from cells. each cell is stringified by to_s. - def CSV.generate_line(row, fs = nil, rs = nil) - if row.size == 0 - return '' - end - fs ||= ',' - if fs.is_a?(Fixnum) - fs = fs.chr - end - if !rs.nil? and rs.is_a?(Fixnum) - rs = rs.chr - end - res_type = :DT_COLSEP - result_str = '' - idx = 0 - while true - generate_body(row[idx], result_str, fs, rs) - idx += 1 - if (idx == row.size) - break - end - generate_separator(:DT_COLSEP, result_str, fs, rs) - end - result_str - end - - # Parse a line from string. Consider using CSV.parse_line instead. - # To parse lines in CSV string, see EXAMPLE below. - # - # EXAMPLE - # src = "a,b\r\nc,d\r\ne,f" - # idx = 0 - # begin - # parsed = [] - # parsed_cells, idx = CSV.parse_row(src, idx, parsed) - # puts "Parsed #{ parsed_cells } cells." - # p parsed - # end while parsed_cells > 0 - # - # ARGS - # src: a CSV data to be parsed. Must respond '[](idx)'. - # src[](idx) must return a char. (Not a string such as 'a', but 97). - # src[](idx_out_of_bounds) must return nil. A String satisfies this - # requirement. - # idx: index of parsing location of 'src'. 0 origin. - # out_dev: buffer for parsed cells. Must respond '<<(aString)'. - # col_sep: Column separator. ?, by default. If you want to separate - # fields with semicolon, give ?; here. - # row_sep: Row separator. nil by default. nil means "\r\n or \n". If you - # want to separate records with \r, give ?\r here. - # - # RETURNS - # parsed_cells: num of parsed cells. - # idx: index of next parsing location of 'src'. - # - def CSV.parse_row(src, idx, out_dev, fs = nil, rs = nil) - fs ||= ',' - if fs.is_a?(Fixnum) - fs = fs.chr - end - if !rs.nil? and rs.is_a?(Fixnum) - rs = rs.chr - end - idx_backup = idx - parsed_cells = 0 - res_type = :DT_COLSEP - begin - while res_type != :DT_ROWSEP - res_type, idx, cell = parse_body(src, idx, fs, rs) - if res_type == :DT_EOS - if idx == idx_backup #((parsed_cells == 0) and cell.nil?) - return 0, 0 - end - res_type = :DT_ROWSEP - end - parsed_cells += 1 - out_dev << cell - end - rescue IllegalFormatError - return 0, 0 - end - return parsed_cells, idx - end - - # Convert a line from cells data to string. Consider using CSV.generate_line - # instead. To generate multi-row CSV string, see EXAMPLE below. - # - # EXAMPLE - # row1 = ['a', 'b'] - # row2 = ['c', 'd'] - # row3 = ['e', 'f'] - # src = [row1, row2, row3] - # buf = '' - # src.each do |row| - # parsed_cells = CSV.generate_row(row, 2, buf) - # puts "Created #{ parsed_cells } cells." - # end - # p buf - # - # ARGS - # src: an Array of String to be converted to CSV string. Must respond to - # 'size' and '[](idx)'. src[idx] must return String. - # cells: num of cells in a line. - # out_dev: buffer for generated CSV string. Must respond to '<<(string)'. - # col_sep: Column separator. ?, by default. If you want to separate - # fields with semicolon, give ?; here. - # row_sep: Row separator. nil by default. nil means "\r\n or \n". If you - # want to separate records with \r, give ?\r here. - # - # RETURNS - # parsed_cells: num of converted cells. - # - def CSV.generate_row(src, cells, out_dev, fs = nil, rs = nil) - fs ||= ',' - if fs.is_a?(Fixnum) - fs = fs.chr - end - if !rs.nil? and rs.is_a?(Fixnum) - rs = rs.chr - end - src_size = src.size - if (src_size == 0) - if cells == 0 - generate_separator(:DT_ROWSEP, out_dev, fs, rs) - end - return 0 - end - res_type = :DT_COLSEP - parsed_cells = 0 - generate_body(src[parsed_cells], out_dev, fs, rs) - parsed_cells += 1 - while ((parsed_cells < cells) and (parsed_cells != src_size)) - generate_separator(:DT_COLSEP, out_dev, fs, rs) - generate_body(src[parsed_cells], out_dev, fs, rs) - parsed_cells += 1 - end - if (parsed_cells == cells) - generate_separator(:DT_ROWSEP, out_dev, fs, rs) - else - generate_separator(:DT_COLSEP, out_dev, fs, rs) - end - parsed_cells - end - - # Private class methods. - class << self - private - - def open_reader(path, mode, fs, rs, &block) - file = File.open(path, mode) - if block - begin - CSV::Reader.parse(file, fs, rs) do |row| - yield(row) - end - ensure - file.close - end - nil - else - reader = CSV::Reader.create(file, fs, rs) - reader.close_on_terminate - reader - end - end - - def open_writer(path, mode, fs, rs, &block) - file = File.open(path, mode) - if block - begin - CSV::Writer.generate(file, fs, rs) do |writer| - yield(writer) - end - ensure - file.close - end - nil - else - writer = CSV::Writer.create(file, fs, rs) - writer.close_on_terminate - writer - end - end - - def parse_body(src, idx, fs, rs) - fs_str = fs - fs_size = fs_str.size - rs_str = rs || "\n" - rs_size = rs_str.size - fs_idx = rs_idx = 0 - cell = '' - state = :ST_START - quoted = cr = false - c = nil - last_idx = idx - while c = src[idx] - unless quoted - fschar = (c == fs_str[fs_idx]) - rschar = (c == rs_str[rs_idx]) - # simple 1 char backtrack - if !fschar and c == fs_str[0] - fs_idx = 0 - fschar = true - if state == :ST_START - state = :ST_DATA - elsif state == :ST_QUOTE - raise IllegalFormatError - end - end - if !rschar and c == rs_str[0] - rs_idx = 0 - rschar = true - if state == :ST_START - state = :ST_DATA - elsif state == :ST_QUOTE - raise IllegalFormatError - end - end - end - if c == ?" - fs_idx = rs_idx = 0 - if cr - raise IllegalFormatError - end - cell << src[last_idx, (idx - last_idx)] - last_idx = idx - if state == :ST_DATA - if quoted - last_idx += 1 - quoted = false - state = :ST_QUOTE - else - raise IllegalFormatError - end - elsif state == :ST_QUOTE - cell << c.chr - last_idx += 1 - quoted = true - state = :ST_DATA - else # :ST_START - quoted = true - last_idx += 1 - state = :ST_DATA - end - elsif fschar or rschar - if fschar - fs_idx += 1 - end - if rschar - rs_idx += 1 - end - sep = nil - if fs_idx == fs_size - if state == :ST_START and rs_idx > 0 and fs_idx < rs_idx - state = :ST_DATA - end - cell << src[last_idx, (idx - last_idx - (fs_size - 1))] - last_idx = idx - fs_idx = rs_idx = 0 - if cr - raise IllegalFormatError - end - sep = :DT_COLSEP - elsif rs_idx == rs_size - if state == :ST_START and fs_idx > 0 and rs_idx < fs_idx - state = :ST_DATA - end - if !(rs.nil? and cr) - cell << src[last_idx, (idx - last_idx - (rs_size - 1))] - last_idx = idx - end - fs_idx = rs_idx = 0 - sep = :DT_ROWSEP - end - if sep - if state == :ST_DATA - return sep, idx + 1, cell; - elsif state == :ST_QUOTE - return sep, idx + 1, cell; - else # :ST_START - return sep, idx + 1, nil - end - end - elsif rs.nil? and c == ?\r - # special \r treatment for backward compatibility - fs_idx = rs_idx = 0 - if cr - raise IllegalFormatError - end - cell << src[last_idx, (idx - last_idx)] - last_idx = idx - if quoted - state = :ST_DATA - else - cr = true - end - else - fs_idx = rs_idx = 0 - if state == :ST_DATA or state == :ST_START - if cr - raise IllegalFormatError - end - state = :ST_DATA - else # :ST_QUOTE - raise IllegalFormatError - end - end - idx += 1 - end - if state == :ST_START - if fs_idx > 0 or rs_idx > 0 - state = :ST_DATA - else - return :DT_EOS, idx, nil - end - elsif quoted - raise IllegalFormatError - elsif cr - raise IllegalFormatError - end - cell << src[last_idx, (idx - last_idx)] - last_idx = idx - return :DT_EOS, idx, cell - end - - def generate_body(cell, out_dev, fs, rs) - if cell.nil? - # empty - else - cell = cell.to_s - row_data = cell.dup - if (row_data.gsub!('"', '""') or - row_data.index(fs) or - (rs and row_data.index(rs)) or - (/[\r\n]/ =~ row_data) or - (cell.empty?)) - out_dev << '"' << row_data << '"' - else - out_dev << row_data - end - end - end - - def generate_separator(type, out_dev, fs, rs) - case type - when :DT_COLSEP - out_dev << fs - when :DT_ROWSEP - out_dev << (rs || "\n") - end - end - end - - - # CSV formatted string/stream reader. - # - # EXAMPLE - # read CSV lines untill the first column is 'stop'. - # - # CSV::Reader.parse(File.open('bigdata', 'rb')) do |row| - # p row - # break if !row[0].is_null && row[0].data == 'stop' - # end - # - class Reader - include Enumerable - - # Parse CSV data and get lines. Given block is called for each parsed row. - # Block value is always nil. Rows are not cached for performance reason. - def Reader.parse(str_or_readable, fs = ',', rs = nil, &block) - reader = Reader.create(str_or_readable, fs, rs) - if block - reader.each do |row| - yield(row) - end - reader.close - nil - else - reader - end - end - - # Returns reader instance. - def Reader.create(str_or_readable, fs = ',', rs = nil) - case str_or_readable - when IO - IOReader.new(str_or_readable, fs, rs) - when String - StringReader.new(str_or_readable, fs, rs) - else - IOReader.new(str_or_readable, fs, rs) - end - end - - def each - while true - row = [] - parsed_cells = get_row(row) - if parsed_cells == 0 - break - end - yield(row) - end - nil - end - - def shift - row = [] - parsed_cells = get_row(row) - row - end - - def close - terminate - end - - private - - def initialize(dev) - raise RuntimeError.new('Do not instanciate this class directly.') - end - - def get_row(row) - raise NotImplementedError.new('Method get_row must be defined in a derived class.') - end - - def terminate - # Define if needed. - end - end - - - class StringReader < Reader - def initialize(string, fs = ',', rs = nil) - @fs = fs - @rs = rs - @dev = string - @idx = 0 - if @dev[0, 3] == "\xef\xbb\xbf" - @idx += 3 - end - end - - private - - def get_row(row) - parsed_cells, next_idx = CSV.parse_row(@dev, @idx, row, @fs, @rs) - if parsed_cells == 0 and next_idx == 0 and @idx != @dev.size - raise IllegalFormatError.new - end - @idx = next_idx - parsed_cells - end - end - - - class IOReader < Reader - def initialize(io, fs = ',', rs = nil) - @io = io - @fs = fs - @rs = rs - @dev = CSV::IOBuf.new(@io) - @idx = 0 - if @dev[0, 3] == "\xef\xbb\xbf" - @idx += 3 - end - @close_on_terminate = false - end - - # Tell this reader to close the IO when terminated (Triggered by invoking - # CSV::IOReader#close). - def close_on_terminate - @close_on_terminate = true - end - - private - - def get_row(row) - parsed_cells, next_idx = CSV.parse_row(@dev, @idx, row, @fs, @rs) - if parsed_cells == 0 and next_idx == 0 and !@dev.is_eos? - raise IllegalFormatError.new - end - dropped = @dev.drop(next_idx) - @idx = next_idx - dropped - parsed_cells - end - - def terminate - if @close_on_terminate - @io.close - end - - if @dev - @dev.close - end - end - end - - - # CSV formatted string/stream writer. - # - # EXAMPLE - # Write rows to 'csvout' file. - # - # outfile = File.open('csvout', 'wb') - # CSV::Writer.generate(outfile) do |csv| - # csv << ['c1', nil, '', '"', "\r\n", 'c2'] - # ... - # end - # - # outfile.close - # - class Writer - # Given block is called with the writer instance. str_or_writable must - # handle '<<(string)'. - def Writer.generate(str_or_writable, fs = ',', rs = nil, &block) - writer = Writer.create(str_or_writable, fs, rs) - if block - yield(writer) - writer.close - nil - else - writer - end - end - - # str_or_writable must handle '<<(string)'. - def Writer.create(str_or_writable, fs = ',', rs = nil) - BasicWriter.new(str_or_writable, fs, rs) - end - - # dump CSV stream to the device. argument must be an Array of String. - def <<(row) - CSV.generate_row(row, row.size, @dev, @fs, @rs) - self - end - alias add_row << - - def close - terminate - end - - private - - def initialize(dev) - raise RuntimeError.new('Do not instanciate this class directly.') - end - - def terminate - # Define if needed. - end - end - - - class BasicWriter < Writer - def initialize(str_or_writable, fs = ',', rs = nil) - @fs = fs - @rs = rs - @dev = str_or_writable - @close_on_terminate = false - end - - # Tell this writer to close the IO when terminated (Triggered by invoking - # CSV::BasicWriter#close). - def close_on_terminate - @close_on_terminate = true - end - - private - - def terminate - if @close_on_terminate - @dev.close - end - end - end - -private - - # Buffered stream. - # - # EXAMPLE 1 -- an IO. - # class MyBuf < StreamBuf - # # Do initialize myself before a super class. Super class might call my - # # method 'read'. (Could be awful for C++ user. :-) - # def initialize(s) - # @s = s - # super() - # end - # - # # define my own 'read' method. - # # CAUTION: Returning nil means EnfOfStream. - # def read(size) - # @s.read(size) - # end - # - # # release buffers. in Ruby which has GC, you do not have to call this... - # def terminate - # @s = nil - # super() - # end - # end - # - # buf = MyBuf.new(STDIN) - # my_str = '' - # p buf[0, 0] # => '' (null string) - # p buf[0] # => 97 (char code of 'a') - # p buf[0, 1] # => 'a' - # my_str = buf[0, 5] - # p my_str # => 'abcde' (5 chars) - # p buf[0, 6] # => "abcde\n" (6 chars) - # p buf[0, 7] # => "abcde\n" (6 chars) - # p buf.drop(3) # => 3 (dropped chars) - # p buf.get(0, 2) # => 'de' (2 chars) - # p buf.is_eos? # => false (is not EOS here) - # p buf.drop(5) # => 3 (dropped chars) - # p buf.is_eos? # => true (is EOS here) - # p buf[0] # => nil (is EOS here) - # - # EXAMPLE 2 -- String. - # This is a conceptual example. No pros with this. - # - # class StrBuf < StreamBuf - # def initialize(s) - # @str = s - # @idx = 0 - # super() - # end - # - # def read(size) - # str = @str[@idx, size] - # @idx += str.size - # str - # end - # end - # - class StreamBuf - # get a char or a partial string from the stream. - # idx: index of a string to specify a start point of a string to get. - # unlike String instance, idx < 0 returns nil. - # n: size of a string to get. - # returns char at idx if n == nil. - # returns a partial string, from idx to (idx + n) if n != nil. at EOF, - # the string size could not equal to arg n. - def [](idx, n = nil) - if idx < 0 - return nil - end - if (idx_is_eos?(idx)) - if n and (@offset + idx == buf_size(@cur_buf)) - # Like a String, 'abc'[4, 1] returns nil and - # 'abc'[3, 1] returns '' not nil. - return '' - else - return nil - end - end - my_buf = @cur_buf - my_offset = @offset - next_idx = idx - while (my_offset + next_idx >= buf_size(my_buf)) - if (my_buf == @buf_tail_idx) - unless add_buf - break - end - end - next_idx = my_offset + next_idx - buf_size(my_buf) - my_buf += 1 - my_offset = 0 - end - loc = my_offset + next_idx - if !n - return @buf_list[my_buf][loc] # Fixnum of char code. - elsif (loc + n - 1 < buf_size(my_buf)) - return @buf_list[my_buf][loc, n] # String. - else # should do loop insted of (tail) recursive call... - res = @buf_list[my_buf][loc, BufSize] - size_added = buf_size(my_buf) - loc - if size_added > 0 - idx += size_added - n -= size_added - ret = self[idx, n] - if ret - res << ret - end - end - return res - end - end - alias get [] - - # drop a string from the stream. - # returns dropped size. at EOF, dropped size might not equals to arg n. - # Once you drop the head of the stream, access to the dropped part via [] - # or get returns nil. - def drop(n) - if is_eos? - return 0 - end - size_dropped = 0 - while (n > 0) - if !@is_eos or (@cur_buf != @buf_tail_idx) - if (@offset + n < buf_size(@cur_buf)) - size_dropped += n - @offset += n - n = 0 - else - size = buf_size(@cur_buf) - @offset - size_dropped += size - n -= size - @offset = 0 - unless rel_buf - unless add_buf - break - end - @cur_buf = @buf_tail_idx - end - end - end - end - size_dropped - end - - def is_eos? - return idx_is_eos?(0) - end - - # WARN: Do not instantiate this class directly. Define your own class - # which derives this class and define 'read' instance method. - def initialize - @buf_list = [] - @cur_buf = @buf_tail_idx = -1 - @offset = 0 - @is_eos = false - add_buf - @cur_buf = @buf_tail_idx - end - - protected - - def terminate - while (rel_buf); end - end - - # protected method 'read' must be defined in derived classes. - # CAUTION: Returning a string which size is not equal to 'size' means - # EnfOfStream. When it is not at EOS, you must block the callee, try to - # read and return the sized string. - def read(size) # raise EOFError - raise NotImplementedError.new('Method read must be defined in a derived class.') - end - - private - - def buf_size(idx) - @buf_list[idx].size - end - - def add_buf - if @is_eos - return false - end - begin - str_read = read(BufSize) - rescue EOFError - str_read = nil - rescue - terminate - raise - end - if str_read.nil? - @is_eos = true - @buf_list.push('') - @buf_tail_idx += 1 - false - else - @buf_list.push(str_read) - @buf_tail_idx += 1 - true - end - end - - def rel_buf - if (@cur_buf < 0) - return false - end - @buf_list[@cur_buf] = nil - if (@cur_buf == @buf_tail_idx) - @cur_buf = -1 - return false - else - @cur_buf += 1 - return true - end - end - - def idx_is_eos?(idx) - (@is_eos and ((@cur_buf < 0) or (@cur_buf == @buf_tail_idx))) - end - - BufSize = 1024 * 8 - end - - # Buffered IO. - # - # EXAMPLE - # # File 'bigdata' could be a giga-byte size one! - # buf = CSV::IOBuf.new(File.open('bigdata', 'rb')) - # CSV::Reader.new(buf).each do |row| - # p row - # break if row[0].data == 'admin' - # end - # - class IOBuf < StreamBuf - def initialize(s) - @s = s - super() - end - - def close - terminate - end - - private - - def read(size) - @s.read(size) - end - - def terminate - super() - end - end -end diff --git a/test/csv/test_csv.rb b/test/csv/test_csv.rb deleted file mode 100644 index 1db540921b..0000000000 --- a/test/csv/test_csv.rb +++ /dev/null @@ -1,1753 +0,0 @@ -require 'test/unit' -require 'tempfile' -require 'fileutils' - -require 'csv' - -class CSV - class StreamBuf - # Let buffer work hard. - remove_const("BufSize") - BufSize = 2 - end -end - - -module CSVTestSupport - 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 - include CSVTestSupport - end - - @@simpleCSVData = { - [nil] => '', - [''] => '""', - [nil, nil] => ',', - [nil, nil, nil] => ',,', - ['foo'] => 'foo', - [','] => '","', - [',', ','] => '",",","', - [';'] => ';', - [';', ';'] => ';,;', - ["\"\r", "\"\r"] => "\"\"\"\r\",\"\"\"\r\"", - ["\"\n", "\"\n"] => "\"\"\"\n\",\"\"\"\n\"", - ["\t"] => "\t", - ["\t", "\t"] => "\t,\t", - ['foo', 'bar'] => 'foo,bar', - ['foo', '"bar"', 'baz'] => 'foo,"""bar""",baz', - ['foo', 'foo,bar', 'baz'] => 'foo,"foo,bar",baz', - ['foo', '""', 'baz'] => 'foo,"""""",baz', - ['foo', '', 'baz'] => 'foo,"",baz', - ['foo', nil, 'baz'] => 'foo,,baz', - [nil, 'foo', 'bar'] => ',foo,bar', - ['foo', 'bar', nil] => 'foo,bar,', - ['foo', "\r", 'baz'] => "foo,\"\r\",baz", - ['foo', "\n", 'baz'] => "foo,\"\n\",baz", - ['foo', "\r\n\r", 'baz'] => "foo,\"\r\n\r\",baz", - ['foo', "\r\n", 'baz'] => "foo,\"\r\n\",baz", - ['foo', "\r.\n", 'baz'] => "foo,\"\r.\n\",baz", - ['foo', "\r\n\n", 'baz'] => "foo,\"\r\n\n\",baz", - ['foo', '"', 'baz'] => 'foo,"""",baz', - } - - @@fullCSVData = { - [d(nil)] => '', - [d('')] => '""', - [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(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", - [d('foo'), d("\r.\n"), d('baz')] => "foo,\"\r.\n\",baz", - [d('foo'), d("\r\n\n"), d('baz')] => "foo,\"\r\n\n\",baz", - [d('foo'), d('"'), d('baz')] => 'foo,"""",baz', - } - - @@fullCSVDataArray = @@fullCSVData.collect { |key, value| key } - - def ssv2csv(ssvStr, row_sep = nil) - sepConv(ssvStr, ?;, ?,, row_sep) - end - - def csv2ssv(csvStr, row_sep = nil) - sepConv(csvStr, ?,, ?;, row_sep) - end - - def tsv2csv(tsvStr, row_sep = nil) - sepConv(tsvStr, ?\t, ?,, row_sep) - end - - def csv2tsv(csvStr, row_sep = nil) - sepConv(csvStr, ?,, ?\t, row_sep) - end - - def sepConv(srcStr, srcSep, destSep, row_sep = nil) - rows = [] - cols, idx = CSV.parse_row(srcStr, 0, rows, srcSep, row_sep) - destStr = '' - cols = CSV.generate_row(rows, rows.size, destStr, destSep, row_sep) - destStr - end - -public - - def setup - @tmpdir = File.join(Dir.tmpdir, "ruby_test_csv_tmp_#{$$}") - Dir.mkdir(@tmpdir) - @infile = File.join(@tmpdir, 'in.csv') - @infiletsv = File.join(@tmpdir, 'in.tsv') - @emptyfile = File.join(@tmpdir, 'empty.csv') - @outfile = File.join(@tmpdir, 'out.csv') - @bomfile = File.join(@tmpdir, "bom.csv") - @macfile = File.join(@tmpdir, "mac.csv") - - CSV.open(@infile, "wb") do |writer| - @@fullCSVDataArray.each do |row| - writer.add_row(row) - end - end - - CSV.open(@infiletsv, "wb", ?\t) do |writer| - @@fullCSVDataArray.each do |row| - writer.add_row(row) - end - end - - CSV.generate(@emptyfile) do |writer| - # Create empty file. - end - - File.open(@bomfile, "wb") do |f| - f.write("\357\273\277\"foo\"\r\n\"bar\"\r\n") - end - - File.open(@macfile, "wb") do |f| - f.write("\"Avenches\",\"aus Umgebung\"\r\"Bad Hersfeld\",\"Ausgrabung\"") - end - end - - def teardown - FileUtils.rm_rf(@tmpdir) - end - - #### CSV::Reader unit test - - def test_Reader_each - file = File.open(@infile, "rb") - begin - reader = CSV::Reader.create(file) - expectedArray = @@fullCSVDataArray.dup - first = true - ret = reader.each { |row| - if first - assert_instance_of(Array, row) - first = false - end - expected = expectedArray.shift - assert_equal(expected, row) - } - assert_nil(ret, "Return is nil") - assert(expectedArray.empty?) - ensure - file.close - end - - # Illegal format. - reader = CSV::Reader.create("a,b\r\na,b,\"c\"\ra") - assert_raises(CSV::IllegalFormatError) do - reader.each do |row| - end - end - - reader = CSV::Reader.create("a,b\r\n\"") - assert_raises(CSV::IllegalFormatError) do - reader.each do |row| - end - end - end - - def test_Reader_shift - file = File.open(@infile, "rb") - begin - reader = CSV::Reader.create(file) - first = true - checked = 0 - @@fullCSVDataArray.each do |expected| - actual = reader.shift - if first - assert_instance_of(Array, actual) - first = false - end - assert_equal(expected, actual) - checked += 1 - end - assert(checked == @@fullCSVDataArray.size) - ensure - file.close - end - - # Illegal format. - reader = CSV::Reader.create("a,b\r\na,b,\"c\"\ra") - assert_raises(CSV::IllegalFormatError) do - reader.shift - reader.shift - end - - reader = CSV::Reader.create("a,b\r\na,b,\"c\"\ra") - assert_raises(CSV::IllegalFormatError) do - reader.shift - reader.shift - end - end - - def test_Reader_getRow - if CSV::Reader.respond_to?(:allocate) - obj = CSV::Reader.allocate - assert_raises(NotImplementedError) do - row = [] - obj.shift - end - end - end - - def test_IOReader_close_on_terminate - f = File.open(@infile, "r") - reader = CSV::IOReader.create(f) - reader.close - assert(!f.closed?) - f.close - - f = File.open(@infile, "r") - writer = CSV::IOReader.create(f) - writer.close_on_terminate - writer.close - assert(f.closed?) - end - - def test_Reader_close - f = File.open(@infile, "r") - reader = CSV::IOReader.create(f) - reader.close_on_terminate - reader.close - assert(f.closed?) - end - - def test_Reader_s_new - assert_raises(RuntimeError) do - CSV::Reader.new(nil) - end - end - - def test_Reader_s_create - reader = CSV::Reader.create("abc") - assert_instance_of(CSV::StringReader, reader, "With a String") - - file = File.open(@infile, "rb") - reader = CSV::Reader.create(file) - assert_instance_of(CSV::IOReader, reader, 'With an IO') - - obj = Object.new - def obj.sysread(size) - "abc" - end - def obj.read(size) - "abc" - end - reader = CSV::Reader.create(obj) - assert_instance_of(CSV::IOReader, reader, "With not an IO or String") - - # No need to test Tempfile because it's a pseudo IO. I test this here - # fors other tests. - reader = CSV::Reader.create(Tempfile.new("in.csv")) - assert_instance_of(CSV::IOReader, reader, "With an pseudo IO.") - 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, "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 - - 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(Array, row, "Block parameter") - } - assert_nil(ret, "Return is nil") - - ret = CSV::Reader.parse("a;b;c", ?;) { |row| - 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(Array, row, "Block parameter") - } - assert_nil(ret, "Return is nil") - - file = Tempfile.new("in.csv") - file << "a,b,c" - file.open - ret = CSV::Reader.parse(file, ?,) { |row| - assert_instance_of(Array, row, "Block parameter") - } - - # Illegal format. - assert_raises(CSV::IllegalFormatError) do - CSV::Reader.parse("a,b\r\na,b,\"c\"\ra") do |row| - end - end - - assert_raises(CSV::IllegalFormatError) do - CSV::Reader.parse("a,b\r\na,b\"") do |row| - end - end - end - - - #### CSV::Writer unit test - - def test_Writer_s_new - assert_raises(RuntimeError) do - CSV::Writer.new(nil) - end - end - - def test_Writer_s_generate - ret = CSV::Writer.generate(STDOUT) { |writer| - assert_instance_of(CSV::BasicWriter, writer, "Block parameter") - } - - ret = CSV::Writer.generate(STDOUT, ?;) { |writer| - assert_instance_of(CSV::BasicWriter, writer, "Block parameter") - } - - assert_nil(ret, "Return is nil") - end - - def test_Writer_s_create - writer = CSV::Writer.create(STDERR) - assert_instance_of(CSV::BasicWriter, writer, "String") - - writer = CSV::Writer.create(STDERR, ?;) - assert_instance_of(CSV::BasicWriter, writer, "String") - - writer = CSV::Writer.create(Tempfile.new("out.csv")) - assert_instance_of(CSV::BasicWriter, writer, "IO") - end - - def test_Writer_LSHIFT # '<<' - file = Tempfile.new("out.csv") - CSV::Writer.generate(file) do |writer| - ret = writer << ['a', 'b', 'c'] - assert_instance_of(CSV::BasicWriter, ret, 'Return is self') - - writer << [nil, 'e', 'f'] << [nil, nil, ''] - end - file.open - file.binmode - str = file.read - 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), d('e'), d('f')] << [d(nil), d(nil), d('')] - end - file.open - file.binmode - str = file.read - 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'), d('b'), d('c')]) - assert_instance_of(CSV::BasicWriter, ret, 'Return is self') - - writer.add_row( - [d(nil), d('e'), d('f')] - ).add_row( - [d(nil), d(nil), d('')] - ) - end - file.open - file.binmode - str = file.read - assert_equal("a,b,c#{RSEP},e,f#{RSEP},,\"\"#{RSEP}", str, 'Normal') - end - - def test_Writer_close - f = File.open(@outfile, "w") - writer = CSV::BasicWriter.create(f) - writer.close_on_terminate - writer.close - assert(f.closed?) - end - - def test_BasicWriter_close_on_terminate - f = File.open(@outfile, "w") - writer = CSV::BasicWriter.create(f) - writer.close - assert(!f.closed?) - f.close - - f = File.open(@outfile, "w") - 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#{RSEP}\",\"\r\",\"#{RSEP}\"\r1,2,3\r", str) - end - - #### CSV unit test - - def test_s_open_reader - assert_raises(ArgumentError, 'Illegal mode') do - CSV.open("temp", "a") - end - - assert_raises(ArgumentError, 'Illegal mode') do - CSV.open("temp", "a", ?;) - end - - reader = CSV.open(@infile, "r") - assert_instance_of(CSV::IOReader, reader) - reader.close - - reader = CSV.open(@infile, "rb") - assert_instance_of(CSV::IOReader, reader) - reader.close - - reader = CSV.open(@infile, "r", ?;) - assert_instance_of(CSV::IOReader, reader) - reader.close - - CSV.open(@infile, "r") do |row| - assert_instance_of(Array, row) - break - end - - CSV.open(@infiletsv, "r", ?\t) do |row| - assert_instance_of(Array, row) - break - end - - assert_raises(Errno::ENOENT) do - CSV.open("NoSuchFileOrDirectory", "r") - end - - assert_raises(Errno::ENOENT) do - CSV.open("NoSuchFileOrDirectory", "r", ?;) - end - - # Illegal format. - File.open(@outfile, "wb") do |f| - f << "a,b\r\na,b,\"c\"\ra" - end - assert_raises(CSV::IllegalFormatError) do - CSV.open(@outfile, "r") do |row| - end - end - - File.open(@outfile, "wb") do |f| - f << "a,b\r\na,b\"" - end - assert_raises(CSV::IllegalFormatError) do - CSV.open(@outfile, "r") do |row| - end - end - - CSV.open(@emptyfile, "r") do |row| - assert_fail("Must not reach here") - end - end - - def test_s_parse - result = CSV.parse(File.read(@infile)) - assert_instance_of(Array, result) - assert_instance_of(Array, result[0]) - - result = CSV.parse(File.read(@infile)) - assert_instance_of(Array, result) - assert_instance_of(Array, result[0]) - - assert_equal([], CSV.parse("")) - assert_equal([[nil]], CSV.parse("\n")) - - CSV.parse(File.read(@infile)) do |row| - assert_instance_of(Array, row) - break - end - - CSV.parse(File.read(@infiletsv), ?\t) do |row| - assert_instance_of(Array, row) - break - end - - CSV.parse("") do |row| - assert(false) - end - - count = 0 - CSV.parse("\n") do |row| - assert_equal([nil], row) - count += 1 - end - assert_equal(1, count) - - 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 - writer = CSV.open(@outfile, "w") - assert_instance_of(CSV::BasicWriter, writer) - writer.close - - writer = CSV.open(@outfile, "wb") - assert_instance_of(CSV::BasicWriter, writer) - writer.close - - writer = CSV.open(@outfile, "wb", ?;) - assert_instance_of(CSV::BasicWriter, writer) - writer.close - - CSV.open(@outfile, "w") do |writer| - assert_instance_of(CSV::BasicWriter, writer) - end - - CSV.open(@outfile, "w", ?;) do |writer| - assert_instance_of(CSV::BasicWriter, writer) - end - - begin - CSV.open(@tmpdir, "w") - assert(false) - rescue Exception => ex - assert(ex.is_a?(Errno::EEXIST) || ex.is_a?(Errno::EISDIR) || ex.is_a?(Errno::EACCES)) - end - end - - def test_s_generate - writer = CSV.generate(@outfile) - assert_instance_of(CSV::BasicWriter, writer) - writer.close - - writer = CSV.generate(@outfile, ?;) - assert_instance_of(CSV::BasicWriter, writer) - writer.close - - CSV.generate(@outfile) do |writer| - assert_instance_of(CSV::BasicWriter, writer) - end - - CSV.generate(@outfile, ?;) do |writer| - assert_instance_of(CSV::BasicWriter, writer) - end - - begin - CSV.generate(@tmpdir) - assert(false) - rescue Exception => ex - assert(ex.is_a?(Errno::EEXIST) || ex.is_a?(Errno::EISDIR) || ex.is_a?(Errno::EACCES)) - end - end - - def test_s_generate_line - str = CSV.generate_line([]) - assert_equal('', str, "Extra boundary check.") - - str = CSV.generate_line([], ?;) - assert_equal('', str, "Extra boundary check.") - - @@simpleCSVData.each do |col, str| - buf = CSV.generate_line(col) - assert_equal(str, buf) - end - - @@simpleCSVData.each do |col, str| - buf = CSV.generate_line(col, ?;) - assert_equal(str + "\n", ssv2csv(buf)) - end - - @@simpleCSVData.each do |col, str| - buf = CSV.generate_line(col, ?\t) - 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("\n", buf, "Extra boundary check.") - - buf = '' - cols = CSV.generate_row([], 0, buf, ?;) - assert_equal(0, cols) - assert_equal("\n", buf, "Extra boundary check.") - - buf = '' - cols = CSV.generate_row([], 0, buf, ?\t) - assert_equal(0, cols) - assert_equal("\n", buf, "Extra boundary check.") - - buf = '' - cols = CSV.generate_row([], 0, buf, ?\t, ?|) - assert_equal(0, cols) - assert_equal("|", buf, "Extra boundary check.") - - buf = '' - cols = CSV.generate_row([d('1')], 2, buf) - assert_equal('1,', buf) - - buf = '' - cols = CSV.generate_row([d('1')], 2, buf, ?;) - assert_equal('1;', buf) - - buf = '' - cols = CSV.generate_row([d('1')], 2, buf, ?\t) - assert_equal("1\t", buf) - - buf = '' - 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\n", buf) - - 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\n", buf) - - buf = '' - 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) - assert_equal("1\r", buf) - - buf = '' - 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 + "\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 + "\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 + "\n", tsv2csv(buf)) - end - - # row separator - @@fullCSVData.each do |col, str| - buf = '' - cols = CSV.generate_row(col, col.size, buf, ?,, ?|) - assert_equal(col.size, cols) - assert_equal(str + "|", buf) - end - - # col and row separator - @@fullCSVData.each do |col, str| - buf = '' - cols = CSV.generate_row(col, col.size, buf, ?\t, ?|) - assert_equal(col.size, cols) - assert_equal(str + "|", tsv2csv(buf, ?|)) - end - - buf = '' - toBe = '' - cols = 0 - colsToBe = 0 - @@fullCSVData.each do |col, str| - cols += CSV.generate_row(col, col.size, buf) - toBe << str << "\n" - colsToBe += col.size - end - assert_equal(colsToBe, cols) - assert_equal(toBe, buf) - - buf = '' - toBe = '' - cols = 0 - colsToBe = 0 - @@fullCSVData.each do |col, str| - lineBuf = '' - cols += CSV.generate_row(col, col.size, lineBuf, ?;) - buf << ssv2csv(lineBuf) << "\n" - toBe << ssv2csv(lineBuf) << "\n" - colsToBe += col.size - end - assert_equal(colsToBe, cols) - assert_equal(toBe, buf) - - buf = '' - toBe = '' - cols = 0 - colsToBe = 0 - @@fullCSVData.each do |col, str| - lineBuf = '' - cols += CSV.generate_row(col, col.size, lineBuf, ?\t) - buf << tsv2csv(lineBuf) << "\n" - toBe << tsv2csv(lineBuf) << "\n" - colsToBe += col.size - end - assert_equal(colsToBe, cols) - assert_equal(toBe, buf) - - buf = '' - toBe = '' - cols = 0 - colsToBe = 0 - @@fullCSVData.each do |col, str| - lineBuf = '' - cols += CSV.generate_row(col, col.size, lineBuf, ?|) - buf << tsv2csv(lineBuf, ?|) - toBe << tsv2csv(lineBuf, ?|) - colsToBe += col.size - end - assert_equal(colsToBe, cols) - assert_equal(toBe, buf) - end - - def test_s_parse_line - @@simpleCSVData.each do |col, str| - row = CSV.parse_line(str) - assert_instance_of(Array, row) - assert_equal(col.size, row.size) - assert_equal(col, row) - end - - @@simpleCSVData.each do |col, str| - str = csv2ssv(str) - row = CSV.parse_line(str, ?;) - 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(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 = [] - row = CSV.parse_line("a,b,\"c\"\ra") - assert_instance_of(Array, row) - assert_equal(0, row.size) - - buf = Array.new - row = CSV.parse_line("a;b;\"c\"\ra", ?;) - assert_instance_of(Array, row) - assert_equal(0, row.size) - - buf = Array.new - row = CSV.parse_line("a\tb\t\"c\"\ra", ?\t) - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("a,b\"") - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("a;b\"", ?;) - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("a\tb\"", ?\t) - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("\"a,b\"\r,") - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("\"a;b\"\r;", ?;) - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("\"a\tb\"\r\t", ?\t) - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("\"a,b\"\r\"") - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("\"a;b\"\r\"", ?;) - assert_instance_of(Array, row) - assert_equal(0, row.size) - - row = CSV.parse_line("\"a\tb\"\r\"", ?\t) - assert_instance_of(Array, row) - assert_equal(0, row.size) - end - - def test_s_parse_row - @@fullCSVData.each do |col, str| - 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_equal(col, buf, str.inspect) - - 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_equal(col, buf, str.inspect) - - # separator: | - buf = Array.new - cols, idx = CSV.parse_row(str + "|", 0, buf, ?,) - 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_equal(col, buf, str.inspect) - end - - @@fullCSVData.each do |col, str| - str = csv2ssv(str) - 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_equal(col, buf, str) - end - - @@fullCSVData.each do |col, str| - str = csv2tsv(str) - 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_equal(col, buf, str) - end - - @@fullCSVData.each do |col, str| - str = csv2tsv(str, ?|) - 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_equal(col, buf, str) - end - - 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 = Array.new - cols, idx = CSV.parse_row("a;b;\"c\r\"", 0, buf, ?;) - assert_equal(["a", "b", "c\r"], buf.to_a) - - 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 = Array.new - cols, idx = CSV.parse_row("a,b,c\n", 0, buf, ?,, ?\n) - assert_equal(["a", "b", "c"], buf.to_a) - - 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 = Array.new - cols, idx = CSV.parse_row("a,b,c\"", 0, buf) - assert_equal(0, cols, "Illegal format; unbalanced double-quote.") - - buf = Array.new - cols, idx = CSV.parse_row("a;b;c\"", 0, buf, ?;) - assert_equal(0, cols, "Illegal format; unbalanced double-quote.") - - buf = Array.new - cols, idx = CSV.parse_row("a,b,\"c\"\ra", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a,b,\"c\"\ra", 0, buf, ?;) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a,b\"", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a;b\"", 0, buf, ?;) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("\"a,b\"\r,", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a\r,", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a\r", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a\rbc", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a\r\"\"", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("a\r\rabc,", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("\"a;b\"\r;", 0, buf, ?;) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("\"a,b\"\r\"", 0, buf) - assert_equal(0, cols) - assert_equal(0, idx) - - buf = Array.new - cols, idx = CSV.parse_row("\"a;b\"\r\"", 0, buf, ?;) - assert_equal(0, cols) - assert_equal(0, idx) - end - - def test_s_parse_rowEOF - @@fullCSVData.each do |col, str| - if str == '' - # String "" is not allowed. - next - end - 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_equal(col, buf) - end - end - - def test_s_parse_rowConcat - buf = '' - toBe = [] - @@fullCSVData.each do |col, str| - buf << str << "\r\n" - toBe.concat(col) - end - idx = 0 - cols = 0 - parsed = Array.new - parsedCols = 0 - begin - cols, idx = CSV.parse_row(buf, idx, parsed) - parsedCols += cols - end while cols > 0 - assert_equal(toBe.size, parsedCols) - assert_equal(toBe.size, parsed.size) - assert_equal(toBe, parsed) - - buf = '' - toBe = [] - @@fullCSVData.each do |col, str| - buf << str << "\n" - toBe.concat(col) - end - idx = 0 - cols = 0 - parsed = Array.new - parsedCols = 0 - begin - 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_equal(toBe, parsed) - - buf = '' - toBe = [] - @@fullCSVData.sort { |a, b| - a[0].length <=> b[0].length - }.each do |col, str| - buf << str << "\n" - toBe.concat(col) - end - idx = 0 - cols = 0 - parsed = Array.new - parsedCols = 0 - begin - 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_equal(toBe, parsed) - - buf = '' - toBe = [] - @@fullCSVData.each do |col, str| - buf << str << "|" - toBe.concat(col) - end - idx = 0 - cols = 0 - parsed = [] - parsedCols = 0 - begin - cols, idx = CSV.parse_row(buf, idx, parsed, ?,, ?|) - parsedCols += cols - end while cols > 0 - assert_equal(toBe.size, parsedCols) - assert_equal(toBe.size, parsed.size) - assert_equal(toBe, parsed) - end - - def test_utf8 - rows = [] - CSV.open(@bomfile, "r") do |row| - rows << row.to_a - end - assert_equal([["foo"], ["bar"]], rows) - - rows = [] - file = File.open(@bomfile) - CSV::Reader.parse(file) do |row| - rows << row.to_a - end - assert_equal([["foo"], ["bar"]], rows) - file.close - end - - def test_macCR - rows = [] - CSV.open(@macfile, "r", ?,, ?\r) do |row| - rows << row.to_a - end - assert_equal([["Avenches", "aus Umgebung"], ["Bad Hersfeld", "Ausgrabung"]], rows) - - rows = [] - assert_raises(CSV::IllegalFormatError) do - CSV.open(@macfile, "r") do |row| - rows << row.to_a - end - assert_equal([["Avenches", "aus Umgebung\r\"Bad Hersfeld", "Ausgrabung"]], rows) - end - - rows = [] - file = File.open(@macfile) - 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 - - rows = [] - file = File.open(@macfile) - 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 - end - - - #### CSV unit test - - InputStreamPattern = '0123456789' - InputStreamPatternSize = InputStreamPattern.size - def expChar(idx) - InputStreamPattern[idx % InputStreamPatternSize] - end - - def expStr(idx, n) - if n > InputStreamPatternSize - InputStreamPattern + expStr(0, n - InputStreamPatternSize) - else - InputStreamPattern[idx % InputStreamPatternSize, n] - end - end - - def setupInputStream(size, bufSize = nil) - setBufSize(bufSize) if bufSize - m = ((size / InputStreamPatternSize) + 1).to_i - File.open(@outfile, "wb") do |f| - f << (InputStreamPattern * m)[0, size] - end - file = File.open(@outfile, "rb") - buf = CSV::IOBuf.new(file) - if block_given? - yield(buf) - file.close - nil - else - buf - end - end - - def setBufSize(size) - CSV::StreamBuf.module_eval('remove_const("BufSize")') - CSV::StreamBuf.module_eval("BufSize = #{ size }") - end - - class StrBuf < CSV::StreamBuf - private - def initialize(string) - @str = string - @idx = 0 - super() - end - - def read(size) - str = @str[@idx, size] - if str.empty? - nil - else - @idx += str.size - str - end - end - end - - class ErrBuf < CSV::StreamBuf - class Error < RuntimeError; end - private - def initialize - @first = true - super() - end - - def read(size) - if @first - @first = false - "a" * size - else - raise ErrBuf::Error.new - end - end - end - - def test_StreamBuf_MyBuf - # At first, check ruby's behaviour. - s = "abc" - assert_equal(?a, s[0]) - assert_equal(?b, s[1]) - assert_equal(?c, s[2]) - assert_equal(nil, s[3]) - assert_equal("a", s[0, 1]) - assert_equal("b", s[1, 1]) - assert_equal("c", s[2, 1]) - assert_equal("", s[3, 1]) - assert_equal(nil, s[4, 1]) - - s = StrBuf.new("abc") - assert_equal(?a, s[0]) - assert_equal(?b, s.get(1)) - assert_equal(?c, s[2]) - assert_equal(nil, s.get(3)) - assert_equal("a", s[0, 1]) - assert_equal("b", s.get(1, 1)) - assert_equal("c", s[2, 1]) - assert_equal("", s.get(3, 1)) - assert_equal(nil, s[4, 1]) - - dropped = s.drop(1) - assert_equal(1, dropped) - assert_equal(?b, s[0]) - assert(!s.is_eos?) - dropped = s.drop(1) - assert_equal(1, dropped) - assert_equal(?c, s[0]) - assert(!s.is_eos?) - dropped = s.drop(1) - assert_equal(1, dropped) - assert_equal(nil, s[0]) - assert(s.is_eos?) - dropped = s.drop(1) - assert_equal(0, dropped) - assert_equal(nil, s[0]) - assert(s.is_eos?) - - s = StrBuf.new("") - assert_equal(nil, s[0]) - - s = StrBuf.new("") - dropped = s.drop(1) - assert_equal(0, dropped) - - assert_raises(TestCSV::ErrBuf::Error) do - s = ErrBuf.new - s[1024] - end - - assert_raises(TestCSV::ErrBuf::Error) do - s = ErrBuf.new - s.drop(1024) - end - end - - def test_StreamBuf_AREF # '[idx]' - setupInputStream(22, 1024) do |s| - [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 - assert_equal(nil, s[-1]) - end - - setupInputStream(22, 1) do |s| - [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 - - setupInputStream(1024, 1) do |s| - [1023, 0].each do |idx| - 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 - - 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 - - def test_StreamBuf_AREF_n # '[idx, n]' - # At first, check ruby's behaviour. - assert_equal("", "abc"[3, 1]) - assert_equal(nil, "abc"[4, 1]) - - setupInputStream(22, 1024) do |s| - [0, 1, 9, 10, 19, 20, 21].each do |idx| - assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) - end - assert_equal("", s[22, 1]) - assert_equal(nil, s[23, 1]) - end - - setupInputStream(22, 1) do |s| - [0, 1, 9, 10, 19, 20, 21].each do |idx| - assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) - end - assert_equal("", s[22, 1]) - assert_equal(nil, s[23, 1]) - end - - setupInputStream(1024, 1) do |s| - [1023, 0].each do |idx| - assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) - end - assert_equal("", s[1024, 1]) - assert_equal(nil, s[1025, 1]) - end - - setupInputStream(1, 1) do |s| - [0].each do |idx| - assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s) - end - assert_equal("", s[1, 1]) - assert_equal(nil, s[2, 1]) - end - - setupInputStream(22, 11) do |s| - [0, 1, 10, 11, 20].each do |idx| - 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 - - def test_StreamBuf_get - setupInputStream(22, 1024) do |s| - [0, 1, 9, 10, 19, 20, 21].each do |idx| - 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 - - def test_StreamBuf_get_n - setupInputStream(22, 1024) do |s| - [0, 1, 9, 10, 19, 20, 21].each do |idx| - assert_equal(expStr(idx, 1), s.get(idx, 1), idx.to_s) - end - assert_equal("", s.get(22, 1)) - assert_equal(nil, s.get(23, 1)) - - assert_equal(nil, s.get(-1, 1)) - assert_equal(nil, s.get(-1, -1)) - end - end - - def test_StreamBuf_drop - setupInputStream(22, 1024) do |s| - assert_equal(expChar(0), s[0]) - assert_equal(expChar(21), s[21]) - assert_equal(nil, s[22]) - - dropped = s.drop(-1) - assert_equal(0, dropped) - assert_equal(expChar(0), s[0]) - - dropped = s.drop(0) - assert_equal(0, dropped) - assert_equal(expChar(0), s[0]) - - dropped = s.drop(1) - assert_equal(1, dropped) - assert_equal(expChar(1), s[0]) - assert_equal(expChar(2), s[1]) - - dropped = s.drop(1) - assert_equal(1, dropped) - assert_equal(expChar(2), s[0]) - assert_equal(expChar(3), s[1]) - end - - setupInputStream(4, 2) do |s| - dropped = s.drop(2) - assert_equal(2, dropped) - assert_equal(expChar(2), s[0]) - assert_equal(expChar(3), s[1]) - dropped = s.drop(1) - assert_equal(1, dropped) - assert_equal(expChar(3), s[0]) - assert_equal(nil, s[1]) - dropped = s.drop(1) - assert_equal(1, dropped) - assert_equal(nil, s[0]) - assert_equal(nil, s[1]) - dropped = s.drop(0) - assert_equal(0, dropped) - assert_equal(nil, s[0]) - assert_equal(nil, s[1]) - end - - setupInputStream(6, 3) do |s| - dropped = s.drop(2) - assert_equal(2, dropped) - dropped = s.drop(2) - assert_equal(2, dropped) - assert_equal(expChar(4), s[0]) - assert_equal(expChar(5), s[1]) - dropped = s.drop(3) - assert_equal(2, dropped) - assert_equal(nil, s[0]) - assert_equal(nil, s[1]) - end - end - - def test_StreamBuf_is_eos? - setupInputStream(3, 1024) do |s| - assert(!s.is_eos?) - s.drop(1) - assert(!s.is_eos?) - s.drop(1) - assert(!s.is_eos?) - s.drop(1) - assert(s.is_eos?) - s.drop(1) - assert(s.is_eos?) - end - - setupInputStream(3, 2) do |s| - assert(!s.is_eos?) - s.drop(1) - assert(!s.is_eos?) - s.drop(1) - assert(!s.is_eos?) - s.drop(1) - assert(s.is_eos?) - s.drop(1) - assert(s.is_eos?) - end - end - - def test_StreamBuf_s_new - # NotImplementedError should be raised from StreamBuf#read. - assert_raises(NotImplementedError) do - CSV::StreamBuf.new - end - end - - def test_IOBuf_close - f = File.open(@outfile, "wb") - f << "tst" - f.close - - f = File.open(@outfile, "rb") - iobuf = CSV::IOBuf.new(f) - iobuf.close - assert(true) # iobuf.close does not raise any exception. - f.close - end - - def test_IOBuf_s_new - iobuf = CSV::IOBuf.new(Tempfile.new("in.csv")) - assert_instance_of(CSV::IOBuf, iobuf) - end - - - #### CSV functional test - - # sample data - # - # 1 2 3 4 5 6 7 8 - # +------+-------+---------+-------+--------+------+----+------+ - # | foo | "foo" | foo,bar | "" |(empty) |(null)| \r | \r\n | - # +------+-------+---------+-------+--------+------+----+------+ - # | NaHi | "Na" | Na,Hi | \r.\n | \r\n\n | " | \n | \r\n | - # +------+-------+---------+-------+--------+------+----+------+ - # - def test_s_parseAndCreate - colSize = 8 - 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 = [] - idx = 0 - col, idx = CSV::parse_row(myStr, 0, res1) - col, idx = CSV::parse_row(myStr, idx, res2) - - buf = '' - col = CSV::generate_row(res1, colSize, buf) - col = CSV::generate_row(res2, colSize, buf) - assert_equal(csvStrTerminated, buf) - - parsed = [] - CSV::Reader.parse(csvStrTerminated) do |row| - parsed << row - end - - buf = '' - CSV::Writer.generate(buf) do |writer| - parsed.each do |row| - writer.add_row(row) - end - end - assert_equal(csvStrTerminated, buf) - - buf = '' - CSV::Writer.generate(buf) do |writer| - parsed.each do |row| - writer << row - end - end - assert_equal(csvStrTerminated, buf) - 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) - - 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 -end