1
#!/usr/local/bin/ruby -w
3
# tc_data_converters.rb
5
# Created by James Edward Gray II on 2005-10-31.
6
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
7
# under the terms of Ruby's license.
13
class TestDataConverters < Test::Unit::TestCase
15
@data = "Numbers,:integer,1,:float,3.015"
16
@parser = CSV.new(@data)
18
@custom = lambda { |field| field =~ /\A:(\S.*?)\s*\Z/ ? $1.to_sym : field }
20
@win_safe_time_str = Time.now.strftime("%a %b %d %H:%M:%S %Y")
23
def test_builtin_integer_converter
25
[-5, 1, 10000000000].each do |n|
26
assert_equal(n, CSV::Converters[:integer][n.to_s])
30
(%w{junk 1.0} + [""]).each do |str|
31
assert_equal(str, CSV::Converters[:integer][str])
35
def test_builtin_float_converter
37
[-5.1234, 0, 2.3e-11].each do |n|
38
assert_equal(n, CSV::Converters[:float][n.to_s])
42
(%w{junk 1..0 .015F} + [""]).each do |str|
43
assert_equal(str, CSV::Converters[:float][str])
47
def test_builtin_date_converter
51
CSV::Converters[:date][@win_safe_time_str.sub(/\d+:\d+:\d+ /, "")]
55
assert_instance_of(String, CSV::Converters[:date]["junk"])
58
def test_builtin_date_time_converter
60
assert_instance_of( DateTime,
61
CSV::Converters[:date_time][@win_safe_time_str] )
64
assert_instance_of(String, CSV::Converters[:date_time]["junk"])
67
def test_convert_with_builtin
69
assert(@parser.respond_to?(:convert))
70
assert_nothing_raised(Exception) { @parser.convert(:integer) }
73
assert_equal(["Numbers", ":integer", 1, ":float", "3.015"], @parser.shift)
78
assert_nothing_raised(Exception) { @parser.convert(:float) }
81
assert_equal(["Numbers", ":integer", 1.0, ":float", 3.015], @parser.shift)
84
def test_convert_order
85
# floats first, then integers...
86
assert_nothing_raised(Exception) do
87
@parser.convert(:float)
88
@parser.convert(:integer)
91
# gets us nothing but floats
92
assert_equal( [String, String, Float, String, Float],
93
@parser.shift.map { |field| field.class } )
97
# integers have precendance...
98
assert_nothing_raised(Exception) do
99
@parser.convert(:integer)
100
@parser.convert(:float)
103
# gives us proper number conversion
104
assert_equal( [String, String, Fixnum, String, Float],
105
@parser.shift.map { |field| field.class } )
108
def test_builtin_numeric_combo_converter
110
assert_nothing_raised(Exception) { @parser.convert(:numeric) }
113
assert_equal( [String, String, Fixnum, String, Float],
114
@parser.shift.map { |field| field.class } )
117
def test_builtin_all_nested_combo_converter
119
@data << ",#{@win_safe_time_str}" # add a DateTime field
120
@parser = CSV.new(@data) # reset parser
121
assert_nothing_raised(Exception) { @parser.convert(:all) }
124
assert_equal( [String, String, Fixnum, String, Float, DateTime],
125
@parser.shift.map { |field| field.class } )
128
def test_convert_with_custom_code
129
# define custom converter...
130
assert_nothing_raised(Exception) do
131
@parser.convert { |field| field =~ /\A:(\S.*?)\s*\Z/ ? $1.to_sym : field }
135
assert_equal(["Numbers", :integer, "1", :float, "3.015"], @parser.shift)
139
# mix built-in and custom...
140
assert_nothing_raised(Exception) { @parser.convert(:numeric) }
141
assert_nothing_raised(Exception) { @parser.convert(&@custom) }
144
assert_equal(["Numbers", :integer, 1, :float, 3.015], @parser.shift)
147
def test_convert_with_custom_code_using_field_info
148
# define custom converter that uses field information...
149
assert_nothing_raised(Exception) do
150
@parser.convert do |field, info|
151
assert_equal(1, info.line)
152
info.index == 4 ? Float(field).floor : field
157
assert_equal(["Numbers", ":integer", "1", ":float", 3], @parser.shift)
160
def test_convert_with_custom_code_using_field_info_header
161
@parser = CSV.new(@data, :headers => %w{one two three four five})
163
# define custom converter that uses field header information...
164
assert_nothing_raised(Exception) do
165
@parser.convert do |field, info|
166
info.header == "three" ? Integer(field) * 100 : field
171
assert_equal( ["Numbers", ":integer", 100, ":float", "3.015"],
172
@parser.shift.fields )
175
def test_shortcut_interface
176
assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
177
CSV.parse_line(@data, :converters => :numeric) )
179
assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
180
CSV.parse_line(@data, :converters => [:integer, :float]) )
182
assert_equal( ["Numbers", :integer, 1, :float, 3.015],
183
CSV.parse_line(@data, :converters => [:numeric, @custom]) )
186
def test_unconverted_fields
188
["Numbers", :integer, 1, :float, 3.015],
189
%w{Numbers :integer 1 :float 3.015} ],
190
["\n", Array.new, Array.new] ].each do |test, fields, unconverted|
192
assert_nothing_raised(Exception) do
193
row = CSV.parse_line( test,
194
:converters => [:numeric, @custom],
195
:unconverted_fields => true )
198
assert_equal(fields, row)
199
assert_respond_to(row, :unconverted_fields)
200
assert_equal(unconverted, row.unconverted_fields)
203
data = <<-END_CSV.gsub(/^\s+/, "")
208
assert_nothing_raised(Exception) do
209
row = CSV.parse_line( data,
210
:converters => :numeric,
211
:unconverted_fields => true,
212
:headers => :first_row )
215
assert_equal([["first", 1], ["second", 2], ["third", 3]], row.to_a)
216
assert_respond_to(row, :unconverted_fields)
217
assert_equal(%w{1 2 3}, row.unconverted_fields)
219
assert_nothing_raised(Exception) do
220
row = CSV.parse_line( data,
221
:converters => :numeric,
222
:unconverted_fields => true,
223
:headers => :first_row,
224
:return_headers => true )
227
assert_equal( [%w{first first}, %w{second second}, %w{third third}],
229
assert_respond_to(row, :unconverted_fields)
230
assert_equal(%w{first second third}, row.unconverted_fields)
232
assert_nothing_raised(Exception) do
233
row = CSV.parse_line( data,
234
:converters => :numeric,
235
:unconverted_fields => true,
236
:headers => :first_row,
237
:return_headers => true,
238
:header_converters => :symbol )
241
assert_equal( [[:first, "first"], [:second, "second"], [:third, "third"]],
243
assert_respond_to(row, :unconverted_fields)
244
assert_equal(%w{first second third}, row.unconverted_fields)
246
assert_nothing_raised(Exception) do
247
row = CSV.parse_line( data,
248
:converters => :numeric,
249
:unconverted_fields => true,
250
:headers => %w{my new headers},
251
:return_headers => true,
252
:header_converters => :symbol )
255
assert_equal( [[:my, "my"], [:new, "new"], [:headers, "headers"]],
257
assert_respond_to(row, :unconverted_fields)
258
assert_equal(Array.new, row.unconverted_fields)