yamlr 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'minitest/unit'
5
+ $: << 'lib' << 'test'
6
+ require 'yamlr/writer'
7
+ require 'yamlr/indicators'
8
+ MiniTest::Unit.autorun
9
+
10
+ class TestWriter < MiniTest::Unit::TestCase
11
+ def setup
12
+ @writer = Yamlr::Writer
13
+ @opt = Yamlr::Indicators.options
14
+ @files = "#{File.dirname(__FILE__)}/files"
15
+ @test = "#{@files}/test.yml"
16
+ end
17
+
18
+ def test_build_hash_one_node_depth
19
+ hsh = {1 => 1, 2 => 2, "awesome" => "awesome"}
20
+ exp = ["1: 1", "2: 2", "awesome: awesome"]
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(Yamlr::Writer::InvalidFilenameError) {@writer.write([1,2,3], "/#####/beer.yml", @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(Yamlr::Writer::EmptyFilenameError) {@writer.write([1,2,3], nil, @opt)}
120
+ end
121
+
122
+ def test_write_raise_emptyfilenameerror
123
+ assert_raises(Yamlr::Writer::EmptyFilenameError) {@writer.write([1,2,3], "", @opt)}
124
+ end
125
+
126
+ def test_write_raise_invalidinputerror
127
+ assert_raises(Yamlr::Writer::InvalidInputError) {@writer.write(5, @test, @opt)}
128
+ end
129
+
130
+ def test_write_raise_emptyinputerror
131
+ assert_raises(Yamlr::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 'yamlr/writer/builder'
7
+ require 'yamlr/indicators'
8
+ MiniTest::Unit.autorun
9
+
10
+ class TestWriterBuilder < MiniTest::Unit::TestCase
11
+ def setup
12
+ @builder = Yamlr::Writer::Builder
13
+ @opt = Yamlr::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
@@ -0,0 +1,185 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'minitest/unit'
5
+ $: << 'lib' << 'test'
6
+ require 'yamlr'
7
+ MiniTest::Unit.autorun
8
+
9
+ class TestYamlr < MiniTest::Unit::TestCase
10
+ def setup
11
+ @files = "#{File.expand_path(File.dirname(__FILE__))}/files"
12
+ @test = "#{@files}/test.yml"
13
+ @opt = Yamlr::Defaults.options.merge(Yamlr::Indicators.options)
14
+ @hom = ENV['HOME']
15
+ @dot = "#{@hom}/.yamlrc"
16
+ @hsh = {
17
+ "grocery_list" => {
18
+ "meatsez" => {
19
+ 1 => {"count"=>5, "model"=>"Spam"},
20
+ 2 => {"count"=>1, "model"=>"Log of ground beef"}},
21
+ "beer"=>{1=>{"count"=>1, "model"=>"24 pack - Coors Lite"}},
22
+ "cigarettes" => {
23
+ 1 => {"count"=>2, "model"=>"Carton - Basic Ultra Menthol Box 100"}},
24
+ "other" => {
25
+ 1 => {"count"=>2, "model"=>"Economy-Size Pork & Beans"},
26
+ 2 => {"count"=>1, "model"=>"Jumbo Miracle Whip"},
27
+ 3 => {"count"=>2, "model"=>"White Wonder Bread"}}}}
28
+ @exp =
29
+ ["grocery_list:",
30
+ " meatsez:",
31
+ " 1:",
32
+ " count: 5",
33
+ " model: Spam",
34
+ " 2:",
35
+ " count: 1",
36
+ " model: Log of ground beef",
37
+ " beer:",
38
+ " 1:",
39
+ " count: 1",
40
+ " model: 24 pack - Coors Lite",
41
+ " cigarettes:",
42
+ " 1:",
43
+ " count: 2",
44
+ " model: Carton - Basic Ultra Menthol Box 100",
45
+ " other:",
46
+ " 1:",
47
+ " count: 2",
48
+ " model: Economy-Size Pork & Beans",
49
+ " 2:",
50
+ " count: 1",
51
+ " model: Jumbo Miracle Whip",
52
+ " 3:",
53
+ " count: 2",
54
+ " model: White Wonder Bread"]
55
+ @nested =<<FEKJA
56
+ domain: test
57
+ manifest:
58
+ manifest_id:
59
+ format: String
60
+ master_id:
61
+ format: String
62
+ owner_id:
63
+ format: String
64
+ log_id:
65
+ desc: log of all transactions withing the entire tree
66
+ format: String
67
+ FEKJA
68
+ end
69
+
70
+ def test_read_hash
71
+ act = Yamlr.read("#{@files}/hashes.yml")
72
+ assert_equal @hsh, act
73
+ end
74
+
75
+ def test_read_hash_options_symbolize_keys
76
+ hsh = ":awesome: dude"
77
+ exp = {:awesome => "dude"}
78
+ act = Yamlr.read(hsh, {:symbolize_keys => true})
79
+ assert_equal exp, act
80
+ end
81
+
82
+ def test_read_nested_hash_options_symbolize
83
+ exp = {
84
+ :domain => :test,
85
+ :manifest => {
86
+ :manifest_id => {
87
+ :format => :String},
88
+ :master_id => {
89
+ :format => :String},
90
+ :owner_id=>{
91
+ :format => :String},
92
+ :log_id => {
93
+ :desc => :"log of all transactions withing the entire tree",
94
+ :format => :String}}}
95
+ act = Yamlr.read(@nested, {:symbolize => true})
96
+ assert_equal exp, act
97
+ end
98
+
99
+ def test_read_nested_hash_options_symbolize_keys
100
+ exp = {
101
+ :domain=>"test",
102
+ :manifest=>{
103
+ :manifest_id=>{
104
+ :format => "String"},
105
+ :master_id=>{
106
+ :format => "String"},
107
+ :owner_id=>{
108
+ :format => "String"},
109
+ :log_id=>{
110
+ :desc=>"log of all transactions withing the entire tree",
111
+ :format => "String"}}}
112
+ act = Yamlr.read(@nested, {:symbolize_keys => true})
113
+ assert_equal exp, act
114
+ end
115
+
116
+ def test_read_string
117
+ str = IO.read("#{@files}/hashes.yml")
118
+ act = Yamlr.read(str)
119
+ assert_equal @hsh, act
120
+ end
121
+
122
+ def test_parse_hash
123
+ act = Yamlr.parse(@hsh)
124
+ assert_equal @exp, act
125
+ end
126
+
127
+ def test_write
128
+ File.delete(@test) if File.exists?(@test)
129
+ refute(File.exists?(@test), "dude!")
130
+ Yamlr.write(@hsh, @test)
131
+ act = (IO.readlines(@test).map {|x| x.chomp})
132
+ assert(File.exists?(@test))
133
+ assert_equal @exp, act
134
+ File.delete(@test)
135
+ end
136
+
137
+ def test_dotfile
138
+ File.delete(@dot) if File.exists?(@dot)
139
+ refute(File.exists?(@dot), "dude!")
140
+ Yamlr.dotfile
141
+ assert(File.exists?(@dot))
142
+ exp_keys = Yamlr::Defaults.options
143
+ act_keys = Yamlr::Reader.read(@dot, @opt.merge({:symbolize_keys => true}))
144
+ assert_equal exp_keys, act_keys
145
+ File.delete(@dot)
146
+ end
147
+
148
+ def test_dotfile_exists
149
+ len = Dir.entries(@hom).length
150
+ mv_by_test = (File.exists?(@dot) ? "#{@dot}.mv_by_test" : "")
151
+ File.rename(@dot, mv_by_test) unless mv_by_test.empty?
152
+ refute(File.exists?(@dot), "dude!")
153
+ dot = Yamlr.dotfile
154
+ assert File.exists?(@dot)
155
+ assert File.exists?(dot)
156
+ assert_equal len, (Dir.entries(@hom).length - 1)
157
+ File.delete(@dot) if File.exists?(@dot)
158
+ File.delete(dot) if File.exists?(dot)
159
+ refute(File.exists?(@dot), "dude!")
160
+ refute(File.exists?(dot), "dude!")
161
+ File.rename(mv_by_test, @dot) if File.exists?(mv_by_test)
162
+ end
163
+
164
+ def test_read_raise_on_empty_string
165
+ assert_raises(Yamlr::Reader::EmptyInputError) {Yamlr.read("")}
166
+ end
167
+
168
+ def test_format_sym_str_nested_2009yml
169
+ file = "#{@files}/2009.yml"
170
+ raise file.inspect unless File.exist?(file)
171
+ act = Yamlr.read(file, {:sym_str => true})
172
+ exp = {:content=>{1=>:"200901.yml", 2=>:"200902.yml"}}
173
+ assert_equal exp, act
174
+ end
175
+
176
+ # this is what was happening! because the "01".to_i # => 1
177
+ # {:content=>{"01"=>:"200901.yml", "02"=>:"200902.yml"}}
178
+ def test_format_sym_str_nested
179
+ file = "#{@files}/nested.yml"
180
+ raise file.inspect unless File.exist?(file)
181
+ act = Yamlr.read(file)
182
+ exp = {1 => "awesome", 2 => "dude", 3 => {1 => "niceone", 2 => "bro", "fekja" => "bacon", 3 => {1 => "beer", 2 => "camaro"}}}
183
+ assert_equal exp, act
184
+ end
185
+ end