~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/csv/tc_data_converters.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/local/bin/ruby -w
 
2
 
 
3
# tc_data_converters.rb
 
4
#
 
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.
 
8
 
 
9
require "test/unit"
 
10
 
 
11
require "csv"
 
12
 
 
13
class TestDataConverters < Test::Unit::TestCase
 
14
  def setup
 
15
    @data   = "Numbers,:integer,1,:float,3.015"
 
16
    @parser = CSV.new(@data)
 
17
    
 
18
    @custom = lambda { |field| field =~ /\A:(\S.*?)\s*\Z/ ? $1.to_sym : field }
 
19
    
 
20
    @win_safe_time_str = Time.now.strftime("%a %b %d %H:%M:%S %Y")
 
21
  end
 
22
  
 
23
  def test_builtin_integer_converter
 
24
    # does convert
 
25
    [-5, 1, 10000000000].each do |n|
 
26
      assert_equal(n, CSV::Converters[:integer][n.to_s])
 
27
    end
 
28
    
 
29
    # does not convert
 
30
    (%w{junk 1.0} + [""]).each do |str|
 
31
      assert_equal(str, CSV::Converters[:integer][str])
 
32
    end
 
33
  end
 
34
 
 
35
  def test_builtin_float_converter
 
36
    # does convert
 
37
    [-5.1234, 0, 2.3e-11].each do |n|
 
38
      assert_equal(n, CSV::Converters[:float][n.to_s])
 
39
    end
 
40
    
 
41
    # does not convert
 
42
    (%w{junk 1..0 .015F} + [""]).each do |str|
 
43
      assert_equal(str, CSV::Converters[:float][str])
 
44
    end
 
45
  end
 
46
 
 
47
  def test_builtin_date_converter
 
48
    # does convert
 
49
    assert_instance_of(
 
50
      Date,
 
51
      CSV::Converters[:date][@win_safe_time_str.sub(/\d+:\d+:\d+ /, "")]
 
52
    )
 
53
 
 
54
    # does not convert
 
55
    assert_instance_of(String, CSV::Converters[:date]["junk"])
 
56
  end
 
57
 
 
58
  def test_builtin_date_time_converter
 
59
    # does convert
 
60
    assert_instance_of( DateTime,
 
61
                        CSV::Converters[:date_time][@win_safe_time_str] )
 
62
 
 
63
    # does not convert
 
64
    assert_instance_of(String, CSV::Converters[:date_time]["junk"])
 
65
  end
 
66
  
 
67
  def test_convert_with_builtin
 
68
    # setup parser...
 
69
    assert(@parser.respond_to?(:convert))
 
70
    assert_nothing_raised(Exception) { @parser.convert(:integer) }
 
71
    
 
72
    # and use
 
73
    assert_equal(["Numbers", ":integer", 1, ":float", "3.015"], @parser.shift)
 
74
    
 
75
    setup  # reset
 
76
    
 
77
    # setup parser...
 
78
    assert_nothing_raised(Exception) { @parser.convert(:float) }
 
79
    
 
80
    # and use
 
81
    assert_equal(["Numbers", ":integer", 1.0, ":float", 3.015], @parser.shift)
 
82
  end
 
83
  
 
84
  def test_convert_order
 
85
    # floats first, then integers...
 
86
    assert_nothing_raised(Exception) do 
 
87
      @parser.convert(:float)
 
88
      @parser.convert(:integer)
 
89
    end
 
90
    
 
91
    # gets us nothing but floats
 
92
    assert_equal( [String, String, Float, String, Float],
 
93
                  @parser.shift.map { |field| field.class } )
 
94
    
 
95
    setup  # reset
 
96
 
 
97
    # integers have precendance...
 
98
    assert_nothing_raised(Exception) do 
 
99
      @parser.convert(:integer)
 
100
      @parser.convert(:float)
 
101
    end
 
102
    
 
103
    # gives us proper number conversion
 
104
    assert_equal( [String, String, Fixnum, String, Float],
 
105
                  @parser.shift.map { |field| field.class } )
 
106
  end
 
107
 
 
108
  def test_builtin_numeric_combo_converter
 
109
    # setup parser...
 
110
    assert_nothing_raised(Exception) { @parser.convert(:numeric) }
 
111
    
 
112
    # and use
 
