1
#!/usr/local/bin/ruby -w
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 TestCSVInterface < Test::Unit::TestCase
15
@path = File.join(File.dirname(__FILE__), "temp_test_data.csv")
17
File.open(@path, "w") do |file|
22
@expected = [%w{1 2 3}, %w{4 5}]
29
### Test Read Interface ###
32
CSV.foreach(@path, :col_sep => "\t", :row_sep => "\r\n") do |row|
33
assert_equal(@expected.shift, row)
37
def test_open_and_close
38
csv = CSV.open(@path, "r+", :col_sep => "\t", :row_sep => "\r\n")
40
assert_instance_of(CSV, csv)
41
assert_equal(false, csv.closed?)
45
ret = CSV.open(@path) do |csv|
46
assert_instance_of(CSV, csv)
50
assert_equal("Return value.", ret)
54
data = File.read(@path)
55
assert_equal( @expected,
56
CSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") )
58
CSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") do |row|
59
assert_equal(@expected.shift, row)
64
row = CSV.parse_line("1;2;3", :col_sep => ";")
66
assert_instance_of(Array, row)
67
assert_equal(%w{1 2 3}, row)
70
row = "1;2;3".parse_csv(:col_sep => ";")
72
assert_instance_of(Array, row)
73
assert_equal(%w{1 2 3}, row)
76
def test_read_and_readlines
77
assert_equal( @expected,
78
CSV.read(@path, :col_sep => "\t", :row_sep => "\r\n") )
79
assert_equal( @expected,
80
CSV.readlines(@path, :col_sep => "\t", :row_sep => "\r\n") )
83
data = CSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
86
assert_equal(@expected, data)
87
data = CSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
90
assert_equal(@expected, data)
94
table = CSV.table(@path, :col_sep => "\t", :row_sep => "\r\n")
95
assert_instance_of(CSV::Table, table)
96
assert_equal([[:"1", :"2", :"3"], [4, 5, nil]], table.to_a)
99
def test_shift # aliased as gets() and readline()
100
CSV.open(@path, "r+", :col_sep => "\t", :row_sep => "\r\n") do |csv|
101
assert_equal(@expected.shift, csv.shift)
102
assert_equal(@expected.shift, csv.shift)
103
assert_equal(nil, csv.shift)
107
### Test Write Interface ###
110
str = CSV.generate do |csv| # default empty String
111
assert_instance_of(CSV, csv)
112
assert_equal(csv, csv << [1, 2, 3])
113
assert_equal(csv, csv << [4, nil, 5])
116
assert_instance_of(String, str)
117
assert_equal("1,2,3\n4,,5\n", str)
119
CSV.generate(str) do |csv| # appending to a String
120
assert_equal(csv, csv << ["last", %Q{"row"}])
122
assert_equal(%Q{1,2,3\n4,,5\nlast,"""row"""\n}, str)
125
def test_generate_line
126
line = CSV.generate_line(%w{1 2 3}, :col_sep => ";")
128
assert_instance_of(String, line)
129
assert_equal("1;2;3\n", line)
132
line = %w{1 2 3}.to_csv(:col_sep => ";")
134
assert_instance_of(String, line)
135
assert_equal("1;2;3\n", line)
138
def test_write_header_detection
142
CSV.open(@path, "w", :headers => true) do |csv|
145
assert_equal(headers, csv.instance_variable_get(:@headers))
149
def test_write_lineno
152
CSV.open(@path, "w") do |csv|
154
lines.times { csv << %w{a b c} }
155
assert_equal(lines, csv.lineno)
162
lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
163
CSV.open( @path, "w", :headers => true,
165
:header_converters => :symbol ) do |csv|
166
csv << lines.first.keys
167
lines.each { |line| csv << line }
169
CSV.open( @path, "w", :headers => true,
171
:header_converters => :symbol ) do |csv|
172
csv.each { |line| assert_equal(lines.shift, line.to_hash) }
176
def test_append # aliased add_row() and puts()
179
CSV.open(@path, "w", :col_sep => "\t", :row_sep => "\r\n") do |csv|
180
@expected.each { |row| csv << row }
185
# same thing using CSV::Row objects
188
CSV.open(@path, "w", :col_sep => "\t", :row_sep => "\r\n") do |csv|
189
@expected.each { |row| csv << CSV::Row.new(Array.new, row) }
195
### Test Read and Write Interface ###
198
assert_respond_to(CSV, :filter)
200
expected = [[1, 2, 3], [4, 5]]
201
CSV.filter( "1;2;3\n4;5\n", (result = String.new),
202
:in_col_sep => ";", :out_col_sep => ",",
203
:converters => :all ) do |row|
204
assert_equal(row, expected.shift)
205
row.map! { |n| n * 2 }
208
assert_equal("2,4,6,\"Added\r\"\n8,10,\"Added\r\"\n", result)
215
assert_nothing_raised(Exception) do
216
first = CSV.instance(csv, :col_sep => ";")
220
assert_equal("a;b;c\n", csv)
223
assert_nothing_raised(Exception) do
224
second = CSV.instance(csv, :col_sep => ";")
228
assert_equal(first.object_id, second.object_id)
229
assert_equal("a;b;c\n1;2;3\n", csv)
232
assert_equal(STDOUT, CSV.instance.instance_eval { @io })
233
assert_equal(STDOUT, CSV { |csv| csv.instance_eval { @io } })