tb 0.9 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/README +13 -11
  3. data/lib/tb.rb +14 -6
  4. data/lib/tb/catreader.rb +2 -2
  5. data/lib/tb/cmd_consecutive.rb +6 -2
  6. data/lib/tb/cmd_crop.rb +22 -3
  7. data/lib/tb/cmd_cross.rb +24 -0
  8. data/lib/tb/cmd_cut.rb +20 -10
  9. data/lib/tb/cmd_git.rb +20 -7
  10. data/lib/tb/cmd_group.rb +32 -0
  11. data/lib/tb/cmd_gsub.rb +21 -0
  12. data/lib/tb/cmd_join.rb +28 -0
  13. data/lib/tb/cmd_ls.rb +9 -0
  14. data/lib/tb/cmd_melt.rb +15 -0
  15. data/lib/tb/cmd_mheader.rb +15 -0
  16. data/lib/tb/cmd_nest.rb +27 -6
  17. data/lib/tb/cmd_newfield.rb +19 -2
  18. data/lib/tb/cmd_rename.rb +20 -0
  19. data/lib/tb/{cmd_grep.rb → cmd_search.rb} +37 -23
  20. data/lib/tb/cmd_shape.rb +69 -25
  21. data/lib/tb/cmd_sort.rb +20 -0
  22. data/lib/tb/cmd_tar.rb +38 -0
  23. data/lib/tb/cmd_to_json.rb +2 -2
  24. data/lib/tb/cmd_to_ltsv.rb +3 -3
  25. data/lib/tb/cmd_to_pnm.rb +3 -3
  26. data/lib/tb/cmd_to_tsv.rb +3 -3
  27. data/lib/tb/cmd_to_yaml.rb +3 -3
  28. data/lib/tb/cmd_unmelt.rb +15 -0
  29. data/lib/tb/cmd_unnest.rb +31 -7
  30. data/lib/tb/cmdmain.rb +2 -0
  31. data/lib/tb/cmdtop.rb +1 -1
  32. data/lib/tb/cmdutil.rb +9 -62
  33. data/lib/tb/csv.rb +21 -79
  34. data/lib/tb/enumerable.rb +42 -68
  35. data/lib/tb/enumerator.rb +15 -7
  36. data/lib/tb/{fieldset.rb → hashreader.rb} +37 -56
  37. data/lib/tb/hashwriter.rb +54 -0
  38. data/lib/tb/headerreader.rb +108 -0
  39. data/lib/tb/headerwriter.rb +116 -0
  40. data/lib/tb/json.rb +17 -15
  41. data/lib/tb/ltsv.rb +35 -96
  42. data/lib/tb/ndjson.rb +63 -0
  43. data/lib/tb/numericreader.rb +66 -0
  44. data/lib/tb/numericwriter.rb +61 -0
  45. data/lib/tb/pnm.rb +206 -200
  46. data/lib/tb/ropen.rb +54 -59
  47. data/lib/tb/tsv.rb +39 -71
  48. data/sample/excel2csv +24 -25
  49. data/sample/poi-xls2csv.rb +13 -14
  50. data/tb.gemspec +154 -0
  51. data/test/test_cmd_cat.rb +28 -6
  52. data/test/test_cmd_consecutive.rb +8 -3
  53. data/test/test_cmd_cut.rb +14 -4
  54. data/test/test_cmd_git_log.rb +50 -50
  55. data/test/test_cmd_grep.rb +6 -6
  56. data/test/test_cmd_gsub.rb +7 -2
  57. data/test/test_cmd_ls.rb +70 -62
  58. data/test/test_cmd_shape.rb +43 -6
  59. data/test/test_cmd_svn_log.rb +26 -27
  60. data/test/test_cmd_to_csv.rb +10 -5
  61. data/test/test_cmd_to_json.rb +16 -0
  62. data/test/test_cmd_to_ltsv.rb +2 -2
  63. data/test/test_cmd_to_pp.rb +7 -2
  64. data/test/test_csv.rb +74 -62
  65. data/test/test_ex_enumerable.rb +0 -1
  66. data/test/test_fileenumerator.rb +3 -3
  67. data/test/test_headercsv.rb +43 -0
  68. data/test/test_json.rb +2 -2
  69. data/test/test_ltsv.rb +22 -17
  70. data/test/test_ndjson.rb +62 -0
  71. data/test/test_numericcsv.rb +36 -0
  72. data/test/test_pnm.rb +69 -70
  73. data/test/test_reader.rb +27 -124
  74. data/test/test_tbenum.rb +18 -18
  75. data/test/test_tsv.rb +21 -32
  76. data/test/util_tbtest.rb +12 -0
  77. metadata +41 -19
  78. data/lib/tb/basic.rb +0 -1070
  79. data/lib/tb/reader.rb +0 -106
  80. data/lib/tb/record.rb +0 -158
  81. data/test/test_basic.rb +0 -403
  82. data/test/test_fieldset.rb +0 -42
  83. data/test/test_record.rb +0 -61
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'tb/cmdtop'
3
3
  require 'tmpdir'
