7
7
# Copyright 2008 James Edward Gray II. You can redistribute or modify this code
8
8
# under the terms of Ruby's license.
14
class TestEncodings < Test::Unit::TestCase
10
require_relative "base"
12
class TestCSV::Encodings < TestCSV
17
18
@temp_csv_file = Tempfile.new(%w"test_csv. .csv")
18
19
@temp_csv_path = @temp_csv_file.path
82
def test_read_with_default_encoding
84
default_external = Encoding.default_external
85
each_encoding do |encoding|
86
File.open(@temp_csv_path, "wb", encoding: encoding) {|f| f << data}
89
Encoding.default_external = encoding
91
result = CSV.read(@temp_csv_path)[0][0]
94
Encoding.default_external = default_external
97
assert_equal(encoding, result.encoding)
80
101
#######################################################################
81
102
### Stress Test ASCII Compatible and Non-ASCII Compatible Encodings ###
82
103
#######################################################################
156
177
def test_foreach_allows_you_to_set_encodings
157
178
encode_for_tests([%w[abc def]]) do |data|
158
179
# read and write in encoding
159
File.open(@temp_csv_path, "wb:#{data.encoding.name}") { |f| f << data }
160
CSV.foreach(@temp_csv_path, encoding: data.encoding.name) do |row|
161
assert( row.all? { |f| f.encoding == data.encoding },
162
"Wrong data encoding." )
180
File.open(@temp_csv_path, "wb", encoding: data.encoding) { |f| f << data }
181
CSV.foreach(@temp_csv_path, encoding: data.encoding) do |row|
182
row.each {|f| assert_equal(f.encoding, data.encoding)}
165
185
# read and write with transcoding
220
240
assert_equal(data, CSV.read(@temp_csv_path, encoding: encoding.name))
224
244
def test_encoding_is_upgraded_during_writing_as_needed
225
245
data = ["foo".force_encoding("US-ASCII"), "\u3042"]
226
246
assert_equal("US-ASCII", data.first.encoding.name)
227
247
assert_equal("UTF-8", data.last.encoding.name)
228
assert_equal("UTF-8", data.join.encoding.name)
248
assert_equal("UTF-8", data.join('').encoding.name)
229
249
assert_equal("UTF-8", data.to_csv.encoding.name)
232
252
def test_encoding_is_upgraded_for_ascii_content_during_writing_as_needed
233
253
data = ["foo".force_encoding("ISO-8859-1"), "\u3042"]
234
254
assert_equal("ISO-8859-1", data.first.encoding.name)
235
255
assert_equal("UTF-8", data.last.encoding.name)
236
assert_equal("UTF-8", data.join.encoding.name)
256
assert_equal("UTF-8", data.join('').encoding.name)
237
257
assert_equal("UTF-8", data.to_csv.encoding.name)
242
262
def assert_parses(fields, encoding, options = { })
243
263
encoding = Encoding.find(encoding) unless encoding.is_a? Encoding
244
265
fields = encode_ary(fields, encoding)
245
parsed = CSV.parse(ary_to_data(fields, options), options)
266
data = ary_to_data(fields, options)
267
parsed = CSV.parse(data, options)
246
268
assert_equal(fields, parsed)
247
269
parsed.flatten.each_with_index do |field, i|
248
270
assert_equal(encoding, field.encoding, "Field[#{i + 1}] was transcoded.")
272
File.open(@temp_csv_path, "wb") {|f| f.print(data)}
273
CSV.open(@temp_csv_path, "rb:#{encoding}", options) do |csv|
274
csv.each_with_index do |row, i|
275
assert_equal(fields[i], row)
279
CSV.open(@temp_csv_path, "rb:#{encoding}:#{__ENCODING__}", options) do |csv|
280
csv.each_with_index do |row, i|
281
assert_equal(orig_fields[i], row)
283
end unless encoding == __ENCODING__
284
rescue Encoding::ConverterNotFoundError
286
options[:encoding] = encoding.name
287
CSV.open(@temp_csv_path, options) do |csv|
288
csv.each_with_index do |row, i|
289
assert_equal(fields[i], row)
292
options.delete(:encoding)
293
options[:external_encoding] = encoding.name
294
options[:internal_encoding] = __ENCODING__.name
296
CSV.open(@temp_csv_path, options) do |csv|
297
csv.each_with_index do |row, i|
298
assert_equal(orig_fields[i], row)
300
end unless encoding == __ENCODING__
301
rescue Encoding::ConverterNotFoundError
252
305
def encode_ary(ary, encoding)
260
313
row_sep = (options[:row_sep] || "\n").encode(encoding)
262
315
row.map { |field|
263
[quote_char, field.encode(encoding), quote_char].join
316
[quote_char, field.encode(encoding), quote_char].join('')
264
317
}.join(col_sep) + row_sep
265
}.join.encode(encoding)
318
}.join('').encode(encoding)
268
321
def encode_for_tests(data, options = { })