Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dbf55ef

Browse files
hanachinkou
authored andcommitted
Ensuring StringIO's encoding in CSV.generate (#111)
1 parent 8a1f171 commit dbf55ef

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

lib/csv.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,13 @@ def foreach(path, mode="r", **options, &block)
531531
# plan to output non-ASCII compatible data.
532532
#
533533
def generate(str=nil, **options)
534+
encoding = options[:encoding]
534535
# add a default empty String, if none was given
535536
if str
536537
str = StringIO.new(str)
537538
str.seek(0, IO::SEEK_END)
539+
str.set_encoding(encoding) if encoding
538540
else
539-
encoding = options[:encoding]
540541
str = +""
541542
str.force_encoding(encoding) if encoding
542543
end

test/csv/helper.rb

+24
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,28 @@ def with_chunk_size(chunk_size)
1515
ENV["CSV_PARSER_SCANNER_TEST_CHUNK_SIZE"] = chunk_size_keep
1616
end
1717
end
18+
19+
def with_verbose(verbose)
20+
original = $VERBOSE
21+
begin
22+
$VERBOSE = verbose
23+
yield
24+
ensure
25+
$VERBOSE = original
26+
end
27+
end
28+
29+
def with_default_internal(encoding)
30+
original = Encoding.default_internal
31+
begin
32+
with_verbose(false) do
33+
Encoding.default_internal = encoding
34+
end
35+
yield
36+
ensure
37+
with_verbose(false) do
38+
Encoding.default_internal = original
39+
end
40+
end
41+
end
1842
end

test/csv/test_encodings.rb

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class TestCSVEncodings < Test::Unit::TestCase
77
extend DifferentOFS
8+
include Helper
89

910
def setup
1011
super
@@ -249,6 +250,15 @@ def test_explicit_encoding
249250
assert_equal(["foo,\u3042\n".encode(Encoding::Windows_31J), Encoding::Windows_31J], [s, s.encoding], bug9766)
250251
end
251252

253+
def test_encoding_with_default_internal
254+
with_default_internal(Encoding::UTF_8) do
255+
s = CSV.generate(String.new(encoding: Encoding::Big5), encoding: Encoding::Big5) do |csv|
256+
csv << ["漢字"]
257+
end
258+
assert_equal(["漢字\n".encode(Encoding::Big5), Encoding::Big5], [s, s.encoding])
259+
end
260+
end
261+
252262
def test_row_separator_detection_with_invalid_encoding
253263
csv = CSV.new("invalid,\xF8\r\nvalid,x\r\n".force_encoding("UTF-8"),
254264
encoding: "UTF-8")

test/csv/write/test_general.rb

+2-24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require_relative "../helper"
55

66
module TestCSVWriteGeneral
7+
include Helper
8+
79
def test_tab
810
assert_equal("\t#{$INPUT_RECORD_SEPARATOR}",
911
generate_line(["\t"]))
@@ -221,30 +223,6 @@ def test_with_default_internal
221223
generate_line(row))
222224
end
223225
end
224-
225-
def with_verbose(verbose)
226-
original = $VERBOSE
227-
begin
228-
$VERBOSE = verbose
229-
yield
230-
ensure
231-
$VERBOSE = original
232-
end
233-
end
234-
235-
def with_default_internal(encoding)
236-
original = Encoding.default_internal
237-
begin
238-
with_verbose(false) do
239-
Encoding.default_internal = encoding
240-
end
241-
yield
242-
ensure
243-
with_verbose(false) do
244-
Encoding.default_internal = original
245-
end
246-
end
247-
end
248226
end
249227

250228
class TestCSVWriteGeneralGenerateLine < Test::Unit::TestCase

0 commit comments

Comments
 (0)