fastcsv 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,420 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ # tc_table.rb
5
+ #
6
+ # Created by James Edward Gray II on 2005-10-31.
7
+ # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
8
+ # under the terms of Ruby's license.
9
+
10
+ require_relative "base"
11
+
12
+ class TestCSV::Table < TestCSV
13
+ extend DifferentOFS
14
+
15
+ def setup
16
+ super
17
+ @rows = [ FastCSV::Row.new(%w{A B C}, [1, 2, 3]),
18
+ FastCSV::Row.new(%w{A B C}, [4, 5, 6]),
19
+ FastCSV::Row.new(%w{A B C}, [7, 8, 9]) ]
20
+ @table = FastCSV::Table.new(@rows)
21
+
22
+ @header_table = FastCSV::Table.new(
23
+ [FastCSV::Row.new(%w{A B C}, %w{A B C}, true)] + @rows
24
+ )
25
+ end
26
+
27
+ def test_initialze
28
+ assert_not_nil(@table)
29
+ assert_instance_of(FastCSV::Table, @table)
30
+ end
31
+
32
+ def test_modes
33
+ assert_equal(:col_or_row, @table.mode)
34
+
35
+ # non-destructive changes, intended for one shot calls
36
+ cols = @table.by_col
37
+ assert_equal(:col_or_row, @table.mode)
38
+ assert_equal(:col, cols.mode)
39
+ assert_equal(@table, cols)
40
+
41
+ rows = @table.by_row
42
+ assert_equal(:col_or_row, @table.mode)
43
+ assert_equal(:row, rows.mode)
44
+ assert_equal(@table, rows)
45
+
46
+ # destructive mode changing calls
47
+ assert_equal(@table, @table.by_row!)
48
+ assert_equal(:row, @table.mode)
49
+ assert_equal(@table, @table.by_col_or_row!)
50
+ assert_equal(:col_or_row, @table.mode)
51
+ end
52
+
53
+ def test_headers
54
+ assert_equal(@rows.first.headers, @table.headers)
55
+ end
56
+
57
+ def test_index
58
+ ##################
59
+ ### Mixed Mode ###
60
+ ##################
61
+ # by row
62
+ @rows.each_index { |i| assert_equal(@rows[i], @table[i]) }
63
+ assert_equal(nil, @table[100]) # empty row
64
+
65
+ # by col
66
+ @rows.first.headers.each do |header|
67
+ assert_equal(@rows.map { |row| row[header] }, @table[header])
68
+ end
69
+ assert_equal([nil] * @rows.size, @table["Z"]) # empty col
70
+
71
+ # by cell, row then col
72
+ assert_equal(2, @table[0][1])
73
+ assert_equal(6, @table[1]["C"])
74
+
75
+ # by cell, col then row
76
+ assert_equal(5, @table["B"][1])
77
+ assert_equal(9, @table["C"][2])
78
+
79
+ # with headers (by col)
80
+ assert_equal(["B", 2, 5, 8], @header_table["B"])
81
+
82
+ ###################
83
+ ### Column Mode ###
84
+ ###################
85
+ @table.by_col!
86
+
87
+ assert_equal([2, 5, 8], @table[1])
88
+ assert_equal([2, 5, 8], @table["B"])
89
+
90
+ ################
91
+ ### Row Mode ###
92
+ ################
93
+ @table.by_row!
94
+
95
+ assert_equal(@rows[1], @table[1])
96
+ assert_raise(TypeError) { @table["B"] }
97
+
98
+ ############################
99
+ ### One Shot Mode Change ###
100
+ ############################
101
+ assert_equal(@rows[1], @table[1])
102
+ assert_equal([2, 5, 8], @table.by_col[1])
103
+ assert_equal(@rows[1], @table[1])
104
+ end
105
+
106
+ def test_set_row_or_column
107
+ ##################
108
+ ### Mixed Mode ###
109
+ ##################
110
+ # set row
111
+ @table[2] = [10, 11, 12]
112
+ assert_equal([%w[A B C], [1, 2, 3], [4, 5, 6], [10, 11, 12]], @table.to_a)
113
+
114
+ @table[3] = FastCSV::Row.new(%w[A B C], [13, 14, 15])
115
+ assert_equal( [%w[A B C], [1, 2, 3], [4, 5, 6], [10, 11, 12], [13, 14, 15]],
116
+ @table.to_a )
117
+
118
+ # set col
119
+ @table["Type"] = "data"
120
+ assert_equal( [ %w[A B C Type],
121
+ [1, 2, 3, "data"],
122
+ [4, 5, 6, "data"],
123
+ [10, 11, 12, "data"],
124
+ [13, 14, 15, "data"] ],
125
+ @table.to_a )
126
+
127
+ @table["Index"] = [1, 2, 3]
128
+ assert_equal( [ %w[A B C Type Index],
129
+ [1, 2, 3, "data", 1],
130
+ [4, 5, 6, "data", 2],
131
+ [10, 11, 12, "data", 3],
132
+ [13, 14, 15, "data", nil] ],
133
+ @table.to_a )
134
+
135
+ @table["B"] = [100, 200]
136
+ assert_equal( [ %w[A B C Type Index],
137
+ [1, 100, 3, "data", 1],
138
+ [4, 200, 6, "data", 2],
139
+ [10, nil, 12, "data", 3],
140
+ [13, nil, 15, "data", nil] ],
141
+ @table.to_a )
142
+
143
+ # verify resulting table
144
+ assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
145
+ A,B,C,Type,Index
146
+ 1,100,3,data,1
147
+ 4,200,6,data,2
148
+ 10,,12,data,3
149
+ 13,,15,data,
150
+ END_RESULT
151
+
152
+ # with headers
153
+ @header_table["Type"] = "data"
154
+ assert_equal(%w[Type data data data], @header_table["Type"])
155
+
156
+ ###################
157
+ ### Column Mode ###
158
+ ###################
159
+ @table.by_col!
160
+
161
+ @table[1] = [2, 5, 11, 14]
162
+ assert_equal( [ %w[A B C Type Index],
163
+ [1, 2, 3, "data", 1],
164
+ [4, 5, 6, "data", 2],
165
+ [10, 11, 12, "data", 3],
166
+ [13, 14, 15, "data", nil] ],
167
+ @table.to_a )
168
+
169
+ @table["Extra"] = "new stuff"
170
+ assert_equal( [ %w[A B C Type Index Extra],
171
+ [1, 2, 3, "data", 1, "new stuff"],
172
+ [4, 5, 6, "data", 2, "new stuff"],
173
+ [10, 11, 12, "data", 3, "new stuff"],
174
+ [13, 14, 15, "data", nil, "new stuff"] ],
175
+ @table.to_a )
176
+
177
+ ################
178
+ ### Row Mode ###
179
+ ################
180
+ @table.by_row!
181
+
182
+ @table[1] = (1..6).to_a
183
+ assert_equal( [ %w[A B C Type Index Extra],
184
+ [1, 2, 3, "data", 1, "new stuff"],
185
+ [1, 2, 3, 4, 5, 6],
186
+ [10, 11, 12, "data", 3, "new stuff"],
187
+ [13, 14, 15, "data", nil, "new stuff"] ],
188
+ @table.to_a )
189
+
190
+ assert_raise(TypeError) { @table["Extra"] = nil }
191
+ end
192
+
193
+ def test_each
194
+ ######################
195
+ ### Mixed/Row Mode ###
196
+ ######################
197
+ i = 0
198
+ @table.each do |row|
199
+ assert_equal(@rows[i], row)
200
+ i += 1
201
+ end
202
+
203
+ # verify that we can chain the call
204
+ assert_equal(@table, @table.each { })
205
+
206
+ ###################
207
+ ### Column Mode ###
208
+ ###################
209
+ @table.by_col!
210
+
211
+ headers = @table.headers
212
+ @table.each do |header, column|
213
+ assert_equal(headers.shift, header)
214
+ assert_equal(@table[header], column)
215
+ end
216
+
217
+ ############################
218
+ ### One Shot Mode Change ###
219
+ ############################
220
+ @table.by_col_or_row!
221
+
222
+ @table.each { |row| assert_instance_of(FastCSV::Row, row) }
223
+ @table.by_col.each { |tuple| assert_instance_of(Array, tuple) }
224
+ @table.each { |row| assert_instance_of(FastCSV::Row, row) }
225
+ end
226
+
227
+ def test_enumerable
228
+ assert_equal( @rows.values_at(0, 2),
229
+ @table.select { |row| (row["B"] % 2).zero? } )
230
+
231
+ assert_equal(@rows[1], @table.find { |row| row["C"] > 5 })
232
+ end
233
+
234
+ def test_to_a
235
+ assert_equal([%w[A B C], [1, 2, 3], [4, 5, 6], [7, 8, 9]], @table.to_a)
236
+
237
+ # with headers
238
+ assert_equal( [%w[A B C], [1, 2, 3], [4, 5, 6], [7, 8, 9]],
239
+ @header_table.to_a )
240
+ end
241
+
242
+ def test_to_csv
243
+ csv = <<-END_CSV.gsub(/^\s+/, "")
244
+ A,B,C
245
+ 1,2,3
246
+ 4,5,6
247
+ 7,8,9
248
+ END_CSV
249
+
250
+ # normal conversion
251
+ assert_equal(csv, @table.to_csv)
252
+ assert_equal(csv, @table.to_s) # alias
253
+
254
+ # with options
255
+ assert_equal( csv.gsub(",", "|").gsub("\n", "\r\n"),
256
+ @table.to_csv(col_sep: "|", row_sep: "\r\n") )
257
+ assert_equal( csv.lines.to_a[1..-1].join(''),
258
+ @table.to_csv(:write_headers => false) )
259
+
260
+ # with headers
261
+ assert_equal(csv, @header_table.to_csv)
262
+ end
263
+
264
+ def test_append
265
+ # verify that we can chain the call
266
+ assert_equal(@table, @table << [10, 11, 12])
267
+
268
+ # Array append
269
+ assert_equal(FastCSV::Row.new(%w[A B C], [10, 11, 12]), @table[-1])
270
+
271
+ # Row append
272
+ assert_equal(@table, @table << FastCSV::Row.new(%w[A B C], [13, 14, 15]))
273
+ assert_equal(FastCSV::Row.new(%w[A B C], [13, 14, 15]), @table[-1])
274
+ end
275
+
276
+ def test_delete_mixed
277
+ ##################
278
+ ### Mixed Mode ###
279
+ ##################
280
+ # delete a row
281
+ assert_equal(@rows[1], @table.delete(1))
282
+
283
+ # delete a col
284
+ assert_equal(@rows.map { |row| row["A"] }, @table.delete("A"))
285
+
286
+ # verify resulting table
287
+ assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
288
+ B,C
289
+ 2,3
290
+ 8,9
291
+ END_RESULT
292
+ end
293
+
294
+ def test_delete_column
295
+ ###################
296
+ ### Column Mode ###
297
+ ###################
298
+ @table.by_col!
299
+
300
+ assert_equal(@rows.map { |row| row[0] }, @table.delete(0))
301
+ assert_equal(@rows.map { |row| row["C"] }, @table.delete("C"))
302
+
303
+ # verify resulting table
304
+ assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
305
+ B
306
+ 2
307
+ 5
308
+ 8
309
+ END_RESULT
310
+ end
311
+
312
+ def test_delete_row
313
+ ################
314
+ ### Row Mode ###
315
+ ################
316
+ @table.by_row!
317
+
318
+ assert_equal(@rows[1], @table.delete(1))
319
+ assert_raise(TypeError) { @table.delete("C") }
320
+
321
+ # verify resulting table
322
+ assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
323
+ A,B,C
324
+ 1,2,3
325
+ 7,8,9
326
+ END_RESULT
327
+ end
328
+
329
+ def test_delete_with_blank_rows
330
+ data = "col1,col2\nra1,ra2\n\nrb1,rb2"
331
+ table = FastCSV.parse(data, :headers => true)
332
+ assert_equal(["ra2", nil, "rb2"], table.delete("col2"))
333
+ end
334
+
335
+ def test_delete_if_row
336
+ ######################
337
+ ### Mixed/Row Mode ###
338
+ ######################
339
+ # verify that we can chain the call
340
+ assert_equal(@table, @table.delete_if { |row| (row["B"] % 2).zero? })
341
+
342
+ # verify resulting table
343
+ assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
344
+ A,B,C
345
+ 4,5,6
346
+ END_RESULT
347
+ end
348
+
349
+ def test_delete_if_column
350
+ ###################
351
+ ### Column Mode ###
352
+ ###################
353
+ @table.by_col!
354
+
355
+ assert_equal(@table, @table.delete_if { |h, v| h > "A" })
356
+ assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
357
+ A
358
+ 1
359
+ 4
360
+ 7
361
+ END_RESULT
362
+ end
363
+
364
+ def test_values_at
365
+ ##################
366
+ ### Mixed Mode ###
367
+ ##################
368
+ # rows
369
+ assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
370
+ assert_equal(@rows.values_at(1..2), @table.values_at(1..2))
371
+
372
+ # cols
373
+ assert_equal([[1, 3], [4, 6], [7, 9]], @table.values_at("A", "C"))
374
+ assert_equal([[2, 3], [5, 6], [8, 9]], @table.values_at("B".."C"))
375
+
376
+ ###################
377
+ ### Column Mode ###
378
+ ###################
379
+ @table.by_col!
380
+
381
+ assert_equal([[1, 3], [4, 6], [7, 9]], @table.values_at(0, 2))
382
+ assert_equal([[1, 3], [4, 6], [7, 9]], @table.values_at("A", "C"))
383
+
384
+ ################
385
+ ### Row Mode ###
386
+ ################
387
+ @table.by_row!
388
+
389
+ assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
390
+ assert_raise(TypeError) { @table.values_at("A", "C") }
391
+
392
+ ############################
393
+ ### One Shot Mode Change ###
394
+ ############################
395
+ assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
396
+ assert_equal([[1, 3], [4, 6], [7, 9]], @table.by_col.values_at(0, 2))
397
+ assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
398
+ end
399
+
400
+ def test_array_delegation
401
+ assert(!@table.empty?, "Table was empty.")
402
+
403
+ assert_equal(@rows.size, @table.size)
404
+ end
405
+
406
+ def test_inspect_shows_current_mode
407
+ str = @table.inspect
408
+ assert(str.include?("mode:#{@table.mode}"), "Mode not shown.")
409
+
410
+ @table.by_col!
411
+ str = @table.inspect
412
+ assert(str.include?("mode:#{@table.mode}"), "Mode not shown.")
413
+ end
414
+
415
+ def test_inspect_encoding_is_ascii_compatible
416
+ assert( Encoding.compatible?( Encoding.find("US-ASCII"),
417
+ @table.inspect.encoding ),
418
+ "inspect() was not ASCII compatible." )
419
+ end
420
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ # ts_all.rb
5
+ #
6
+ # Created by James Edward Gray II on 2005-10-31.
7
+ # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
8
+ # under the terms of Ruby's license.
9
+
10
+ require "test/unit"
11
+
12
+ require "test_csv_parsing"
13
+ require "test_features"
14
+ require "test_interface"
15
+ require "test_csv_writing"
16
+ require "test_data_converters"
17
+ require "test_row"
18
+ require "test_table"
19
+ require "test_headers"
20
+ require "test_encodings"
data/test/runner.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "fastcsv"
2
+ require 'rbconfig'
3
+
4
+ require 'test/unit'
5
+
6
+ src_testdir = File.dirname(File.realpath(__FILE__))
7
+ $LOAD_PATH << src_testdir
8
+ module Gem
9
+ end
10
+ class Gem::TestCase < MiniTest::Unit::TestCase
11
+ @@project_dir = File.dirname($LOAD_PATH.last)
12
+ end
13
+
14
+ ENV["GEM_SKIP"] = ENV["GEM_HOME"] = ENV["GEM_PATH"] = "".freeze
15
+
16
+ require_relative 'profile_test_all' if ENV.has_key?('RUBY_TEST_ALL_PROFILE')
17
+
18
+ module Test::Unit
19
+ module ZombieHunter
20
+ def after_teardown
21
+ super
22
+ assert_empty(Process.waitall)
23
+ end
24
+ end
25
+ class TestCase
26
+ include ZombieHunter
27
+ end
28
+ end
29
+
30
+ begin
31
+ exit Test::Unit::AutoRunner.run(true, src_testdir)
32
+ rescue NoMemoryError
33
+ system("cat /proc/meminfo") if File.exist?("/proc/meminfo")
34
+ system("ps x -opid,args,%cpu,%mem,nlwp,rss,vsz,wchan,stat,start,time,etime,blocked,caught,ignored,pending,f") if File.exist?("/bin/ps")
35
+ raise
36
+ end
@@ -0,0 +1,17 @@
1
+ module DifferentOFS
2
+ module WithDifferentOFS
3
+ def setup
4
+ super
5
+ @ofs, $, = $,, "-"
6
+ end
7
+ def teardown
8
+ $, = @ofs
9
+ super
10
+ end
11
+ end
12
+
13
+ def self.extended(klass)
14
+ super(klass)
15
+ klass.const_set(:DifferentOFS, Class.new(klass).class_eval {include WithDifferentOFS}).name
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastcsv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Open North
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-12 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coveralls
@@ -90,10 +90,12 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - ".gitignore"
92
92
  - ".rspec"
