6 # Created by James Edward Gray II on 2005-10-31.
7 # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
8 # under the terms of Ruby's license.
14 class TestCSVHeaders < Test::Unit::TestCase
16 @data = <<-END_CSV.gsub(/^\s+/, "")
24 [:first_row, true].each do |setting| # two names for the same setting
27 assert_nothing_raised(Exception) do
28 csv = CSV.parse(@data, headers: setting)
31 # first data row - skipping headers
34 assert_instance_of(CSV::Row, row)
35 assert_equal([%w{first A}, %w{second B}, %w{third C}], row.to_a)
40 assert_instance_of(CSV::Row, row)
41 assert_equal([%w{first 1}, %w{second 2}, %w{third 3}], row.to_a)
48 def test_array_of_headers
51 assert_nothing_raised(Exception) do
52 csv = CSV.parse(@data, headers: [:my, :new, :headers])
55 # first data row - skipping headers
58 assert_instance_of(CSV::Row, row)
59 assert_equal( [[:my, "first"], [:new, "second"], [:headers, "third"]],
65 assert_instance_of(CSV::Row, row)
66 assert_equal([[:my, "A"], [:new, "B"], [:headers, "C"]], row.to_a)
71 assert_instance_of(CSV::Row, row)
72 assert_equal([[:my, "1"], [:new, "2"], [:headers, "3"]], row.to_a)
77 # with return and convert
78 assert_nothing_raised(Exception) do
79 csv = CSV.parse( @data, headers: [:my, :new, :headers],
81 header_converters: lambda { |h| h.to_s } )
85 assert_instance_of(CSV::Row, row)
86 assert_equal([["my", :my], ["new", :new], ["headers", :headers]], row.to_a)
87 assert(row.header_row?)
88 assert(!row.field_row?)
91 def test_csv_header_string
94 assert_nothing_raised(Exception) do
95 csv = CSV.parse(@data, headers: "my,new,headers")
98 # first data row - skipping headers
101 assert_instance_of(CSV::Row, row)
102 assert_equal([%w{my first}, %w{new second}, %w{headers third}], row.to_a)
107 assert_instance_of(CSV::Row, row)
108 assert_equal([%w{my A}, %w{new B}, %w{headers C}], row.to_a)
113 assert_instance_of(CSV::Row, row)
114 assert_equal([%w{my 1}, %w{new 2}, %w{headers 3}], row.to_a)
119 # with return and convert
120 assert_nothing_raised(Exception) do
121 csv = CSV.parse( @data, headers: "my,new,headers",
122 return_headers: true,
123 header_converters: :symbol )
127 assert_instance_of(CSV::Row, row)
128 assert_equal([[:my, "my"], [:new, "new"], [:headers, "headers"]], row.to_a)
129 assert(row.header_row?)
130 assert(!row.field_row?)
133 def test_csv_header_string_inherits_separators
134 # parse with custom col_sep
136 assert_nothing_raised(Exception) do
137 csv = CSV.parse( @data.tr(",", "|"), col_sep: "|",
138 headers: "my|new|headers" )
141 # verify headers were recognized
144 assert_instance_of(CSV::Row, row)
145 assert_equal([%w{my first}, %w{new second}, %w{headers third}], row.to_a)
148 def test_return_headers
149 # activate headers and request they are returned
151 assert_nothing_raised(Exception) do
152 csv = CSV.parse(@data, headers: true, return_headers: true)
158 assert_instance_of(CSV::Row, row)
159 assert_equal( [%w{first first}, %w{second second}, %w{third third}],
161 assert(row.header_row?)
162 assert(!row.field_row?)
164 # first data row - skipping headers
167 assert_instance_of(CSV::Row, row)
168 assert_equal([%w{first A}, %w{second B}, %w{third C}], row.to_a)
169 assert(!row.header_row?)
170 assert(row.field_row?)
175 assert_instance_of(CSV::Row, row)
176 assert_equal([%w{first 1}, %w{second 2}, %w{third 3}], row.to_a)
177 assert(!row.header_row?)
178 assert(row.field_row?)
185 # create test data where headers and fields look alike
186 data = <<-END_MATCHING_CSV.gsub(/^\s+/, "")
191 # normal converters do not affect headers
192 csv = CSV.parse( data, headers: true,
193 return_headers: true,
194 converters: :numeric )
195 assert_equal([%w{1 1}, %w{2 2}, %w{3 3}], csv[0].to_a)
196 assert_equal([["1", 1], ["2", 2], ["3", 3]], csv[1].to_a)
199 # header converters do affect headers (only)
200 assert_nothing_raised(Exception) do
201 csv = CSV.parse( data, headers: true,
202 return_headers: true,
203 converters: :numeric,
204 header_converters: :symbol )
206 assert_equal([[:"1", "1"], [:"2", "2"], [:"3", "3"]], csv[0].to_a)
207 assert_equal([[:"1", 1], [:"2", 2], [:"3", 3]], csv[1].to_a)
211 def test_builtin_downcase_converter
212 csv = CSV.parse( "One,TWO Three", headers: true,
213 return_headers: true,
214 header_converters: :downcase )
215 assert_equal(%w{one two\ three}, csv.headers)
218 def test_builtin_symbol_converter
219 csv = CSV.parse( "One,TWO Three", headers: true,
220 return_headers: true,
221 header_converters: :symbol )
222 assert_equal([:one, :two_three], csv.headers)
225 def test_custom_converter
226 converter = lambda { |header| header.tr(" ", "_") }
227 csv = CSV.parse( "One,TWO Three",
229 return_headers: true,
230 header_converters: converter )
231 assert_equal(%w{One TWO_Three}, csv.headers)
234 def test_table_support
236 assert_nothing_raised(Exception) do
237 csv = CSV.parse(@data, headers: true)
240 assert_instance_of(CSV::Table, csv)
244 @data = <<-END_CSV.gsub(/^ +/, "")
255 expected = [%w[1 2 3]]
256 CSV.parse(@data, headers: true, skip_blanks: true) do |row|
257 assert_equal(expected.shift, row.fields)
260 expected = [%w[A B C], %w[1 2 3]]
263 return_headers: true,
264 skip_blanks: true ) do |row|
265 assert_equal(expected.shift, row.fields)
269 def test_headers_reader
271 assert_nil(CSV.new(@data).headers)
274 csv = CSV.new(@data, headers: true)
275 assert_equal(true, csv.headers) # before headers are read
276 csv.shift # set headers
277 assert_equal(%w[first second third], csv.headers) # after headers are read
280 def test_blank_row_bug_fix
281 @data += "\n#{@data}" # add a blank row
283 # ensure that everything returned is a Row object
284 CSV.parse(@data, headers: true) do |row|
285 assert_instance_of(CSV::Row, row)