1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# -*- coding: utf-8 -*-
# frozen_string_literal: false
require_relative "../helper"
class TestCSVParseUnconvertedFields < Test::Unit::TestCase
extend DifferentOFS
def setup
super
@custom = lambda {|field| /\A:(\S.*?)\s*\Z/ =~ field ? $1.to_sym : field}
@headers = ["first", "second", "third"]
@data = <<-CSV
first,second,third
1,2,3
CSV
end
def test_custom
row = CSV.parse_line("Numbers,:integer,1,:float,3.015",
converters: [:numeric, @custom],
unconverted_fields: true)
assert_equal([
["Numbers", :integer, 1, :float, 3.015],
["Numbers", ":integer", "1", ":float", "3.015"],
],
[
row,
row.unconverted_fields,
])
end
def test_no_fields
row = CSV.parse_line("\n",
converters: [:numeric, @custom],
unconverted_fields: true)
assert_equal([
[],
[],
],
[
row,
row.unconverted_fields,
])
end
def test_parsed_header
row = CSV.parse_line(@data,
converters: :numeric,
unconverted_fields: true,
headers: :first_row)
assert_equal([
CSV::Row.new(@headers,
[1, 2, 3]),
["1", "2", "3"],
],
[
row,
row.unconverted_fields,
])
end
def test_return_headers
row = CSV.parse_line(@data,
converters: :numeric,
unconverted_fields: true,
headers: :first_row,
return_headers: true)
assert_equal([
CSV::Row.new(@headers,
@headers),
@headers,
],
[
row,
row.unconverted_fields,
])
end
def test_header_converters
row = CSV.parse_line(@data,
converters: :numeric,
unconverted_fields: true,
headers: :first_row,
return_headers: true,
header_converters: :symbol)
assert_equal([
CSV::Row.new(@headers.collect(&:to_sym),
@headers),
@headers,
],
[
row,
row.unconverted_fields,
])
end
def test_specified_headers
row = CSV.parse_line("\n",
converters: :numeric,
unconverted_fields: true,
headers: %w{my new headers},
return_headers: true,
header_converters: :symbol)
assert_equal([
CSV::Row.new([:my, :new, :headers],
["my", "new", "headers"]),
[],
],
[
row,
row.unconverted_fields,
])
end
end
|