4
+ require_relative 'util_tbtest'
4
5
 
5
6
  class TestTbCmdCat < Test::Unit::TestCase
6
7
  def setup
@@ -69,14 +70,18 @@ class TestTbCmdCat < Test::Unit::TestCase
69
70
  5,6
70
71
  7,8,y
71
72
  End
72
- Tb::Cmd.main_cat(['-o', o="o.csv", i1, i2])
73
+ o = "o.csv"
74
+ stderr = capture_stderr {
75
+ Tb::Cmd.main_cat(['-o', o, i1, i2])
76
+ }
73
77
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
74
78
  a,b,c
75
79
  1,2
76
- 3,4,,x
80
+ 3,4
77
81
  ,5,6
78
- ,7,8,y
82
+ ,7,8
79
83
  End
84
+ assert_match(/Header too short/, stderr)
80
85
  end
81
86
 
82
87
  def test_extend_both
@@ -88,12 +93,16 @@ class TestTbCmdCat < Test::Unit::TestCase
88
93
  b
89
94
  2,y
90
95
  End
91
- Tb::Cmd.main_cat(['-o', o="o.csv", i1, i2])
96
+ o = "o.csv"
97
+ stderr = capture_stderr {
98
+ Tb::Cmd.main_cat(['-o', o, i1, i2])
99
+ }
92
100
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
93
101
  a,b
94
- 1,,x
95
- ,2,y
102
+ 1
103
+ ,2
96
104
  End
105
+ assert_match(/Header too short/, stderr)
97
106
  end
98
107
 
99
108
  def test_field_order
@@ -177,6 +186,19 @@ class TestTbCmdCat < Test::Unit::TestCase
177
186
  End
178
187
  end
179
188
 
189
+ def test_ltsv_output
190
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
191
+ a,b,c
192
+ 1,2,3
193
+ 4,5,6
194
+ End
195
+ Tb::Cmd.main_cat(['-o', o="o.ltsv", i1])
196
+ assert_equal(<<-"End".gsub(/^ */, '').gsub(/ /, "\t"), File.read(o))
197
+ a:1 b:2 c:3
198
+ a:4 b:5 c:6
199
+ End
200
+ end
201
+
180
202
  def test_invalid_output_format
181
203
  File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
182
204
  a,b,c
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'tb/cmdtop'
3
3
  require 'tmpdir'
4
+ require_relative 'util_tbtest'
4
5
 
5
6
  class TestTbCmdConsecutive < Test::Unit::TestCase
6
7
  def setup
@@ -52,12 +53,16 @@ class TestTbCmdConsecutive < Test::Unit::TestCase
52
53
  4,5,6
53
54
  7,8,9
54
55
  End
55
- Tb::Cmd.main_consecutive(['-o', o="o.csv", i])
56
+ o = "o.csv"
57
+ stderr = capture_stderr {
58
+ Tb::Cmd.main_consecutive(['-o', o, i])
59
+ }
56
60
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
57
61
  a_1,a_2,b_1,b_2
