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

* lib/csv.rb, test/csv: should not assume $, invariant.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30353 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2010-12-25 06:58:58 +00:00
parent 47a1cd1291
commit d05217109f
13 changed files with 80 additions and 54 deletions

View file

@ -1,3 +1,7 @@
Sat Dec 25 15:58:55 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/csv.rb, test/csv: should not assume $, invariant.
Sat Dec 25 16:08:06 2010 KOSAKI Motohiro <kosaki.motohiro@gmail.com> Sat Dec 25 16:08:06 2010 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* signal.c: change rb_atomic_t definition from uchar to uint. * signal.c: change rb_atomic_t definition from uchar to uint.

View file

@ -505,12 +505,12 @@ class CSV
end end
str << ">" str << ">"
begin begin
str.join str.join('')
rescue # any encoding error rescue # any encoding error
str.map do |s| str.map do |s|
e = Encoding::Converter.asciicompat_encoding(s.encoding) e = Encoding::Converter.asciicompat_encoding(s.encoding)
e ? s.encode(e) : s.force_encoding("ASCII-8BIT") e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
end.join end.join('')
end end
end end
end end
@ -845,7 +845,7 @@ class CSV
else else
rows + [row.fields.to_csv(options)] rows + [row.fields.to_csv(options)]
end end
end.join end.join('')
end end
alias_method :to_s, :to_csv alias_method :to_s, :to_csv
@ -1973,12 +1973,12 @@ class CSV
end end
str << ">" str << ">"
begin begin
str.join str.join('')
rescue # any encoding error rescue # any encoding error
str.map do |s| str.map do |s|
e = Encoding::Converter.asciicompat_encoding(s.encoding) e = Encoding::Converter.asciicompat_encoding(s.encoding)
e ? s.encode(e) : s.force_encoding("ASCII-8BIT") e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
end.join end.join('')
end end
end end
@ -2262,7 +2262,7 @@ class CSV
# a backslash cannot be transcoded. # a backslash cannot be transcoded.
# #
def escape_re(str) def escape_re(str)
str.chars.map { |c| @re_chars.include?(c) ? @re_esc + c : c }.join str.chars.map { |c| @re_chars.include?(c) ? @re_esc + c : c }.join('')
end end
# #
@ -2278,7 +2278,7 @@ class CSV
# that encoding. # that encoding.
# #
def encode_str(*chunks) def encode_str(*chunks)
chunks.map { |chunk| chunk.encode(@encoding.name) }.join chunks.map { |chunk| chunk.encode(@encoding.name) }.join('')
end end
# #

20
test/csv/base.rb Normal file
View file

@ -0,0 +1,20 @@
require "test/unit"
require "csv"
class TestCSV < Test::Unit::TestCase
module DifferentOFS
def setup
super
@ofs, $, = $,, "-"
end
def teardown
$, = @ofs
super
end
end
def self.with_diffrent_ofs
Class.new(self).class_eval {include DifferentOFS}
end
end

7
test/csv/test_csv_parsing.rb Normal file → Executable file
View file

@ -7,10 +7,9 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit"
require "timeout" require "timeout"
require "csv" require_relative "base"
# #
# Following tests are my interpretation of the # Following tests are my interpretation of the
@ -18,7 +17,7 @@ require "csv"
# document in one place (intentionally) and that is to make the default row # document in one place (intentionally) and that is to make the default row
# separator <tt>$/</tt>. # separator <tt>$/</tt>.
# #
class TestCSVParsing < Test::Unit::TestCase class TestCSV::Parsing < TestCSV
BIG_DATA = "123456789\n" * 1024 BIG_DATA = "123456789\n" * 1024
def test_mastering_regex_example def test_mastering_regex_example
@ -217,4 +216,6 @@ class TestCSVParsing < Test::Unit::TestCase
end end
end end
end end
with_diffrent_ofs
end end

8
test/csv/test_csv_writing.rb Normal file → Executable file
View file

