tb 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/README +156 -5
  2. data/bin/tb +2 -1110
  3. data/lib/tb.rb +4 -2
  4. data/lib/tb/catreader.rb +131 -0
  5. data/lib/tb/cmd_cat.rb +65 -0
  6. data/lib/tb/cmd_consecutive.rb +79 -0
  7. data/lib/tb/cmd_crop.rb +105 -0
  8. data/lib/tb/cmd_cross.rb +119 -0
  9. data/lib/tb/cmd_csv.rb +42 -0
  10. data/lib/tb/cmd_cut.rb +77 -0
  11. data/lib/tb/cmd_grep.rb +76 -0
  12. data/lib/tb/cmd_group.rb +82 -0
  13. data/lib/tb/cmd_gsub.rb +77 -0
  14. data/lib/tb/cmd_help.rb +98 -0
  15. data/lib/tb/cmd_join.rb +81 -0
  16. data/lib/tb/cmd_json.rb +60 -0
  17. data/lib/tb/cmd_ls.rb +273 -0
  18. data/lib/tb/cmd_mheader.rb +77 -0
  19. data/lib/tb/cmd_newfield.rb +59 -0
  20. data/lib/tb/cmd_pnm.rb +43 -0
  21. data/lib/tb/cmd_pp.rb +70 -0
  22. data/lib/tb/cmd_rename.rb +58 -0
  23. data/lib/tb/cmd_shape.rb +67 -0
  24. data/lib/tb/cmd_sort.rb +58 -0
  25. data/lib/tb/cmd_svn_log.rb +158 -0
  26. data/lib/tb/cmd_tsv.rb +43 -0
  27. data/lib/tb/cmd_yaml.rb +47 -0
  28. data/lib/tb/cmdmain.rb +45 -0
  29. data/lib/tb/cmdtop.rb +58 -0
  30. data/lib/tb/cmdutil.rb +327 -0
  31. data/lib/tb/csv.rb +30 -6
  32. data/lib/tb/fieldset.rb +39 -41
  33. data/lib/tb/pager.rb +132 -0
  34. data/lib/tb/pnm.rb +357 -0
  35. data/lib/tb/reader.rb +18 -128
  36. data/lib/tb/record.rb +3 -3
  37. data/lib/tb/ropen.rb +70 -0
  38. data/lib/tb/{pathfinder.rb → search.rb} +69 -34
  39. data/lib/tb/tsv.rb +29 -1
  40. data/sample/colors.ppm +0 -0
  41. data/sample/gradation.pgm +0 -0
  42. data/sample/langs.csv +46 -0
  43. data/sample/tbplot +293 -0
  44. data/test-all-cov.rb +65 -0
  45. data/test-all.rb +5 -0
  46. data/test/test_basic.rb +99 -2
  47. data/test/test_catreader.rb +27 -0
  48. data/test/test_cmd_cat.rb +118 -0
  49. data/test/test_cmd_consecutive.rb +90 -0
  50. data/test/test_cmd_crop.rb +101 -0
  51. data/test/test_cmd_cross.rb +113 -0
  52. data/test/test_cmd_csv.rb +129 -0
  53. data/test/test_cmd_cut.rb +100 -0
  54. data/test/test_cmd_grep.rb +89 -0
  55. data/test/test_cmd_group.rb +181 -0
  56. data/test/test_cmd_gsub.rb +103 -0
  57. data/test/test_cmd_help.rb +190 -0
  58. data/test/test_cmd_join.rb +197 -0
  59. data/test/test_cmd_json.rb +75 -0
  60. data/test/test_cmd_ls.rb +203 -0
  61. data/test/test_cmd_mheader.rb +86 -0
  62. data/test/test_cmd_newfield.rb +63 -0
  63. data/test/test_cmd_pnm.rb +35 -0
  64. data/test/test_cmd_pp.rb +62 -0
  65. data/test/test_cmd_rename.rb +91 -0
  66. data/test/test_cmd_shape.rb +50 -0
  67. data/test/test_cmd_sort.rb +105 -0
  68. data/test/test_cmd_tsv.rb +67 -0
  69. data/test/test_cmd_yaml.rb +55 -0
  70. data/test/test_cmdtty.rb +154 -0
  71. data/test/test_cmdutil.rb +43 -0
  72. data/test/test_csv.rb +10 -0
  73. data/test/test_fieldset.rb +42 -0
  74. data/test/test_pager.rb +142 -0
  75. data/test/test_pnm.rb +374 -0
  76. data/test/test_reader.rb +147 -0
  77. data/test/test_record.rb +49 -0
  78. data/test/test_search.rb +575 -0
  79. data/test/test_tsv.rb +7 -0
  80. metadata +108 -5
  81. data/lib/tb/qtsv.rb +0 -93