58
- 1,4,2,5,3,6
59
- 4,7,5,8,6,9
62
+ 1,4,2,5
63
+ 4,7,5,8
60
64
  End
65
+ assert_match(/Header too short/, stderr)
61
66
  end
62
67
 
63
68
  def test_n3
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'tb/cmdtop'
3
3
  require 'tmpdir'
4
+ require_relative 'util_tbtest'
4
5
 
5
6
  class TestTbCmdCut < Test::Unit::TestCase
6
7
  def setup
@@ -61,19 +62,28 @@ class TestTbCmdCut < Test::Unit::TestCase
61
62
  a,b
62
63
  0,1,2,3
63
64
  End
64
- Tb::Cmd.main_cut(['-o', o="o.csv", 'a,2,1', i])
65
+ o = "o.csv"
66
+ stderr = capture_stderr {
67
+ Tb::Cmd.main_cut(['-o', o, 'a,2,1', i])
68
+ }
65
69
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
66
70
  a,2,1
67
- 0,3,2
71
+ 0
68
72
  End
73
+ assert_match(/Header too short/, stderr)
69
74
  end
70
75
 
71
- def test_unextendable
76
+ def test_zero
72
77
  File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
73
78
  a,b
74
79
  0,1,2,3
75
80
  End
76
- assert_raise(ArgumentError) { Tb::Cmd.main_cut(['-o', "o.csv", '0', i]) }
81
+ o = "o.csv"
82
+ stderr = capture_stderr {
83
+ Tb::Cmd.main_cut(['-o', o, '0', i])
84
+ }
85
+ assert_equal("0\n\n", File.read(o))
86
+ assert_match(/Header too short/, stderr)
77
87
  end
78
88
 
79
89
  def test_twofile
@@ -31,10 +31,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
31
31
  system("git add foo")
32
32
  system("git commit -q -m msg foo")
33
33
  Tb::Cmd.main_git(['-o', o="o.csv"])
34
- result = File.read(o)
35
- tb = Tb.parse_csv(result)
36
- assert_equal(1, tb.size)
37
- assert_match(/,A,foo\n/, tb.get_record(0)["files"])
34
+ aa = CSV.read(o)
35
+ assert_equal(2, aa.length)
36
+ header, row = aa
37
+ assert_match(/,A,foo\n/, row[header.index "files"])
38
38
  end
39
39
 
40
40
  def test_escaped_filename
@@ -44,13 +44,14 @@ class TestTbCmdGitLog < Test::Unit::TestCase
44
44
  system("git", "add", filename)
45
45
  system("git", "commit", "-q", "-m", "msg", filename)
46
46
  Tb::Cmd.main_git(['-o', o="o.csv"])
47
- result = File.read(o)
48
- tb = Tb.parse_csv(result)
49
- assert_equal(1, tb.size)
50
- assert_match(/,A,/, tb.get_record(0)["files"])
51
- ftb = Tb.parse_csv(tb.get_record(0)["files"])
52
- assert_equal(1, ftb.size)
53
- assert_equal(filename, ftb.get_record(0)["filename"])
47
+ aa = CSV.read(o)
48
+ assert_equal(2, aa.length)
49
+ header, row = aa
50
+ assert_match(/,A,/, row[header.index "files"])
51
+ faa = CSV.parse(row[header.index "files"])
52
+ assert_equal(2, faa.length)
53
+ fheader, frow = faa
54
+ assert_equal(filename, frow[fheader.index "filename"])
54
55
  end
55
56
 
56
57
  def test_debug_git_output_input
@@ -59,18 +60,18 @@ class TestTbCmdGitLog < Test::Unit::TestCase
59
60
  system("git add foo")
60
61
  system("git commit -q -m msg foo")
61
62
  Tb::Cmd.main_git(['-o', o="o.csv", '--debug-git-output', g='gitlog'])
62
- result = File.read(o)
63
- tb = Tb.parse_csv(result)
64
- assert_equal(1, tb.size)
65
- assert_match(/,A,foo\n/, tb.get_record(0)["files"])
63
+ aa = CSV.read(o)
64
+ assert_equal(2, aa.length)
65
+ header, row = aa
66
+ assert_match(/,A,foo\n/, row[header.index "files"])
66
67
  gresult = File.read(g)
