Feature #9143 » csv-freeze-headers-2.patch
lib/csv.rb | ||
---|---|---|
#
|
||
def initialize(headers, fields, header_row = false)
|
||
@header_row = header_row
|
||
headers.each { |h| h.freeze if h.is_a? String }
|
||
# handle extra headers or fields
|
||
@row = if headers.size > fields.size
|
||
... | ... | |
# prepare converted and unconverted copies
|
||
row = @headers if row.nil?
|
||
@headers = convert_fields(@headers, true)
|
||
@headers.each { |h| h.freeze if h.is_a? String }
|
||
if @return_headers # return headers
|
||
return self.class::Row.new(@headers, row, true)
|
test/csv/test_interface.rb | ||
---|---|---|
end
|
||
end
|
||
def test_write_hash_with_string_keys
|
||
File.unlink(@path)
|
||
lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
|
||
CSV.open( @path, "wb", headers: true ) do |csv|
|
||
csv << lines.first.keys
|
||
lines.each { |line| csv << line }
|
||
end
|
||
CSV.open( @path, "rb", headers: true ) do |csv|
|
||
csv.each do |line|
|
||
csv.headers.each_with_index do |header, h|
|
||
keys = line.to_hash.keys
|
||
assert_instance_of(String, keys[h])
|
||
assert_same(header, keys[h])
|
||
end
|
||
end
|
||
end
|
||
end
|
||
def test_write_hash_with_headers_array
|
||
File.unlink(@path)
|
||
test/csv/test_row.rb | ||
---|---|---|
end
|
||
def test_to_hash
|
||
assert_equal({"A" => nil, "B" => 2, "C" => 3}, @row.to_hash)
|
||
hash = @row.to_hash
|
||
assert_equal({"A" => nil, "B" => 2, "C" => 3}, hash)
|
||
hash.keys.each_with_index do |string_key, h|
|
||
assert(string_key.frozen?)
|
||
assert_same(string_key, @row.headers[h])
|
||
end
|
||
end
|
||
def test_to_csv
|
- « Previous
- 1
- 2
- Next »