~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/csv/test_encodings.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#  Copyright 2008 James Edward Gray II. You can redistribute or modify this code
8
8
#  under the terms of Ruby's license.
9
9
 
10
 
require "test/unit"
11
 
 
12
 
require "csv"
13
 
 
14
 
class TestEncodings < Test::Unit::TestCase
 
10
require_relative "base"
 
11
 
 
12
class TestCSV::Encodings < TestCSV
 
13
  extend DifferentOFS
 
14
 
15
15
  def setup
 
16
    super
16
17
    require 'tempfile'
17
18
    @temp_csv_file = Tempfile.new(%w"test_csv. .csv")
18
19
    @temp_csv_path = @temp_csv_file.path
21
22
 
22
23
  def teardown
23
24
    @temp_csv_file.close!
 
25
    super
24
26
  end
25
27
 
26
28
  ########################################
77
79
    end
78
80
  end
79
81
 
 
82
  def test_read_with_default_encoding
 
83
    data             = "abc"
 
84
    default_external = Encoding.default_external
 
85
    each_encoding do |encoding|
 
86
      File.open(@temp_csv_path, "wb", encoding: encoding) {|f| f << data}
 
87
      begin
 
88
        no_warnings do
 
89
          Encoding.default_external = encoding
 
90
        end
 
91
        result = CSV.read(@temp_csv_path)[0][0]
 
92
      ensure
 
93
        no_warnings do
 
94
          Encoding.default_external = default_external
 
95
        end
 
96
      end
 
97
      assert_equal(encoding, result.encoding)
 
98
    end
 
99
  end
 
100
 
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)}
163
183
      end
164
184
 
165
185
      # read and write with transcoding
220
240
      assert_equal(data, CSV.read(@temp_csv_path, encoding: encoding.name))
221
241
    end
222
242
  end
223
 
  
 
243
 
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)
230
250
  end
231
 
  
 
251
 
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)
238
258
  end
239
259
 
241
261
 
242
262
  def assert_parses(fields, encoding, options = { })
243
263
    encoding = Encoding.find(encoding) unless encoding.is_a? Encoding
 
264
    orig_fields = fields
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.")
249
271
    end
 
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)
 
276
      end
 
277
    end
 
278
    begin
 
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)
 
282
        end
 
283
      end unless encoding == __ENCODING__
 
284
    rescue Encoding::ConverterNotFoundError
 
285
    end
 
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)
 
290
      end
 
291
    end
 
292
    options.delete(:encoding)
 
293
    options[:external_encoding] = encoding.name
 
294
    options[:internal_encoding] = __ENCODING__.name
 
295
    begin
 
296
      CSV.open(@temp_csv_path, options) do |csv|
 
297
        csv.each_with_index do |row, i|
 
298
          assert_equal(orig_fields[i], row)
 
299
        end
 
300
      end unless encoding == __ENCODING__
 
301
    rescue Encoding::ConverterNotFoundError
 
302
    end
250
303
  end
251
304
 
252
305
  def encode_ary(ary, encoding)
260
313
    row_sep    = (options[:row_sep]    || "\n").encode(encoding)
261
314
    ary.map { |row|
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)
266
319
  end
267
320
 
268
321
  def encode_for_tests(data, options = { })
276
329
      yield encoding
277
330
    end
278
331
  end
 
332
  
 
333
  def no_warnings
 
334
    old_verbose, $VERBOSE = $VERBOSE, nil
 
335
    yield
 
336
  ensure
 
337
    $VERBOSE = old_verbose
 
338
  end
279
339
end