@@ -0,0 +1,103 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdGsub < Test::Unit::TestCase
6
+ def setup
7
+ Tb::Cmd.reset_option
8
+ @curdir = Dir.pwd
9
+ @tmpdir = Dir.mktmpdir
10
+ Dir.chdir @tmpdir
11
+ end
12
+ def teardown
13
+ Tb::Cmd.reset_option
14
+ Dir.chdir @curdir
15
+ FileUtils.rmtree @tmpdir
16
+ end
17
+
18
+ def test_basic
19
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
20
+ a,b,c
21
+ foo,bar,baz
22
+ qux,quuux
23
+ End
24
+ Tb::Cmd.main_gsub(['-o', o="o.csv", '[au]', 'YY', i])
25
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
26
+ a,b,c
27
+ foo,bYYr,bYYz
28
+ qYYx,qYYYYYYx
29
+ End
30
+ end
31
+
32
+ def test_no_regexp
33
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_gsub([]) }
34
+ assert(!exc.success?)
35
+ end
36
+
37
+ def test_no_subst
38
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_gsub(['foo']) }
39
+ assert(!exc.success?)
40
+ end
41
+
42
+ def test_opt_e
43
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
44
+ a,b,c
45
+ foo,bar,baz
46
+ qux,quuux
47
+ End
48
+ Tb::Cmd.main_gsub(['-o', o="o.csv", '-e', '[au]', 'YY', i])
49
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
50
+ a,b,c
51
+ foo,bYYr,bYYz
52
+ qYYx,qYYYYYYx
53
+ End
54
+ end
55
+
56
+ def test_opt_f
57
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
58
+ a,b,c
59
+ foo,bar,baz
60
+ qux,quuux
61
+ End
62
+ Tb::Cmd.main_gsub(['-o', o="o.csv", '-f', 'b', '[au]', 'YY', i])
63
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
64
+ a,b,c
65
+ foo,bYYr,baz
66
+ qux,qYYYYYYx
67
+ End
68
+ end
69
+
70
+ def test_opt_f_extend
71
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
72
+ a,b
73
+ foo,bar,baz
74
+ End
75
+ Tb::Cmd.main_gsub(['-o', o="o.csv", '-f', '1', 'baz', 'Y', i])
76
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
77
+ a,b
78
+ foo,bar,Y
79
+ End
80
+ end
81
+
82
+ def test_twofile
83
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
84
+ a,b
85
+ 1,2
86
+ 3,4
87
+ End
88
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
89
+ b,a
90
+ 5,6
91
+ 7,8
92
+ End
93
+ Tb::Cmd.main_gsub(['-o', o="o.csv", '[46]', 'z', i1, i2])
94
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
95
+ a,b
96
+ 1,2
97
+ 3,z
98
+ z,5
99
+ 8,7
100
+ End
101
+ end
102
+
103
+ end
@@ -0,0 +1,190 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdHelp < Test::Unit::TestCase
6
+ def setup
7
+ Tb::Cmd.reset_option
8
+ @curdir = Dir.pwd
9
+ @tmpdir = Dir.mktmpdir
10
+ Dir.chdir @tmpdir
11
+ end
12
+ def teardown
13
+ Tb::Cmd.reset_option
14
+ Dir.chdir @curdir
15
+ FileUtils.rmtree @tmpdir
16
+ end
17
+
18
+ def with_stdout(io)
19
+ save = STDOUT.dup
20
+ STDOUT.reopen(io)
21
+ begin
22
+ yield
23
+ ensure
24
+ STDOUT.reopen(save)
25
+ save.close
26
+ end
27
+ end
28
+
29
+ def with_stderr(io)
30
+ save = STDERR.dup
31
+ STDERR.reopen(io)
32
+ begin
33
+ yield
34
+ ensure
35
+ STDERR.reopen(save)
36
+ save.close
37
+ end
38
+ end
39
+
40
+ def with_pipe
41
+ r, w = IO.pipe
42
+ begin
43
+ yield r, w
44
+ ensure
45
+ r.close if !r.closed?
46
+ w.close if !w.closed?
47
+ end
48
+ end
49
+
50
+ def reader_thread(io)
51
+ Thread.new {
52
+ r = ''
53
+ loop {
54
+ begin
55
+ r << io.readpartial(4096)
56
+ rescue EOFError, Errno::EIO
57
+ break
58
+ end
59
+ }
60
+ r
61
+ }
62
+ end
63
+
64
+ def assert_exit_success(comanndline_words)
65
+ exc = assert_raise(SystemExit) { Tb::Cmd.main(comanndline_words) }
66
+ assert(exc.success?)
67
+ end
68
+
69
+ def assert_exit_fail(comanndline_words)
70
+ exc = assert_raise(SystemExit) { Tb::Cmd.main(comanndline_words) }
71
+ assert(!exc.success?)
72
+ end
73
+
74
+ def test_noarg
75
+ with_pipe {|r, w|
76
+ th = reader_thread(r)
77
+ with_stdout(w) {
78
+ Tb::Cmd.main([])
79
+ }
80
+ w.close
81
+ msg = th.value
82
+ r.close
83
+ assert_match(/Usage:/, msg)
84
+ assert_match(/ tb csv /, msg)
85
+ assert_match(/ tb cut /, msg)
86
+ }
87
+ end
88
+
89
+ def test_h
90
+ assert_equal(true, Tb::Cmd.main(['-h', '-o', o="msg"]))
91
+ msg = File.read(o)
92
+ assert_match(/Usage:/, msg)
93
+ assert_match(/ tb csv /, msg)
94
+ assert_match(/ tb cut /, msg)
95
+ end
96
+
97
+ def test_ohelp
98
+ assert_equal(true, Tb::Cmd.main(['--help', '-o', o="msg"]))
99
+ msg = File.read(o)
100
+ assert_match(/Usage:/, msg)
101
+ assert_match(/ tb csv /, msg)
102
+ assert_match(/ tb cut /, msg)
103
+ end
104
+
105
+ def test_help
106
+ assert_equal(true, Tb::Cmd.main(['help', '-o', o="msg"]))
107
+ msg = File.read(o)
108
+ assert_match(/Usage:/, msg)
109
+ assert_match(/ tb csv /, msg)
110
+ assert_match(/ tb cut /, msg)
111
+ end
112
+
113
+ def test_help_h
114
+ assert_exit_success(['help', '-o', o="msg", '-h'])
115
+ msg = File.read(o)
116
+ assert_match(/Usage: tb help/, msg)
117
+ assert_no_match(/Example:/, msg)
118
+ end
119
+
120
+ def test_help_hh
121
+ assert_exit_success(['help', '-o', o="msg", '-hh'])
122
+ msg = File.read(o)
123
+ assert_match(/Usage: tb help/, msg)
124
+ assert_match(/Example:/, msg)
125
+ end
126
+
127
+ def test_help_help
128
+ assert_exit_success(['help', '-o', o="msg", 'help'])
129
+ msg = File.read(o)
130
+ assert_match(/Usage: tb help/, msg)
131
+ assert_no_match(/Example:/, msg)
132
+ end
133
+
134
+ def test_help_h_help
135
+ assert_exit_success(['help', '-o', o="msg", '-h', 'help'])
136
+ msg = File.read(o)
137
+ assert_match(/Usage: tb help/, msg)
138
+ assert_match(/Example:/, msg)
139
+ end
140
+
141
+ def test_cat_h
142
+ assert_exit_success(['cat', '-o', o="msg", '-h'])
143
+ msg = File.read(o)
144
+ assert_match(/tb cat /, msg)
145
+ assert_no_match(/Example:/, msg)
146
+ end
147
+
148
+ def test_cat_hh
149
+ assert_exit_success(['cat', '-o', o="msg", '-hh'])
150
+ msg = File.read(o)
151
+ assert_match(/tb cat /, msg)
152
+ assert_match(/Example:/, msg)
153
+ end
154
+
155
+ def test_help_cat
156
+ assert_exit_success(['help', '-o', o="msg", 'cat'])
157
+ msg = File.read(o)
158
+ assert_match(/tb cat /, msg)
159
+ assert_no_match(/Example:/, msg)
160
+ end
161
+
162
+ def test_help_h_cat
163
+ assert_exit_success(['help', '-o', o="msg", '-h', 'cat'])
164
+ msg = File.read(o)
165
+ assert_match(/tb cat /, msg)
166
+ assert_match(/Example:/, msg)
167
+ end
168
+
169
+ def test_help_unexpected_subcommand1
170
+ File.open("log", "w") {|log|
171
+ with_stderr(log) {
172
+ assert_exit_fail(['help', '-o', "msg", 'foo'])
173
+ }
174
+ }
175
+ assert_match(/unexpected subcommand/, File.read("log"))
176
+ end
177
+
178
+ def test_help_unexpected_subcommand2
179
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_help(['foo']) }
180
+ assert(!exc.success?)
181
+ assert_match(/unexpected subcommand/, exc.message)
182
+ end
183
+
184
+ def test_top_unexpected_subcommand
185
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_body(['foo']) }
186
+ assert(!exc.success?)
187
+ assert_match(/unexpected subcommand/, exc.message)
188
+ end
189
+
190
+ end
@@ -0,0 +1,197 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdJoin < Test::Unit::TestCase
6
+ def setup
7
+ Tb::Cmd.reset_option
8
+ @curdir = Dir.pwd
9
+ @tmpdir = Dir.mktmpdir
10
+ Dir.chdir @tmpdir
11
+ end
12
+ def teardown
13
+ Tb::Cmd.reset_option
14
+ Dir.chdir @curdir
15
+ FileUtils.rmtree @tmpdir
16
+ end
17
+
18
+ def test_basic
19
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
20
+ a,b
21
+ 1,2
22
+ 3,4
23
+ End
24
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
25
+ a,c
26
+ 1,a
27
+ 3,b
28
+ End
29
+ Tb::Cmd.main_join(['-o', o="o.csv", i1, i2])
30
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
31
+ a,b,c
32
+ 1,2,a
33
+ 3,4,b
34
+ End
35
+ end
36
+
37
+ def test_noarg
38
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_join(['-o', "o.csv"]) }
39
+ assert(!exc.success?)
40
+ end
41
+
42
+ def test_onearg
43
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_join(['-o', "o.csv", 'foo.csv']) }
44
+ assert(!exc.success?)
45
+ end
46
+
47
+ def test_outer
48
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
49
+ a,b
50
+ 1,2
51
+ 3,4
52
+ End
53
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
54
+ b,c
55
+ 4,5
56
+ 6,7
57
+ End
58
+ Tb::Cmd.main_join(['-o', o="o.csv", '--outer', i1, i2])
59
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
60
+ a,b,c
61
+ 1,2,
62
+ 3,4,5
63
+ ,6,7
64
+ End
65
+ end
66
+
67
+ def test_outer_missing
68
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
69
+ a,b
70
+ 1,2
71
+ 3,4
72
+ End
73
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
74
+ b,c
75
+ 4,5
76
+ 6,7
77
+ End
78
+ Tb::Cmd.main_join(['-o', o="o.csv", '--outer-missing=z', i1, i2])
79
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
80
+ a,b,c
81
+ 1,2,z
82
+ 3,4,5
83
+ z,6,7
84
+ End
85
+ end
86
+
87
+ def test_outer_left
88
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
89
+ a,b
90
+ 1,2
91
+ 3,4
92
+ End
93
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
94
+ b,c
95
+ 4,5
96
+ 6,7
97
+ End
98
+ Tb::Cmd.main_join(['-o', o="o.csv", '--left', i1, i2])
99
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
100
+ a,b,c
101
+ 1,2,
102
+ 3,4,5
103
+ End
104
+ end
105
+
106
+ def test_outer_right
107
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
108
+ a,b
109
+ 1,2
110
+ 3,4
111
+ End
112
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
113
+ b,c
114
+ 4,5
115
+ 6,7
116
+ End
117
+ Tb::Cmd.main_join(['-o', o="o.csv", '--right', i1, i2])
118
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
119
+ a,b,c
120
+ 3,4,5
121
+ ,6,7
122
+ End
123
+ end
124
+
125
+ def test_3file_inner
126
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
127
+ a,b
128
+ 1,2
129
+ 3,4
130
+ End
131
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
132
+ a,c
133
+ 1,a
134
+ 3,b
135
+ End
136
+ File.open(i3="i3.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
137
+ a,d
138
+ 1,x
139
+ 3,z
140
+ End
141
+ Tb::Cmd.main_join(['-o', o="o.csv", i1, i2, i3])
142
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
143
+ a,b,c,d
144
+ 1,2,a,x
145
+ 3,4,b,z
146
+ End
147
+ end
148
+
149
+ def test_3file_left
150
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
151
+ a,b
152
+ 1,A
153
+ 2,B
154
+ End
155
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
156
+ a,c
157
+ 1,C
158
+ 3,D
159
+ End
160
+ File.open(i3="i3.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
161
+ a,d
162
+ 2,E
163
+ 4,F
164
+ End
165
+ Tb::Cmd.main_join(['-o', o="o.csv", '--left', i1, i2, i3])
166
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
167
+ a,b,c,d
168
+ 1,A,C,
169
+ 2,B,,E
170
+ End
171
+ end
172
+
173
+ def test_3file_right
174
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
175
+ a,b
176
+ 1,A
177
+ 2,B
178
+ End
179
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
180
+ a,c
181
+ 1,C
182
+ 3,D
183
+ End
184
+ File.open(i3="i3.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
185
+ a,d
186
+ 2,E
187
+ 3,F
188
+ End
189
+ Tb::Cmd.main_join(['-o', o="o.csv", '--right', i1, i2, i3])
190
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
191
+ a,b,c,d
192
+ 3,,D,F
193
+ 2,,,E
194
+ End
195
+ end
196
+
197
+ end