67
68
  assert(!gresult.empty?)
68
69
  FileUtils.rmtree('.git')
69
70
  Tb::Cmd.main_git(['-o', o="o.csv", '--debug-git-input', g])
70
- result = File.read(o)
71
- tb = Tb.parse_csv(result)
72
- assert_equal(1, tb.size)
73
- assert_match(/,A,foo\n/, tb.get_record(0)["files"])
71
+ aa = CSV.read(o)
72
+ assert_equal(2, aa.length)
73
+ header, row = aa
74
+ assert_match(/,A,foo\n/, row[header.index "files"])
74
75
  end
75
76
 
76
77
  def test_warn1
@@ -79,10 +80,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
79
80
  system("git add foo")
80
81
  system("git commit -q -m msg foo")
81
82
  Tb::Cmd.main_git(['-o', o="o.csv", '--debug-git-output', g='gitlog'])
82
- result = File.read(o)
83
- tb = Tb.parse_csv(result)
84
- assert_equal(1, tb.size)
85
- assert_match(/,A,foo\n/, tb.get_record(0)["files"])
83
+ aa = CSV.read(o)
84
+ assert_equal(2, aa.length)
85
+ header, row = aa
86
+ assert_match(/,A,foo\n/, row[header.index "files"])
86
87
  gresult = File.binread(g)
87
88
  FileUtils.rmtree('.git')
88
89
  ###
@@ -94,10 +95,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
94
95
  Tb::Cmd.main_git(['-o', o2, '--debug-git-input', g])
95
96
  }
96
97
  }
97
- result = File.read(o2)
98
- tb = Tb.parse_csv(result)
99
- assert_equal(1, tb.size)
100
- assert_not_match(/,A,foo\n/, tb.get_record(0)["files"])
98
+ aa = CSV.read(o2)
99
+ assert_equal(2, aa.length)
100
+ header, row = aa
101
+ assert_not_match(/,A,foo\n/, row[header.index "files"])
101
102
  log = File.read('log')
102
103
  assert(!log.empty?)
103
104
  end
@@ -108,10 +109,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
108
109
  system("git add foo")
109
110
  system("git commit -q -m msg foo")
110
111
  Tb::Cmd.main_git(['-o', o="o.csv", '--debug-git-output', g='gitlog'])
111
- result = File.read(o)
112
- tb = Tb.parse_csv(result)
113
- assert_equal(1, tb.size)
114
- assert_match(/,A,foo\n/, tb.get_record(0)["files"])
112
+ aa = CSV.read(o)
113
+ assert_equal(2, aa.length)
114
+ header, row = aa
115
+ assert_match(/,A,foo\n/, row[header.index "files"])
115
116
  gresult = File.binread(g)
116
117
  FileUtils.rmtree('.git')
117
118
  ###
@@ -123,10 +124,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
123
124
  Tb::Cmd.main_git(['-o', o2, '--debug-git-input', g])
124
125
  }
125
126
  }
126
- result = File.read(o2)
127
- tb = Tb.parse_csv(result)
128
- assert_equal(1, tb.size)
129
- assert_match(/,A,foo\n/, tb.get_record(0)["files"])
127
+ aa = CSV.read(o2)
128
+ assert_equal(2, aa.length)
129
+ header, row = aa
130
+ assert_match(/,A,foo\n/, row[header.index "files"])
130
131
  log = File.read('log')
131
132
  assert(!log.empty?)
132
133
  end
@@ -137,10 +138,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
137
138
  system("git add foo")
138
139
  system("git commit -q -m msg foo")
139
140
  Tb::Cmd.main_git(['-o', o="o.csv", '--debug-git-output', g='gitlog'])
