tb 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +156 -5
- data/bin/tb +2 -1110
- data/lib/tb.rb +4 -2
- data/lib/tb/catreader.rb +131 -0
- data/lib/tb/cmd_cat.rb +65 -0
- data/lib/tb/cmd_consecutive.rb +79 -0
- data/lib/tb/cmd_crop.rb +105 -0
- data/lib/tb/cmd_cross.rb +119 -0
- data/lib/tb/cmd_csv.rb +42 -0
- data/lib/tb/cmd_cut.rb +77 -0
- data/lib/tb/cmd_grep.rb +76 -0
- data/lib/tb/cmd_group.rb +82 -0
- data/lib/tb/cmd_gsub.rb +77 -0
- data/lib/tb/cmd_help.rb +98 -0
- data/lib/tb/cmd_join.rb +81 -0
- data/lib/tb/cmd_json.rb +60 -0
- data/lib/tb/cmd_ls.rb +273 -0
- data/lib/tb/cmd_mheader.rb +77 -0
- data/lib/tb/cmd_newfield.rb +59 -0
- data/lib/tb/cmd_pnm.rb +43 -0
- data/lib/tb/cmd_pp.rb +70 -0
- data/lib/tb/cmd_rename.rb +58 -0
- data/lib/tb/cmd_shape.rb +67 -0
- data/lib/tb/cmd_sort.rb +58 -0
- data/lib/tb/cmd_svn_log.rb +158 -0
- data/lib/tb/cmd_tsv.rb +43 -0
- data/lib/tb/cmd_yaml.rb +47 -0
- data/lib/tb/cmdmain.rb +45 -0
- data/lib/tb/cmdtop.rb +58 -0
- data/lib/tb/cmdutil.rb +327 -0
- data/lib/tb/csv.rb +30 -6
- data/lib/tb/fieldset.rb +39 -41
- data/lib/tb/pager.rb +132 -0
- data/lib/tb/pnm.rb +357 -0
- data/lib/tb/reader.rb +18 -128
- data/lib/tb/record.rb +3 -3
- data/lib/tb/ropen.rb +70 -0
- data/lib/tb/{pathfinder.rb → search.rb} +69 -34
- data/lib/tb/tsv.rb +29 -1
- data/sample/colors.ppm +0 -0
- data/sample/gradation.pgm +0 -0
- data/sample/langs.csv +46 -0
- data/sample/tbplot +293 -0
- data/test-all-cov.rb +65 -0
- data/test-all.rb +5 -0
- data/test/test_basic.rb +99 -2
- data/test/test_catreader.rb +27 -0
- data/test/test_cmd_cat.rb +118 -0
- data/test/test_cmd_consecutive.rb +90 -0
- data/test/test_cmd_crop.rb +101 -0
- data/test/test_cmd_cross.rb +113 -0
- data/test/test_cmd_csv.rb +129 -0
- data/test/test_cmd_cut.rb +100 -0
- data/test/test_cmd_grep.rb +89 -0
- data/test/test_cmd_group.rb +181 -0
- data/test/test_cmd_gsub.rb +103 -0
- data/test/test_cmd_help.rb +190 -0
- data/test/test_cmd_join.rb +197 -0
- data/test/test_cmd_json.rb +75 -0
- data/test/test_cmd_ls.rb +203 -0
- data/test/test_cmd_mheader.rb +86 -0
- data/test/test_cmd_newfield.rb +63 -0
- data/test/test_cmd_pnm.rb +35 -0
- data/test/test_cmd_pp.rb +62 -0
- data/test/test_cmd_rename.rb +91 -0
- data/test/test_cmd_shape.rb +50 -0
- data/test/test_cmd_sort.rb +105 -0
- data/test/test_cmd_tsv.rb +67 -0
- data/test/test_cmd_yaml.rb +55 -0
- data/test/test_cmdtty.rb +154 -0
- data/test/test_cmdutil.rb +43 -0
- data/test/test_csv.rb +10 -0
- data/test/test_fieldset.rb +42 -0
- data/test/test_pager.rb +142 -0
- data/test/test_pnm.rb +374 -0
- data/test/test_reader.rb +147 -0
- data/test/test_record.rb +49 -0
- data/test/test_search.rb +575 -0
- data/test/test_tsv.rb +7 -0
- metadata +108 -5
- data/lib/tb/qtsv.rb +0 -93
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tb/cmdtop'
|
3
|
+
require 'tmpdir'
|
4
|
+
|
5
|
+
class TestTbCmdCross < 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
|
+
name,year,observ
|
21
|
+
aaaa,2000,1
|
22
|
+
bbbb,2001,3
|
23
|
+
bbbb,2000,4
|
24
|
+
cccc,2002,5
|
25
|
+
End
|
26
|
+
Tb::Cmd.main_cross(['-o', o="o.csv", 'name', 'year', i])
|
27
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
28
|
+
name,aaaa,bbbb,cccc
|
29
|
+
year,count,count,count
|
30
|
+
2000,1,1,
|
31
|
+
2001,,1,
|
32
|
+
2002,,,1
|
33
|
+
End
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_no_hkey_fields
|
37
|
+
exc = assert_raise(SystemExit) { Tb::Cmd.main_cross([]) }
|
38
|
+
assert(!exc.success?)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_no_vkey_fields
|
42
|
+
exc = assert_raise(SystemExit) { Tb::Cmd.main_cross(['hk']) }
|
43
|
+
assert(!exc.success?)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_compact
|
47
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
48
|
+
name,year,observ
|
49
|
+
aaaa,2000,1
|
50
|
+
bbbb,2001,3
|
51
|
+
bbbb,2000,4
|
52
|
+
cccc,2002,5
|
53
|
+
End
|
54
|
+
Tb::Cmd.main_cross(['-o', o="o.csv", 'name', 'year', '-c', i])
|
55
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
56
|
+
year,aaaa,bbbb,cccc
|
57
|
+
2000,1,1,
|
58
|
+
2001,,1,
|
59
|
+
2002,,,1
|
60
|
+
End
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_sum
|
64
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
65
|
+
name,year,observ
|
66
|
+
aaaa,2000,1
|
67
|
+
bbbb,2001,3
|
68
|
+
bbbb,2000,4
|
69
|
+
cccc,2002,5
|
70
|
+
aaaa,2000,2
|
71
|
+
End
|
72
|
+
Tb::Cmd.main_cross(['-o', o="o.csv", 'name', 'year', '-a', 'sum(observ)', i])
|
73
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
74
|
+
name,aaaa,bbbb,cccc
|
75
|
+
year,sum(observ),sum(observ),sum(observ)
|
76
|
+
2000,3,4,
|
77
|
+
2001,,3,
|
78
|
+
2002,,,5
|
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_cross(['-o', o="o.csv", 'a', 'b', i1, i2])
|
94
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
95
|
+
a,1,3,6,8
|
96
|
+
b,count,count,count,count
|
97
|
+
2,1,,,
|
98
|
+
4,,1,,
|
99
|
+
5,,,1,
|
100
|
+
7,,,,1
|
101
|
+
End
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_invalid_aggregator
|
105
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
106
|
+
a,b
|
107
|
+
1,2
|
108
|
+
3,4
|
109
|
+
End
|
110
|
+
exc = assert_raise(SystemExit) { Tb::Cmd.main_cross(['-o', "o.csv", 'a', 'b', '-a', 'foo', i]) }
|
111
|
+
assert(!exc.success?)
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tb/cmdtop'
|
3
|
+
require 'tmpdir'
|
4
|
+
|
5
|
+
class TestTbCmdCSV < 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.tsv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
20
|
+
a\tb\tc
|
21
|
+
0\t1\t2
|
22
|
+
4\t5\t6
|
23
|
+
End
|
24
|
+
Tb::Cmd.main_csv(['-o', o="o.csv", i])
|
25
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
26
|
+
a,b,c
|
27
|
+
0,1,2
|
28
|
+
4,5,6
|
29
|
+
End
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_complement_header
|
33
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
34
|
+
a,b
|
35
|
+
0,1,2
|
36
|
+
4,5,6
|
37
|
+
End
|
38
|
+
Tb::Cmd.main_csv(['-o', o="o.csv", i])
|
39
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
40
|
+
a,b,1
|
41
|
+
0,1,2
|
42
|
+
4,5,6
|
43
|
+
End
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_numeric
|
47
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
48
|
+
a
|
49
|
+
0,1,2
|
50
|
+
4,5,6
|
51
|
+
End
|
52
|
+
Tb::Cmd.main_csv(['-o', o="o.csv", '-N', i])
|
53
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
54
|
+
a,,
|
55
|
+
0,1,2
|
56
|
+
4,5,6
|
57
|
+
End
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_noarg
|
61
|
+
File.open(i="i.tsv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
62
|
+
a,b,c
|
63
|
+
0,1,2
|
64
|
+
4,5,6
|
65
|
+
End
|
66
|
+
save = STDIN.dup
|
67
|
+
input = File.open(i)
|
68
|
+
STDIN.reopen(input)
|
69
|
+
Tb::Cmd.main_csv(['-o', o="o.csv"])
|
70
|
+
STDIN.reopen(save)
|
71
|
+
save.close
|
72
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
73
|
+
a,b,c
|
74
|
+
0,1,2
|
75
|
+
4,5,6
|
76
|
+
End
|
77
|
+
ensure
|
78
|
+
save.close if save && !save.closed?
|
79
|
+
input.close if input && !input.closed?
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_pipeout
|
83
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
84
|
+
a,b,c
|
85
|
+
0,1,2
|
86
|
+
4,5,6
|
87
|
+
End
|
88
|
+
r, w = IO.pipe
|
89
|
+
th = Thread.new { r.read }
|
90
|
+
save = STDOUT.dup
|
91
|
+
STDOUT.reopen(w)
|
92
|
+
w.close
|
93
|
+
Tb::Cmd.main_csv([i])
|
94
|
+
STDOUT.reopen(save)
|
95
|
+
save.close
|
96
|
+
result = th.value
|
97
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), result)
|
98
|
+
a,b,c
|
99
|
+
0,1,2
|
100
|
+
4,5,6
|
101
|
+
End
|
102
|
+
ensure
|
103
|
+
r.close if r && !r.closed?
|
104
|
+
w.close if w && !w.closed?
|
105
|
+
save.close if save && !save.closed?
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_twofile
|
109
|
+
File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
110
|
+
a,b
|
111
|
+
1,2
|
112
|
+
3,4
|
113
|
+
End
|
114
|
+
File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
115
|
+
b,a
|
116
|
+
5,6
|
117
|
+
7,8
|
118
|
+
End
|
119
|
+
Tb::Cmd.main_csv(['-o', o="o.csv", i1, i2])
|
120
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
121
|
+
a,b
|
122
|
+
1,2
|
123
|
+
3,4
|
124
|
+
6,5
|
125
|
+
8,7
|
126
|
+
End
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tb/cmdtop'
|
3
|
+
require 'tmpdir'
|
4
|
+
|
5
|
+
class TestTbCmdCut < 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,d
|
21
|
+
0,1,2,3
|
22
|
+
4,5,6,7
|
23
|
+
8,9,a,b
|
24
|
+
c,d,e,f
|
25
|
+
End
|
26
|
+
Tb::Cmd.main_cut(['-o', o="o.csv", 'b,d', i])
|
27
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
28
|
+
b,d
|
29
|
+
1,3
|
30
|
+
5,7
|
31
|
+
9,b
|
32
|
+
d,f
|
33
|
+
End
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_no_cut_fields
|
37
|
+
exc = assert_raise(SystemExit) { Tb::Cmd.main_cut([]) }
|
38
|
+
assert(!exc.success?)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_opt_v
|
42
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
43
|
+
a,b,c,d
|
44
|
+
0,1,2,3
|
45
|
+
4,5,6,7
|
46
|
+
8,9,a,b
|
47
|
+
c,d,e,f
|
48
|
+
End
|
49
|
+
Tb::Cmd.main_cut(['-o', o="o.csv", '-v', 'b,d', i])
|
50
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
51
|
+
a,c
|
52
|
+
0,2
|
53
|
+
4,6
|
54
|
+
8,a
|
55
|
+
c,e
|
56
|
+
End
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_extend
|
60
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
61
|
+
a,b
|
62
|
+
0,1,2,3
|
63
|
+
End
|
64
|
+
Tb::Cmd.main_cut(['-o', o="o.csv", 'a,2,1', i])
|
65
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
66
|
+
a,2,1
|
67
|
+
0,3,2
|
68
|
+
End
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_unextendable
|
72
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
73
|
+
a,b
|
74
|
+
0,1,2,3
|
75
|
+
End
|
76
|
+
assert_raise(ArgumentError) { Tb::Cmd.main_cut(['-o', "o.csv", '0', i]) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_twofile
|
80
|
+
File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
81
|
+
a,b
|
82
|
+
1,2
|
83
|
+
3,4
|
84
|
+
End
|
85
|
+
File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
86
|
+
b,a
|
87
|
+
5,6
|
88
|
+
7,8
|
89
|
+
End
|
90
|
+
Tb::Cmd.main_cut(['-o', o="o.csv", 'a', i1, i2])
|
91
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
92
|
+
a
|
93
|
+
1
|
94
|
+
3
|
95
|
+
6
|
96
|
+
8
|
97
|
+
End
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tb/cmdtop'
|
3
|
+
require 'tmpdir'
|
4
|
+
|
5
|
+
class TestTbCmdGrep < 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,d
|
21
|
+
0,1,2,3
|
22
|
+
4,5,6,7
|
23
|
+
8,9,a,b
|
24
|
+
c,d,e,f
|
25
|
+
End
|
26
|
+
Tb::Cmd.main_grep(['-o', o="o.csv", '[6f]', i])
|
27
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
28
|
+
a,b,c,d
|
29
|
+
4,5,6,7
|
30
|
+
c,d,e,f
|
31
|
+
End
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_no_regexp
|
35
|
+
exc = assert_raise(SystemExit) { Tb::Cmd.main_grep([]) }
|
36
|
+
assert(!exc.success?)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_opt_e
|
40
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
41
|
+
a,b,c,d
|
42
|
+
0,1,2,3
|
43
|
+
4,5,6,7
|
44
|
+
8,9,a,b
|
45
|
+
c,d,e,f
|
46
|
+
End
|
47
|
+
Tb::Cmd.main_grep(['-o', o="o.csv", '-e', '[6f]', i])
|
48
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
49
|
+
a,b,c,d
|
50
|
+
4,5,6,7
|
51
|
+
c,d,e,f
|
52
|
+
End
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_ruby_pred
|
56
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
57
|
+
a,b,c,d
|
58
|
+
0,1,2,3
|
59
|
+
4,5,6,7
|
60
|
+
8,9,a,b
|
61
|
+
c,d,e,f
|
62
|
+
End
|
63
|
+
Tb::Cmd.main_grep(['-o', o="o.csv", '--ruby', '_["b"] == "5"', i])
|
64
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
65
|
+
a,b,c,d
|
66
|
+
4,5,6,7
|
67
|
+
End
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_twofile
|
71
|
+
File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
72
|
+
a,b
|
73
|
+
1,2
|
74
|
+
3,4
|
75
|
+
End
|
76
|
+
File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
77
|
+
b,a
|
78
|
+
5,6
|
79
|
+
7,8
|
80
|
+
End
|
81
|
+
Tb::Cmd.main_grep(['-o', o="o.csv", '[46]', i1, i2])
|
82
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
83
|
+
a,b
|
84
|
+
3,4
|
85
|
+
6,5
|
86
|
+
End
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tb/cmdtop'
|
3
|
+
require 'tmpdir'
|
4
|
+
|
5
|
+
class TestTbCmdGroup < 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,d
|
21
|
+
0,1,2,3
|
22
|
+
4,5,6,7
|
23
|
+
8,9,a,b
|
24
|
+
c,d,e,f
|
25
|
+
x,5,6,y
|
26
|
+
End
|
27
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'b,c', '-a', 'count', i])
|
28
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
29
|
+
b,c,count
|
30
|
+
1,2,1
|
31
|
+
5,6,2
|
32
|
+
9,a,1
|
33
|
+
d,e,1
|
34
|
+
End
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_no_keyfields
|
38
|
+
exc = assert_raise(SystemExit) { Tb::Cmd.main_group([]) }
|
39
|
+
assert(!exc.success?)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_sum
|
43
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
44
|
+
a,b,c,d
|
45
|
+
0,1,2,3
|
46
|
+
4,5,6,7
|
47
|
+
8,9,4,1
|
48
|
+
c,d,5,2.5
|
49
|
+
x,5,6,3
|
50
|
+
End
|
51
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'b', '-a', 'sum(d)', i])
|
52
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
53
|
+
b,sum(d)
|
54
|
+
1,3
|
55
|
+
5,10
|
56
|
+
9,1
|
57
|
+
d,2.5
|
58
|
+
End
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_max
|
62
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
63
|
+
a,b,c,d
|
64
|
+
0,1,2,3
|
65
|
+
4,5,6,7
|
66
|
+
8,9,4,1
|
67
|
+
c,d,5,2.5
|
68
|
+
x,5,6,3
|
69
|
+
End
|
70
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'b', '-a', 'max(d)', i])
|
71
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
72
|
+
b,max(d)
|
73
|
+
1,3
|
74
|
+
5,7
|
75
|
+
9,1
|
76
|
+
d,2.5
|
77
|
+
End
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_min
|
81
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
82
|
+
a,b,c,d
|
83
|
+
0,1,2,3
|
84
|
+
4,5,6,7
|
85
|
+
8,9,4,1
|
86
|
+
c,d,5,2.5
|
87
|
+
x,5,6,3
|
88
|
+
End
|
89
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'b', '-a', 'min(d)', i])
|
90
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
91
|
+
b,min(d)
|
92
|
+
1,3
|
93
|
+
5,3
|
94
|
+
9,1
|
95
|
+
d,2.5
|
96
|
+
End
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_avg
|
100
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
101
|
+
a,b,c,d
|
102
|
+
0,1,2,3
|
103
|
+
4,5,6,7
|
104
|
+
8,9,4,1
|
105
|
+
c,d,5,2.5
|
106
|
+
x,5,6,3
|
107
|
+
End
|
108
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'b', '-a', 'avg(d)', i])
|
109
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
110
|
+
b,avg(d)
|
111
|
+
1,3.0
|
112
|
+
5,5.0
|
113
|
+
9,1.0
|
114
|
+
d,2.5
|
115
|
+
End
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_values
|
119
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
120
|
+
a,b
|
121
|
+
A,1
|
122
|
+
A,2
|
123
|
+
B,3
|
124
|
+
A,1
|
125
|
+
End
|
126
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'a', '-a', 'values(b)', i])
|
127
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
128
|
+
a,values(b)
|
129
|
+
A,"1,2,1"
|
130
|
+
B,3
|
131
|
+
End
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_uniquevalues
|
135
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
136
|
+
a,b
|
137
|
+
A,1
|
138
|
+
A,2
|
139
|
+
B,3
|
140
|
+
A,1
|
141
|
+
End
|
142
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'a', '-a', 'uniquevalues(b)', i])
|
143
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
144
|
+
a,uniquevalues(b)
|
145
|
+
A,"1,2"
|
146
|
+
B,3
|
147
|
+
End
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_twofile
|
151
|
+
File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
152
|
+
a,b
|
153
|
+
1,2
|
154
|
+
3,4
|
155
|
+
End
|
156
|
+
File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
157
|
+
b,a
|
158
|
+
5,6
|
159
|
+
7,8
|
160
|
+
End
|
161
|
+
Tb::Cmd.main_group(['-o', o="o.csv", 'a', '-a', 'count', i1, i2])
|
162
|
+
assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
|
163
|
+
a,count
|
164
|
+
1,1
|
165
|
+
3,1
|
166
|
+
6,1
|
167
|
+
8,1
|
168
|
+
End
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_invalid_aggregator
|
172
|
+
File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
|
173
|
+
a,b
|
174
|
+
1,2
|
175
|
+
3,4
|
176
|
+
End
|
177
|
+
exc = assert_raise(SystemExit) { Tb::Cmd.main_group(['-o', "o.csv", 'a', '-a', 'foo', i]) }
|
178
|
+
assert(!exc.success?)
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|