@ -7,11 +7,9 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv" class TestCSV::Writing < TestCSV
class TestCSVWriting < Test::Unit::TestCase
def test_writing def test_writing
[ ["\t", ["\t"]], [ ["\t", ["\t"]],
["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]], ["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
@ -94,4 +92,6 @@ class TestCSVWriting < Test::Unit::TestCase
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
with_diffrent_ofs
end end

8
test/csv/test_data_converters.rb Normal file → Executable file
View file

@ -7,11 +7,9 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv" class TestCSV::DataConverters < TestCSV
class TestDataConverters < Test::Unit::TestCase
def setup def setup
@data = "Numbers,:integer,1,:float,3.015" @data = "Numbers,:integer,1,:float,3.015"
@parser = CSV.new(@data) @parser = CSV.new(@data)
@ -258,4 +256,6 @@ class TestDataConverters < Test::Unit::TestCase
assert_respond_to(row, :unconverted_fields) assert_respond_to(row, :unconverted_fields)
assert_equal(Array.new, row.unconverted_fields) assert_equal(Array.new, row.unconverted_fields)
end end
with_diffrent_ofs
end end

16
test/csv/test_encodings.rb Normal file → Executable file
View file

@ -7,11 +7,9 @@
# Copyright 2008 James Edward Gray II. You can redistribute or modify this code # Copyright 2008 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv" class TestCSV::Encodings < TestCSV
class TestEncodings < Test::Unit::TestCase
def setup def setup
require 'tempfile' require 'tempfile'
@temp_csv_file = Tempfile.new(%w"test_csv. .csv") @temp_csv_file = Tempfile.new(%w"test_csv. .csv")
@ -225,7 +223,7 @@ class TestEncodings < Test::Unit::TestCase
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)
assert_equal("UTF-8", data.last.encoding.name) assert_equal("UTF-8", data.last.encoding.name)
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
@ -233,7 +231,7 @@ class TestEncodings < Test::Unit::TestCase
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)
assert_equal("UTF-8", data.last.encoding.name) assert_equal("UTF-8", data.last.encoding.name)
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
@ -260,9 +258,9 @@ class TestEncodings < Test::Unit::TestCase
row_sep = (options[:row_sep] || "\n").encode(encoding) row_sep = (options[:row_sep] || "\n").encode(encoding)
ary.map { |row| ary.map { |row|
row.map { |field| row.map { |field|
[quote_char, field.encode(encoding), quote_char].join [quote_char, field.encode(encoding), quote_char].join('')
}.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 = { })
@ -276,4 +274,6 @@ class TestEncodings < Test::Unit::TestCase
yield encoding yield encoding
end end
end end
with_diffrent_ofs
end end

7
test/csv/test_features.rb Normal file → Executable file
View file

@ -7,12 +7,11 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit"
require "zlib" require "zlib"
require "csv" require_relative "base"
class TestCSVFeatures < Test::Unit::TestCase class TestCSV::Features < TestCSV
TEST_CASES = [ [%Q{a,b}, ["a", "b"]], TEST_CASES = [ [%Q{a,b}, ["a", "b"]],
[%Q{a,"""b"""}, ["a", "\"b\""]], [%Q{a,"""b"""}, ["a", "\"b\""]],
[%Q{a,"""b"}, ["a", "\"b"]], [%Q{a,"""b"}, ["a", "\"b"]],
@ -264,4 +263,6 @@ class TestCSVFeatures < Test::Unit::TestCase
assert(CSV::VERSION.frozen?) assert(CSV::VERSION.frozen?)
assert_match(/\A\d\.\d\.\d\Z/, CSV::VERSION) assert_match(/\A\d\.\d\.\d\Z/, CSV::VERSION)
end end
with_diffrent_ofs
end end

8
test/csv/test_headers.rb Normal file → Executable file
View file

@ -7,11 +7,9 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv" class TestCSV::Headers < TestCSV
class TestCSVHeaders < Test::Unit::TestCase
def setup def setup
@data = <<-END_CSV.gsub(/^\s+/, "") @data = <<-END_CSV.gsub(/^\s+/, "")
first,second,third first,second,third
@ -285,4 +283,6 @@ class TestCSVHeaders < Test::Unit::TestCase
assert_instance_of(CSV::Row, row) assert_instance_of(CSV::Row, row)
end end
end end
with_diffrent_ofs
end end

8
test/csv/test_interface.rb Normal file → Executable file
View file

@ -7,11 +7,9 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv" class TestCSV::Interface < TestCSV
class TestCSVInterface < Test::Unit::TestCase
def setup def setup
@path = File.join(File.dirname(__FILE__), "temp_test_data.csv") @path = File.join(File.dirname(__FILE__), "temp_test_data.csv")
@ -306,4 +304,6 @@ class TestCSVInterface < Test::Unit::TestCase
assert_equal(STDOUT, CSV.instance.instance_eval { @io }) assert_equal(STDOUT, CSV.instance.instance_eval { @io })
assert_equal(STDOUT, CSV { |new_csv| new_csv.instance_eval { @io } }) assert_equal(STDOUT, CSV { |new_csv| new_csv.instance_eval { @io } })
end end
with_diffrent_ofs
end end

8
test/csv/test_row.rb Normal file → Executable file
View file

@ -7,11 +7,9 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv" class TestCSV::Row < TestCSV
class TestCSVRow < Test::Unit::TestCase
def setup def setup
@row = CSV::Row.new(%w{A B C A A}, [1, 2, 3, 4]) @row = CSV::Row.new(%w{A B C A A}, [1, 2, 3, 4])
end end
@ -309,4 +307,6 @@ class TestCSVRow < Test::Unit::TestCase
"Header field pair not found." ) "Header field pair not found." )
end end
end end
with_diffrent_ofs
end end

16
test/csv/test_serialization.rb Normal file → Executable file
View file

@ -7,9 +7,7 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv"
# An example of how to provide custom CSV serialization. # An example of how to provide custom CSV serialization.
class Hash class Hash
@ -26,7 +24,7 @@ class Hash
end end
end end
class TestSerialization < Test::Unit::TestCase class TestCSV::Serialization < TestCSV
### Classes Used to Test Serialization ### ### Classes Used to Test Serialization ###
@ -71,7 +69,7 @@ class TestSerialization < Test::Unit::TestCase
@data = CSV.dump(@names) @data = CSV.dump(@names)
end end
assert_equal(<<-END_CLASS_DUMP.gsub(/^\s*/, ""), @data) assert_equal(<<-END_CLASS_DUMP.gsub(/^\s*/, ""), @data)
class,TestSerialization::ReadOnlyName class,TestCSV::Serialization::ReadOnlyName
@first,@last @first,@last
James,Gray James,Gray
Dana,Gray Dana,Gray
@ -90,7 +88,7 @@ class TestSerialization < Test::Unit::TestCase
@data = CSV.dump(@names) @data = CSV.dump(@names)
end end
assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data) assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
class,TestSerialization::Name class,TestCSV::Serialization::Name
first=,last= first=,last=
James,Gray James,Gray
Dana,Gray Dana,Gray
@ -109,7 +107,7 @@ class TestSerialization < Test::Unit::TestCase
@data = CSV.dump(@names) @data = CSV.dump(@names)
end end
assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data) assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
class,TestSerialization::FullName class,TestCSV::Serialization::FullName
@suffix,first=,last= @suffix,first=,last=
II,James,Gray II,James,Gray
,Dana,Gray ,Dana,Gray
@ -137,7 +135,7 @@ class TestSerialization < Test::Unit::TestCase
assert(File.exist?(data_file)) assert(File.exist?(data_file))
assert_equal(<<-END_IO_DUMP.gsub(/^\s*/, ""), File.read(data_file)) assert_equal(<<-END_IO_DUMP.gsub(/^\s*/, ""), File.read(data_file))
class,TestSerialization::ReadOnlyName class,TestCSV::Serialization::ReadOnlyName
@first,@last @first,@last
James,Gray James,Gray
Dana,Gray Dana,Gray
@ -153,4 +151,6 @@ class TestSerialization < Test::Unit::TestCase
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
with_diffrent_ofs
end end

10
test/csv/test_table.rb Normal file → Executable file
View file

@ -7,11 +7,9 @@
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
# under the terms of Ruby's license. # under the terms of Ruby's license.
require "test/unit" require_relative "base"
require "csv" class TestCSV::Table < TestCSV
class TestCSVTable < Test::Unit::TestCase
def setup def setup
@rows = [ CSV::Row.new(%w{A B C}, [1, 2, 3]), @rows = [ CSV::Row.new(%w{A B C}, [1, 2, 3]),
CSV::Row.new(%w{A B C}, [4, 5, 6]), CSV::Row.new(%w{A B C}, [4, 5, 6]),
@ -253,7 +251,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") )
assert_equal( csv.lines.to_a[1..-1].join, assert_equal( csv.lines.to_a[1..-1].join(''),
@table.to_csv(:write_headers => false) ) @table.to_csv(:write_headers => false) )
# with headers # with headers
@ -413,4 +411,6 @@ class TestCSVTable < Test::Unit::TestCase
@table.inspect.encoding ), @table.inspect.encoding ),
"inspect() was not ASCII compatible." ) "inspect() was not ASCII compatible." )
end end
with_diffrent_ofs
end end