140
- result = File.read(o)
141
- tb = Tb.parse_csv(result)
142
- assert_equal(1, tb.size)
143
- assert_match(/,A,foo\n/, tb.get_record(0)["files"])
141
+ aa = CSV.read(o)
142
+ assert_equal(2, aa.length)
143
+ header, row = aa
144
+ assert_match(/,A,foo\n/, row[header.index "files"])
144
145
  gresult = File.binread(g)
145
146
  FileUtils.rmtree('.git')
146
147
  ###
@@ -152,9 +153,8 @@ class TestTbCmdGitLog < Test::Unit::TestCase
152
153
  Tb::Cmd.main_git(['-o', o2, '--debug-git-input', g])
153
154
  }
154
155
  }
155
- result = File.read(o2)
156
- tb = Tb.parse_csv(result)
157
- assert_equal(0, tb.size)
156
+ aa = CSV.read(o2)
157
+ assert_equal(1, aa.length)
158
158
  log = File.read('log')
159
159
  assert(!log.empty?)
160
160
  end
@@ -165,10 +165,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
165
165
  system("git add foo")
166
166
  system("git commit -q -m msg foo")
167
167
  Tb::Cmd.main_git(['-o', o="o.csv"])
168
- result = File.read(o)
169
- tb = Tb.parse_csv(result)
170
- assert_equal(1, tb.size)
171
- assert_match(/,,,A,foo\n/, tb.get_record(0)["files"])
168
+ aa = CSV.read(o)
169
+ assert_equal(2, aa.length)
170
+ header, row = aa
171
+ assert_match(/,,,A,foo\n/, row[header.index "files"])
172
172
  end
173
173
 
174
174
  def test_subdir
@@ -181,10 +181,10 @@ class TestTbCmdGitLog < Test::Unit::TestCase
181
181
  system("git add bar")
182
182
  system("git commit -q -m msg bar")
183
183
  Tb::Cmd.main_git(['-o', o="o.csv", "bar"])
184
- result = File.read(o)
185
- tb = Tb.parse_csv(result)
186
- assert_equal(1, tb.size)
187
- assert_not_match(/foo\n/, tb.get_record(0)["files"])
184
+ aa = CSV.read(o)
185
+ assert_equal(2, aa.length)
186
+ header, row = aa
187
+ assert_not_match(/foo\n/, row[header.index "files"])
188
188
  end
189
189
 
190
190
  end
@@ -2,7 +2,7 @@ require 'test/unit'
2
2
  require 'tb/cmdtop'
3
3
  require 'tmpdir'
4
4
 
5
- class TestTbCmdGrep < Test::Unit::TestCase
5
+ class TestTbCmdSearch < Test::Unit::TestCase
6
6
  def setup
7
7
  Tb::Cmd.reset_option
8
8
  @curdir = Dir.pwd
@@ -23,7 +23,7 @@ class TestTbCmdGrep < Test::Unit::TestCase
23
23
  8,9,a,b
24
24
  c,d,e,f
25
25
  End
26
- Tb::Cmd.main_grep(['-o', o="o.csv", '[6f]', i])
26
+ Tb::Cmd.main_search(['-o', o="o.csv", '[6f]', i])
27
27
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
28
28
  a,b,c,d
29
29
  4,5,6,7
@@ -32,7 +32,7 @@ class TestTbCmdGrep < Test::Unit::TestCase
32
32
  end
33
33
 
34
34
  def test_no_regexp
35
- exc = assert_raise(SystemExit) { Tb::Cmd.main_grep([]) }
35
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_search([]) }
36
36
  assert(!exc.success?)
37
37
  end
38
38
 
@@ -44,7 +44,7 @@ class TestTbCmdGrep < Test::Unit::TestCase
44
44
  8,9,a,b
45
45
  c,d,e,f
46
46
  End
47
- Tb::Cmd.main_grep(['-o', o="o.csv", '-e', '[6f]', i])
47
+ Tb::Cmd.main_search(['-o', o="o.csv", '-e', '[6f]', i])
48
48
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
49
49
  a,b,c,d
50
50
  4,5,6,7
@@ -60,7 +60,7 @@ class TestTbCmdGrep < Test::Unit::TestCase
60
60
  8,9,a,b
61
61
  c,d,e,f
