kor 0.0.1 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c58658c4082c827571e029a7851e4827ab0b75a3
4
- data.tar.gz: 56ecb7a4bbc4ae99c2f6a95f32e2cebfcdf7f75f
3
+ metadata.gz: 51199de55957675466e9c245d132e9dce9aa7cbf
4
+ data.tar.gz: 751abcb7c42a7c87f548ab81b99d692419431690
5
5
  SHA512:
6
- metadata.gz: 470b0b4d6420f96372afd6aece408e0c889a9081d075a4d1852c4f5cfbd356e30c9f29aaf2f032791743bf55ab27814b513649e8cf148e18a1e3b07e60397804
7
- data.tar.gz: 97952a281731440dd46875791159eb6a890da919a088f319992f2b00928b01567a7abf8379709f6022784278afa1613247f132d3bd3b4e8a675383b753654c1d
6
+ metadata.gz: 0919829c9f6a662b4177452978e880ec626db3a1c2bf1453e1e975ca896a8ea508306acf48226d451f5b8a51e495a920e364183e5650fdb4c6d9c918b7060c57
7
+ data.tar.gz: f1cf7f676811c2d3e4e7b941d3fe9b1ab78c6ef3327d2212ea793b4077d5350e932582deacc5e52ceecd655ae7a6a4be4a11021ffc8c89ad5ec29df3cd081c50
@@ -1,15 +1,24 @@
1
+ require 'csv'
2
+
1
3
  module Kor
2
4
  module Input
3
5
  class Csv < Base
6
+ class ReadError < StandardError
7
+ end
8
+
4
9
  DELIM = ","
5
10
 
6
11
  def head
7
- io.gets.chomp.split(self.class::DELIM).map(&:strip)
12
+ if line = io.gets
13
+ ::CSV.new(line.strip, col_sep: self.class::DELIM).gets
14
+ else
15
+ raise ReadError, "cannot get csv header"
16
+ end
8
17
  end
9
18
 
10
19
  def gets
11
20
  if line = io.gets
12
- line.chomp.split(self.class::DELIM).map(&:strip)
21
+ ::CSV.new(line.strip, col_sep: self.class::DELIM).gets
13
22
  else
14
23
  nil
15
24
  end
@@ -4,8 +4,8 @@ require 'kor/input/csv'
4
4
  module KorInputCsvTest
5
5
  def test_main(m)
6
6
  @io = StringIO.new(<<-CSV)
7
- foo,bar,baz
8
- 1,2,3
7
+ foo,bar,"baz,qux"
8
+ 1,"2,3,4",5
9
9
  a,b,c
10
10
  CSV
11
11
  exit m.run
@@ -15,8 +15,15 @@ CSV
15
15
  @io.rewind
16
16
  csv = Kor::Input::Csv.new(@io)
17
17
  head = csv.head
18
- if head != %w(foo bar baz)
19
- t.error("expect #{%w(foo bar baz)} got #{head}")
18
+ if head != %w(foo bar baz,qux)
19
+ t.error("expect #{%w(foo bar baz,qux)} got #{head}")
20
+ end
21
+
22
+ io2 = StringIO.new("")
23
+ csv = Kor::Input::Csv.new(io2)
24
+ head, err = go { csv.head }
25
+ unless Kor::Input::Csv::ReadError === err
26
+ t.error("expect Kor::Input::Csv::ReadError got #{err.class}:#{err}")
20
27
  end
21
28
  end
22
29
 
@@ -26,8 +33,8 @@ CSV
26
33
  csv.head
27
34
 
28
35
  value = csv.gets
29
- if value != %w(1 2 3)
30
- t.error("expect #{%w(1 2 3)} got #{value}")
36
+ if value != %w(1 2,3,4 5)
37
+ t.error("expect #{%w(1 2,3,4 5)} got #{value}")
31
38
  end
32
39
  value = csv.gets
33
40
  if value != %w(a b c)
@@ -40,4 +47,12 @@ CSV
40
47
  end
