1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* strip trailing spaces.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31357 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2011-04-26 15:57:04 +00:00
parent c26ab1ef1c
commit 0b98b9a95d
4 changed files with 38 additions and 38 deletions

View file

@ -154,11 +154,11 @@ require "stringio"
# CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String # CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String
# CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr # CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr
# CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin # CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin
# #
# == Advanced Usage # == Advanced Usage
# #
# === Wrap an IO Object # === Wrap an IO Object
# #
# csv = CSV.new(io, options) # csv = CSV.new(io, options)
# # ... read (with gets() or each()) from and write (with <<) to csv here ... # # ... read (with gets() or each()) from and write (with <<) to csv here ...
# #
@ -836,7 +836,7 @@ class CSV
# #
# This method assumes you want the Table.headers(), unless you explicitly # This method assumes you want the Table.headers(), unless you explicitly
# pass <tt>:write_headers => false</tt>. # pass <tt>:write_headers => false</tt>.
# #
def to_csv(options = Hash.new) def to_csv(options = Hash.new)
wh = options.fetch(:write_headers, true) wh = options.fetch(:write_headers, true)
@table.inject(wh ? [headers.to_csv(options)] : [ ]) do |rows, row| @table.inject(wh ? [headers.to_csv(options)] : [ ]) do |rows, row|

View file

