* lib/csv/csv.rb: Worked around some minor encoding changes in Ruby

pointed out by Nobu.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
jeg2 2008-09-28 00:06:21 +00:00
parent 356b051532
commit bde8a01d0a
4 changed files with 15 additions and 9 deletions

View File

@ -1,3 +1,8 @@
Sun Sep 28 09:05:53 2008 James Edward Gray II <jeg2@ruby-lang.org>
* lib/csv/csv.rb: Worked around some minor encoding changes in Ruby
pointed out by Nobu.
Sun Sep 28 08:37:12 2008 Tadayoshi Funaba <tadf@dotrb.org> Sun Sep 28 08:37:12 2008 Tadayoshi Funaba <tadf@dotrb.org>
* lib/mathn.rb: a hack to provide canonicalization. This must be * lib/mathn.rb: a hack to provide canonicalization. This must be

View File

@ -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.1".freeze VERSION = "2.4.2".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
@ -831,7 +831,7 @@ class CSV
# Shows the mode and size of this table in a US-ASCII String. # Shows the mode and size of this table in a US-ASCII String.
def inspect def inspect
"#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>" "#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>".encode("US-ASCII")
end end
end end
@ -1244,9 +1244,9 @@ class CSV
encoding = options.delete(:encoding) encoding = options.delete(:encoding)
str = "" str = ""
if encoding if encoding
str.encode!(encoding) str.force_encoding(encoding)
elsif field = row.find { |f| not f.nil? } elsif field = row.find { |f| not f.nil? }
str.encode!(String(field).encoding) str.force_encoding(String(field).encoding)
end end
(new(str, options) << row).string (new(str, options) << row).string
end end

View File

@ -141,7 +141,7 @@ class TestCSVParsing < Test::Unit::TestCase
assert_equal( "Unquoted fields do not allow \\r or \\n (line 4).", assert_equal( "Unquoted fields do not allow \\r or \\n (line 4).",
$!.message ) $!.message )
end end
assert_raise(CSV::MalformedCSVError) { CSV.parse_line('1,2,"3...') } assert_raise(CSV::MalformedCSVError) { CSV.parse_line('1,2,"3...') }
bad_data = <<-END_DATA.gsub(/^ +/, "") bad_data = <<-END_DATA.gsub(/^ +/, "")

View File

@ -207,8 +207,8 @@ class TestEncodings < Test::Unit::TestCase
# writing to files # writing to files
data = encode_ary([%w[abc d,ef], %w[123 456 ]], encoding) data = encode_ary([%w[abc d,ef], %w[123 456 ]], encoding)
CSV.open(@temp_csv_path, "wb:#{encoding.name}") do |csv| CSV.open(@temp_csv_path, "wb:#{encoding.name}") do |f|
data.each { |row| csv << 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
@ -221,8 +221,9 @@ class TestEncodings < Test::Unit::TestCase
fields = encode_ary(fields, encoding) fields = encode_ary(fields, encoding)
parsed = CSV.parse(ary_to_data(fields, options), options) parsed = CSV.parse(ary_to_data(fields, options), options)
assert_equal(fields, parsed) assert_equal(fields, parsed)
assert( parsed.flatten.all? { |field| field.encoding == encoding }, parsed.flatten.each_with_index do |field, i|
"Fields were transcoded." ) assert_equal(encoding, field.encoding, "Field[#{i + 1}] was transcoded.")
end
end end
def encode_ary(ary, encoding) def encode_ary(ary, encoding)