tb 0.9 → 1.0
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 +4 -4
- data/README +13 -11
- data/lib/tb.rb +14 -6
- data/lib/tb/catreader.rb +2 -2
- data/lib/tb/cmd_consecutive.rb +6 -2
- data/lib/tb/cmd_crop.rb +22 -3
- data/lib/tb/cmd_cross.rb +24 -0
- data/lib/tb/cmd_cut.rb +20 -10
- data/lib/tb/cmd_git.rb +20 -7
- data/lib/tb/cmd_group.rb +32 -0
- data/lib/tb/cmd_gsub.rb +21 -0
- data/lib/tb/cmd_join.rb +28 -0
- data/lib/tb/cmd_ls.rb +9 -0
- data/lib/tb/cmd_melt.rb +15 -0
- data/lib/tb/cmd_mheader.rb +15 -0
- data/lib/tb/cmd_nest.rb +27 -6
- data/lib/tb/cmd_newfield.rb +19 -2
- data/lib/tb/cmd_rename.rb +20 -0
- data/lib/tb/{cmd_grep.rb → cmd_search.rb} +37 -23
- data/lib/tb/cmd_shape.rb +69 -25
- data/lib/tb/cmd_sort.rb +20 -0
- data/lib/tb/cmd_tar.rb +38 -0
- data/lib/tb/cmd_to_json.rb +2 -2
- data/lib/tb/cmd_to_ltsv.rb +3 -3
- data/lib/tb/cmd_to_pnm.rb +3 -3
- data/lib/tb/cmd_to_tsv.rb +3 -3
- data/lib/tb/cmd_to_yaml.rb +3 -3
- data/lib/tb/cmd_unmelt.rb +15 -0
- data/lib/tb/cmd_unnest.rb +31 -7
- data/lib/tb/cmdmain.rb +2 -0
- data/lib/tb/cmdtop.rb +1 -1
- data/lib/tb/cmdutil.rb +9 -62
- data/lib/tb/csv.rb +21 -79
- data/lib/tb/enumerable.rb +42 -68
- data/lib/tb/enumerator.rb +15 -7
- data/lib/tb/{fieldset.rb → hashreader.rb} +37 -56
- data/lib/tb/hashwriter.rb +54 -0
- data/lib/tb/headerreader.rb +108 -0
- data/lib/tb/headerwriter.rb +116 -0
- data/lib/tb/json.rb +17 -15
- data/lib/tb/ltsv.rb +35 -96
- data/lib/tb/ndjson.rb +63 -0
- data/lib/tb/numericreader.rb +66 -0
- data/lib/tb/numericwriter.rb +61 -0
- data/lib/tb/pnm.rb +206 -200
- data/lib/tb/ropen.rb +54 -59
- data/lib/tb/tsv.rb +39 -71
- data/sample/excel2csv +24 -25
- data/sample/poi-xls2csv.rb +13 -14
- data/tb.gemspec +154 -0
- data/test/test_cmd_cat.rb +28 -6
- data/test/test_cmd_consecutive.rb +8 -3
- data/test/test_cmd_cut.rb +14 -4
- data/test/test_cmd_git_log.rb +50 -50
- data/test/test_cmd_grep.rb +6 -6
- data/test/test_cmd_gsub.rb +7 -2
- data/test/test_cmd_ls.rb +70 -62
- data/test/test_cmd_shape.rb +43 -6
- data/test/test_cmd_svn_log.rb +26 -27
- data/test/test_cmd_to_csv.rb +10 -5
- data/test/test_cmd_to_json.rb +16 -0
- data/test/test_cmd_to_ltsv.rb +2 -2
- data/test/test_cmd_to_pp.rb +7 -2
- data/test/test_csv.rb +74 -62
- data/test/test_ex_enumerable.rb +0 -1
- data/test/test_fileenumerator.rb +3 -3
- data/test/test_headercsv.rb +43 -0
- data/test/test_json.rb +2 -2
- data/test/test_ltsv.rb +22 -17
- data/test/test_ndjson.rb +62 -0
- data/test/test_numericcsv.rb +36 -0
- data/test/test_pnm.rb +69 -70
- data/test/test_reader.rb +27 -124
- data/test/test_tbenum.rb +18 -18
- data/test/test_tsv.rb +21 -32
- data/test/util_tbtest.rb +12 -0
- metadata +41 -19
- data/lib/tb/basic.rb +0 -1070
- data/lib/tb/reader.rb +0 -106
- data/lib/tb/record.rb +0 -158
- data/test/test_basic.rb +0 -403
- data/test/test_fieldset.rb +0 -42
- data/test/test_record.rb +0 -61
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'tb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestTbHeaderCSV < Test::Unit::TestCase
|
5
|
+
def test_reader
|
6
|
+
csv = <<-'End'.gsub(/^\s*/, '')
|
7
|
+
A,B,C
|
8
|
+
a,b,c
|
9
|
+
d,e,f
|
10
|
+
End
|
11
|
+
reader = Tb::HeaderCSVReader.new(StringIO.new(csv))
|
12
|
+
assert_equal({"A"=>"a", "B"=>"b", "C"=>"c"}, reader.get_hash)
|
13
|
+
assert_equal({"A"=>"d", "B"=>"e", "C"=>"f"}, reader.get_hash)
|
14
|
+
assert_equal(nil, reader.get_hash)
|
15
|
+
assert_equal(nil, reader.get_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_writer
|
19
|
+
arys = []
|
20
|
+
writer = Tb::HeaderCSVWriter.new(arys)
|
21
|
+
assert_equal([], arys)
|
22
|
+
writer.put_hash({"A"=>"a", "B"=>"b", "C"=>"c"})
|
23
|
+
assert_equal([], arys)
|
24
|
+
writer.put_hash({"A"=>"d", "B"=>"e", "C"=>"f"})
|
25
|
+
assert_equal([], arys)
|
26
|
+
writer.finish
|
27
|
+
assert_equal(["A,B,C\n", "a,b,c\n", "d,e,f\n"], arys)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_writer_known_header
|
31
|
+
arys = []
|
32
|
+
writer = Tb::HeaderCSVWriter.new(arys)
|
33
|
+
writer.header_generator = lambda { %w[A B C] }
|
34
|
+
assert_equal([], arys)
|
35
|
+
writer.put_hash({"A"=>"a", "B"=>"b", "C"=>"c"})
|
36
|
+
assert_equal(["A,B,C\n", "a,b,c\n"], arys)
|
37
|
+
writer.put_hash({"A"=>"d", "B"=>"e", "C"=>"f"})
|
38
|
+
assert_equal(["A,B,C\n", "a,b,c\n", "d,e,f\n"], arys)
|
39
|
+
writer.finish
|
40
|
+
assert_equal(["A,B,C\n", "a,b,c\n", "d,e,f\n"], arys)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/test/test_json.rb
CHANGED
@@ -3,13 +3,13 @@ require 'test/unit'
|
|
3
3
|
|
4
4
|
class TestTbJSON < Test::Unit::TestCase
|
5
5
|
def test_parse
|
6
|
-
r = Tb::JSONReader.new('[{"a":1, "b":2}, {"a":3, "b":4}]')
|
6
|
+
r = Tb::JSONReader.new(StringIO.new('[{"a":1, "b":2}, {"a":3, "b":4}]'))
|
7
7
|
result = []
|
8
8
|
r.with_header {|header|
|
9
9
|
result << header
|
10
10
|
}.each {|obj|
|
11
11
|
result << obj
|
12
12
|
}
|
13
|
-
assert_equal([
|
13
|
+
assert_equal([%w[a b], {"a"=>1, "b"=>2}, {"a"=>3, "b"=>4}], result)
|
14
14
|
end
|
15
15
|
end
|
data/test/test_ltsv.rb
CHANGED
@@ -2,6 +2,17 @@ require 'tb'
|
|
2
2
|
require 'test/unit'
|
3
3
|
|
4
4
|
class TestTbLTSV < Test::Unit::TestCase
|
5
|
+
def parse_ltsv(ltsv)
|
6
|
+
Tb::LTSVReader.new(StringIO.new(ltsv)).to_a
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate_ltsv(ary)
|
10
|
+
writer = Tb::LTSVWriter.new(out = '')
|
11
|
+
ary.each {|h| writer.put_hash h }
|
12
|
+
writer.finish
|
13
|
+
out
|
14
|
+
end
|
15
|
+
|
5
16
|
def test_escape_and_unescape
|
6
17
|
0x00.upto(0x7f) {|c|
|
7
18
|
s = [c].pack("C")
|
@@ -11,38 +22,32 @@ class TestTbLTSV < Test::Unit::TestCase
|
|
11
22
|
end
|
12
23
|
|
13
24
|
def test_parse
|
14
|
-
r = Tb::LTSVReader.new("a:1\tb:2\na:3\tb:4\n")
|
25
|
+
r = Tb::LTSVReader.new(StringIO.new("a:1\tb:2\na:3\tb:4\n"))
|
15
26
|
result = []
|
16
27
|
r.with_header {|header|
|
17
28
|
result << header
|
18
29
|
}.each {|obj|
|
19
30
|
result << obj
|
20
31
|
}
|
21
|
-
assert_equal([
|
32
|
+
assert_equal([%w[a b], {"a"=>"1", "b"=>"2"}, {"a"=>"3", "b"=>"4"}], result)
|
22
33
|
end
|
23
34
|
|
24
35
|
def test_parse2
|
25
|
-
|
26
|
-
t = Tb.parse_ltsv(ltsv)
|
27
|
-
records = []
|
28
|
-
t.each_record {|record|
|
29
|
-
records << record.to_h_with_reserved
|
30
|
-
}
|
36
|
+
t = parse_ltsv("a:1\tb:2\n")
|
31
37
|
assert_equal(
|
32
|
-
[{"
|
33
|
-
|
38
|
+
[{"a"=>"1", "b"=>"2"}],
|
39
|
+
t)
|
34
40
|
end
|
35
41
|
|
36
42
|
def test_generate_ltsv
|
37
|
-
|
38
|
-
|
39
|
-
assert_equal("a:foo\tb:bar\n", out)
|
43
|
+
t = [{'a' => 'foo', 'b' => 'bar'}]
|
44
|
+
assert_equal("a:foo\tb:bar\n", generate_ltsv(t))
|
40
45
|
end
|
41
46
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
assert_equal("a:
|
47
|
+
def test_generate_ltsv2
|
48
|
+
t = [{'a' => 'foo', 'b' => 'bar'},
|
49
|
+
{'a' => 'q', 'b' => 'w'}]
|
50
|
+
assert_equal("a:foo\tb:bar\na:q\tb:w\n", generate_ltsv(t))
|
46
51
|
end
|
47
52
|
|
48
53
|
end
|
data/test/test_ndjson.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'tb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestTbNDJSON < Test::Unit::TestCase
|
5
|
+
def parse_ndjson(ndjson)
|
6
|
+
Tb::NDJSONReader.new(StringIO.new(ndjson)).to_a
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate_ndjson(ary)
|
10
|
+
writer = Tb::NDJSONWriter.new(out = '')
|
11
|
+
ary.each {|h| writer.put_hash h }
|
12
|
+
writer.finish
|
13
|
+
out
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_reader
|
17
|
+
ndjson = <<-'End'
|
18
|
+
{"A":"a","B":"b","C":"c"}
|
19
|
+
{"A":"d","B":"e","C":"f"}
|
20
|
+
End
|
21
|
+
reader = Tb::NDJSONReader.new(StringIO.new(ndjson))
|
22
|
+
assert_equal({"A"=>"a", "B"=>"b", "C"=>"c"}, reader.get_hash)
|
23
|
+
assert_equal({"A"=>"d", "B"=>"e", "C"=>"f"}, reader.get_hash)
|
24
|
+
assert_equal(nil, reader.get_hash)
|
25
|
+
assert_equal(nil, reader.get_hash)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_reader_header
|
29
|
+
ndjson = <<-'End'
|
30
|
+
{"A":"a"}
|
31
|
+
{"A":"d","B":"e","C":"f"}
|
32
|
+
End
|
33
|
+
reader = Tb::NDJSONReader.new(StringIO.new(ndjson))
|
34
|
+
assert_equal(%w[A B C], reader.get_named_header)
|
35
|
+
assert_equal({"A"=>"a"}, reader.get_hash)
|
36
|
+
assert_equal({"A"=>"d", "B"=>"e", "C"=>"f"}, reader.get_hash)
|
37
|
+
assert_equal(nil, reader.get_hash)
|
38
|
+
assert_equal(nil, reader.get_hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_writer
|
42
|
+
arys = []
|
43
|
+
writer = Tb::NDJSONWriter.new(arys)
|
44
|
+
writer.header_generator = lambda { flunk }
|
45
|
+
assert_equal([], arys)
|
46
|
+
writer.put_hash({"A"=>"a", "B"=>"b", "C"=>"c"})
|
47
|
+
assert_equal(['{"A":"a","B":"b","C":"c"}'+"\n"], arys)
|
48
|
+
writer.put_hash({"A"=>"d", "B"=>"e", "C"=>"f"})
|
49
|
+
assert_equal(['{"A":"a","B":"b","C":"c"}'+"\n", '{"A":"d","B":"e","C":"f"}'+"\n"], arys)
|
50
|
+
writer.finish
|
51
|
+
assert_equal(['{"A":"a","B":"b","C":"c"}'+"\n", '{"A":"d","B":"e","C":"f"}'+"\n"], arys)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_newline_in_string
|
55
|
+
assert_equal('{"r":"\r","n":"\n"}'+"\n", generate_ndjson([{"r"=>"\r", "n"=>"\n"}]))
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_empty_line
|
59
|
+
assert_equal([{"a"=>"1"}, {"a"=>"2"}], parse_ndjson('{"a":"1"}'+"\n\n" + '{"a":"2"}'+"\n"))
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'tb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestTbNumericCSV < Test::Unit::TestCase
|
5
|
+
def test_reader
|
6
|
+
csv = <<-'End'.gsub(/^\s*/, '')
|
7
|
+
A,B,C
|
8
|
+
a,b,c
|
9
|
+
d,e,f
|
10
|
+
End
|
11
|
+
reader = Tb::NumericCSVReader.new(StringIO.new(csv))
|
12
|
+
assert_equal({"1"=>"A", "2"=>"B", "3"=>"C"}, reader.get_hash)
|
13
|
+
assert_equal({"1"=>"a", "2"=>"b", "3"=>"c"}, reader.get_hash)
|
14
|
+
assert_equal({"1"=>"d", "2"=>"e", "3"=>"f"}, reader.get_hash)
|
15
|
+
assert_equal(nil, reader.get_hash)
|
16
|
+
assert_equal(nil, reader.get_hash)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_writer
|
20
|
+
arys = []
|
21
|
+
writer = Tb::NumericCSVWriter.new(arys)
|
22
|
+
writer.put_hash({"1"=>"A", "2"=>"B", "3"=>"C"})
|
23
|
+
assert_equal(["A,B,C\n"], arys)
|
24
|
+
writer.put_hash({"1"=>"a", "2"=>"b", "3"=>"c"})
|
25
|
+
assert_equal(["A,B,C\n", "a,b,c\n"], arys)
|
26
|
+
writer.put_hash({"1"=>"d", "2"=>"e", "3"=>"f"})
|
27
|
+
assert_equal(["A,B,C\n", "a,b,c\n", "d,e,f\n"], arys)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_writer_invalid_field
|
31
|
+
arys = []
|
32
|
+
writer = Tb::NumericCSVWriter.new(arys)
|
33
|
+
assert_raise(ArgumentError) { writer.put_hash({"A"=>"1"}) }
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/test/test_pnm.rb
CHANGED
@@ -5,9 +5,20 @@ require 'tmpdir'
|
|
5
5
|
require 'test/unit'
|
6
6
|
|
7
7
|
class TestTbPNM < Test::Unit::TestCase
|
8
|
+
def parse_pnm(pnm)
|
9
|
+
Tb::PNMReader.new(StringIO.new(pnm)).to_a
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate_pnm(ary)
|
13
|
+
writer = Tb::PNMWriter.new(out = '')
|
14
|
+
ary.each {|h| writer.put_hash h }
|
15
|
+
writer.finish
|
16
|
+
out
|
17
|
+
end
|
18
|
+
|
8
19
|
def test_parse_pbm_ascii
|
9
20
|
pbm = "P1\n2 3\n101101\n"
|
10
|
-
t =
|
21
|
+
t = parse_pnm(pbm)
|
11
22
|
assert_equal(
|
12
23
|
[
|
13
24
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P1"},
|
@@ -20,13 +31,13 @@ class TestTbPNM < Test::Unit::TestCase
|
|
20
31
|
{"type"=>"pixel", "x"=>1, "y"=>1, "component"=>"V", "value"=>0.0},
|
21
32
|
{"type"=>"pixel", "x"=>0, "y"=>2, "component"=>"V", "value"=>1.0},
|
22
33
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"V", "value"=>0.0}
|
23
|
-
], t
|
24
|
-
assert_equal("P1\n2 3\n10\n11\n01\n", t
|
34
|
+
], t)
|
35
|
+
assert_equal("P1\n2 3\n10\n11\n01\n", generate_pnm(t))
|
25
36
|
end
|
26
37
|
|
27
38
|
def test_parse_pbm_binary
|
28
39
|
pbm = "P4\n2 3\n\x80\xc0\x40"
|
29
|
-
t =
|
40
|
+
t = parse_pnm(pbm)
|
30
41
|
assert_equal(
|
31
42
|
[
|
32
43
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P4"},
|
@@ -39,13 +50,13 @@ class TestTbPNM < Test::Unit::TestCase
|
|
39
50
|
{"type"=>"pixel", "x"=>1, "y"=>1, "component"=>"V", "value"=>0.0},
|
40
51
|
{"type"=>"pixel", "x"=>0, "y"=>2, "component"=>"V", "value"=>1.0},
|
41
52
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"V", "value"=>0.0}
|
42
|
-
], t
|
43
|
-
assert_equal(pbm, t
|
53
|
+
], t)
|
54
|
+
assert_equal(pbm, generate_pnm(t))
|
44
55
|
end
|
45
56
|
|
46
57
|
def test_parse_pgm_ascii
|
47
58
|
pgm = "P2\n2 3\n255\n0 1\n100 101\n254 255\n"
|
48
|
-
t =
|
59
|
+
t = parse_pnm(pgm)
|
49
60
|
assert_equal(
|
50
61
|
[
|
51
62
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P2"},
|
@@ -58,13 +69,13 @@ class TestTbPNM < Test::Unit::TestCase
|
|
58
69
|
{"type"=>"pixel", "x"=>1, "y"=>1, "component"=>"V", "value"=>101.0/255},
|
59
70
|
{"type"=>"pixel", "x"=>0, "y"=>2, "component"=>"V", "value"=>254.0/255},
|
60
71
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"V", "value"=>255.0/255}
|
61
|
-
], t
|
62
|
-
assert_equal(pgm, t
|
72
|
+
], t)
|
73
|
+
assert_equal(pgm, generate_pnm(t))
|
63
74
|
end
|
64
75
|
|
65
76
|
def test_parse_pgm_binary
|
66
77
|
pgm = "P5\n2 3\n255\n\x00\x01\x64\x65\xfe\xff"
|
67
|
-
t =
|
78
|
+
t = parse_pnm(pgm)
|
68
79
|
assert_equal(
|
69
80
|
[
|
70
81
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P5"},
|
@@ -77,13 +88,13 @@ class TestTbPNM < Test::Unit::TestCase
|
|
77
88
|
{"type"=>"pixel", "x"=>1, "y"=>1, "component"=>"V", "value"=>101.0/255},
|
78
89
|
{"type"=>"pixel", "x"=>0, "y"=>2, "component"=>"V", "value"=>254.0/255},
|
79
90
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"V", "value"=>255.0/255}
|
80
|
-
], t
|
81
|
-
assert_equal(pgm, t
|
91
|
+
], t)
|
92
|
+
assert_equal(pgm, generate_pnm(t))
|
82
93
|
end
|
83
94
|
|
84
95
|
def test_parse_ppm_ascii
|
85
96
|
ppm = "P3\n2 3\n255\n0 1 2 3 4 5\n100 101 102 103 104 105\n250 251 252 253 254 255\n"
|
86
|
-
t =
|
97
|
+
t = parse_pnm(ppm)
|
87
98
|
assert_equal(
|
88
99
|
[
|
89
100
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P3"},
|
@@ -108,13 +119,13 @@ class TestTbPNM < Test::Unit::TestCase
|
|
108
119
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"R", "value"=>253.0/255},
|
109
120
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"G", "value"=>254.0/255},
|
110
121
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"B", "value"=>255.0/255}
|
111
|
-
], t
|
112
|
-
assert_equal(ppm, t
|
122
|
+
], t)
|
123
|
+
assert_equal(ppm, generate_pnm(t))
|
113
124
|
end
|
114
125
|
|
115
126
|
def test_parse_ppm_binary
|
116
127
|
ppm = "P6\n2 3\n255\n\x00\x01\x02\x03\x04\x05\x64\x65\x66\x67\x68\x69\xfa\xfb\xfc\xfd\xfe\xff"
|
117
|
-
t =
|
128
|
+
t = parse_pnm(ppm)
|
118
129
|
assert_equal(
|
119
130
|
[
|
120
131
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P6"},
|
@@ -139,13 +150,13 @@ class TestTbPNM < Test::Unit::TestCase
|
|
139
150
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"R", "value"=>253.0/255},
|
140
151
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"G", "value"=>254.0/255},
|
141
152
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"B", "value"=>255.0/255}
|
142
|
-
], t
|
143
|
-
assert_equal(ppm, t
|
153
|
+
], t)
|
154
|
+
assert_equal(ppm, generate_pnm(t))
|
144
155
|
end
|
145
156
|
|
146
157
|
def test_parse_ppm_binary2
|
147
158
|
ppm = "P6\n2 3\n65535\n\x00\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x64\x00\x65\x00\x66\x00\x67\x00\x68\x00\x69\x00\xfa\x00\xfb\x00\xfc\x00\xfd\x00\xfe\x00\xff"
|
148
|
-
t =
|
159
|
+
t = parse_pnm(ppm)
|
149
160
|
assert_equal(
|
150
161
|
[
|
151
162
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P6"},
|
@@ -170,13 +181,13 @@ class TestTbPNM < Test::Unit::TestCase
|
|
170
181
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"R", "value"=>253.0/65535},
|
171
182
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"G", "value"=>254.0/65535},
|
172
183
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"B", "value"=>255.0/65535}
|
173
|
-
], t
|
174
|
-
assert_equal(ppm, t
|
184
|
+
], t)
|
185
|
+
assert_equal(ppm, generate_pnm(t))
|
175
186
|
end
|
176
187
|
|
177
188
|
def test_parse_pbm_comment
|
178
189
|
pbm = "P1\n\#foo\n2 3\n101101\n"
|
179
|
-
t =
|
190
|
+
t = parse_pnm(pbm)
|
180
191
|
assert_equal(
|
181
192
|
[
|
182
193
|
{"type"=>"meta", "component"=>"pnm_type", "value"=>"P1"},
|
@@ -190,124 +201,112 @@ class TestTbPNM < Test::Unit::TestCase
|
|
190
201
|
{"type"=>"pixel", "x"=>1, "y"=>1, "component"=>"V", "value"=>0.0},
|
191
202
|
{"type"=>"pixel", "x"=>0, "y"=>2, "component"=>"V", "value"=>1.0},
|
192
203
|
{"type"=>"pixel", "x"=>1, "y"=>2, "component"=>"V", "value"=>0.0}
|
193
|
-
], t
|
194
|
-
assert_equal("P1\n\#foo\n2 3\n10\n11\n01\n", t
|
204
|
+
], t)
|
205
|
+
assert_equal("P1\n\#foo\n2 3\n10\n11\n01\n", generate_pnm(t))
|
195
206
|
end
|
196
207
|
|
197
208
|
def test_parse_pbm_ascii_wide
|
198
209
|
pbm = "P1\n71 3\n" + "0" * (71 * 3)
|
199
|
-
t =
|
200
|
-
assert_equal("P1\n71 3\n" + ("0"*70+"\n0\n")*3, t
|
210
|
+
t = parse_pnm(pbm)
|
211
|
+
assert_equal("P1\n71 3\n" + ("0"*70+"\n0\n")*3, generate_pnm(t))
|
201
212
|
end
|
202
213
|
|
203
214
|
def test_parse_pgm_ascii_wide
|
204
215
|
pgm = "P2\n40 3\n255\n" + "0 " * (40*3)
|
205
|
-
t =
|
206
|
-
assert_equal("P2\n40 3\n255\n" + ("0 "*34 + "0\n" + "0 "*4 + "0\n")*3, t
|
216
|
+
t = parse_pnm(pgm)
|
217
|
+
assert_equal("P2\n40 3\n255\n" + ("0 "*34 + "0\n" + "0 "*4 + "0\n")*3, generate_pnm(t))
|
207
218
|
end
|
208
219
|
|
209
220
|
def test_parse_invalid
|
210
221
|
invalid = "foo"
|
211
|
-
assert_raise(ArgumentError) {
|
222
|
+
assert_raise(ArgumentError) { parse_pnm(invalid) }
|
212
223
|
end
|
213
224
|
|
214
225
|
def test_parse_too_short
|
215
226
|
too_short = "P1\n2 3\n10110\n"
|
216
|
-
assert_raise(ArgumentError) {
|
227
|
+
assert_raise(ArgumentError) { parse_pnm(too_short) }
|
217
228
|
end
|
218
229
|
|
219
230
|
def test_generate_invalid_fields
|
220
|
-
t =
|
221
|
-
assert_raise(ArgumentError) { t
|
231
|
+
t = [{ "foo" => 1 }]
|
232
|
+
assert_raise(ArgumentError) { generate_pnm(t) }
|
222
233
|
end
|
223
234
|
|
224
235
|
def test_generate_inconsistent_color_component
|
225
|
-
t =
|
226
|
-
assert_raise(ArgumentError) { t
|
236
|
+
t = [{'component' => 'V', 'value' => 1.0}, {'component' => 'R'}]
|
237
|
+
assert_raise(ArgumentError) { generate_pnm(t) }
|
227
238
|
end
|
228
239
|
|
229
240
|
def test_generate_complement
|
230
|
-
t =
|
231
|
-
[
|
241
|
+
t = [
|
232
242
|
{"x"=>0, "y"=>0, "component"=>"V", "value"=>0.0},
|
233
243
|
{"x"=>1, "y"=>0, "component"=>"V", "value"=>1.0},
|
234
244
|
{"x"=>0, "y"=>1, "component"=>"V", "value"=>0.0},
|
235
245
|
{"x"=>1, "y"=>1, "component"=>"V", "value"=>0.0},
|
236
246
|
{"x"=>0, "y"=>2, "component"=>"V", "value"=>1.0},
|
237
247
|
{"x"=>1, "y"=>2, "component"=>"V", "value"=>0.0}
|
238
|
-
]
|
239
|
-
assert_equal("P4\n2 3\n\x80\xc0\x40", t
|
248
|
+
]
|
249
|
+
assert_equal("P4\n2 3\n\x80\xc0\x40", generate_pnm(t))
|
240
250
|
end
|
241
251
|
|
242
252
|
def test_generate_complement2
|
243
|
-
t =
|
244
|
-
[
|
253
|
+
t = [
|
245
254
|
{"x"=>0, "y"=>0, "component"=>"V", "value"=>1.0/65535},
|
246
255
|
{"x"=>1, "y"=>0, "component"=>"V", "value"=>1.0},
|
247
256
|
{"x"=>0, "y"=>1, "component"=>"V", "value"=>0.0},
|
248
257
|
{"x"=>1, "y"=>1, "component"=>"V", "value"=>0.0},
|
249
258
|
{"x"=>0, "y"=>2, "component"=>"V", "value"=>1.0},
|
250
259
|
{"x"=>1, "y"=>2, "component"=>"V", "value"=>0.0}
|
251
|
-
]
|
252
|
-
assert_equal("P5\n2 3\n65535\n\x00\x01\xff\xff\x00\x00\x00\x00\xff\xff\x00\x00", t
|
260
|
+
]
|
261
|
+
assert_equal("P5\n2 3\n65535\n\x00\x01\xff\xff\x00\x00\x00\x00\xff\xff\x00\x00", generate_pnm(t))
|
253
262
|
end
|
254
263
|
|
255
264
|
def test_generate_complement1
|
256
|
-
t =
|
257
|
-
[
|
265
|
+
t = [
|
258
266
|
{"x"=>0, "y"=>0, "component"=>"V", "value"=>1.0/255},
|
259
267
|
{"x"=>1, "y"=>0, "component"=>"V", "value"=>1.0},
|
260
268
|
{"x"=>0, "y"=>1, "component"=>"V", "value"=>0.0},
|
261
269
|
{"x"=>1, "y"=>1, "component"=>"V", "value"=>0.0},
|
262
270
|
{"x"=>0, "y"=>2, "component"=>"V", "value"=>1.0},
|
263
271
|
{"x"=>1, "y"=>2, "component"=>"V", "value"=>0.0}
|
264
|
-
]
|
265
|
-
assert_equal("P5\n2 3\n255\n\x01\xff\x00\x00\xff\x00", t
|
272
|
+
]
|
273
|
+
assert_equal("P5\n2 3\n255\n\x01\xff\x00\x00\xff\x00", generate_pnm(t))
|
266
274
|
end
|
267
275
|
|
268
276
|
def test_generate_complement_ppm
|
269
|
-
t =
|
270
|
-
[
|
277
|
+
t = [
|
271
278
|
{"x"=>0, "y"=>0, "component"=>"R", "value"=>0.0},
|
272
279
|
{"x"=>0, "y"=>0, "component"=>"G", "value"=>1.0},
|
273
280
|
{"x"=>0, "y"=>0, "component"=>"B", "value"=>1.0},
|
274
|
-
]
|
275
|
-
assert_equal("P6\n1 1\n255\n\x00\xff\xff", t
|
281
|
+
]
|
282
|
+
assert_equal("P6\n1 1\n255\n\x00\xff\xff", generate_pnm(t))
|
276
283
|
end
|
277
284
|
|
278
285
|
def test_generate_overrange
|
279
|
-
t =
|
280
|
-
[
|
286
|
+
t = [
|
281
287
|
{"x"=>0, "y"=>0, "component"=>"V", "value"=>-0.5},
|
282
288
|
{"x"=>0, "y"=>1, "component"=>"V", "value"=>1.5},
|
283
|
-
]
|
284
|
-
assert_equal("P5\n1 2\n255\n\x00\xff", t
|
289
|
+
]
|
290
|
+
assert_equal("P5\n1 2\n255\n\x00\xff", generate_pnm(t))
|
285
291
|
end
|
286
292
|
|
287
293
|
def test_invalid_pnm_type
|
288
|
-
t =
|
289
|
-
assert_raise(ArgumentError) { t
|
294
|
+
t = [{'component' => 'pnm_type', 'value' => "foo"}]
|
295
|
+
assert_raise(ArgumentError) { generate_pnm(t) }
|
290
296
|
end
|
291
297
|
|
292
298
|
def test_invalid_comment
|
293
|
-
t =
|
294
|
-
assert_raise(ArgumentError) { t
|
295
|
-
end
|
296
|
-
|
297
|
-
def test_load_pnm
|
298
|
-
Dir.mktmpdir {|d|
|
299
|
-
File.open(fn="#{d}/foo.pbm", "w") {|f| f << "P4\n1 1\n\0" }
|
300
|
-
t = Tb.load_pnm(fn)
|
301
|
-
assert_equal({"type"=>"meta", "component"=>"pnm_type", "value"=>"P4"}, t.get_record(0).to_h)
|
302
|
-
}
|
299
|
+
t = [{'component' => 'comment', 'value' => "\n"}]
|
300
|
+
assert_raise(ArgumentError) { generate_pnm(t) }
|
303
301
|
end
|
304
302
|
|
305
303
|
def test_pnmreader_to_a
|
306
304
|
pbm = "P1\n2 3\n101101\n"
|
307
|
-
r = Tb::PNMReader.new(pbm)
|
305
|
+
r = Tb::PNMReader.new(StringIO.new(pbm))
|
306
|
+
header = r.get_named_header
|
307
|
+
assert_equal(["type", "x", "y", "component", "value"], header)
|
308
308
|
assert_equal(
|
309
|
-
[["
|
310
|
-
["meta", nil, nil, "pnm_type", "P1"],
|
309
|
+
[["meta", nil, nil, "pnm_type", "P1"],
|
311
310
|
["meta", nil, nil, "width", 2],
|
312
311
|
["meta", nil, nil, "height", 3],
|
313
312
|
["meta", nil, nil, "max", 1],
|
@@ -317,7 +316,7 @@ class TestTbPNM < Test::Unit::TestCase
|
|
317
316
|
["pixel", 1, 1, "V", 0.0],
|
318
317
|
["pixel", 0, 2, "V", 1.0],
|
319
318
|
["pixel", 1, 2, "V", 0.0]],
|
320
|
-
r.to_a)
|
319
|
+
r.to_a.map {|h| h.values_at(*header) })
|
321
320
|
end
|
322
321
|
|
323
322
|
def test_reader_open
|