@ -19,44 +19,44 @@ class TestCSV::Encodings < TestCSV
@temp_csv_path = @temp_csv_file.path @temp_csv_path = @temp_csv_file.path
@temp_csv_file.close @temp_csv_file.close
end end
def teardown def teardown
@temp_csv_file.close! @temp_csv_file.close!
super super
end end
######################################## ########################################
### Hand Test Some Popular Encodings ### ### Hand Test Some Popular Encodings ###
######################################## ########################################
def test_parses_utf8_encoding def test_parses_utf8_encoding
assert_parses( [ %w[ one two … ], assert_parses( [ %w[ one two … ],
%w[ 1 … 3 ], %w[ 1 … 3 ],
%w[ … 5 6 ] ], "UTF-8" ) %w[ … 5 6 ] ], "UTF-8" )
end end
def test_parses_latin1_encoding def test_parses_latin1_encoding
assert_parses( [ %w[ one two Résumé ], assert_parses( [ %w[ one two Résumé ],
%w[ 1 Résumé 3 ], %w[ 1 Résumé 3 ],
%w[ Résumé 5 6 ] ], "ISO-8859-1" ) %w[ Résumé 5 6 ] ], "ISO-8859-1" )
end end
def test_parses_utf16be_encoding def test_parses_utf16be_encoding
assert_parses( [ %w[ one two … ], assert_parses( [ %w[ one two … ],
%w[ 1 … 3 ], %w[ 1 … 3 ],
%w[ … 5 6 ] ], "UTF-16BE" ) %w[ … 5 6 ] ], "UTF-16BE" )
end end
def test_parses_shift_jis_encoding def test_parses_shift_jis_encoding
assert_parses( [ %w[ 一 二 三 ], assert_parses( [ %w[ 一 二 三 ],
%w[ 四 五 六 ], %w[ 四 五 六 ],
%w[ 七 八 九 ] ], "Shift_JIS" ) %w[ 七 八 九 ] ], "Shift_JIS" )
end end
########################################################### ###########################################################
### Try Simple Reading for All Non-dummy Ruby Encodings ### ### Try Simple Reading for All Non-dummy Ruby Encodings ###
########################################################### ###########################################################
def test_reading_with_most_encodings def test_reading_with_most_encodings
each_encoding do |encoding| each_encoding do |encoding|
begin begin
@ -67,7 +67,7 @@ class TestCSV::Encodings < TestCSV
end end
end end
end end
def test_regular_expression_escaping def test_regular_expression_escaping
each_encoding do |encoding| each_encoding do |encoding|
begin begin
@ -78,18 +78,18 @@ class TestCSV::Encodings < TestCSV
end end
end end
end end
####################################################################### #######################################################################
### Stress Test ASCII Compatible and Non-ASCII Compatible Encodings ### ### Stress Test ASCII Compatible and Non-ASCII Compatible Encodings ###
####################################################################### #######################################################################
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
def test_csv_chars_are_transcoded def test_csv_chars_are_transcoded
encode_for_tests([%w[abc def]]) do |data| encode_for_tests([%w[abc def]]) do |data|
%w[col_sep row_sep quote_char].each do |csv_char| %w[col_sep row_sep quote_char].each do |csv_char|
@ -98,7 +98,7 @@ class TestCSV::Encodings < TestCSV
end end
end end
end end
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)
@ -110,7 +110,7 @@ class TestCSV::Encodings < TestCSV
end end
end end
end end
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)
@ -119,7 +119,7 @@ class TestCSV::Encodings < TestCSV
assert_equal([1, 2, 3], parsed[1]) assert_equal([1, 2, 3], parsed[1])
end end
end end
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,
@ -130,7 +130,7 @@ class TestCSV::Encodings < TestCSV
"Wrong data encoding." ) "Wrong data encoding." )
end end
end end
def test_open_allows_you_to_set_encodings def test_open_allows_you_to_set_encodings
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
@ -141,7 +141,7 @@ class TestCSV::Encodings < TestCSV
"Wrong data encoding." ) "Wrong data encoding." )
end end
end end
# read and write with transcoding # read and write with transcoding
File.open(@temp_csv_path, "wb:UTF-32BE:#{data.encoding.name}") do |f| File.open(@temp_csv_path, "wb:UTF-32BE:#{data.encoding.name}") do |f|
f << data f << data
@ -154,7 +154,7 @@ class TestCSV::Encodings < TestCSV
end end
end end
end end
def test_foreach_allows_you_to_set_encodings def test_foreach_allows_you_to_set_encodings
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
@ -162,7 +162,7 @@ class TestCSV::Encodings < TestCSV
CSV.foreach(@temp_csv_path, encoding: data.encoding) do |row| CSV.foreach(@temp_csv_path, encoding: data.encoding) do |row|
row.each {|f| assert_equal(f.encoding, data.encoding)} row.each {|f| assert_equal(f.encoding, data.encoding)}
end end
# read and write with transcoding # read and write with transcoding
File.open(@temp_csv_path, "wb:UTF-32BE:#{data.encoding.name}") do |f| File.open(@temp_csv_path, "wb:UTF-32BE:#{data.encoding.name}") do |f|
f << data f << data
@ -174,7 +174,7 @@ class TestCSV::Encodings < TestCSV
end end
end end
end end
def test_read_allows_you_to_set_encodings def test_read_allows_you_to_set_encodings
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
@ -182,7 +182,7 @@ class TestCSV::Encodings < TestCSV
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." )
# read and write with transcoding # read and write with transcoding
File.open(@temp_csv_path, "wb:UTF-32BE:#{data.encoding.name}") do |f| File.open(@temp_csv_path, "wb:UTF-32BE:#{data.encoding.name}") do |f|
f << data f << data
@ -193,11 +193,11 @@ class TestCSV::Encodings < TestCSV
"Wrong data encoding." ) "Wrong data encoding." )
end end
end end
################################# #################################
### Write CSV in any Encoding ### ### Write CSV in any Encoding ###
################################# #################################
def test_can_write_csv_in_any_encoding def test_can_write_csv_in_any_encoding
each_encoding do |encoding| each_encoding do |encoding|
# test generate_line with encoding hint # test generate_line with encoding hint
@ -208,11 +208,11 @@ class TestCSV::Encodings < TestCSV
next next
end end
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
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 |f| CSV.open(@temp_csv_path, "wb:#{encoding.name}") do |f|
@ -221,7 +221,7 @@ class TestCSV::Encodings < TestCSV
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
def test_encoding_is_upgraded_during_writing_as_needed def test_encoding_is_upgraded_during_writing_as_needed
data = ["foo".force_encoding("US-ASCII"), "\u3042"] data = ["foo".force_encoding("US-ASCII"), "\u3042"]
assert_equal("US-ASCII", data.first.encoding.name) assert_equal("US-ASCII", data.first.encoding.name)
@ -229,7 +229,7 @@ class TestCSV::Encodings < TestCSV
assert_equal("UTF-8", data.join('').encoding.name) assert_equal("UTF-8", data.join('').encoding.name)
assert_equal("UTF-8", data.to_csv.encoding.name) assert_equal("UTF-8", data.to_csv.encoding.name)
end end
def test_encoding_is_upgraded_for_ascii_content_during_writing_as_needed def test_encoding_is_upgraded_for_ascii_content_during_writing_as_needed
data = ["foo".force_encoding("ISO-8859-1"), "\u3042"] data = ["foo".force_encoding("ISO-8859-1"), "\u3042"]
assert_equal("ISO-8859-1", data.first.encoding.name) assert_equal("ISO-8859-1", data.first.encoding.name)
@ -239,7 +239,7 @@ class TestCSV::Encodings < TestCSV
end end
private private
def assert_parses(fields, encoding, options = { }) def assert_parses(fields, encoding, options = { })
encoding = Encoding.find(encoding) unless encoding.is_a? Encoding encoding = Encoding.find(encoding) unless encoding.is_a? Encoding
orig_fields = fields orig_fields = fields
@ -282,11 +282,11 @@ class TestCSV::Encodings < TestCSV
rescue Encoding::ConverterNotFoundError rescue Encoding::ConverterNotFoundError
end end
end end
def encode_ary(ary, encoding) def encode_ary(ary, encoding)
ary.map { |row| row.map { |field| field.encode(encoding) } } ary.map { |row| row.map { |field| field.encode(encoding) } }
end end
def ary_to_data(ary, options = { }) def ary_to_data(ary, options = { })
encoding = ary.flatten.first.encoding encoding = ary.flatten.first.encoding
quote_char = (options[:quote_char] || '"').encode(encoding) quote_char = (options[:quote_char] || '"').encode(encoding)
@ -298,12 +298,12 @@ class TestCSV::Encodings < TestCSV
}.join(col_sep) + row_sep }.join(col_sep) + row_sep
}.join('').encode(encoding) }.join('').encode(encoding)
end end
def encode_for_tests(data, options = { }) def encode_for_tests(data, options = { })
yield ary_to_data(encode_ary(data, "UTF-8"), options) yield ary_to_data(encode_ary(data, "UTF-8"), options)
yield ary_to_data(encode_ary(data, "UTF-16BE"), options) yield ary_to_data(encode_ary(data, "UTF-16BE"), options)
end end
def each_encoding def each_encoding
Encoding.list.each do |encoding| Encoding.list.each do |encoding|
next if encoding.dummy? # skip "dummy" encodings next if encoding.dummy? # skip "dummy" encodings

View file

@ -81,7 +81,7 @@ class TestCSV::Interface < TestCSV
assert_equal(nil, CSV.parse_line("")) # to signal eof assert_equal(nil, CSV.parse_line("")) # to signal eof
assert_equal(Array.new, CSV.parse_line("\n1,2,3")) assert_equal(Array.new, CSV.parse_line("\n1,2,3"))
end end
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") )

View file

@ -331,7 +331,7 @@ class TestCSV::Table < TestCSV
table = CSV.parse(data, :headers => true) table = CSV.parse(data, :headers => true)
assert_equal(["ra2", nil, "rb2"], table.delete("col2")) assert_equal(["ra2", nil, "rb2"], table.delete("col2"))
end end
def test_delete_if_row def test_delete_if_row
###################### ######################
### Mixed/Row Mode ### ### Mixed/Row Mode ###