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