62
62
  End
63
- Tb::Cmd.main_grep(['-o', o="o.csv", '--ruby', '_["b"] == "5"', i])
63
+ Tb::Cmd.main_search(['-o', o="o.csv", '--ruby', '_["b"] == "5"', i])
64
64
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
65
65
  a,b,c,d
66
66
  4,5,6,7
@@ -78,7 +78,7 @@ class TestTbCmdGrep < Test::Unit::TestCase
78
78
  5,6
79
79
  7,8
80
80
  End
81
- Tb::Cmd.main_grep(['-o', o="o.csv", '[46]', i1, i2])
81
+ Tb::Cmd.main_search(['-o', o="o.csv", '[46]', i1, i2])
82
82
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
83
83
  a,b
84
84
  3,4
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'tb/cmdtop'
3
3
  require 'tmpdir'
4
+ require_relative 'util_tbtest'
4
5
 
5
6
  class TestTbCmdGsub < Test::Unit::TestCase
6
7
  def setup
@@ -72,11 +73,15 @@ class TestTbCmdGsub < Test::Unit::TestCase
72
73
  a,b
73
74
  foo,bar,baz
74
75
  End
75
- Tb::Cmd.main_gsub(['-o', o="o.csv", '-f', '1', 'baz', 'Y', i])
76
+ o = "o.csv"
77
+ stderr = capture_stderr {
78
+ Tb::Cmd.main_gsub(['-o', o, '-f', '1', 'baz', 'Y', i])
79
+ }
76
80
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
77
81
  a,b
78
- foo,bar,Y
82
+ foo,bar
79
83
  End
84
+ assert_match(/Header too short/, stderr)
80
85
  end
81
86
 
82
87
  def test_twofile
@@ -72,16 +72,17 @@ class TestTbCmdLs < Test::Unit::TestCase
72
72
  File.open("a", "w") {}
73
73
  File.chmod(0754, "a")
74
74
  Tb::Cmd.main_ls(['-o', o="o.csv", '-l', 'a'])
75
- tb = Tb.load_csv(o)
76
- assert_equal(1, tb.size)
77
- assert_equal(%w[filemode nlink user group size mtime filename symlink], tb.list_fields)
78
- rec = tb.get_record(0)
79
- assert_equal("-rwxr-xr--", rec["filemode"])
80
- assert_equal("1", rec["nlink"])
81
- assert_match(/\A\d+\z/, rec["size"])
82
- assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["mtime"])
83
- assert_equal("a", rec["filename"])
84
- assert_equal(nil, rec["symlink"])
75
+ aa = CSV.read(o)
76
+ assert_equal(2, aa.length)
77
+ header, row = aa
78
+ assert_equal(%w[filemode nlink user group size mtime filename symlink], header)
79
+ assert_equal(8, row.length)
80
+ assert_equal("-rwxr-xr--", row[header.index "filemode"])
81
+ assert_equal("1", row[header.index "nlink"])
82
+ assert_match(/\A\d+\z/, row[header.index "size"])
83
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, row[header.index "mtime"])
84
+ assert_equal("a", row[header.index "filename"])
85
+ assert_equal(nil, row[header.index "symlink"])
85
86
  end
86
87
 
87
88
  def test_opt_l
@@ -89,56 +90,60 @@ class TestTbCmdLs < Test::Unit::TestCase
89
90
  File.open("d/a", "w") {}
90
91
  Dir.mkdir("d/d2")
91
92
  Tb::Cmd.main_ls(['-o', o="o.csv", '-l', 'd'])
92
- tb = Tb.load_csv(o)
93
- assert_equal(2, tb.size)
94
- assert_equal("d/a", tb.get_record(0)["filename"])
95
- assert_equal("d/d2", tb.get_record(1)["filename"])
93
+ aa = CSV.read(o)
94
+ assert_equal(3, aa.length)
95
+ header, row1, row2 = aa
96
+ i = header.index("filename")
97
+ assert_equal("d/a", row1[i])
98
+ assert_equal("d/d2", row2[i])
96
99
  end