113
    assert_equal( [String, String, Fixnum, String, Float],
 
114
                  @parser.shift.map { |field| field.class } )
 
115
  end
 
116
 
 
117
  def test_builtin_all_nested_combo_converter
 
118
    # setup parser...
 
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) }
 
122
    
 
123
    # and use
 
124
    assert_equal( [String, String, Fixnum, String, Float, DateTime],
 
125
                  @parser.shift.map { |field| field.class } )
 
126
  end
 
127
  
 
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 }
 
132
    end
 
133
    
 
134
    # and use
 
135
    assert_equal(["Numbers", :integer, "1", :float, "3.015"], @parser.shift)
 
136
 
 
137
    setup  # reset
 
138
 
 
139
    # mix built-in and custom...
 
140
    assert_nothing_raised(Exception) { @parser.convert(:numeric) }
 
141
    assert_nothing_raised(Exception) { @parser.convert(&@custom) }
 
142
    
 
143
    # and use
 
144
    assert_equal(["Numbers", :integer, 1, :float, 3.015], @parser.shift)
 
145
  end
 
146
  
 
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
 
153
      end
 
154
    end
 
155
    
 
156
    # and use
 
157
    assert_equal(["Numbers", ":integer", "1", ":float", 3], @parser.shift)
 
158
  end
 
159
  
 
160
  def test_convert_with_custom_code_using_field_info_header
 
161
    @parser = CSV.new(@data, :headers => %w{one two three four five})
 
162
    
 
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
 
167
      end
 
168
    end
 
169
    
 
170
    # and use
 
171
    assert_equal( ["Numbers", ":integer", 100, ":float", "3.015"],
 
172
                  @parser.shift.fields )
 
173
  end
 
174
  
 
175
  def test_shortcut_interface
 
176
    assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
 
177
                  CSV.parse_line(@data, :converters => :numeric) )
 
178
 
 
179
    assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
 
180
                  CSV.parse_line(@data, :converters => [:integer, :float]) )
 
181
 
 
182
    assert_equal( ["Numbers", :integer, 1, :float, 3.015],
 
183
                  CSV.parse_line(@data, :converters => [:numeric, @custom]) )
 
184
  end
 
185
  
 
186
  def test_unconverted_fields
 
187
    [ [ @data,
 
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|
 
191
      row = nil
 
192
      assert_nothing_raised(Exception) do 
 
193
        row = CSV.parse_line( test,
 
194
                              :converters         => [:numeric, @custom],
 
195
                              :unconverted_fields => true )
 
196
      end
 
197
      assert_not_nil(row)
 
198
      assert_equal(fields, row)
 
199
      assert_respond_to(row, :unconverted_fields)
 
200
      assert_equal(unconverted, row.unconverted_fields)
 
201
    end
 
202
 
 
203
    data = <<-END_CSV.gsub(/^\s+/, "")
 
204
    first,second,third
 
205
    1,2,3
 
206
    END_CSV
 
207
    row = nil
 
208
    assert_nothing_raised(Exception) do 
 
209
      row = CSV.parse_line( data,
 
210
                            :converters         => :numeric,
 
211
                            :unconverted_fields => true,
 
212
                            :headers            => :first_row )
 
213
    end
 
214
    assert_not_nil(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)
 
218
 
 
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 )
 
225
    end
 
226
    assert_not_nil(row)
 
227
    assert_equal( [%w{first first}, %w{second second}, %w{third third}],
 
228
                  row.to_a )
 
229
    assert_respond_to(row, :unconverted_fields)
 
230
    assert_equal(%w{first second third}, row.unconverted_fields)
 
231
 
 
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 )
 
239
    end
 
240
    assert_not_nil(row)
 
241
    assert_equal( [[:first, "first"], [:second, "second"], [:third, "third"]],
 
242
                  row.to_a )
 
243
    assert_respond_to(row, :unconverted_fields)
 
244
    assert_equal(%w{first second third}, row.unconverted_fields)
 
245
 
 
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 )
 
253
    end
 
254
    assert_not_nil(row)
 
255
    assert_equal( [[:my, "my"], [:new, "new"], [:headers, "headers"]],
 
256
                  row.to_a )
 
257
    assert_respond_to(row, :unconverted_fields)
 
258
    assert_equal(Array.new, row.unconverted_fields)
 
259
  end
 
260
end