lightcsv 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ # $Id: test_lightcsv.rb 371 2009-04-01 11:41:43Z tommy $
2
+ # Copyright:: (C) 2007 TOMITA Masahiro <tommy@tmtm.org>
3
+
4
+ require "test/unit"
5
+ require "lightcsv"
6
+ require "tempfile"
7
+ require "stringio"
8
+
9
+ class TC_LightCsv < Test::Unit::TestCase
10
+ def setup()
11
+ @tmpf = Tempfile.new("test")
12
+ @tmpf.puts <<EOS
13
+ a,b,c
14
+ 0,1,2
15
+ EOS
16
+ @tmpf.flush
17
+ @tmpf.rewind
18
+ end
19
+ def teardown()
20
+ @tmpf.close!
21
+ end
22
+
23
+ def test_foreach()
24
+ expect = [["a","b","c"],["0","1","2"]]
25
+ LightCsv.foreach(@tmpf.path) do |r|
26
+ assert_equal(expect.shift, r)
27
+ end
28
+ assert(expect.empty?)
29
+ end
30
+
31
+ def test_readlines()
32
+ expect = [["a","b","c"],["0","1","2"]]
33
+ assert_equal(expect, LightCsv.readlines(@tmpf.path))
34
+ end
35
+
36
+ def test_parse()
37
+ expect = [["a","b","c"],["0","1","2"]]
38
+ assert_equal(expect, LightCsv.parse("a,b,c\n0,1,2"))
39
+ end
40
+
41
+ def test_open()
42
+ assert_kind_of(LightCsv, LightCsv.open(@tmpf.path))
43
+ end
44
+
45
+ def test_open_block()
46
+ ret = LightCsv.open(@tmpf.path) do |csv|
47
+ assert_kind_of(LightCsv, csv)
48
+ 12345
49
+ end
50
+ assert_equal(12345, ret)
51
+ end
52
+
53
+ def test_initialize()
54
+ assert_kind_of(LightCsv, LightCsv.new(@tmpf))
55
+ assert_kind_of(LightCsv, LightCsv.new("a,b,c"))
56
+ end
57
+
58
+ def test_close()
59
+ LightCsv.new(@tmpf).close
60
+ assert(@tmpf.closed?)
61
+ end
62
+
63
+ def test_shift()
64
+ csv = LightCsv.new(<<EOS)
65
+ a,b,c
66
+ 1,2,3
67
+ EOS
68
+ assert_equal(["a","b","c"], csv.shift)
69
+ assert_equal(["1","2","3"], csv.shift)
70
+ assert_equal(nil, csv.shift)
71
+ end
72
+
73
+ def test_shift_crlf()
74
+ csv = LightCsv.new(<<EOS)
75
+ a,b,c\r
76
+ 1,2,3\r
77
+ EOS
78
+ assert_equal(["a","b","c"], csv.shift)
79
+ assert_equal(["1","2","3"], csv.shift)
80
+ assert_equal(nil, csv.shift)
81
+ end
82
+
83
+ def test_shift_cr()
84
+ csv = LightCsv.new("a,b,c\r1,2,3\r")
85
+ assert_equal(["a","b","c"], csv.shift)
86
+ assert_equal(["1","2","3"], csv.shift)
87
+ assert_equal(nil, csv.shift)
88
+ end
89
+
90
+ def test_shift_quote()
91
+ csv = LightCsv.new(<<EOS)
92
+ "a","b","c"
93
+ "1","2","3"
94
+ EOS
95
+ assert_equal(["a","b","c"], csv.shift)
96
+ assert_equal(["1","2","3"], csv.shift)
97
+ assert_equal(nil, csv.shift)
98
+ end
99
+
100
+ def test_shift_quote_in_quote()
101
+ csv = LightCsv.new(<<EOS)
102
+ "a","""","c"
103
+ "1","2""3","4"
104
+ EOS
105
+ assert_equal(["a","\"","c"], csv.shift)
106
+ assert_equal(["1","2\"3", "4"], csv.shift)
107
+ assert_equal(nil, csv.shift)
108
+ end
109
+
110
+ def test_shift_invalid_dq()
111
+ csv = LightCsv.new(<<EOS)
112
+ "a","b","c"
113
+ "1",2","3"
114
+ EOS
115
+ assert_equal(["a","b","c"], csv.shift)
116
+ assert_raises(LightCsv::InvalidFormat){csv.shift}
117
+ end
118
+
119
+ def test_shift_no_dq_end()
120
+ csv = LightCsv.new(<<EOS)
121
+ "a","b","c"
122
+ "1","2,
123
+ EOS
124
+ assert_equal(["a","b","c"], csv.shift)
125
+ assert_raises(LightCsv::InvalidFormat){csv.shift}
126
+ end
127
+
128
+ def test_shift_lf_in_data()
129
+ csv = LightCsv.new(<<EOS)
130
+ "a","b","c
131
+ 1","2","3"
132
+ EOS
133
+ assert_equal(["a","b","c\n1","2","3"], csv.shift)
134
+ end
135
+
136
+ def test_shift_empty_line()
137
+ csv = LightCsv.new(<<EOS)
138
+ a,b,c
139
+
140
+ 1,2,3
141
+ EOS
142
+ assert_equal(["a","b","c"], csv.shift)
143
+ assert_equal([], csv.shift)
144
+ assert_equal(["1","2","3"], csv.shift)
145
+ end
146
+
147
+ def test_shift_empty()
148
+ csv = LightCsv.new("")
149
+ assert_equal(nil, csv.shift)
150
+ end
151
+
152
+ def test_shift_comma_end()
153
+ csv = LightCsv.new("a,b,")
154
+ assert_equal(["a","b",""], csv.shift)
155
+ end
156
+
157
+ def test_over_bufsize()
158
+ s = StringIO.new(%|"aaa""aaa","bbb\r\n""\r\nbbb","ccc""ccc"\r\n| * 3)
159
+ csv = LightCsv.new(s)
160
+ csv.bufsize = 1
161
+ expect = [["aaa\"aaa","bbb\r\n\"\r\nbbb","ccc\"ccc"]] * 3
162
+ assert_equal expect, csv.readlines
163
+ end
164
+
165
+ def test_large_col()
166
+ row = LightCsv.parse('"'+"a"*10000+'"')
167
+ assert_equal [["a"*10000]], row
168
+ end
169
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightcsv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - !binary |
8
+ 44Go44G/44Gf44G+44GV44Gy44KNIDx0b21teUB0bXRtLm9yZz4=
9
+
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-09-29 00:00:00 +01:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: A pure Ruby CSV parser wrapped into a gem version for easier install.
19
+ email: ""
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files:
25
+ - README.txt
26
+ - lib/lightcsv.rb
27
+ files:
28
+ - README.txt
29
+ - setup.rb
30
+ - test/test_lightcsv.rb
31
+ - lib/lightcsv.rb
32
+ - Makefile
33
+ - Rakefile
34
+ - Manifest
35
+ - lightcsv.gemspec
36
+ has_rdoc: true
37
+ homepage: http://www.tmtm.org/ruby/lightcsv/
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --line-numbers
43
+ - --inline-source
44
+ - --title
45
+ - Lightcsv
46
+ - --main
47
+ - README.txt
48
+ - --charset
49
+ - utf-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "1.2"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: lightcsv
67
+ rubygems_version: 1.3.3
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: A pure Ruby CSV parser
71
+ test_files:
72
+ - test/test_lightcsv.rb