97
100
 
98
101
  def test_opt_ll_single
99
102
  File.open("a", "w") {}
100
103
  File.chmod(0754, "a")
101
104
  Tb::Cmd.main_ls(['-o', o="o.csv", '-ll', 'a'])
102
- tb = Tb.load_csv(o)
103
- assert_equal(1, tb.size)
104
- assert_equal(%w[dev ino mode filemode nlink uid user gid group rdev size blksize blocks atime mtime ctime filename symlink], tb.list_fields)
105
- rec = tb.get_record(0)
106
- assert_match(/\A0x[0-9a-f]+\z/, rec["dev"])
107
- assert_match(/\A\d+\z/, rec["ino"])
108
- assert_match(/\A0[0-7]+\z/, rec["mode"])
109
- assert_equal("-rwxr-xr--", rec["filemode"])
110
- assert_equal("1", rec["nlink"])
111
- assert_match(/\A\d+\z/, rec["uid"])
112
- assert_match(/\A\d+\z/, rec["gid"])
113
- assert_match(/\A0x[0-9a-f]+\z/, rec["rdev"])
114
- assert_match(/\A\d+\z/, rec["size"])
115
- assert_match(/\A\d+\z/, rec["blksize"])
116
- assert_match(/\A\d+\z/, rec["blocks"])
117
- assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["atime"])
118
- assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["mtime"])
119
- assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["ctime"])
120
- assert_equal("a", rec["filename"])
121
- assert_equal(nil, rec["symlink"])
105
+ aa = CSV.read(o)
106
+ assert_equal(2, aa.length)
107
+ header, row = aa
108
+ assert_equal(%w[dev ino mode filemode nlink uid user gid group rdev size blksize blocks atime mtime ctime filename symlink], header)
109
+ assert_match(/\A0x[0-9a-f]+\z/, row[header.index "dev"])
110
+ assert_match(/\A\d+\z/, row[header.index "ino"])
111
+ assert_match(/\A0[0-7]+\z/, row[header.index "mode"])
112
+ assert_equal("-rwxr-xr--", row[header.index "filemode"])
113
+ assert_equal("1", row[header.index "nlink"])
114
+ assert_match(/\A\d+\z/, row[header.index "uid"])
115
+ assert_match(/\A\d+\z/, row[header.index "gid"])
116
+ assert_match(/\A0x[0-9a-f]+\z/, row[header.index "rdev"])
117
+ assert_match(/\A\d+\z/, row[header.index "size"])
118
+ assert_match(/\A\d+\z/, row[header.index "blksize"])
119
+ assert_match(/\A\d+\z/, row[header.index "blocks"])
120
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, row[header.index "atime"])
121
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, row[header.index "mtime"])
122
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, row[header.index "ctime"])
123
+ assert_equal("a", row[header.index "filename"])
124
+ assert_equal(nil, row[header.index "symlink"])
122
125
  end
123
126
 
124
127
  def test_opt_a
125
128
  Dir.mkdir("d")
126
129
  File.open("d/.foo", "w") {}
127
130
  Tb::Cmd.main_ls(['-o', o="o.csv", '-a', 'd'])
128
- tb = Tb.load_csv(o)
129
- assert_equal(3, tb.size)
130
- assert_equal("d", tb.get_record(0)["filename"]) # d/.
131
- assert_equal(".", tb.get_record(1)["filename"]) # d/..
132
- assert_equal("d/.foo", tb.get_record(2)["filename"])
131
+ aa = CSV.read(o)
132
+ assert_equal(4, aa.length)
133
+ header, row1, row2, row3 = aa
134
+ assert_equal("d", row1[header.index "filename"]) # d/.
135
+ assert_equal(".", row2[header.index "filename"]) # d/..
136
+ assert_equal("d/.foo", row3[header.index "filename"])
133
137
  end
134
138
 
135
139
  def test_opt_A
136
140
  Dir.mkdir("d")
137
141
  File.open("d/.foo", "w") {}
138
142
  Tb::Cmd.main_ls(['-o', o="o.csv", '-A', 'd'])
