* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
#!/usr/bin/env ruby -w
|
|
|
|
# encoding: UTF-8
|
2007-12-24 21:46:26 -05:00
|
|
|
|
|
|
|
# tc_interface.rb
|
|
|
|
#
|
|
|
|
# Created by James Edward Gray II on 2005-10-31.
|
|
|
|
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
|
|
|
# under the terms of Ruby's license.
|
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
|
|
|
|
require "csv"
|
|
|
|
|
|
|
|
class TestCSVInterface < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@path = File.join(File.dirname(__FILE__), "temp_test_data.csv")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-03-29 12:00:47 -04:00
|
|
|
File.open(@path, "wb") do |file|
|
2007-12-24 21:46:26 -05:00
|
|
|
file << "1\t2\t3\r\n"
|
|
|
|
file << "4\t5\r\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
@expected = [%w{1 2 3}, %w{4 5}]
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def teardown
|
|
|
|
File.unlink(@path)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
### Test Read Interface ###
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_foreach
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.foreach(@path, col_sep: "\t", row_sep: "\r\n") do |row|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@expected.shift, row)
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_open_and_close
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.open(@path, "r+", col_sep: "\t", row_sep: "\r\n")
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_not_nil(csv)
|
|
|
|
assert_instance_of(CSV, csv)
|
|
|
|
assert_equal(false, csv.closed?)
|
|
|
|
csv.close
|
|
|
|
assert(csv.closed?)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
ret = CSV.open(@path) do |new_csv|
|
|
|
|
csv = new_csv
|
|
|
|
assert_instance_of(CSV, new_csv)
|
2007-12-24 21:46:26 -05:00
|
|
|
"Return value."
|
|
|
|
end
|
|
|
|
assert(csv.closed?)
|
|
|
|
assert_equal("Return value.", ret)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_parse
|
2009-03-30 19:20:44 -04:00
|
|
|
data = File.binread(@path)
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal( @expected,
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.parse(data, col_sep: "\t", row_sep: "\r\n") )
|
2007-12-24 21:46:26 -05:00
|
|
|
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.parse(data, col_sep: "\t", row_sep: "\r\n") do |row|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@expected.shift, row)
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_parse_line
|
2008-10-10 11:09:34 -04:00
|
|
|
row = CSV.parse_line("1;2;3", col_sep: ";")
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(Array, row)
|
|
|
|
assert_equal(%w{1 2 3}, row)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# shortcut interface
|
2008-10-10 11:09:34 -04:00
|
|
|
row = "1;2;3".parse_csv(col_sep: ";")
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(Array, row)
|
|
|
|
assert_equal(%w{1 2 3}, row)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_read_and_readlines
|
|
|
|
assert_equal( @expected,
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.read(@path, col_sep: "\t", row_sep: "\r\n") )
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal( @expected,
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.readlines(@path, col_sep: "\t", row_sep: "\r\n") )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
|
2008-10-10 11:09:34 -04:00
|
|
|
data = CSV.open(@path, col_sep: "\t", row_sep: "\r\n") do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
csv.read
|
|
|
|
end
|
|
|
|
assert_equal(@expected, data)
|
2008-10-10 11:09:34 -04:00
|
|
|
data = CSV.open(@path, col_sep: "\t", row_sep: "\r\n") do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
csv.readlines
|
|
|
|
end
|
|
|
|
assert_equal(@expected, data)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_table
|
2008-10-10 11:09:34 -04:00
|
|
|
table = CSV.table(@path, col_sep: "\t", row_sep: "\r\n")
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_instance_of(CSV::Table, table)
|
|
|
|
assert_equal([[:"1", :"2", :"3"], [4, 5, nil]], table.to_a)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_shift # aliased as gets() and readline()
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open(@path, "rb+", col_sep: "\t", row_sep: "\r\n") do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@expected.shift, csv.shift)
|
|
|
|
assert_equal(@expected.shift, csv.shift)
|
|
|
|
assert_equal(nil, csv.shift)
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
### Test Write Interface ###
|
|
|
|
|
|
|
|
def test_generate
|
|
|
|
str = CSV.generate do |csv| # default empty String
|
|
|
|
assert_instance_of(CSV, csv)
|
|
|
|
assert_equal(csv, csv << [1, 2, 3])
|
|
|
|
assert_equal(csv, csv << [4, nil, 5])
|
|
|
|
end
|
|
|
|
assert_not_nil(str)
|
|
|
|
assert_instance_of(String, str)
|
|
|
|
assert_equal("1,2,3\n4,,5\n", str)
|
|
|
|
|
|
|
|
CSV.generate(str) do |csv| # appending to a String
|
|
|
|
assert_equal(csv, csv << ["last", %Q{"row"}])
|
|
|
|
end
|
|
|
|
assert_equal(%Q{1,2,3\n4,,5\nlast,"""row"""\n}, str)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_generate_line
|
2008-10-10 11:09:34 -04:00
|
|
|
line = CSV.generate_line(%w{1 2 3}, col_sep: ";")
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_not_nil(line)
|
|
|
|
assert_instance_of(String, line)
|
|
|
|
assert_equal("1;2;3\n", line)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# shortcut interface
|
2008-10-10 11:09:34 -04:00
|
|
|
line = %w{1 2 3}.to_csv(col_sep: ";")
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_not_nil(line)
|
|
|
|
assert_instance_of(String, line)
|
|
|
|
assert_equal("1;2;3\n", line)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_header_detection
|
|
|
|
File.unlink(@path)
|
|
|
|
|
|
|
|
headers = %w{a b c}
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.open(@path, "w", headers: true) do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
csv << headers
|
|
|
|
csv << %w{1 2 3}
|
|
|
|
assert_equal(headers, csv.instance_variable_get(:@headers))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_lineno
|
|
|
|
File.unlink(@path)
|
|
|
|
|
|
|
|
CSV.open(@path, "w") do |csv|
|
|
|
|
lines = 20
|
|
|
|
lines.times { csv << %w{a b c} }
|
|
|
|
assert_equal(lines, csv.lineno)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_hash
|
|
|
|
File.unlink(@path)
|
|
|
|
|
2008-10-10 11:09:34 -04:00
|
|
|
lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
|
|
|
|
CSV.open( @path, "w", headers: true,
|
|
|
|
header_converters: :symbol ) do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
csv << lines.first.keys
|
|
|
|
lines.each { |line| csv << line }
|
|
|
|
end
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.open( @path, "w", headers: true,
|
|
|
|
converters: :all,
|
|
|
|
header_converters: :symbol ) do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
def test_write_hash_with_headers_array
|
|
|
|
File.unlink(@path)
|
|
|
|
|
2008-10-10 11:09:34 -04:00
|
|
|
lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open(@path, "wb", headers: [:b, :a, :c]) do |csv|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
lines.each { |line| csv << line }
|
|
|
|
end
|
|
|
|
|
|
|
|
# test writing fields in the correct order
|
2009-03-30 19:20:44 -04:00
|
|
|
File.open(@path, "rb") do |f|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
assert_equal("2,1,3", f.gets.strip)
|
|
|
|
assert_equal("5,4,6", f.gets.strip)
|
|
|
|
end
|
|
|
|
|
|
|
|
# test reading CSV with headers
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open( @path, "rb", headers: [:b, :a, :c],
|
|
|
|
converters: :all ) do |csv|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_hash_with_headers_string
|
|
|
|
File.unlink(@path)
|
|
|
|
|
|
|
|
lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}]
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open(@path, "wb", headers: "b|a|c", col_sep: "|") do |csv|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
lines.each { |line| csv << line }
|
|
|
|
end
|
|
|
|
|
|
|
|
# test writing fields in the correct order
|
2009-03-30 19:20:44 -04:00
|
|
|
File.open(@path, "rb") do |f|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
assert_equal("2|1|3", f.gets.strip)
|
|
|
|
assert_equal("5|4|6", f.gets.strip)
|
|
|
|
end
|
|
|
|
|
|
|
|
# test reading CSV with headers
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open( @path, "rb", headers: "b|a|c",
|
|
|
|
col_sep: "|",
|
|
|
|
converters: :all ) do |csv|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
def test_write_headers
|
|
|
|
File.unlink(@path)
|
|
|
|
|
|
|
|
lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}]
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open( @path, "wb", headers: "b|a|c",
|
|
|
|
write_headers: true,
|
|
|
|
col_sep: "|" ) do |csv|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
lines.each { |line| csv << line }
|
|
|
|
end
|
|
|
|
|
|
|
|
# test writing fields in the correct order
|
2009-03-30 19:20:44 -04:00
|
|
|
File.open(@path, "rb") do |f|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
assert_equal("b|a|c", f.gets.strip)
|
|
|
|
assert_equal("2|1|3", f.gets.strip)
|
|
|
|
assert_equal("5|4|6", f.gets.strip)
|
|
|
|
end
|
|
|
|
|
|
|
|
# test reading CSV with headers
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open( @path, "rb", headers: true,
|
|
|
|
col_sep: "|",
|
|
|
|
converters: :all ) do |csv|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_append # aliased add_row() and puts()
|
|
|
|
File.unlink(@path)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open(@path, "wb", col_sep: "\t", row_sep: "\r\n") do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
@expected.each { |row| csv << row }
|
|
|
|
end
|
|
|
|
|
|
|
|
test_shift
|
|
|
|
|
|
|
|
# same thing using CSV::Row objects
|
|
|
|
File.unlink(@path)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-03-30 19:20:44 -04:00
|
|
|
CSV.open(@path, "wb", col_sep: "\t", row_sep: "\r\n") do |csv|
|
2007-12-24 21:46:26 -05:00
|
|
|
@expected.each { |row| csv << CSV::Row.new(Array.new, row) }
|
|
|
|
end
|
|
|
|
|
|
|
|
test_shift
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
### Test Read and Write Interface ###
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_filter
|
|
|
|
assert_respond_to(CSV, :filter)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
expected = [[1, 2, 3], [4, 5]]
|
|
|
|
CSV.filter( "1;2;3\n4;5\n", (result = String.new),
|
2008-10-10 11:09:34 -04:00
|
|
|
in_col_sep: ";", out_col_sep: ",",
|
|
|
|
converters: :all ) do |row|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(row, expected.shift)
|
|
|
|
row.map! { |n| n * 2 }
|
|
|
|
row << "Added\r"
|
|
|
|
end
|
|
|
|
assert_equal("2,4,6,\"Added\r\"\n8,10,\"Added\r\"\n", result)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_instance
|
|
|
|
csv = String.new
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
first = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
first = CSV.instance(csv, col_sep: ";")
|
2007-12-24 21:46:26 -05:00
|
|
|
first << %w{a b c}
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal("a;b;c\n", csv)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
second = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
second = CSV.instance(csv, col_sep: ";")
|
2007-12-24 21:46:26 -05:00
|
|
|
second << [1, 2, 3]
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(first.object_id, second.object_id)
|
|
|
|
assert_equal("a;b;c\n1;2;3\n", csv)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# shortcuts
|
|
|
|
assert_equal(STDOUT, CSV.instance.instance_eval { @io })
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
assert_equal(STDOUT, CSV { |new_csv| new_csv.instance_eval { @io } })
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
end
|