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:
parent
47a1cd1291
commit
d05217109f
13 changed files with 80 additions and 54 deletions
|
@ -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>
|
||||
|
||||
* signal.c: change rb_atomic_t definition from uchar to uint.
|
||||
|
|
14
lib/csv.rb
14
lib/csv.rb
|
@ -505,12 +505,12 @@ class CSV
|
|||
end
|
||||
str << ">"
|
||||
begin
|
||||
str.join
|
||||
str.join('')
|
||||
rescue # any encoding error
|
||||
str.map do |s|
|
||||
e = Encoding::Converter.asciicompat_encoding(s.encoding)
|
||||
e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
|
||||
end.join
|
||||
end.join('')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -845,7 +845,7 @@ class CSV
|
|||
else
|
||||
rows + [row.fields.to_csv(options)]
|
||||
end
|
||||
end.join
|
||||
end.join('')
|
||||
end
|
||||
alias_method :to_s, :to_csv
|
||||
|
||||
|
@ -1973,12 +1973,12 @@ class CSV
|
|||
end
|
||||
str << ">"
|
||||
begin
|
||||
str.join
|
||||
str.join('')
|
||||
rescue # any encoding error
|
||||
str.map do |s|
|
||||
e = Encoding::Converter.asciicompat_encoding(s.encoding)
|
||||
e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
|
||||
end.join
|
||||
end.join('')
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -2262,7 +2262,7 @@ class CSV
|
|||
# a backslash cannot be transcoded.
|
||||
#
|
||||
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
|
||||
|
||||
#
|
||||
|
@ -2278,7 +2278,7 @@ class CSV
|
|||
# that encoding.
|
||||
#
|
||||
def encode_str(*chunks)
|
||||
chunks.map { |chunk| chunk.encode(@encoding.name) }.join
|
||||
chunks.map { |chunk| chunk.encode(@encoding.name) }.join('')
|
||||
end
|
||||
|
||||
#
|
||||
|
|
20
test/csv/base.rb
Normal file
20
test/csv/base.rb
Normal 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
7
test/csv/test_csv_parsing.rb
Normal file → Executable file
|
@ -7,10 +7,9 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require "timeout"
|
||||
|
||||
require "csv"
|
||||
require_relative "base"
|
||||
|
||||
#
|
||||
# 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
|
||||
# separator <tt>$/</tt>.
|
||||
#
|
||||
class TestCSVParsing < Test::Unit::TestCase
|
||||
class TestCSV::Parsing < TestCSV
|
||||
BIG_DATA = "123456789\n" * 1024
|
||||
|
||||
def test_mastering_regex_example
|
||||
|
@ -217,4 +216,6 @@ class TestCSVParsing < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
8
test/csv/test_csv_writing.rb
Normal file → Executable file
8
test/csv/test_csv_writing.rb
Normal file → Executable file
|
@ -7,11 +7,9 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require_relative "base"
|
||||
|
||||
require "csv"
|
||||
|
||||
class TestCSVWriting < Test::Unit::TestCase
|
||||
class TestCSV::Writing < TestCSV
|
||||
def test_writing
|
||||
[ ["\t", ["\t"]],
|
||||
["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
|
||||
|
@ -94,4 +92,6 @@ class TestCSVWriting < Test::Unit::TestCase
|
|||
CSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
|
||||
force_quotes: true ) )
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
8
test/csv/test_data_converters.rb
Normal file → Executable file
8
test/csv/test_data_converters.rb
Normal file → Executable file
|
@ -7,11 +7,9 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require_relative "base"
|
||||
|
||||
require "csv"
|
||||
|
||||
class TestDataConverters < Test::Unit::TestCase
|
||||
class TestCSV::DataConverters < TestCSV
|
||||
def setup
|
||||
@data = "Numbers,:integer,1,:float,3.015"
|
||||
@parser = CSV.new(@data)
|
||||
|
@ -258,4 +256,6 @@ class TestDataConverters < Test::Unit::TestCase
|
|||
assert_respond_to(row, :unconverted_fields)
|
||||
assert_equal(Array.new, row.unconverted_fields)
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
16
test/csv/test_encodings.rb
Normal file → Executable file
16
test/csv/test_encodings.rb
Normal file → Executable file
|
@ -7,11 +7,9 @@
|
|||
# Copyright 2008 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require_relative "base"
|
||||
|
||||
require "csv"
|
||||
|
||||
class TestEncodings < Test::Unit::TestCase
|
||||
class TestCSV::Encodings < TestCSV
|
||||
def setup
|
||||
require 'tempfile'
|
||||
@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"]
|
||||
assert_equal("US-ASCII", data.first.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)
|
||||
end
|
||||
|
||||
|
@ -233,7 +231,7 @@ class TestEncodings < Test::Unit::TestCase
|
|||
data = ["foo".force_encoding("ISO-8859-1"), "\u3042"]
|
||||
assert_equal("ISO-8859-1", data.first.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)
|
||||
end
|
||||
|
||||
|
@ -260,9 +258,9 @@ class TestEncodings < Test::Unit::TestCase
|
|||
row_sep = (options[:row_sep] || "\n").encode(encoding)
|
||||
ary.map { |row|
|
||||
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.encode(encoding)
|
||||
}.join('').encode(encoding)
|
||||
end
|
||||
|
||||
def encode_for_tests(data, options = { })
|
||||
|
@ -276,4 +274,6 @@ class TestEncodings < Test::Unit::TestCase
|
|||
yield encoding
|
||||
end
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
7
test/csv/test_features.rb
Normal file → Executable file
7
test/csv/test_features.rb
Normal file → Executable file
|
@ -7,12 +7,11 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require "zlib"
|
||||
|
||||
require "csv"
|
||||
require_relative "base"
|
||||
|
||||
class TestCSVFeatures < Test::Unit::TestCase
|
||||
class TestCSV::Features < TestCSV
|
||||
TEST_CASES = [ [%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_match(/\A\d\.\d\.\d\Z/, CSV::VERSION)
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
8
test/csv/test_headers.rb
Normal file → Executable file
8
test/csv/test_headers.rb
Normal file → Executable file
|
@ -7,11 +7,9 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require_relative "base"
|
||||
|
||||
require "csv"
|
||||
|
||||
class TestCSVHeaders < Test::Unit::TestCase
|
||||
class TestCSV::Headers < TestCSV
|
||||
def setup
|
||||
@data = <<-END_CSV.gsub(/^\s+/, "")
|
||||
first,second,third
|
||||
|
@ -285,4 +283,6 @@ class TestCSVHeaders < Test::Unit::TestCase
|
|||
assert_instance_of(CSV::Row, row)
|
||||
end
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
8
test/csv/test_interface.rb
Normal file → Executable file
8
test/csv/test_interface.rb
Normal file → Executable file
|
@ -7,11 +7,9 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require_relative "base"
|
||||
|
||||
require "csv"
|
||||
|
||||
class TestCSVInterface < Test::Unit::TestCase
|
||||
class TestCSV::Interface < TestCSV
|
||||
def setup
|
||||
@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 { |new_csv| new_csv.instance_eval { @io } })
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
8
test/csv/test_row.rb
Normal file → Executable file
8
test/csv/test_row.rb
Normal file → Executable file
|
@ -7,11 +7,9 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require_relative "base"
|
||||
|
||||
require "csv"
|
||||
|
||||
class TestCSVRow < Test::Unit::TestCase
|
||||
class TestCSV::Row < TestCSV
|
||||
def setup
|
||||
@row = CSV::Row.new(%w{A B C A A}, [1, 2, 3, 4])
|
||||
end
|
||||
|
@ -309,4 +307,6 @@ class TestCSVRow < Test::Unit::TestCase
|
|||
"Header field pair not found." )
|
||||
end
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
16
test/csv/test_serialization.rb
Normal file → Executable file
16
test/csv/test_serialization.rb
Normal file → Executable file
|
@ -7,9 +7,7 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
|
||||
require "csv"
|
||||
require_relative "base"
|
||||
|
||||
# An example of how to provide custom CSV serialization.
|
||||
class Hash
|
||||
|
@ -26,7 +24,7 @@ class Hash
|
|||
end
|
||||
end
|
||||
|
||||
class TestSerialization < Test::Unit::TestCase
|
||||
class TestCSV::Serialization < TestCSV
|
||||
|
||||
### Classes Used to Test Serialization ###
|
||||
|
||||
|
@ -71,7 +69,7 @@ class TestSerialization < Test::Unit::TestCase
|
|||
@data = CSV.dump(@names)
|
||||
end
|
||||
assert_equal(<<-END_CLASS_DUMP.gsub(/^\s*/, ""), @data)
|
||||
class,TestSerialization::ReadOnlyName
|
||||
class,TestCSV::Serialization::ReadOnlyName
|
||||
@first,@last
|
||||
James,Gray
|
||||
Dana,Gray
|
||||
|
@ -90,7 +88,7 @@ class TestSerialization < Test::Unit::TestCase
|
|||
@data = CSV.dump(@names)
|
||||
end
|
||||
assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
|
||||
class,TestSerialization::Name
|
||||
class,TestCSV::Serialization::Name
|
||||
first=,last=
|
||||
James,Gray
|
||||
Dana,Gray
|
||||
|
@ -109,7 +107,7 @@ class TestSerialization < Test::Unit::TestCase
|
|||
@data = CSV.dump(@names)
|
||||
end
|
||||
assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
|
||||
class,TestSerialization::FullName
|
||||
class,TestCSV::Serialization::FullName
|
||||
@suffix,first=,last=
|
||||
II,James,Gray
|
||||
,Dana,Gray
|
||||
|
@ -137,7 +135,7 @@ class TestSerialization < Test::Unit::TestCase
|
|||
|
||||
assert(File.exist?(data_file))
|
||||
assert_equal(<<-END_IO_DUMP.gsub(/^\s*/, ""), File.read(data_file))
|
||||
class,TestSerialization::ReadOnlyName
|
||||
class,TestCSV::Serialization::ReadOnlyName
|
||||
@first,@last
|
||||
James,Gray
|
||||
Dana,Gray
|
||||
|
@ -153,4 +151,6 @@ class TestSerialization < Test::Unit::TestCase
|
|||
obj = {1 => "simple", test: Hash}
|
||||
assert_equal(obj, CSV.load(CSV.dump([obj])).first)
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
10
test/csv/test_table.rb
Normal file → Executable file
10
test/csv/test_table.rb
Normal file → Executable file
|
@ -7,11 +7,9 @@
|
|||
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
||||
# under the terms of Ruby's license.
|
||||
|
||||
require "test/unit"
|
||||
require_relative "base"
|
||||
|
||||
require "csv"
|
||||
|
||||
class TestCSVTable < Test::Unit::TestCase
|
||||
class TestCSV::Table < TestCSV
|
||||
def setup
|
||||
@rows = [ CSV::Row.new(%w{A B C}, [1, 2, 3]),
|
||||
CSV::Row.new(%w{A B C}, [4, 5, 6]),
|
||||
|
@ -253,7 +251,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") )
|
||||
assert_equal( csv.lines.to_a[1..-1].join,
|
||||
assert_equal( csv.lines.to_a[1..-1].join(''),
|
||||
@table.to_csv(:write_headers => false) )
|
||||
|
||||
# with headers
|
||||
|
@ -413,4 +411,6 @@ class TestCSVTable < Test::Unit::TestCase
|
|||
@table.inspect.encoding ),
|
||||
"inspect() was not ASCII compatible." )
|
||||
end
|
||||
|
||||
with_diffrent_ofs
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue