awesome-cheeba 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,190 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'minitest/unit'
5
+ $: << 'lib' << 'test'
6
+ require 'cheeba/reader/node'
7
+ require 'cheeba/reader/parser'
8
+ require 'cheeba/indicators'
9
+ MiniTest::Unit.autorun
10
+
11
+ class TestReaderParser < MiniTest::Unit::TestCase
12
+ def setup
13
+ @parser = Cheeba::Reader::Parser
14
+ @opt = Cheeba::Indicators.options
15
+ end
16
+
17
+ def test_parser_module
18
+ assert_kind_of Module, @parser
19
+ end
20
+
21
+ def test_parse_raise_indenterror
22
+ lin1 = " 7:7"
23
+ spc1 = 1
24
+ idt1 = 2
25
+ num1 = 88
26
+ assert_raises(Cheeba::Reader::Parser::IndentError) {
27
+ @parser.check_indent(lin1, spc1, idt1, num1)
28
+ }
29
+ end
30
+
31
+ def test_check_indent_no_raise_indenterror
32
+ lin1 = " 7:7"
33
+ spc1 = 2
34
+ idt1 = 2
35
+ num1 = 88
36
+ assert_nil @parser.check_indent(lin1, spc1, idt1, num1)
37
+ end
38
+
39
+ def test_parse_comment
40
+ line = "#dude"
41
+ phs = @parser.parse(line, @opt, 77)
42
+ exp_msg = "com".to_sym
43
+ exp_spc = 0
44
+ exp_key = ""
45
+ exp_val = "dude"
46
+ exp_ask = nil
47
+ exp_asv = nil
48
+ exp_opt = @opt
49
+ assert_equal exp_msg, phs[:msg]
50
+ assert_equal exp_spc, phs[:spc]
51
+ assert_equal exp_key, phs[:key]
52
+ assert_equal exp_val, phs[:val]
53
+ assert_equal exp_ask, phs[:ask]
54
+ assert_equal exp_asv, phs[:asv]
55
+ assert_equal exp_opt, phs[:opt]
56
+ end
57
+
58
+ # parsed line => [msg, spc, key, val, ask, asv, opt]
59
+ def test_results_to_hash
60
+ results = [1, " ", "3", "4", 5, 6]
61
+ phs = @parser.results_to_hash(results, 7)
62
+ exp_msg = 1
63
+ exp_spc = 2
64
+ exp_key = "3"
65
+ exp_val = "4"
66
+ exp_ask = 5
67
+ exp_asv = 6
68
+ exp_opt = 7
69
+ assert_equal exp_msg, phs[:msg]
70
+ assert_equal exp_spc, phs[:spc]
71
+ assert_equal exp_key, phs[:key]
72
+ assert_equal exp_val, phs[:val]
73
+ assert_equal exp_ask, phs[:ask]
74
+ assert_equal exp_asv, phs[:asv]
75
+ assert_equal exp_opt, phs[:opt]
76
+ end
77
+
78
+ def test_parse_line_malformed
79
+ line = "awesome"
80
+ phs = @parser.parse_line(line, @opt)
81
+ assert_equal :mal, phs[0]
82
+ assert_equal "awesome", phs[3]
83
+ end
84
+
85
+ def test_parse_line_blank
86
+ line1 = " "
87
+ line2 = ""
88
+ exp_phs1 = @parser.parse_line(line1, @opt)
89
+ exp_phs2 = @parser.parse_line(line2, @opt)
90
+ assert_equal :bla, exp_phs1[0]
91
+ assert_equal :bla, exp_phs2[0]
92
+ end
93
+
94
+ def test_parse_line_comment
95
+ line1 = " #awesome"
96
+ line2 = "# dude"
97
+ line3 = "###beer"
98
+ line4 = " # camaro"
99
+ exp_phs1 = @parser.parse_line(line1, @opt)
100
+ exp_phs2 = @parser.parse_line(line2, @opt)
101
+ exp_phs3 = @parser.parse_line(line3, @opt)
102
+ exp_phs4 = @parser.parse_line(line4, @opt)
103
+ assert_equal :com, exp_phs1[0]
104
+ assert_equal :com, exp_phs2[0]
105
+ assert_equal :com, exp_phs3[0]
106
+ assert_equal :com, exp_phs4[0]
107
+ assert_equal "awesome", exp_phs1.flatten[3]
108
+ assert_equal "dude", exp_phs2.flatten[3]
109
+ assert_equal "##beer", exp_phs3.flatten[3]
110
+ assert_equal "camaro", exp_phs4.flatten[3]
111
+ end
112
+
113
+ def test_parse_line_array
114
+ line1 = "- awesome"
115
+ line2 = " -dude"
116
+ line3 = " -- beer"
117
+ line4 = " - #camaro"
118
+ exp_phs1 = @parser.parse_line(line1, @opt)
119
+ exp_phs2 = @parser.parse_line(line2, @opt)
120
+ exp_phs3 = @parser.parse_line(line3, @opt)
121
+ exp_phs4 = @parser.parse_line(line4, @opt)
122
+ assert_equal :arr, exp_phs1[0]
123
+ assert_equal :arr, exp_phs2[0]
124
+ assert_equal :arr, exp_phs3[0]
125
+ assert_equal :arr, exp_phs4[0]
126
+ assert_equal "", exp_phs1.flatten[1]
127
+ assert_equal " ", exp_phs2.flatten[1]
128
+ assert_equal " ", exp_phs3.flatten[1]
129
+ assert_equal " ", exp_phs4.flatten[1]
130
+ assert_equal "awesome", exp_phs1.flatten[3]
131
+ assert_equal "dude", exp_phs2.flatten[3]
132
+ assert_equal "- beer", exp_phs3.flatten[3]
133
+ assert_equal "#camaro", exp_phs4.flatten[3]
134
+ end
135
+
136
+ def test_parse_line_hashkey
137
+ line1 = "awesome:"
138
+ line2 = " dude :"
139
+ line3 = " :beer:"
140
+ line4 = ' "camaro":'
141
+ exp_phs1 = @parser.parse_line(line1, @opt)
142
+ exp_phs2 = @parser.parse_line(line2, @opt)
143
+ exp_phs3 = @parser.parse_line(line3, @opt)
144
+ exp_phs4 = @parser.parse_line(line4, @opt)
145
+ assert_equal :hky, exp_phs1[0]
146
+ assert_equal :mal, exp_phs2[0]
147
+ assert_equal :hky, exp_phs3[0]
148
+ assert_equal :hky, exp_phs4[0]
149
+ assert_equal "", exp_phs1.flatten[1]
150
+ assert_equal " ", exp_phs3.flatten[1]
151
+ assert_equal " ", exp_phs4.flatten[1]
152
+ assert_equal "awesome", exp_phs1.flatten[2]
153
+ assert_equal ":beer", exp_phs3.flatten[2]
154
+ assert_equal '"camaro"', exp_phs4.flatten[2]
155
+ assert_equal nil, exp_phs1.flatten[4]
156
+ assert_equal true, exp_phs3.flatten[4]
157
+ assert_equal nil, exp_phs4.flatten[4]
158
+ end
159
+
160
+ def test_parse_line_hashpair
161
+ line1 = "awesome:dude"
162
+ line2 = " :dude::fekja"
163
+ line3 = " :beer:coke"
164
+ line4 = ' "camaro"::firebird'
165
+ exp_phs1 = @parser.parse_line(line1, @opt)
166
+ exp_phs2 = @parser.parse_line(line2, @opt)
167
+ exp_phs3 = @parser.parse_line(line3, @opt)
168
+ exp_phs4 = @parser.parse_line(line4, @opt)
169
+ assert_equal :hpr, exp_phs1[0]
170
+ assert_equal :hpr, exp_phs2[0]
171
+ assert_equal :hpr, exp_phs3[0]
172
+ assert_equal :hpr, exp_phs4[0]
173
+ assert_equal "", exp_phs1.flatten[1]
174
+ assert_equal " ", exp_phs2.flatten[1]
175
+ assert_equal " ", exp_phs3.flatten[1]
176
+ assert_equal " ", exp_phs4.flatten[1]
177
+ assert_equal "awesome", exp_phs1.flatten[2]
178
+ assert_equal ":dude", exp_phs2.flatten[2]
179
+ assert_equal ":beer", exp_phs3.flatten[2]
180
+ assert_equal '"camaro"', exp_phs4.flatten[2]
181
+ assert_equal nil, exp_phs1.flatten[4]
182
+ assert_equal true, exp_phs2.flatten[4]
183
+ assert_equal true, exp_phs3.flatten[4]
184
+ assert_equal nil, exp_phs4.flatten[4]
185
+ assert_equal nil, exp_phs1.flatten[5]
186
+ assert_equal true, exp_phs2.flatten[5]
187
+ assert_equal nil, exp_phs3.flatten[5]
188
+ assert_equal true, exp_phs4.flatten[5]
189
+ end
190
+ end
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'minitest/unit'
5
+ $: << 'lib' << 'test'
6
+ require 'cheeba/writer'
7
+ require 'cheeba/indicators'
8
+ MiniTest::Unit.autorun
9
+
10
+ class TestWriter < MiniTest::Unit::TestCase
11
+ def setup
12
+ @writer = Cheeba::Writer
13
+ @opt = Cheeba::Indicators.options
14
+ @files = "#{File.dirname(__FILE__)}/files"
15
+ @test = "#{@files}/test.cash"
16
+ end
17
+
18
+ def test_build_hash_one_node_depth
19
+ hsh = {1 => 1, 2 => 2, "awesome" => "awesome"}
20
+ exp = ["awesome: awesome", "1: 1", "2: 2"]
21
+ act = @writer.build(hsh, @opt)
22
+ assert_equal exp, act
23
+ end
24
+
25
+ def test_build_hash_multi_node_depth
26
+ hsh = {1 => {"a" => {2 => {"b" => {"awesome" => "awesome"}}}}, 3 => "dude"}
27
+ exp = ["1:", " a:", " 2:", " b:", " awesome: awesome", "3: dude"]
28
+ act = @writer.build(hsh, @opt)
29
+ assert_equal exp, act
30
+ end
31
+
32
+ def test_build_array_one_node_depth
33
+ arr = [1, 2, 3, "awesome"]
34
+ exp = ["- 1", "- 2", "- 3", "- awesome"]
35
+ act = @writer.build(arr, @opt)
36
+ assert_equal exp, act
37
+ end
38
+
39
+ def test_build_array_multi_node_depth
40
+ arr = [1, 2, 3, [["awesome", 4, 5], 6]]
41
+ exp = ["- 1", "- 2", "- 3", " - awesome",
42
+ " - 4", " - 5", " - 6"]
43
+ act = @writer.build(arr, @opt)
44
+ assert_equal exp, act
45
+ end
46
+
47
+ def test_build_array_multi_node_depth_yaml
48
+ arr = [1, 2, 3, [["awesome", 4, 5], 6]]
49
+ exp = ["- 1", "- 2", "- 3", "-", " -", " - awesome",
50
+ " - 4", " - 5", " - 6"]
51
+ opt = @opt.merge({:yaml => true})
52
+ act = @writer.build(arr, opt)
53
+ assert_equal exp, act
54
+ end
55
+
56
+ def test_build_mixed_multi_node_depth
57
+ arr = {1 => 2, 3 => {"awesome" => [["awesome", 4, 5], 6], 7 => 8}}
58
+ exp = ["1: 2", "3:", " awesome:", " - awesome",
59
+ " - 4", " - 5", " - 6", " 7: 8"]
60
+ act = @writer.build(arr, @opt)
61
+ assert_equal exp, act
62
+ end
63
+
64
+ def test_build_mixed_multi_node_depth_yaml
65
+ arr = {1 => 2, 3 => {"awesome" => [["awesome", 4, 5], 6], 7 => 8}}
66
+ exp = ["1: 2", "3:", " awesome:", " -", " - awesome",
67
+ " - 4", " - 5", " - 6", " 7: 8"]
68
+ opt = @opt.merge({:yaml => true})
69
+ act = @writer.build(arr, opt)
70
+ assert_equal exp, act
71
+ end
72
+
73
+ def test_build_mixed_multi_node_depth_docs
74
+ arr = {1 => {1 => 2}, 2 => {3 => {"awesome" => [["awesome", 4, 5], 6], 7 => 8}}}
75
+ exp = ["---", "1: 2", "---", "3:", " awesome:", " - awesome",
76
+ " - 4", " - 5", " - 6", " 7: 8"]
77
+ opt = @opt.merge({:docs => true})
78
+ act = @writer.build(arr, opt)
79
+ assert_equal exp, act
80
+ end
81
+
82
+ def test_build_string
83
+ str = "1\n2\n3\nawesome"
84
+ exp = ["- 1", "- 2", "- 3", "- awesome"]
85
+ act = @writer.build(str, @opt)
86
+ assert_equal exp, act
87
+ end
88
+
89
+ def test_write
90
+ hsh = {1 => 1, 2 => 2, 3 => 3}
91
+ exp = "1: 1\n2: 2\n3: 3\n"
92
+ File.delete(@test) if File.exist?(@test)
93
+ refute(File.exists?(@test), "dude!")
94
+ @writer.write(hsh, @test, @opt)
95
+ act = IO.read(@test)
96
+ assert(File.exists?(@test))
97
+ assert_equal exp, act
98
+ File.delete(@test)
99
+ end
100
+
101
+ def test_write_raise_invalidfilenameerror
102
+ assert_raises(Cheeba::Writer::InvalidFilenameError) {@writer.write([1,2,3], "/#####/beer.cash", @opt)}
103
+ end
104
+
105
+ def test_write_to_home
106
+ home = "~/write_to_home_test"
107
+ hsh = {1 => 1, 2 => 2, 3 => 3}
108
+ exp = "1: 1\n2: 2\n3: 3\n"
109
+ File.delete(home) if File.exist?(home)
110
+ refute(File.exists?(home), "dude!")
111
+ fizzle = @writer.write(hsh, home, @opt)
112
+ assert(File.exists?(fizzle))
113
+ act = IO.read(fizzle)
114
+ assert_equal exp, act
115
+ File.delete(fizzle)
116
+ end
117
+
118
+ def test_write_raise_emptyfilenameerror_on_nil
119
+ assert_raises(Cheeba::Writer::EmptyFilenameError) {@writer.write([1,2,3], nil, @opt)}
120
+ end
121
+
122
+ def test_write_raise_emptyfilenameerror
123
+ assert_raises(Cheeba::Writer::EmptyFilenameError) {@writer.write([1,2,3], "", @opt)}
124
+ end
125
+
126
+ def test_write_raise_invalidinputerror
127
+ assert_raises(Cheeba::Writer::InvalidInputError) {@writer.write(5, @test, @opt)}
128
+ end
129
+
130
+ def test_write_raise_emptyinputerror
131
+ assert_raises(Cheeba::Writer::EmptyInputError) {@writer.write("", @test, @opt)}
132
+ end
133
+ end
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'minitest/unit'
5
+ $: << 'lib' << 'test'
6
+ require 'cheeba/writer/builder'
7
+ require 'cheeba/indicators'
8
+ MiniTest::Unit.autorun
9
+
10
+ class TestWriterBuilder < MiniTest::Unit::TestCase
11
+ def setup
12
+ @builder = Cheeba::Writer::Builder
13
+ @opt = Cheeba::Indicators.options
14
+ end
15
+
16
+ def test_build_string
17
+ act_str = []
18
+ str = "awesome\nawesome\nawesome"
19
+ exp_str = ["- awesome", "- awesome", "- awesome"]
20
+ @builder.build(act_str, str, @opt)
21
+ assert_equal exp_str, act_str
22
+ end
23
+
24
+ def test_sym_to_str
25
+ exp1 = [":awesome", ":dude"]
26
+ exp2 = [":awesome", "dude"]
27
+ exp3 = ["awesome", ":dude"]
28
+ exp4 = ["awesome", "dude"]
29
+ exp5 = [":awesome", nil]
30
+ exp6 = ["awesome", nil]
31
+ act1 = @builder.sym_to_str("awesome".to_sym, "dude".to_sym)
32
+ act2 = @builder.sym_to_str("awesome".to_sym, "dude")
33
+ act3 = @builder.sym_to_str("awesome", "dude".to_sym)
34
+ act4 = @builder.sym_to_str("awesome", "dude")
35
+ act5 = @builder.sym_to_str("awesome".to_sym)
36
+ act6 = @builder.sym_to_str("awesome")
37
+ assert_equal act1, exp1
38
+ assert_equal act2, exp2
39
+ assert_equal act3, exp3
40
+ assert_equal act4, exp4
41
+ assert_equal act5, exp5
42
+ assert_equal act6, exp6
43
+ end
44
+
45
+ def test_build_hash
46
+ act_hsh = []
47
+ hsh = {"awesome" => "awesome"}
48
+ exp_hsh = ["awesome: awesome"]
49
+ @builder.build(act_hsh, hsh, @opt)
50
+ assert_equal exp_hsh, act_hsh
51
+ end
52
+
53
+ def test_build_hash_symbols
54
+ act_hsh = []
55
+ hsh = {:awesome => :awesome}
56
+ exp_hsh = [":awesome: :awesome"]
57
+ @builder.build(act_hsh, hsh, @opt)
58
+ assert_equal exp_hsh, act_hsh
59
+ end
60
+
61
+ def test_build_array
62
+ act_arr = []
63
+ arr = ["awesome", "awesome", "awesome"]
64
+ exp_arr = ["- awesome", "- awesome", "- awesome"]
65
+ @builder.build(act_arr, arr, @opt)
66
+ assert_equal exp_arr, act_arr
67
+ end
68
+
69
+ def test_string_to_array
70
+ act = []
71
+ str = "awesome\nawesome\nawesome"
72
+ exp = ["- awesome", "- awesome", "- awesome"]
73
+ @builder.string_to_array(act, str, @opt, " ")
74
+ assert_equal exp, act
75
+ end
76
+
77
+ def test_hash_to_array
78
+ act = []
79
+ hsh = {1 => 1, 2 => "awesome", "3" => "awesome"}
80
+ exp = ["1: 1", "2: awesome", "3: awesome"]
81
+ @builder.hash_to_array(act, hsh, @opt, " ")
82
+ assert_equal exp, act
83
+ end
84
+
85
+ def test_hash_to_array_doc
86
+ act = []
87
+ hsh = {1 => {1 => 1, 2 => "awesome", "3" => "awesome"}, 2 => {2 => 2}}
88
+ exp = ["---", "1: 1", "2: awesome", "3: awesome", "---", "2: 2"]
89
+ opt = @opt.merge({:docs => true})
90
+ @builder.hash_to_array(act, hsh, opt, " ")
91
+ assert_equal exp, act
92
+ end
93
+
94
+ def test_hash_to_array_nested
95
+ act = []
96
+ hsh = {1 => 1, 2 => "awesome", "3" => {1 => 1, 2 => 2, 3 => 3}}
97
+ exp = ["1: 1", "2: awesome", "3:", " 1: 1", " 2: 2", " 3: 3"]
98
+ @builder.hash_to_array(act, hsh, @opt, " ")
99
+ assert_equal exp, act
100
+ end
101
+
102
+ def test_array_to_lines
103
+ act = []
104
+ arr = [1, 2, "awesome"]
105
+ exp = ["- 1", "- 2", "- awesome"]
106
+ @builder.array_to_lines(act, arr, @opt, " ")
107
+ assert_equal exp, act
108
+ end
109
+
110
+ def test_array_to_lines_nested
111
+ act = []
112
+ arr = [1, 2, [1, 2, [3]]]
113
+ exp = ["- 1", "- 2", " - 1", " - 2", " - 3"]
114
+ @builder.array_to_lines(act, arr, @opt, " ")
115
+ assert_equal exp, act
116
+ end
117
+
118
+ def test_array_to_lines_nested_yaml
119
+ act = []
120
+ arr = [1, 2, [1, 2, [3]]]
121
+ exp = ["- 1", "- 2", "-", " - 1", " - 2", " -", " - 3"]
122
+ opt = @opt.merge({:yaml => true})
123
+ @builder.array_to_lines(act, arr, opt, " ")
124
+ assert_equal exp, act
125
+ end
126
+
127
+ def test_hash_to_lines
128
+ act = []
129
+ hsh = {1 => 1, 2 => "awesome", "3" => "awesome"}
130
+ exp = ["1: 1", "2: awesome", "3: awesome"]
131
+ @builder.hash_to_lines(act, hsh, hsh.keys, @opt, " ")
132
+ assert_equal exp, act
133
+ end
134
+
135
+ def test_hash_to_lines_nested_hash
136
+ act = []
137
+ hsh = {1 => 1, 2 => "awesome", "3" => {1 => 1, 2 => 2}}
138
+ exp = ["1: 1", "2: awesome", "3:", " 1: 1", " 2: 2"]
139
+ @builder.hash_to_lines(act, hsh, hsh.keys, @opt, " ")
140
+ assert_equal exp, act
141
+ end
142
+
143
+ def test_hash_to_lines_nested_array
144
+ act = []
145
+ hsh = {1 => 1, 2 => "awesome", "3" => [1, 2, 3]}
146
+ exp = ["1: 1", "2: awesome", "3:", " - 1", " - 2", " - 3"]
147
+ @builder.hash_to_lines(act, hsh, hsh.keys, @opt, " ")
148
+ assert_equal exp, act
149
+ end
150
+ end