139
- tb = Tb.load_csv(o)
140
- assert_equal(1, tb.size)
141
- assert_equal("d/.foo", tb.get_record(0)["filename"])
143
+ aa = CSV.read(o)
144
+ assert_equal(2, aa.length)
145
+ header, row = aa
146
+ assert_equal("d/.foo", row[header.index "filename"])
142
147
  end
143
148
 
144
149
  def test_opt_R
@@ -148,12 +153,13 @@ class TestTbCmdLs < Test::Unit::TestCase
148
153
  File.open("d/d2/b", "w") {}
149
154
  File.open("d/d2/c", "w") {}
150
155
  Tb::Cmd.main_ls(['-o', o="o.csv", '-R', 'd'])
151
- tb = Tb.load_csv(o)
152
- assert_equal(4, tb.size)
153
- assert_equal("d/a", tb.get_record(0)["filename"])
154
- assert_equal("d/d2", tb.get_record(1)["filename"])
155
- assert_equal("d/d2/b", tb.get_record(2)["filename"])
156
- assert_equal("d/d2/c", tb.get_record(3)["filename"])
156
+ aa = CSV.read(o)
157
+ assert_equal(5, aa.length)
158
+ header, row1, row2, row3, row4 = aa
159
+ assert_equal("d/a", row1[header.index "filename"])
160
+ assert_equal("d/d2", row2[header.index "filename"])
161
+ assert_equal("d/d2/b", row3[header.index "filename"])
162
+ assert_equal("d/d2/c", row4[header.index "filename"])
157
163
  end
158
164
 
159
165
  def test_opt_Ra
@@ -163,25 +169,27 @@ class TestTbCmdLs < Test::Unit::TestCase
163
169
  File.open("d/d2/b", "w") {}
164
170
  File.open("d/d2/c", "w") {}
165
171
  Tb::Cmd.main_ls(['-o', o="o.csv", '-Ra', 'd'])
166
- tb = Tb.load_csv(o)
167
- assert_equal(8, tb.size)
168
- assert_equal("d/.", tb.get_record(0)["filename"])
169
- assert_equal("d/..", tb.get_record(1)["filename"])
170
- assert_equal("d/a", tb.get_record(2)["filename"])
171
- assert_equal("d/d2", tb.get_record(3)["filename"])
172
- assert_equal("d/d2/.", tb.get_record(4)["filename"])
173
- assert_equal("d/d2/..", tb.get_record(5)["filename"])
174
- assert_equal("d/d2/b", tb.get_record(6)["filename"])
175
- assert_equal("d/d2/c", tb.get_record(7)["filename"])
172
+ aa = CSV.read(o)
173
+ assert_equal(9, aa.length)
174
+ header, *rows = aa
175
+ assert_equal("d/.", rows[0][header.index "filename"])
176
+ assert_equal("d/..", rows[1][header.index "filename"])
177
+ assert_equal("d/a", rows[2][header.index "filename"])
178
+ assert_equal("d/d2", rows[3][header.index "filename"])
179
+ assert_equal("d/d2/.", rows[4][header.index "filename"])
180
+ assert_equal("d/d2/..", rows[5][header.index "filename"])
181
+ assert_equal("d/d2/b", rows[6][header.index "filename"])
182
+ assert_equal("d/d2/c", rows[7][header.index "filename"])
176
183
  end
177
184
 
178
185
  def test_symlink
179
186
  File.symlink("a", "b")
180
187
  Tb::Cmd.main_ls(['-o', o="o.csv", '-l', 'b'])
181
- tb = Tb.load_csv(o)
182
- assert_equal(1, tb.size)
183
- assert_equal("b", tb.get_record(0)["filename"])
184
- assert_equal("a", tb.get_record(0)["symlink"])
188
+ aa = CSV.read(o)
189
+ assert_equal(2, aa.length)
190
+ header, row = aa
191
+ assert_equal("b", row[header.index "filename"])
192
+ assert_equal("a", row[header.index "symlink"])
185
193
  end
186
194
 
187
195
  def test_not_found