41
48
  end
42
49
  end
50
+
51
+ private
52
+
53
+ def go
54
+ [yield, nil]
55
+ rescue Exception => err
56
+ [nil, err]
57
+ end
43
58
  end
@@ -4,11 +4,11 @@ module Kor
4
4
  DELIM = ","
5
5
 
6
6
  def head(keys)
7
- io.puts keys.join(self.class::DELIM)
7
+ io.puts keys.map{|k| "\"#{k}\""}.join(self.class::DELIM)
8
8
  end
9
9
 
10
10
  def puts(values)
11
- io.puts values.join(self.class::DELIM)
11
+ io.puts values.map{|k| "\"#{k}\""}.join(self.class::DELIM)
12
12
  end
13
13
  end
14
14
  end
@@ -5,18 +5,20 @@ module KorOutputCsvTest
5
5
  def test_head(t)
6
6
  io = StringIO.new
7
7
  csv = Kor::Output::Csv.new(io)
8
- csv.head(%w(foo bar baz))
9
- if io.string != "foo,bar,baz\n"
10
- t.error("expect output 'foo,bar,baz\\n' got #{io.string.inspect}")
8
+ csv.head(%w(foo bar baz,qux))
9
+ expect = %Q{"foo","bar","baz,qux"\n}
10
+ if io.string != expect
11
+ t.error("expect output #{expect} got #{io.string.inspect}")
11
12
  end
12
13
  end
13
14
 
14
15
  def test_puts(t)
15
16
  io = StringIO.new
16
17
  csv = Kor::Output::Csv.new(io)
17
- csv.puts(%w(aaa bbb ccc))
18
- if io.string != "aaa,bbb,ccc\n"
19
- t.error("expect output 'aaa,bbb,ccc\\n' got #{io.string.inspect}")
18
+ csv.puts(%w(aaa bbb,ccc ddd))
19
+ expect = %Q{"aaa","bbb,ccc","ddd"\n}
20
+ if io.string != expect
21
+ t.error("expect output #{expect} got #{io.string.inspect}")
20
22
  end
21
23
  end
22
24
  end
@@ -1,7 +1,7 @@
1
1
  require 'kor'
2
2
  require 'kor/output/markdown'
3
3
 
4
- module KorOutputCsvTest
4
+ module KorOutputMarkdownTest
5
5
  def test_head(t)
6
6
  io = StringIO.new
7
7
  csv = Kor::Output::Markdown.new(io)
@@ -5,18 +5,20 @@ module KorOutputTsvTest
5
5
  def test_head(t)
6
6
  io = StringIO.new
7
7
  tsv = Kor::Output::Tsv.new(io)
8
- tsv.head(%w(foo bar baz))
9
- if io.string != "foo\tbar\tbaz\n"
10
- t.error("expect output 'foo\\tbar\\tbaz\\n' got #{io.string.inspect}")
8
+ tsv.head(["foo", "bar", "baz\tqux"])
9
+ expect = %Q{"foo"\t"bar"\t"baz\tqux"\n}
10
+ if io.string != expect
11
+ t.error("expect output #{expect.inspect} got #{io.string.inspect}")
11
12
  end
12
13
  end
13
14
 
14
15
  def test_puts(t)
15
16
  io = StringIO.new
16
17
  tsv = Kor::Output::Tsv.new(io)
17
- tsv.puts(%w(aaa bbb ccc))
18
- if io.string != "aaa\tbbb\tccc\n"
19
- t.error("expect output 'aaa\\tbbb\\tccc\\n' got #{io.string.inspect}")
18
+ tsv.puts(["aaa", "bbb\tccc", "ddd"])
19
+ expect = %Q{"aaa"\t"bbb\tccc"\t"ddd"\n}
20
+ if io.string != expect
21
+ t.error("expect output #{expect.inspect} got #{io.string.inspect}")
20
22
  end
21
23
  end
22
24
  end
@@ -1,3 +1,3 @@
1
1
  module Kor
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss