mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/csv/csv.rb: Added support for Encoding::default_internal.
* lib/csv/csv.rb: Switched to new Hash syntax. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19751 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
aa2797ee41
commit
7d3d353558
12 changed files with 184 additions and 179 deletions
|
@ -1,3 +1,8 @@
|
|||
Sat Oct 11 12:09:05 2008 James Edward Gray II <jeg2@ruby-lang.org>
|
||||
|
||||
* lib/csv/csv.rb: Added support for Encoding::default_internal.
|
||||
* lib/csv/csv.rb: Switched to new Hash syntax.
|
||||
|
||||
Fri Oct 10 22:16:55 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* parse.y (comment_at_top): needed for ripper too.
|
||||
|
|
84
lib/csv.rb
84
lib/csv.rb
|
@ -199,7 +199,7 @@ require "stringio"
|
|||
#
|
||||
class CSV
|
||||
# The version of the installed library.
|
||||
VERSION = "2.4.2".freeze
|
||||
VERSION = "2.4.3".freeze
|
||||
|
||||
#
|
||||
# A CSV::Row is part Array and part Hash. It retains an order for the fields
|
||||
|
@ -885,14 +885,14 @@ class CSV
|
|||
# To add a combo field, the value should be an Array of names. Combo fields
|
||||
# can be nested with other combo fields.
|
||||
#
|
||||
Converters = { :integer => lambda { |f|
|
||||
Converters = { integer: lambda { |f|
|
||||
Integer(f.encode(ConverterEncoding)) rescue f
|
||||
},
|
||||
:float => lambda { |f|
|
||||
float: lambda { |f|
|
||||
Float(f.encode(ConverterEncoding)) rescue f
|
||||
},
|
||||
:numeric => [:integer, :float],
|
||||
:date => lambda { |f|
|
||||
numeric: [:integer, :float],
|
||||
date: lambda { |f|
|
||||
begin
|
||||
e = f.encode(ConverterEncoding)
|
||||
e =~ DateMatcher ? Date.parse(e) : f
|
||||
|
@ -900,7 +900,7 @@ class CSV
|
|||
f
|
||||
end
|
||||
},
|
||||
:date_time => lambda { |f|
|
||||
date_time: lambda { |f|
|
||||
begin
|
||||
e = f.encode(ConverterEncoding)
|
||||
e =~ DateTimeMatcher ? DateTime.parse(e) : f
|
||||
|
@ -908,7 +908,7 @@ class CSV
|
|||
f
|
||||
end
|
||||
},
|
||||
:all => [:date_time, :numeric] }
|
||||
all: [:date_time, :numeric] }
|
||||
|
||||
#
|
||||
# This Hash holds the built-in header converters of CSV that can be accessed
|
||||
|
@ -931,8 +931,8 @@ class CSV
|
|||
# can be nested with other combo fields.
|
||||
#
|
||||
HeaderConverters = {
|
||||
:downcase => lambda { |h| h.encode(ConverterEncoding).downcase },
|
||||
:symbol => lambda { |h|
|
||||
downcase: lambda { |h| h.encode(ConverterEncoding).downcase },
|
||||
symbol: lambda { |h|
|
||||
h.encode(ConverterEncoding).downcase.gsub(/\s+/, "_").
|
||||
gsub(/\W+/, "").to_sym
|
||||
}
|
||||
|
@ -953,17 +953,17 @@ class CSV
|
|||
# <b><tt>:skip_blanks</tt></b>:: +false+
|
||||
# <b><tt>:force_quotes</tt></b>:: +false+
|
||||
#
|
||||
DEFAULT_OPTIONS = { :col_sep => ",",
|
||||
:row_sep => :auto,
|
||||
:quote_char => '"',
|
||||
:field_size_limit => nil,
|
||||
:converters => nil,
|
||||
:unconverted_fields => nil,
|
||||
:headers => false,
|
||||
:return_headers => false,
|
||||
:header_converters => nil,
|
||||
:skip_blanks => false,
|
||||
:force_quotes => false }.freeze
|
||||
DEFAULT_OPTIONS = { col_sep: ",",
|
||||
row_sep: :auto,
|
||||
quote_char: '"',
|
||||
field_size_limit: nil,
|
||||
converters: nil,
|
||||
unconverted_fields: nil,
|
||||
headers: false,
|
||||
return_headers: false,
|
||||
header_converters: nil,
|
||||
skip_blanks: false,
|
||||
force_quotes: false }.freeze
|
||||
|
||||
#
|
||||
# This method will return a CSV instance, just like CSV::new(), but the
|
||||
|
@ -1143,7 +1143,7 @@ class CSV
|
|||
#
|
||||
def self.filter(*args)
|
||||
# parse options for input, output, or both
|
||||
in_options, out_options = Hash.new, {:row_sep => $INPUT_RECORD_SEPARATOR}
|
||||
in_options, out_options = Hash.new, {row_sep: $INPUT_RECORD_SEPARATOR}
|
||||
if args.last.is_a? Hash
|
||||
args.pop.each do |key, value|
|
||||
case key.to_s
|
||||
|
@ -1179,8 +1179,8 @@ class CSV
|
|||
# this unless your data is in Encoding::default_external(). CSV will use this
|
||||
# to deterime how to parse the data. You may provide a second Encoding to
|
||||
# have the data transcoded as it is read. For example,
|
||||
# <tt>:encoding => "UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the
|
||||
# file but transcode it to UTF-8 before CSV parses it.
|
||||
# <tt>encoding: "UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file
|
||||
# but transcode it to UTF-8 before CSV parses it.
|
||||
#
|
||||
def self.foreach(path, options = Hash.new, &block)
|
||||
encoding = options.delete(:encoding)
|
||||
|
@ -1240,7 +1240,7 @@ class CSV
|
|||
# (<tt>$/</tt>) when calling this method.
|
||||
#
|
||||
def self.generate_line(row, options = Hash.new)
|
||||
options = {:row_sep => $INPUT_RECORD_SEPARATOR}.merge(options)
|
||||
options = {row_sep: $INPUT_RECORD_SEPARATOR}.merge(options)
|
||||
encoding = options.delete(:encoding)
|
||||
str = ""
|
||||
if encoding
|
||||
|
@ -1378,8 +1378,8 @@ class CSV
|
|||
# your data is in Encoding::default_external(). CSV will use this to deterime
|
||||
# how to parse the data. You may provide a second Encoding to have the data
|
||||
# transcoded as it is read. For example,
|
||||
# <tt>:encoding => "UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the
|
||||
# file but transcode it to UTF-8 before CSV parses it.
|
||||
# <tt>encoding: "UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file
|
||||
# but transcode it to UTF-8 before CSV parses it.
|
||||
#
|
||||
def self.read(path, options = Hash.new)
|
||||
encoding = options.delete(:encoding)
|
||||
|
@ -1396,14 +1396,14 @@ class CSV
|
|||
#
|
||||
# A shortcut for:
|
||||
#
|
||||
# CSV.read( path, { :headers => true,
|
||||
# :converters => :numeric,
|
||||
# :header_converters => :symbol }.merge(options) )
|
||||
# CSV.read( path, { headers: true,
|
||||
# converters: :numeric,
|
||||
# header_converters: :symbol }.merge(options) )
|
||||
#
|
||||
def self.table(path, options = Hash.new)
|
||||
read( path, { :headers => true,
|
||||
:converters => :numeric,
|
||||
:header_converters => :symbol }.merge(options) )
|
||||
read( path, { headers: true,
|
||||
converters: :numeric,
|
||||
header_converters: :symbol }.merge(options) )
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -1544,12 +1544,12 @@ class CSV
|
|||
# create the IO object we will read from
|
||||
@io = if data.is_a? String then StringIO.new(data) else data end
|
||||
# honor the IO encoding if we can, otherwise default to ASCII-8BIT
|
||||
@encoding = if @io.respond_to? :internal_encoding
|
||||
@encoding = if @io.respond_to? :internal_encoding
|
||||
@io.internal_encoding || @io.external_encoding
|
||||
elsif @io.is_a? StringIO
|
||||
@io.string.encoding
|
||||
end
|
||||
@encoding ||= Encoding.default_external
|
||||
@encoding ||= Encoding.default_internal || Encoding.default_external
|
||||
#
|
||||
# prepare for build safe regular expressions in the target encoding,
|
||||
# if we can transcode the needed characters
|
||||
|
@ -2038,9 +2038,9 @@ class CSV
|
|||
esc_quote = escape_re(@quote_char)
|
||||
@parsers = {
|
||||
# for empty leading fields
|
||||
:leading_fields => encode_re("\\A(?:", esc_col_sep, ")+"),
|
||||
leading_fields: encode_re("\\A(?:", esc_col_sep, ")+"),
|
||||
# The Primary Parser
|
||||
:csv_row => encode_re(
|
||||
csv_row: encode_re(
|
||||
"\\G(?:\\A|", esc_col_sep, ")", # anchor the match
|
||||
"(?:", esc_quote, # find quoted fields
|
||||
"((?>[^", esc_quote, "]*)", # "unrolling the loop"
|
||||
|
@ -2052,7 +2052,7 @@ class CSV
|
|||
"(?=", esc_col_sep, "|\\z)" # ensure field is ended
|
||||
),
|
||||
# a test for unescaped quotes
|
||||
:bad_field => encode_re(
|
||||
bad_field: encode_re(
|
||||
"\\A", esc_col_sep, "?", # an optional comma
|
||||
"(?:", esc_quote, # a quoted field
|
||||
"(?>[^", esc_quote, "]*)", # "unrolling the loop"
|
||||
|
@ -2065,9 +2065,9 @@ class CSV
|
|||
esc_quote, ")" # an extra quote
|
||||
),
|
||||
# safer than chomp!()
|
||||
:line_end => encode_re(esc_row_sep, "\\z"),
|
||||
line_end: encode_re(esc_row_sep, "\\z"),
|
||||
# illegal unquoted characters
|
||||
:return_newline => encode_str("\r\n")
|
||||
return_newline: encode_str("\r\n")
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -2189,9 +2189,9 @@ class CSV
|
|||
# CSV header String
|
||||
when String
|
||||
self.class.parse_line( @use_headers,
|
||||
:col_sep => @col_sep,
|
||||
:row_sep => @row_sep,
|
||||
:quote_char => @quote_char )
|
||||
col_sep: @col_sep,
|
||||
row_sep: @row_sep,
|
||||
quote_char: @quote_char )
|
||||
# first row is headers
|
||||
else row
|
||||
end
|
||||
|
|
|
@ -117,7 +117,7 @@ class TestCSVParsing < Test::Unit::TestCase
|
|||
|
||||
def test_malformed_csv
|
||||
assert_raise(CSV::MalformedCSVError) do
|
||||
CSV.parse_line("1,2\r,3", :row_sep => "\n")
|
||||
CSV.parse_line("1,2\r,3", row_sep: "\n")
|
||||
end
|
||||
|
||||
bad_data = <<-END_DATA.gsub(/^ +/, "")
|
||||
|
@ -176,7 +176,7 @@ class TestCSVParsing < Test::Unit::TestCase
|
|||
|
||||
def test_field_size_limit_controls_lookahead
|
||||
assert_parse_errors_out( 'valid,fields,"' + BIG_DATA + '"',
|
||||
:field_size_limit => 2048 )
|
||||
field_size_limit: 2048 )
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -79,19 +79,19 @@ class TestCSVWriting < Test::Unit::TestCase
|
|||
|
||||
def test_col_sep
|
||||
assert_equal( "a;b;;c\n", CSV.generate_line( ["a", "b", nil, "c"],
|
||||
:col_sep => ";" ) )
|
||||
col_sep: ";" ) )
|
||||
assert_equal( "a\tb\t\tc\n", CSV.generate_line( ["a", "b", nil, "c"],
|
||||
:col_sep => "\t" ) )
|
||||
col_sep: "\t" ) )
|
||||
end
|
||||
|
||||
def test_row_sep
|
||||
assert_equal( "a,b,,c\r\n", CSV.generate_line( ["a", "b", nil, "c"],
|
||||
:row_sep => "\r\n" ) )
|
||||
row_sep: "\r\n" ) )
|
||||
end
|
||||
|
||||
def test_force_quotes
|
||||
assert_equal( %Q{"1","b","","already ""quoted"""\n},
|
||||
CSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
|
||||
:force_quotes => true ) )
|
||||
force_quotes: true ) )
|
||||
end
|
||||
end
|
||||
|
|
|
@ -159,7 +159,7 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_convert_with_custom_code_using_field_info_header
|
||||
@parser = CSV.new(@data, :headers => %w{one two three four five})
|
||||
@parser = CSV.new(@data, headers: %w{one two three four five})
|
||||
|
||||
# define custom converter that uses field header information...
|
||||
assert_nothing_raised(Exception) do
|
||||
|
@ -175,13 +175,13 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
|
||||
def test_shortcut_interface
|
||||
assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
|
||||
CSV.parse_line(@data, :converters => :numeric) )
|
||||
CSV.parse_line(@data, converters: :numeric) )
|
||||
|
||||
assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
|
||||
CSV.parse_line(@data, :converters => [:integer, :float]) )
|
||||
CSV.parse_line(@data, converters: [:integer, :float]) )
|
||||
|
||||
assert_equal( ["Numbers", :integer, 1, :float, 3.015],
|
||||
CSV.parse_line(@data, :converters => [:numeric, @custom]) )
|
||||
CSV.parse_line(@data, converters: [:numeric, @custom]) )
|
||||
end
|
||||
|
||||
def test_unconverted_fields
|
||||
|
@ -192,8 +192,8 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
row = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
row = CSV.parse_line( test,
|
||||
:converters => [:numeric, @custom],
|
||||
:unconverted_fields => true )
|
||||
converters: [:numeric, @custom],
|
||||
unconverted_fields: true )
|
||||
end
|
||||
assert_not_nil(row)
|
||||
assert_equal(fields, row)
|
||||
|
@ -208,9 +208,9 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
row = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
row = CSV.parse_line( data,
|
||||
:converters => :numeric,
|
||||
:unconverted_fields => true,
|
||||
:headers => :first_row )
|
||||
converters: :numeric,
|
||||
unconverted_fields: true,
|
||||
headers: :first_row )
|
||||
end
|
||||
assert_not_nil(row)
|
||||
assert_equal([["first", 1], ["second", 2], ["third", 3]], row.to_a)
|
||||
|
@ -219,10 +219,10 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
|
||||
assert_nothing_raised(Exception) do
|
||||
row = CSV.parse_line( data,
|
||||
:converters => :numeric,
|
||||
:unconverted_fields => true,
|
||||
:headers => :first_row,
|
||||
:return_headers => true )
|
||||
converters: :numeric,
|
||||
unconverted_fields: true,
|
||||
headers: :first_row,
|
||||
return_headers: true )
|
||||
end
|
||||
assert_not_nil(row)
|
||||
assert_equal( [%w{first first}, %w{second second}, %w{third third}],
|
||||
|
@ -232,11 +232,11 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
|
||||
assert_nothing_raised(Exception) do
|
||||
row = CSV.parse_line( data,
|
||||
:converters => :numeric,
|
||||
:unconverted_fields => true,
|
||||
:headers => :first_row,
|
||||
:return_headers => true,
|
||||
:header_converters => :symbol )
|
||||
converters: :numeric,
|
||||
unconverted_fields: true,
|
||||
headers: :first_row,
|
||||
return_headers: true,
|
||||
header_converters: :symbol )
|
||||
end
|
||||
assert_not_nil(row)
|
||||
assert_equal( [[:first, "first"], [:second, "second"], [:third, "third"]],
|
||||
|
@ -246,11 +246,11 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
|
||||
assert_nothing_raised(Exception) do
|
||||
row = CSV.parse_line( data,
|
||||
:converters => :numeric,
|
||||
:unconverted_fields => true,
|
||||
:headers => %w{my new headers},
|
||||
:return_headers => true,
|
||||
:header_converters => :symbol )
|
||||
converters: :numeric,
|
||||
unconverted_fields: true,
|
||||
headers: %w{my new headers},
|
||||
return_headers: true,
|
||||
header_converters: :symbol )
|
||||
end
|
||||
assert_not_nil(row)
|
||||
assert_equal( [[:my, "my"], [:new, "new"], [:headers, "headers"]],
|
||||
|
|
|
@ -67,7 +67,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
each_encoding do |encoding|
|
||||
begin
|
||||
assert_parses( [ %w[ abc def ],
|
||||
%w[ ghi jkl ] ], encoding, :col_sep => "|" )
|
||||
%w[ ghi jkl ] ], encoding, col_sep: "|" )
|
||||
rescue Encoding::ConverterNotFoundError
|
||||
fail("Failed to properly escape #{encoding.name}.")
|
||||
end
|
||||
|
@ -80,7 +80,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
|
||||
def test_auto_line_ending_detection
|
||||
# arrange data to place a \r at the end of CSV's read ahead point
|
||||
encode_for_tests([["a" * 509]], :row_sep => "\r\n") do |data|
|
||||
encode_for_tests([["a" * 509]], row_sep: "\r\n") do |data|
|
||||
assert_equal("\r\n".encode(data.encoding), CSV.new(data).row_sep)
|
||||
end
|
||||
end
|
||||
|
@ -96,7 +96,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
|
||||
def test_parser_works_with_encoded_headers
|
||||
encode_for_tests([%w[one two three], %w[1 2 3]]) do |data|
|
||||
parsed = CSV.parse(data, :headers => true)
|
||||
parsed = CSV.parse(data, headers: true)
|
||||
assert( parsed.headers.all? { |h| h.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
parsed.each do |row|
|
||||
|
@ -108,7 +108,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
|
||||
def test_built_in_converters_transcode_to_utf_8_then_convert
|
||||
encode_for_tests([%w[one two three], %w[1 2 3]]) do |data|
|
||||
parsed = CSV.parse(data, :converters => :integer)
|
||||
parsed = CSV.parse(data, converters: :integer)
|
||||
assert( parsed[0].all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_equal([1, 2, 3], parsed[1])
|
||||
|
@ -117,8 +117,8 @@ class TestEncodings < Test::Unit::TestCase
|
|||
|
||||
def test_built_in_header_converters_transcode_to_utf_8_then_convert
|
||||
encode_for_tests([%w[one two three], %w[1 2 3]]) do |data|
|
||||
parsed = CSV.parse( data, :headers => true,
|
||||
:header_converters => :downcase )
|
||||
parsed = CSV.parse( data, headers: true,
|
||||
header_converters: :downcase )
|
||||
assert( parsed.headers.all? { |h| h.encoding.name == "UTF-8" },
|
||||
"Wrong data encoding." )
|
||||
assert( parsed[0].fields.all? { |f| f.encoding == data.encoding },
|
||||
|
@ -154,7 +154,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
encode_for_tests([%w[abc def]]) do |data|
|
||||
# read and write in encoding
|
||||
File.open(@temp_csv_path, "wb:#{data.encoding.name}") { |f| f << data }
|
||||
CSV.foreach(@temp_csv_path, :encoding => data.encoding.name) do |row|
|
||||
CSV.foreach(@temp_csv_path, encoding: data.encoding.name) do |row|
|
||||
assert( row.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
end
|
||||
|
@ -164,7 +164,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
f << data
|
||||
end
|
||||
CSV.foreach( @temp_csv_path,
|
||||
:encoding => "UTF-32BE:#{data.encoding.name}" ) do |row|
|
||||
encoding: "UTF-32BE:#{data.encoding.name}" ) do |row|
|
||||
assert( row.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
end
|
||||
|
@ -175,7 +175,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
encode_for_tests([%w[abc def]]) do |data|
|
||||
# read and write in encoding
|
||||
File.open(@temp_csv_path, "wb:#{data.encoding.name}") { |f| f << data }
|
||||
rows = CSV.read(@temp_csv_path, :encoding => data.encoding.name)
|
||||
rows = CSV.read(@temp_csv_path, encoding: data.encoding.name)
|
||||
assert( rows.flatten.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
|
||||
|
@ -184,7 +184,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
f << data
|
||||
end
|
||||
rows = CSV.read( @temp_csv_path,
|
||||
:encoding => "UTF-32BE:#{data.encoding.name}" )
|
||||
encoding: "UTF-32BE:#{data.encoding.name}" )
|
||||
assert( rows.flatten.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
end
|
||||
|
@ -198,11 +198,11 @@ class TestEncodings < Test::Unit::TestCase
|
|||
each_encoding do |encoding|
|
||||
# test generate_line with encoding hint
|
||||
csv = %w[abc d|ef].map { |f| f.encode(encoding) }.
|
||||
to_csv(:col_sep => "|", :encoding => encoding.name)
|
||||
to_csv(col_sep: "|", encoding: encoding.name)
|
||||
assert_equal(encoding, csv.encoding)
|
||||
|
||||
# test generate_line with encoding guessing from fields
|
||||
csv = %w[abc d|ef].map { |f| f.encode(encoding) }.to_csv(:col_sep => "|")
|
||||
csv = %w[abc d|ef].map { |f| f.encode(encoding) }.to_csv(col_sep: "|")
|
||||
assert_equal(encoding, csv.encoding)
|
||||
|
||||
# writing to files
|
||||
|
@ -210,7 +210,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
CSV.open(@temp_csv_path, "wb:#{encoding.name}") do |f|
|
||||
data.each { |row| f << row }
|
||||
end
|
||||
assert_equal(data, CSV.read(@temp_csv_path, :encoding => encoding.name))
|
||||
assert_equal(data, CSV.read(@temp_csv_path, encoding: encoding.name))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -46,25 +46,25 @@ class TestCSVFeatures < Test::Unit::TestCase
|
|||
TEST_CASES.each do |test_case|
|
||||
assert_equal( test_case.last.map { |t| t.tr(",", sep) unless t.nil? },
|
||||
CSV.parse_line( test_case.first.tr(",", sep),
|
||||
:col_sep => sep ) )
|
||||
col_sep: sep ) )
|
||||
end
|
||||
end
|
||||
assert_equal([",,,", nil], CSV.parse_line(",,,;", :col_sep => ";"))
|
||||
assert_equal([",,,", nil], CSV.parse_line(",,,;", col_sep: ";"))
|
||||
end
|
||||
|
||||
def test_row_sep
|
||||
assert_raise(CSV::MalformedCSVError) do
|
||||
CSV.parse_line("1,2,3\n,4,5\r\n", :row_sep => "\r\n")
|
||||
CSV.parse_line("1,2,3\n,4,5\r\n", row_sep: "\r\n")
|
||||
end
|
||||
assert_equal( ["1", "2", "3\n", "4", "5"],
|
||||
CSV.parse_line(%Q{1,2,"3\n",4,5\r\n}, :row_sep => "\r\n"))
|
||||
CSV.parse_line(%Q{1,2,"3\n",4,5\r\n}, row_sep: "\r\n"))
|
||||
end
|
||||
|
||||
def test_quote_char
|
||||
TEST_CASES.each do |test_case|
|
||||
assert_equal( test_case.last.map { |t| t.tr('"', "'") unless t.nil? },
|
||||
CSV.parse_line( test_case.first.tr('"', "'"),
|
||||
:quote_char => "'" ) )
|
||||
quote_char: "'" ) )
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -109,13 +109,13 @@ class TestCSVFeatures < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_unknown_options
|
||||
assert_raise(ArgumentError) { CSV.new(String.new, :unknown => :error) }
|
||||
assert_raise(ArgumentError) { CSV.new(String.new, unknown: :error) }
|
||||
end
|
||||
|
||||
def test_skip_blanks
|
||||
assert_equal(4, @csv.to_a.size)
|
||||
|
||||
@csv = CSV.new(@sample_data, :skip_blanks => true)
|
||||
@csv = CSV.new(@sample_data, skip_blanks: true)
|
||||
|
||||
count = 0
|
||||
@csv.each do |row|
|
||||
|
@ -138,18 +138,18 @@ class TestCSVFeatures < Test::Unit::TestCase
|
|||
def test_converters_reader
|
||||
# no change
|
||||
assert_equal( [:integer],
|
||||
CSV.new("abc,def", :converters => [:integer]).converters )
|
||||
CSV.new("abc,def", converters: [:integer]).converters )
|
||||
|
||||
# just one
|
||||
assert_equal( [:integer],
|
||||
CSV.new("abc,def", :converters => :integer).converters )
|
||||
CSV.new("abc,def", converters: :integer).converters )
|
||||
|
||||
# expanded
|
||||
assert_equal( [:integer, :float],
|
||||
CSV.new("abc,def", :converters => :numeric).converters )
|
||||
CSV.new("abc,def", converters: :numeric).converters )
|
||||
|
||||
# custom
|
||||
csv = CSV.new("abc,def", :converters => [:integer, lambda { }])
|
||||
csv = CSV.new("abc,def", converters: [:integer, lambda { }])
|
||||
assert_equal(2, csv.converters.size)
|
||||
assert_equal(:integer, csv.converters.first)
|
||||
assert_instance_of(Proc, csv.converters.last)
|
||||
|
@ -172,12 +172,12 @@ class TestCSVFeatures < Test::Unit::TestCase
|
|||
|
||||
# reported by Kev Jackson
|
||||
def test_failing_to_escape_col_sep_bug_fix
|
||||
assert_nothing_raised(Exception) { CSV.new(String.new, :col_sep => "|") }
|
||||
assert_nothing_raised(Exception) { CSV.new(String.new, col_sep: "|") }
|
||||
end
|
||||
|
||||
# reported by Chris Roos
|
||||
def test_failing_to_reset_headers_in_rewind_bug_fix
|
||||
csv = CSV.new("forename,surname", :headers => true, :return_headers => true)
|
||||
csv = CSV.new("forename,surname", headers: true, return_headers: true)
|
||||
csv.each { |row| assert row.header_row? }
|
||||
csv.rewind
|
||||
csv.each { |row| assert row.header_row? }
|
||||
|
@ -189,7 +189,7 @@ class TestCSVFeatures < Test::Unit::TestCase
|
|||
<=><=>A<=>B<=>C
|
||||
1<=>2<=>3
|
||||
END_DATA
|
||||
parsed = CSV.parse(data, :col_sep => "<=>")
|
||||
parsed = CSV.parse(data, col_sep: "<=>")
|
||||
assert_equal([[nil, nil, "A", "B", "C"], ["1", "2", "3"]], parsed)
|
||||
end
|
||||
|
||||
|
@ -243,7 +243,7 @@ class TestCSVFeatures < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_inspect_shows_headers_when_available
|
||||
CSV.new("one,two,three\n1,2,3\n", :headers => true) do |csv|
|
||||
CSV.new("one,two,three\n1,2,3\n", headers: true) do |csv|
|
||||
assert(csv.inspect.include?("headers:true"), "Header hint not shown.")
|
||||
csv.shift # load headers
|
||||
assert_match(/headers:\[[^\]]+\]/, csv.inspect)
|
||||
|
|
|
@ -25,7 +25,7 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
# activate headers
|
||||
csv = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse(@data, :headers => setting)
|
||||
csv = CSV.parse(@data, headers: setting)
|
||||
end
|
||||
|
||||
# first data row - skipping headers
|
||||
|
@ -49,7 +49,7 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
# activate headers
|
||||
csv = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse(@data, :headers => [:my, :new, :headers])
|
||||
csv = CSV.parse(@data, headers: [:my, :new, :headers])
|
||||
end
|
||||
|
||||
# first data row - skipping headers
|
||||
|
@ -76,9 +76,9 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
|
||||
# with return and convert
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse(@data, :headers => [:my, :new, :headers],
|
||||
:return_headers => true,
|
||||
:header_converters => lambda { |h| h.to_s } )
|
||||
csv = CSV.parse( @data, headers: [:my, :new, :headers],
|
||||
return_headers: true,
|
||||
header_converters: lambda { |h| h.to_s } )
|
||||
end
|
||||
row = csv[0]
|
||||
assert_not_nil(row)
|
||||
|
@ -92,7 +92,7 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
# activate headers
|
||||
csv = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse(@data, :headers => "my,new,headers")
|
||||
csv = CSV.parse(@data, headers: "my,new,headers")
|
||||
end
|
||||
|
||||
# first data row - skipping headers
|
||||
|
@ -118,9 +118,9 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
|
||||
# with return and convert
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse(@data, :headers => "my,new,headers",
|
||||
:return_headers => true,
|
||||
:header_converters => :symbol )
|
||||
csv = CSV.parse( @data, headers: "my,new,headers",
|
||||
return_headers: true,
|
||||
header_converters: :symbol )
|
||||
end
|
||||
row = csv[0]
|
||||
assert_not_nil(row)
|
||||
|
@ -134,8 +134,8 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
# parse with custom col_sep
|
||||
csv = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse( @data.tr(",", "|"), :col_sep => "|",
|
||||
:headers => "my|new|headers" )
|
||||
csv = CSV.parse( @data.tr(",", "|"), col_sep: "|",
|
||||
headers: "my|new|headers" )
|
||||
end
|
||||
|
||||
# verify headers were recognized
|
||||
|
@ -149,7 +149,7 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
# activate headers and request they are returned
|
||||
csv = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse(@data, :headers => true, :return_headers => true)
|
||||
csv = CSV.parse(@data, headers: true, return_headers: true)
|
||||
end
|
||||
|
||||
# header row
|
||||
|
@ -189,19 +189,19 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
END_MATCHING_CSV
|
||||
|
||||
# normal converters do not affect headers
|
||||
csv = CSV.parse( data, :headers => true,
|
||||
:return_headers => true,
|
||||
:converters => :numeric )
|
||||
csv = CSV.parse( data, headers: true,
|
||||
return_headers: true,
|
||||
converters: :numeric )
|
||||
assert_equal([%w{1 1}, %w{2 2}, %w{3 3}], csv[0].to_a)
|
||||
assert_equal([["1", 1], ["2", 2], ["3", 3]], csv[1].to_a)
|
||||
assert_nil(csv[2])
|
||||
|
||||
# header converters do affect headers (only)
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse( data, :headers => true,
|
||||
:return_headers => true,
|
||||
:converters => :numeric,
|
||||
:header_converters => :symbol )
|
||||
csv = CSV.parse( data, headers: true,
|
||||
return_headers: true,
|
||||
converters: :numeric,
|
||||
header_converters: :symbol )
|
||||
end
|
||||
assert_equal([[:"1", "1"], [:"2", "2"], [:"3", "3"]], csv[0].to_a)
|
||||
assert_equal([[:"1", 1], [:"2", 2], [:"3", 3]], csv[1].to_a)
|
||||
|
@ -209,32 +209,32 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_builtin_downcase_converter
|
||||
csv = CSV.parse( "One,TWO Three", :headers => true,
|
||||
:return_headers => true,
|
||||
:header_converters => :downcase )
|
||||
csv = CSV.parse( "One,TWO Three", headers: true,
|
||||
return_headers: true,
|
||||
header_converters: :downcase )
|
||||
assert_equal(%w{one two\ three}, csv.headers)
|
||||
end
|
||||
|
||||
def test_builtin_symbol_converter
|
||||
csv = CSV.parse( "One,TWO Three", :headers => true,
|
||||
:return_headers => true,
|
||||
:header_converters => :symbol )
|
||||
csv = CSV.parse( "One,TWO Three", headers: true,
|
||||
return_headers: true,
|
||||
header_converters: :symbol )
|
||||
assert_equal([:one, :two_three], csv.headers)
|
||||
end
|
||||
|
||||
def test_custom_converter
|
||||
converter = lambda { |header| header.tr(" ", "_") }
|
||||
csv = CSV.parse( "One,TWO Three",
|
||||
:headers => true,
|
||||
:return_headers => true,
|
||||
:header_converters => converter )
|
||||
headers: true,
|
||||
return_headers: true,
|
||||
header_converters: converter )
|
||||
assert_equal(%w{One TWO_Three}, csv.headers)
|
||||
end
|
||||
|
||||
def test_table_support
|
||||
csv = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
csv = CSV.parse(@data, :headers => true)
|
||||
csv = CSV.parse(@data, headers: true)
|
||||
end
|
||||
|
||||
assert_instance_of(CSV::Table, csv)
|
||||
|
@ -253,15 +253,15 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
END_CSV
|
||||
|
||||
expected = [%w[1 2 3]]
|
||||
CSV.parse(@data, :headers => true, :skip_blanks => true) do |row|
|
||||
CSV.parse(@data, headers: true, skip_blanks: true) do |row|
|
||||
assert_equal(expected.shift, row.fields)
|
||||
end
|
||||
|
||||
expected = [%w[A B C], %w[1 2 3]]
|
||||
CSV.parse( @data,
|
||||
:headers => true,
|
||||
:return_headers => true,
|
||||
:skip_blanks => true ) do |row|
|
||||
headers: true,
|
||||
return_headers: true,
|
||||
skip_blanks: true ) do |row|
|
||||
assert_equal(expected.shift, row.fields)
|
||||
end
|
||||
end
|
||||
|
@ -271,7 +271,7 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
assert_nil(CSV.new(@data).headers)
|
||||
|
||||
# headers
|
||||
csv = CSV.new(@data, :headers => true)
|
||||
csv = CSV.new(@data, headers: true)
|
||||
assert_equal(true, csv.headers) # before headers are read
|
||||
csv.shift # set headers
|
||||
assert_equal(%w[first second third], csv.headers) # after headers are read
|
||||
|
@ -281,7 +281,7 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
@data += "\n#{@data}" # add a blank row
|
||||
|
||||
# ensure that everything returned is a Row object
|
||||
CSV.parse(@data, :headers => true) do |row|
|
||||
CSV.parse(@data, headers: true) do |row|
|
||||
assert_instance_of(CSV::Row, row)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -30,13 +30,13 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
### Test Read Interface ###
|
||||
|
||||
def test_foreach
|
||||
CSV.foreach(@path, :col_sep => "\t", :row_sep => "\r\n") do |row|
|
||||
CSV.foreach(@path, col_sep: "\t", row_sep: "\r\n") do |row|
|
||||
assert_equal(@expected.shift, row)
|
||||
end
|
||||
end
|
||||
|
||||
def test_open_and_close
|
||||
csv = CSV.open(@path, "r+", :col_sep => "\t", :row_sep => "\r\n")
|
||||
csv = CSV.open(@path, "r+", col_sep: "\t", row_sep: "\r\n")
|
||||
assert_not_nil(csv)
|
||||
assert_instance_of(CSV, csv)
|
||||
assert_equal(false, csv.closed?)
|
||||
|
@ -55,21 +55,21 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
def test_parse
|
||||
data = File.read(@path)
|
||||
assert_equal( @expected,
|
||||
CSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") )
|
||||
CSV.parse(data, col_sep: "\t", row_sep: "\r\n") )
|
||||
|
||||
CSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") do |row|
|
||||
CSV.parse(data, col_sep: "\t", row_sep: "\r\n") do |row|
|
||||
assert_equal(@expected.shift, row)
|
||||
end
|
||||
end
|
||||
|
||||
def test_parse_line
|
||||
row = CSV.parse_line("1;2;3", :col_sep => ";")
|
||||
row = CSV.parse_line("1;2;3", col_sep: ";")
|
||||
assert_not_nil(row)
|
||||
assert_instance_of(Array, row)
|
||||
assert_equal(%w{1 2 3}, row)
|
||||
|
||||
# shortcut interface
|
||||
row = "1;2;3".parse_csv(:col_sep => ";")
|
||||
row = "1;2;3".parse_csv(col_sep: ";")
|
||||
assert_not_nil(row)
|
||||
assert_instance_of(Array, row)
|
||||
assert_equal(%w{1 2 3}, row)
|
||||
|
@ -77,29 +77,29 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
|
||||
def test_read_and_readlines
|
||||
assert_equal( @expected,
|
||||
CSV.read(@path, :col_sep => "\t", :row_sep => "\r\n") )
|
||||
CSV.read(@path, col_sep: "\t", row_sep: "\r\n") )
|
||||
assert_equal( @expected,
|
||||
CSV.readlines(@path, :col_sep => "\t", :row_sep => "\r\n") )
|
||||
CSV.readlines(@path, col_sep: "\t", row_sep: "\r\n") )
|
||||
|
||||
|
||||
data = CSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
|
||||
data = CSV.open(@path, col_sep: "\t", row_sep: "\r\n") do |csv|
|
||||
csv.read
|
||||
end
|
||||
assert_equal(@expected, data)
|
||||
data = CSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
|
||||
data = CSV.open(@path, col_sep: "\t", row_sep: "\r\n") do |csv|
|
||||
csv.readlines
|
||||
end
|
||||
assert_equal(@expected, data)
|
||||
end
|
||||
|
||||
def test_table
|
||||
table = CSV.table(@path, :col_sep => "\t", :row_sep => "\r\n")
|
||||
table = CSV.table(@path, col_sep: "\t", row_sep: "\r\n")
|
||||
assert_instance_of(CSV::Table, table)
|
||||
assert_equal([[:"1", :"2", :"3"], [4, 5, nil]], table.to_a)
|
||||
end
|
||||
|
||||
def test_shift # aliased as gets() and readline()
|
||||
CSV.open(@path, "r+", :col_sep => "\t", :row_sep => "\r\n") do |csv|
|
||||
CSV.open(@path, "r+", col_sep: "\t", row_sep: "\r\n") do |csv|
|
||||
assert_equal(@expected.shift, csv.shift)
|
||||
assert_equal(@expected.shift, csv.shift)
|
||||
assert_equal(nil, csv.shift)
|
||||
|
@ -125,13 +125,13 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_generate_line
|
||||
line = CSV.generate_line(%w{1 2 3}, :col_sep => ";")
|
||||
line = CSV.generate_line(%w{1 2 3}, col_sep: ";")
|
||||
assert_not_nil(line)
|
||||
assert_instance_of(String, line)
|
||||
assert_equal("1;2;3\n", line)
|
||||
|
||||
# shortcut interface
|
||||
line = %w{1 2 3}.to_csv(:col_sep => ";")
|
||||
line = %w{1 2 3}.to_csv(col_sep: ";")
|
||||
assert_not_nil(line)
|
||||
assert_instance_of(String, line)
|
||||
assert_equal("1;2;3\n", line)
|
||||
|
@ -141,7 +141,7 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
File.unlink(@path)
|
||||
|
||||
headers = %w{a b c}
|
||||
CSV.open(@path, "w", :headers => true) do |csv|
|
||||
CSV.open(@path, "w", headers: true) do |csv|
|
||||
csv << headers
|
||||
csv << %w{1 2 3}
|
||||
assert_equal(headers, csv.instance_variable_get(:@headers))
|
||||
|
@ -161,15 +161,15 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
def test_write_hash
|
||||
File.unlink(@path)
|
||||
|
||||
lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
|
||||
CSV.open( @path, "w", :headers => true,
|
||||
:header_converters => :symbol ) do |csv|
|
||||
lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
|
||||
CSV.open( @path, "w", headers: true,
|
||||
header_converters: :symbol ) do |csv|
|
||||
csv << lines.first.keys
|
||||
lines.each { |line| csv << line }
|
||||
end
|
||||
CSV.open( @path, "w", :headers => true,
|
||||
:converters => :all,
|
||||
:header_converters => :symbol ) do |csv|
|
||||
CSV.open( @path, "w", headers: true,
|
||||
converters: :all,
|
||||
header_converters: :symbol ) do |csv|
|
||||
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
||||
end
|
||||
end
|
||||
|
@ -177,8 +177,8 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
def test_write_hash_with_headers_array
|
||||
File.unlink(@path)
|
||||
|
||||
lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
|
||||
CSV.open(@path, "w", :headers => [:b, :a, :c]) do |csv|
|
||||
lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
|
||||
CSV.open(@path, "w", headers: [:b, :a, :c]) do |csv|
|
||||
lines.each { |line| csv << line }
|
||||
end
|
||||
|
||||
|
@ -189,8 +189,8 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
# test reading CSV with headers
|
||||
CSV.open( @path, "r", :headers => [:b, :a, :c],
|
||||
:converters => :all ) do |csv|
|
||||
CSV.open( @path, "r", headers: [:b, :a, :c],
|
||||
converters: :all ) do |csv|
|
||||
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
||||
end
|
||||
end
|
||||
|
@ -199,7 +199,7 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
File.unlink(@path)
|
||||
|
||||
lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}]
|
||||
CSV.open(@path, "w", :headers => "b|a|c", :col_sep => "|") do |csv|
|
||||
CSV.open(@path, "w", headers: "b|a|c", col_sep: "|") do |csv|
|
||||
lines.each { |line| csv << line }
|
||||
end
|
||||
|
||||
|
@ -210,9 +210,9 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
# test reading CSV with headers
|
||||
CSV.open( @path, "r", :headers => "b|a|c",
|
||||
:col_sep => "|",
|
||||
:converters => :all ) do |csv|
|
||||
CSV.open( @path, "r", headers: "b|a|c",
|
||||
col_sep: "|",
|
||||
converters: :all ) do |csv|
|
||||
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
||||
end
|
||||
end
|
||||
|
@ -221,9 +221,9 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
File.unlink(@path)
|
||||
|
||||
lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}]
|
||||
CSV.open( @path, "w", :headers => "b|a|c",
|
||||
:write_headers => true,
|
||||
:col_sep => "|" ) do |csv|
|
||||
CSV.open( @path, "w", headers: "b|a|c",
|
||||
write_headers: true,
|
||||
col_sep: "|" ) do |csv|
|
||||
lines.each { |line| csv << line }
|
||||
end
|
||||
|
||||
|
@ -235,9 +235,9 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
# test reading CSV with headers
|
||||
CSV.open( @path, "r", :headers => true,
|
||||
:col_sep => "|",
|
||||
:converters => :all ) do |csv|
|
||||
CSV.open( @path, "r", headers: true,
|
||||
col_sep: "|",
|
||||
converters: :all ) do |csv|
|
||||
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
|
||||
end
|
||||
end
|
||||
|
@ -245,7 +245,7 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
def test_append # aliased add_row() and puts()
|
||||
File.unlink(@path)
|
||||
|
||||
CSV.open(@path, "w", :col_sep => "\t", :row_sep => "\r\n") do |csv|
|
||||
CSV.open(@path, "w", col_sep: "\t", row_sep: "\r\n") do |csv|
|
||||
@expected.each { |row| csv << row }
|
||||
end
|
||||
|
||||
|
@ -254,7 +254,7 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
# same thing using CSV::Row objects
|
||||
File.unlink(@path)
|
||||
|
||||
CSV.open(@path, "w", :col_sep => "\t", :row_sep => "\r\n") do |csv|
|
||||
CSV.open(@path, "w", col_sep: "\t", row_sep: "\r\n") do |csv|
|
||||
@expected.each { |row| csv << CSV::Row.new(Array.new, row) }
|
||||
end
|
||||
|
||||
|
@ -268,8 +268,8 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
|
||||
expected = [[1, 2, 3], [4, 5]]
|
||||
CSV.filter( "1;2;3\n4;5\n", (result = String.new),
|
||||
:in_col_sep => ";", :out_col_sep => ",",
|
||||
:converters => :all ) do |row|
|
||||
in_col_sep: ";", out_col_sep: ",",
|
||||
converters: :all ) do |row|
|
||||
assert_equal(row, expected.shift)
|
||||
row.map! { |n| n * 2 }
|
||||
row << "Added\r"
|
||||
|
@ -282,7 +282,7 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
|
||||
first = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
first = CSV.instance(csv, :col_sep => ";")
|
||||
first = CSV.instance(csv, col_sep: ";")
|
||||
first << %w{a b c}
|
||||
end
|
||||
|
||||
|
@ -290,7 +290,7 @@ class TestCSVInterface < Test::Unit::TestCase
|
|||
|
||||
second = nil
|
||||
assert_nothing_raised(Exception) do
|
||||
second = CSV.instance(csv, :col_sep => ";")
|
||||
second = CSV.instance(csv, col_sep: ";")
|
||||
second << [1, 2, 3]
|
||||
end
|
||||
|
||||
|
|
|
@ -145,7 +145,7 @@ class TestCSVRow < Test::Unit::TestCase
|
|||
%w{Header Field} ], @row.to_a )
|
||||
|
||||
# a pair with Hash syntax
|
||||
assert_equal(@row, @row << {:key => :value})
|
||||
assert_equal(@row, @row << {key: :value})
|
||||
assert_equal( [ ["A", 1],
|
||||
["B", 2],
|
||||
["C", 3],
|
||||
|
@ -279,7 +279,7 @@ class TestCSVRow < Test::Unit::TestCase
|
|||
|
||||
# with options
|
||||
assert_equal( "1|2|3|4|\r\n",
|
||||
@row.to_csv(:col_sep => "|", :row_sep => "\r\n") )
|
||||
@row.to_csv(col_sep: "|", row_sep: "\r\n") )
|
||||
end
|
||||
|
||||
def test_array_delegation
|
||||
|
|
|
@ -150,7 +150,7 @@ class TestSerialization < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_custom_dump_and_load
|
||||
obj = {1 => "simple", :test => Hash}
|
||||
obj = {1 => "simple", test: Hash}
|
||||
assert_equal(obj, CSV.load(CSV.dump([obj])).first)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -252,7 +252,7 @@ class TestCSVTable < Test::Unit::TestCase
|
|||
|
||||
# with options
|
||||
assert_equal( csv.gsub(",", "|").gsub("\n", "\r\n"),
|
||||
@table.to_csv(:col_sep => "|", :row_sep => "\r\n") )
|
||||
@table.to_csv(col_sep: "|", row_sep: "\r\n") )
|
||||
|
||||
# with headers
|
||||
assert_equal(csv, @header_table.to_csv)
|
||||
|
|
Loading…
Add table
Reference in a new issue