csv-freeze-headers-2.patch

Andrew Vit, 11/24/2013 08:35 AM

Download (2.1 KB)

View differences:

lib/csv.rb
235 235
    #
236 236
    def initialize(headers, fields, header_row = false)
237 237
      @header_row = header_row
238
      headers.each { |h| h.freeze if h.is_a? String }
238 239

  
239 240
      # handle extra headers or fields
240 241
      @row = if headers.size > fields.size
......
2205 2206
      # prepare converted and unconverted copies
2206 2207
      row      = @headers                       if row.nil?
2207 2208
      @headers = convert_fields(@headers, true)
2209
      @headers.each { |h| h.freeze if h.is_a? String }
2208 2210

  
2209 2211
      if @return_headers                                     # return headers
2210 2212
        return self.class::Row.new(@headers, row, true)
test/csv/test_interface.rb
198 198
    end
199 199
  end
200 200

  
201
  def test_write_hash_with_string_keys
202
    File.unlink(@path)
203

  
204
    lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
205
    CSV.open( @path, "wb", headers: true ) do |csv|
206
      csv << lines.first.keys
207
      lines.each { |line| csv << line }
208
    end
209
    CSV.open( @path, "rb", headers: true ) do |csv|
210
      csv.each do |line|
211
        csv.headers.each_with_index do |header, h|
212
          keys = line.to_hash.keys
213
          assert_instance_of(String, keys[h])
214
          assert_same(header, keys[h])
215
        end
216
      end
217
    end
218
  end
219

  
201 220
  def test_write_hash_with_headers_array
202 221
    File.unlink(@path)
203 222

  
test/csv/test_row.rb
297 297
  end
298 298

  
299 299
  def test_to_hash
300
    assert_equal({"A" => nil, "B" => 2, "C" => 3}, @row.to_hash)
300
    hash = @row.to_hash
301
    assert_equal({"A" => nil, "B" => 2, "C" => 3}, hash)
302
    hash.keys.each_with_index do |string_key, h|
303
      assert(string_key.frozen?)
304
      assert_same(string_key, @row.headers[h])
305
    end
301 306
  end
302 307

  
303 308
  def test_to_csv