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

« back to all changes in this revision

Viewing changes to test/csv/tc_interface.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_interface.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 TestCSVInterface < Test::Unit::TestCase
 
14
  def setup
 
15
    @path = File.join(File.dirname(__FILE__), "temp_test_data.csv")
 
16
    
 
17
    File.open(@path, "w") do |file|
 
18
      file << "1\t2\t3\r\n"
 
19
      file << "4\t5\r\n"
 
20
    end
 
21
 
 
22
    @expected = [%w{1 2 3}, %w{4 5}]
 
23
  end
 
24
  
 
25
  def teardown
 
26
    File.unlink(@path)
 
27
  end
 
28
  
 
29
  ### Test Read Interface ###
 
30
  
 
31
  def test_foreach
 
32
    CSV.foreach(@path, :col_sep => "\t", :row_sep => "\r\n") do |row|
 
33
      assert_equal(@expected.shift, row)
 
34
    end
 
35
  end
 
36
  
 
37
  def test_open_and_close
 
38
    csv = CSV.open(@path, "r+", :col_sep => "\t", :row_sep => "\r\n")
 
39
    assert_not_nil(csv)
 
40
    assert_instance_of(CSV, csv)
 
41
    assert_equal(false, csv.closed?)
 
42
    csv.close
 
43
    assert(csv.closed?)
 
44
    
 
45
    ret = CSV.open(@path) do |csv|
 
46
      assert_instance_of(CSV, csv)
 
47
      "Return value."
 
48
    end
 
49
    assert(csv.closed?)
 
50
    assert_equal("Return value.", ret)
 
51
  end
 
52
  
 
53
  def test_parse
 
54
    data = File.read(@path)
 
55
    assert_equal( @expected,
 
56
                  CSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") )
 
57
 
 
58
    CSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") do |row|
 
59
      assert_equal(@expected.shift, row)
 
60
    end
 
61
  end
 
62
  
 
63
  def test_parse_line
 
64
    row = CSV.parse_line("1;2;3", :col_sep => ";")
 
65
    assert_not_nil(row)
 
66
    assert_instance_of(Array, row)
 
67
    assert_equal(%w{1 2 3}, row)
 
68
    
 
69
    # shortcut interface
 
70
    row = "1;2;3".parse_csv(:col_sep => ";")
 
71
    assert_not_nil(row)
 
72
    assert_instance_of(Array, row)
 
73
    assert_equal(%w{1 2 3}, row)
 
74
  end
 
75
  
 
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") )
 
81
    
 
82
    
 
83
    data = CSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
 
84
      csv.read
 
85
    end
 
86
    assert_equal(@expected, data)
 
87
    data = CSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
 
88
      csv.readlines
 
89
    end
 
90
    assert_equal(@expected, data)
 
91
  end
 
92
  
 
93
  def test_table
 
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)
 
97
  end
 
98
  
 
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)
 
104
    end
 
105
  end
 
106
  
 
107
  ### Test Write Interface ###
 
108
 
 
109
  def test_generate
 
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])
 
114
    end
 
115
    assert_not_nil(str)
 
116
    assert_instance_of(String, str)
 
117
    assert_equal("1,2,3\n4,,5\n", str)
 
118
 
 
119
    CSV.generate(str) do |csv|   # appending to a String
 
120
      assert_equal(csv, csv << ["last", %Q{"row"}])
 
121
    end
 
122
    assert_equal(%Q{1,2,3\n4,,5\nlast,"""row"""\n}, str)
 
123
  end
 
124
  
 
125
  def test_generate_line
 
126
    line = CSV.generate_line(%w{1 2 3}, :col_sep => ";")
 
127
    assert_not_nil(line)
 
128
    assert_instance_of(String, line)
 
129
    assert_equal("1;2;3\n", line)
 
130
    
 
131
    # shortcut interface
 
132
    line = %w{1 2 3}.to_csv(:col_sep => ";")
 
133
    assert_not_nil(line)
 
134
    assert_instance_of(String, line)
 
135
    assert_equal("1;2;3\n", line)
 
136
  end
 
137
 
 
138
  def test_write_header_detection
 
139
    File.unlink(@path)
 
140
 
 
141
    headers = %w{a b c}
 
142
    CSV.open(@path, "w", :headers => true) do |csv|
 
143
      csv << headers
 
144
      csv << %w{1 2 3}
 
145
      assert_equal(headers, csv.instance_variable_get(:@headers))
 
146
    end
 
147
  end
 
148
 
 
149
  def test_write_lineno
 
150
    File.unlink(@path)
 
151
 
 
152
    CSV.open(@path, "w") do |csv|
 
153
      lines = 20
 
154
      lines.times { csv << %w{a b c} }
 
155
      assert_equal(lines, csv.lineno)
 
156
    end
 
157
  end
 
158
 
 
159
  def test_write_hash
 
160
    File.unlink(@path)
 
161
 
 
162
    lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
 
163
    CSV.open( @path, "w", :headers           => true,
 
164
                          :converters        => :all,
 
165
                          :header_converters => :symbol ) do |csv|
 
166
      csv << lines.first.keys
 
167
      lines.each { |line| csv << line }
 
168
    end
 
169
    CSV.open( @path, "w", :headers           => true,
 
170
                          :converters        => :all,
 
171
                          :header_converters => :symbol ) do |csv|
 
172
      csv.each { |line| assert_equal(lines.shift, line.to_hash) }
 
173
    end
 
174
  end
 
175
  
 
176
  def test_append  # aliased add_row() and puts()
 
177
    File.unlink(@path)
 
178
    
 
179
    CSV.open(@path, "w", :col_sep => "\t", :row_sep => "\r\n") do |csv|
 
180
      @expected.each { |row| csv << row }
 
181
    end
 
182
 
 
183
    test_shift
 
184
 
 
185
    # same thing using CSV::Row objects
 
186
    File.unlink(@path)
 
187
    
 
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) }
 
190
    end
 
191
 
 
192
    test_shift
 
193
  end
 
194
  
 
195
  ### Test Read and Write Interface ###
 
196
  
 
197
  def test_filter
 
198
    assert_respond_to(CSV, :filter)
 
199
    
 
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 }
 
206
      row << "Added\r"
 
207
    end
 
208
    assert_equal("2,4,6,\"Added\r\"\n8,10,\"Added\r\"\n", result)
 
209
  end
 
210
  
 
211
  def test_instance
 
212
    csv = String.new
 
213
    
 
214
    first = nil
 
215
    assert_nothing_raised(Exception) do 
 
216
      first =  CSV.instance(csv, :col_sep => ";")
 
217
      first << %w{a b c}
 
218
    end
 
219
    
 
220
    assert_equal("a;b;c\n", csv)
 
221
    
 
222
    second = nil
 
223
    assert_nothing_raised(Exception) do 
 
224
      second =  CSV.instance(csv, :col_sep => ";")
 
225
      second << [1, 2, 3]
 
226
    end
 
227
    
 
228
    assert_equal(first.object_id, second.object_id)
 
229
    assert_equal("a;b;c\n1;2;3\n", csv)
 
230
    
 
231
    # shortcuts
 
232
    assert_equal(STDOUT, CSV.instance.instance_eval { @io })
 
233
    assert_equal(STDOUT, CSV { |csv| csv.instance_eval { @io } })
 
234
  end
 
235
end