From d05217109fb5e909bbd7849bcc799b6404c7c8b0 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 25 Dec 2010 06:58:58 +0000 Subject: * 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 --- ChangeLog | 4 ++++ lib/csv.rb | 14 +++++++------- test/csv/base.rb | 20 ++++++++++++++++++++ test/csv/test_csv_parsing.rb | 7 ++++--- test/csv/test_csv_writing.rb | 8 ++++---- test/csv/test_data_converters.rb | 8 ++++---- test/csv/test_encodings.rb | 16 ++++++++-------- test/csv/test_features.rb | 7 ++++--- test/csv/test_headers.rb | 8 ++++---- test/csv/test_interface.rb | 8 ++++---- test/csv/test_row.rb | 8 ++++---- test/csv/test_serialization.rb | 16 ++++++++-------- test/csv/test_table.rb | 10 +++++----- 13 files changed, 80 insertions(+), 54 deletions(-) create mode 100644 test/csv/base.rb mode change 100644 => 100755 test/csv/test_csv_parsing.rb mode change 100644 => 100755 test/csv/test_csv_writing.rb mode change 100644 => 100755 test/csv/test_data_converters.rb mode change 100644 => 100755 test/csv/test_encodings.rb mode change 100644 => 100755 test/csv/test_features.rb mode change 100644 => 100755 test/csv/test_headers.rb mode change 100644 => 100755 test/csv/test_interface.rb mode change 100644 => 100755 test/csv/test_row.rb mode change 100644 => 100755 test/csv/test_serialization.rb mode change 100644 => 100755 test/csv/test_table.rb diff --git a/ChangeLog b/ChangeLog index dc4fda560d..215bf69131 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sat Dec 25 15:58:55 2010 Nobuyoshi Nakada + + * lib/csv.rb, test/csv: should not assume $, invariant. + Sat Dec 25 16:08:06 2010 KOSAKI Motohiro * signal.c: change rb_atomic_t definition from uchar to uint. diff --git a/lib/csv.rb b/lib/csv.rb index 31388bc9da..1ad019a269 100644 --- a/lib/csv.rb +++ b/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 # diff --git a/test/csv/base.rb b/test/csv/base.rb new file mode 100644 index 0000000000..c0f5bd990b --- /dev/null +++ b/test/csv/base.rb @@ -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 diff --git a/test/csv/test_csv_parsing.rb b/test/csv/test_csv_parsing.rb old mode 100644 new mode 100755 index c0b8d83f96..6b50ab382e --- a/test/csv/test_csv_parsing.rb +++ b/test/csv/test_csv_parsing.rb @@ -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 $/. # -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 diff --git a/test/csv/test_csv_writing.rb b/test/csv/test_csv_writing.rb old mode 100644 new mode 100755 index eacba49613..73a27d2b96 --- a/test/csv/test_csv_writing.rb +++ b/test/csv/test_csv_writing.rb @@ -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 diff --git a/test/csv/test_data_converters.rb b/test/csv/test_data_converters.rb old mode 100644 new mode 100755 index 1ad5093424..06653f5ec9 --- a/test/csv/test_data_converters.rb +++ b/test/csv/test_data_converters.rb @@ -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 diff --git a/test/csv/test_encodings.rb b/test/csv/test_encodings.rb old mode 100644 new mode 100755 index 82dca914fe..6681470097 --- a/test/csv/test_encodings.rb +++ b/test/csv/test_encodings.rb @@ -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 diff --git a/test/csv/test_features.rb b/test/csv/test_features.rb old mode 100644 new mode 100755 index 58efd1c9b3..4141f39480 --- a/test/csv/test_features.rb +++ b/test/csv/test_features.rb @@ -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 diff --git a/test/csv/test_headers.rb b/test/csv/test_headers.rb old mode 100644 new mode 100755 index 6e56ffc27c..ef955fb492 --- a/test/csv/test_headers.rb +++ b/test/csv/test_headers.rb @@ -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 diff --git a/test/csv/test_interface.rb b/test/csv/test_interface.rb old mode 100644 new mode 100755 index a58e9c6179..8bcd134781 --- a/test/csv/test_interface.rb +++ b/test/csv/test_interface.rb @@ -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 diff --git a/test/csv/test_row.rb b/test/csv/test_row.rb old mode 100644 new mode 100755 index f9aa959701..b2d94fa4d6 --- a/test/csv/test_row.rb +++ b/test/csv/test_row.rb @@ -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 diff --git a/test/csv/test_serialization.rb b/test/csv/test_serialization.rb old mode 100644 new mode 100755 index 5f0201ed90..989a7e98b5 --- a/test/csv/test_serialization.rb +++ b/test/csv/test_serialization.rb @@ -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 diff --git a/test/csv/test_table.rb b/test/csv/test_table.rb old mode 100644 new mode 100755 index d0b421750f..67e5b54daf --- a/test/csv/test_table.rb +++ b/test/csv/test_table.rb @@ -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 -- cgit v1.2.3