test/tc/csvutil.rb in rio-0.4.2 vs test/tc/csvutil.rb in rio-0.5.1
- old
+ new
@@ -2,12 +2,10 @@
if $0 == __FILE__
Dir.chdir File.dirname(__FILE__)+'/../'
$:.unshift File.expand_path('../lib/')
end
-require 'rio'
-
module CSV_Util
def records_to_rows(records)
n_records = records.size
rows = []
head = records[0]
@@ -19,37 +17,76 @@
end
rows << row
end
rows
end
- def records_to_strings(records)
- records.map { |values| values.join(',') }
+ def records_to_strings(records,fs=',')
+ records.map { |values| values.join(fs) }
end
- def records_to_string(records)
- records_to_strings(records).join("\n") + "\n"
+ def records_to_string(records,fs=',',rs=$/)
+ records_to_strings(records,fs).join(rs) + rs
end
- def strings_to_string(strings)
- strings.join("\n") + "\n"
+ def strings_to_string(strings,rs=$/)
+ strings.join(rs) + rs
end
- def strings_to_lines(strings)
- strings.map { |s| s + "\n" }
+ def strings_to_lines(strings,rs=$/)
+ strings.map { |s| s + rs }
end
def create_test_csv_records(n_rows,n_cols,header=true)
records = []
- records << (0...n_cols).map { |n| "Head#{n}" } if header
+ records << (0...n_cols).map { |n| %{Head#{n}} } if header
(0...n_rows).each do |nrow|
- records << (0...n_cols).map { |n| "Dat#{nrow}#{n}" }
+ records << (0...n_cols).map { |n| %{Dat#{nrow}#{n}} }
end
records
end
- def create_test_csv_data(frio,n_rows,n_cols,header=true)
+ def tcsv_gen_header_fields(n_cols)
+ (0...n_cols).map { |n| %{Head#{n}} }
+ end
+ def tcsv_gen_data_fields(nrow,n_cols)
+ (0...n_cols).map { |n| %{Dat#{nrow}#{n}} }
+ end
+ def tcsv_gen_recs(n_rows,n_cols,header=true)
+ recs = []
+ recs << tcsv_gen_header_fields(n_cols) if header
+ (0...n_rows).each do |nrow|
+ recs << tcsv_gen_data_fields(nrow,n_cols)
+ end
+ recs
+ end
+
+
+ def tcsv_rec_strings(recs,fs,qc)
+ tcsv_rec_lines(recs,fs,"",qc).map{ |s| s.chomp }
+ end
+
+ def tcsv_rec_lines(recs,fs,rs,qc)
+ lines = []
+ recs.each do |rec|
+ lines << ::CSV.generate_line(rec,{:col_sep => fs,
+ :row_sep => $/,
+ :quote_char => qc}).chomp + $/
+ end
+ lines
+ end
+
+ def create_test_csv_file(frio,n_rows,n_cols,fs,rs,qc,header=true)
+ records = tcsv_gen_recs(n_rows,n_cols,header)
+ lines = tcsv_rec_lines(records,fs,rs,qc)
+ strings = tcsv_rec_strings(records,fs,qc)
+ string = strings.join($/) + $/
+ frio < string
+ [records,strings,lines,string]
+ end
+
+ def create_test_csv_data(frio,n_rows,n_cols,fs,rs,header=true)
records = create_test_csv_records(n_rows,n_cols,header)
- strings = records_to_strings(records)
- lines = strings_to_lines(strings)
- string = strings_to_string(strings)
+ strings = records_to_strings(records,fs)
+ lines = strings_to_lines(strings,rs)
+ string = strings_to_string(strings,rs)
frio < string
[records,strings,lines,string]
end
end