93
+ - ".travis.yml"
93
94
  - Gemfile
94
95
  - LICENSE
95
96
  - README.md
96
97
  - Rakefile
98
+ - TESTS.md
97
99
  - USAGE
98
100
  - ext/fastcsv/extconf.rb
99
101
  - ext/fastcsv/fastcsv.c
@@ -101,9 +103,26 @@ files:
101
103
  - fastcsv.gemspec
102
104
  - lib/fastcsv.rb
103
105
  - spec/fastcsv_spec.rb
106
+ - spec/fixtures/csv.csv
107
+ - spec/fixtures/iso-8859-1-quoted.csv
104
108
  - spec/fixtures/iso-8859-1.csv
109
+ - spec/fixtures/utf-8-quoted.csv
105
110
  - spec/fixtures/utf-8.csv
106
111
  - spec/spec_helper.rb
112
+ - test/csv/base.rb
113
+ - test/csv/line_endings.gz
114
+ - test/csv/test_csv_parsing.rb
115
+ - test/csv/test_csv_writing.rb
116
+ - test/csv/test_data_converters.rb
117
+ - test/csv/test_encodings.rb
118
+ - test/csv/test_features.rb
119
+ - test/csv/test_headers.rb
120
+ - test/csv/test_interface.rb
121
+ - test/csv/test_row.rb
122
+ - test/csv/test_table.rb
123
+ - test/csv/ts_all.rb
124
+ - test/runner.rb
125
+ - test/with_different_ofs.rb
107
126
  homepage: http://github.com/opennorth/fastcsv
108
127
  licenses:
109
128
  - MIT
@@ -130,7 +149,24 @@ specification_version: 4
130
149
  summary: A fast Ragel-based CSV parser
131
150
  test_files:
132
151
  - spec/fastcsv_spec.rb
152
+ - spec/fixtures/csv.csv
153
+ - spec/fixtures/iso-8859-1-quoted.csv
133
154
  - spec/fixtures/iso-8859-1.csv
155
+ - spec/fixtures/utf-8-quoted.csv
134
156
  - spec/fixtures/utf-8.csv
135
157
  - spec/spec_helper.rb
158
+ - test/csv/base.rb
159
+ - test/csv/line_endings.gz
160
+ - test/csv/test_csv_parsing.rb
161
+ - test/csv/test_csv_writing.rb
162
+ - test/csv/test_data_converters.rb
163
+ - test/csv/test_encodings.rb
164
+ - test/csv/test_features.rb
165
+ - test/csv/test_headers.rb
166
+ - test/csv/test_interface.rb
167
+ - test/csv/test_row.rb
168
+ - test/csv/test_table.rb
169
+ - test/csv/ts_all.rb
170
+ - test/runner.rb
171
+ - test/with_different_ofs.rb
136
172
  has_rdoc: