awesome-cheeba 1.0.1
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.
- data/History.txt +6 -0
- data/Manifest.txt +96 -0
- data/README.txt +33 -0
- data/Rakefile +13 -0
- data/bin/cheeba +68 -0
- data/cheeba.gemspec +37 -0
- data/lib/cheeba.rb +50 -0
- data/lib/cheeba/defaults.rb +69 -0
- data/lib/cheeba/errors.rb +10 -0
- data/lib/cheeba/indicators.rb +25 -0
- data/lib/cheeba/reader.rb +33 -0
- data/lib/cheeba/reader/builder.rb +264 -0
- data/lib/cheeba/reader/format.rb +126 -0
- data/lib/cheeba/reader/node.rb +61 -0
- data/lib/cheeba/reader/parser.rb +64 -0
- data/lib/cheeba/version.rb +3 -0
- data/lib/cheeba/writer.rb +49 -0
- data/lib/cheeba/writer/builder.rb +95 -0
- data/test/files/arrays.cash +15 -0
- data/test/files/blank.cash +0 -0
- data/test/files/comments.cash +17 -0
- data/test/files/hashes.cash +26 -0
- data/test/files/malformed.cash +3 -0
- data/test/files/mixed.cash +12 -0
- data/test/files/split.cash +9 -0
- data/test/test_cheeba.rb +120 -0
- data/test/test_reader.rb +210 -0
- data/test/test_reader_builder.rb +345 -0
- data/test/test_reader_format.rb +316 -0
- data/test/test_reader_parser.rb +190 -0
- data/test/test_writer.rb +133 -0
- data/test/test_writer_builder.rb +150 -0
- metadata +102 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
module Cheeba
|
2
|
+
module Reader
|
3
|
+
module Parser
|
4
|
+
class IndentError < StandardError; end
|
5
|
+
|
6
|
+
def self.parse(line, opts, num)
|
7
|
+
results = self.parse_line(line, opts)
|
8
|
+
phs = self.results_to_hash(results, opts)
|
9
|
+
self.check_indent(line, phs[:spc], opts[:indent], num)
|
10
|
+
phs
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# invalid number of spaces, line num and truncated line for context
|
15
|
+
#
|
16
|
+
def self.check_indent(line, spc, idt, num)
|
17
|
+
raise IndentError, "#{num} #{line[1..50]}" if spc % idt != 0
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# parses line, returns array, dependent on Node module
|
22
|
+
#
|
23
|
+
def self.parse_line(line, opts)
|
24
|
+
chb = Cheeba::Reader::Node
|
25
|
+
spc = opts[:space]
|
26
|
+
hsh = opts[:hash]
|
27
|
+
sym = opts[:symbol]
|
28
|
+
dcs = opts[:doc_start]
|
29
|
+
dct = opts[:doc_term]
|
30
|
+
arr = opts[:array]
|
31
|
+
com = opts[:comment]
|
32
|
+
|
33
|
+
case line
|
34
|
+
when chb.left_match(arr, spc): [:arr, Regexp.last_match.captures]
|
35
|
+
when chb.left_match(com, spc): [:com, Regexp.last_match.captures]
|
36
|
+
when chb.hashkey_sym(hsh, sym, spc): [:hky, Regexp.last_match.captures, nil, true]
|
37
|
+
when chb.hashkey(hsh, spc): [:hky, Regexp.last_match.captures]
|
38
|
+
when chb.hashpair_sym_all(hsh, sym, spc): [:hpr, Regexp.last_match.captures, true, true]
|
39
|
+
when chb.hashpair_sym_key(hsh, sym, spc): [:hpr, Regexp.last_match.captures, true]
|
40
|
+
when chb.hashpair_sym_val(hsh, sym, spc): [:hpr, Regexp.last_match.captures, nil, true]
|
41
|
+
when chb.hashpair(hsh, sym, spc): [:hpr, Regexp.last_match.captures]
|
42
|
+
when /^\s*$/: [:bla]
|
43
|
+
when chb.document(dcs, spc): [:dcs]
|
44
|
+
when chb.document(dct, spc): [:dct]
|
45
|
+
else; [:mal, nil, nil, line]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# creates hash with array vals, includes options
|
51
|
+
#
|
52
|
+
def self.results_to_hash(results, opt)
|
53
|
+
msg, spc, key, val, ask, asv = results.flatten
|
54
|
+
{ :msg => msg,
|
55
|
+
:spc => (spc.nil? ? 0 : spc.length),
|
56
|
+
:key => key.to_s.sub(/^:/, ""),
|
57
|
+
:val => val.to_s.sub(/^:/, ""),
|
58
|
+
:ask => ask,
|
59
|
+
:asv => asv,
|
60
|
+
:opt => opt}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'cheeba/writer/builder'
|
2
|
+
|
3
|
+
module Cheeba
|
4
|
+
module Writer
|
5
|
+
class EmptyFilenameError < StandardError; end
|
6
|
+
class EmptyInputError < StandardError; end
|
7
|
+
class InvalidInputError < StandardError; end
|
8
|
+
class InvalidFilenameError < StandardError; end
|
9
|
+
#
|
10
|
+
# array, hash, or string to array of lines of .cash file
|
11
|
+
#
|
12
|
+
def self.build(object, options)
|
13
|
+
raise Cheeba::Writer::EmptyInputError if (object.is_a?(String) && object.strip.empty?)
|
14
|
+
raise Cheeba::Writer::InvalidInputError unless (object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash))
|
15
|
+
Cheeba::Writer::Builder.build([], object, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# array, hash, or string to file
|
20
|
+
#
|
21
|
+
def self.write(object, filename, options)
|
22
|
+
filename = "#{ENV['HOME']}/#{File.basename(filename.to_s)}" if (filename =~ /^~/)
|
23
|
+
raise Cheeba::Writer::EmptyInputError if (object.is_a?(String) && object.strip.empty?)
|
24
|
+
raise Cheeba::Writer::InvalidInputError unless (object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash))
|
25
|
+
raise Cheeba::Writer::EmptyFilenameError if File.basename(filename.to_s).strip.empty?
|
26
|
+
raise Cheeba::Writer::InvalidFilenameError unless File.exists?(File.dirname(filename))
|
27
|
+
File.open(filename, "w") do |file|
|
28
|
+
self.build(object, options).each {|line|
|
29
|
+
file.print("#{line}\n")
|
30
|
+
}
|
31
|
+
end
|
32
|
+
filename
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# write a cheeba dotfile to home dir
|
37
|
+
#
|
38
|
+
def self.dotfile(options, home)
|
39
|
+
filename = "#{home.chomp("/")}/.cheeba"
|
40
|
+
new_name = nil
|
41
|
+
if File.exists?(filename)
|
42
|
+
new_name = "#{filename}.#{Time.now.strftime("%Y%m%d%H%M%S")}"
|
43
|
+
File.rename(filename, new_name)
|
44
|
+
end
|
45
|
+
self.write(options, filename, Cheeba::Indicators.options)
|
46
|
+
new_name ? new_name : filename
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Cheeba
|
2
|
+
module Writer
|
3
|
+
module Builder
|
4
|
+
def self.build(arr, inp, opt)
|
5
|
+
spc = (opt[:space] * opt[:indent])
|
6
|
+
case
|
7
|
+
when inp.is_a?(Hash): self.hash_to_array(arr, inp, opt, spc)
|
8
|
+
when inp.is_a?(Array): self.array_to_lines(arr, inp, opt, spc)
|
9
|
+
when inp.is_a?(String): self.string_to_array(arr, inp, opt, spc)
|
10
|
+
end
|
11
|
+
arr
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.sym_to_str(x, y = nil)
|
15
|
+
a = x.is_a?(Symbol) ? x.inspect : x
|
16
|
+
b = (!y.nil? && y.is_a?(Symbol)) ? y.inspect : y
|
17
|
+
[a, b]
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.string_to_array(arr, inp, opt, spc)
|
21
|
+
self.array_to_lines(arr, inp.split(opt[:line_end]), opt, spc)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.hash_to_array(arr, inp, opt, spc)
|
25
|
+
inp.each do |key,val|
|
26
|
+
case
|
27
|
+
when val.is_a?(Hash)
|
28
|
+
key = self.sym_to_str(key)
|
29
|
+
arr << opt[:doc_start] if opt[:docs]
|
30
|
+
arr << "#{key}#{opt[:hash]}" unless opt[:docs]
|
31
|
+
self.hash_to_lines(arr, val, val.keys, opt, spc, (opt[:docs] ? 0 : 1))
|
32
|
+
when val.is_a?(Array)
|
33
|
+
arr << opt[:doc_start] if opt[:docs]
|
34
|
+
self.array_to_lines(arr, val, opt)
|
35
|
+
else
|
36
|
+
key, val = self.sym_to_str(key, val)
|
37
|
+
arr << "#{key}#{opt[:hash]} #{val}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# line is array, push each indice to array, assumes every indice is a string
|
44
|
+
#
|
45
|
+
def self.array_to_lines(arr, val, opt, spc, idt = nil)
|
46
|
+
idt = 0 if idt.nil?
|
47
|
+
ist = spc * idt
|
48
|
+
ind = opt[:array]
|
49
|
+
until val.empty? do
|
50
|
+
x = val.shift
|
51
|
+
case
|
52
|
+
when x.is_a?(Hash)
|
53
|
+
arr << "#{ist}#{ind}"
|
54
|
+
self.hash_to_lines(arr, x, x.keys, opt, spc, (idt + 1))
|
55
|
+
when x.is_a?(Array)
|
56
|
+
i = idt
|
57
|
+
arr << "#{ist}#{ind}" if opt[:yaml]
|
58
|
+
i += 1
|
59
|
+
self.array_to_lines(arr, x, opt, spc, i)
|
60
|
+
else
|
61
|
+
x = self.sym_to_str(x)
|
62
|
+
arr << "#{ist}#{ind} #{x}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# recursively add lines to array
|
69
|
+
#
|
70
|
+
def self.hash_to_lines(arr, hsh, kys, opt, spc, idt = nil)
|
71
|
+
idt = 0 if idt.nil?
|
72
|
+
while !kys.empty?
|
73
|
+
ist = spc * idt
|
74
|
+
key = kys.shift
|
75
|
+
val = hsh[key]
|
76
|
+
ind = opt[:hash]
|
77
|
+
case
|
78
|
+
when val.is_a?(Hash)
|
79
|
+
# push key into lines_array
|
80
|
+
arr << "#{ist}#{key}#{ind}"
|
81
|
+
# step indent
|
82
|
+
# call this again (the recursive part)
|
83
|
+
self.hash_to_lines(arr, val, val.keys, opt, spc, (idt + 1))
|
84
|
+
when val.is_a?(Array)
|
85
|
+
arr << "#{ist}#{key}#{ind}"
|
86
|
+
self.array_to_lines(arr, val, opt, spc, (idt + 1))
|
87
|
+
else
|
88
|
+
# if not Writer or Array, it must be String
|
89
|
+
arr << "#{ist}#{key}#{ind} #{val}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
grocery_list:
|
2
|
+
beer:
|
3
|
+
1:
|
4
|
+
model: 24 pack - Coors Lite
|
5
|
+
count: 1
|
6
|
+
meatsez:
|
7
|
+
1:
|
8
|
+
model: Spam
|
9
|
+
count: 5
|
10
|
+
2:
|
11
|
+
model: Log of ground beef
|
12
|
+
count: 1
|
13
|
+
cigarettes:
|
14
|
+
1:
|
15
|
+
model: Carton - Basic Ultra Menthol Box 100
|
16
|
+
count: 2
|
17
|
+
other:
|
18
|
+
1:
|
19
|
+
model: Economy-Size Pork & Beans
|
20
|
+
count: 2
|
21
|
+
2:
|
22
|
+
model: Jumbo Miracle Whip
|
23
|
+
count: 1
|
24
|
+
3:
|
25
|
+
model: White Wonder Bread
|
26
|
+
count: 2
|
data/test/test_cheeba.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
#!/usr/bin/env ruby -w
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'minitest/unit'
|
5
|
+
$: << 'lib' << 'test'
|
6
|
+
require 'cheeba'
|
7
|
+
MiniTest::Unit.autorun
|
8
|
+
|
9
|
+
class TestCheeba < MiniTest::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@files = "#{File.dirname(__FILE__)}/files"
|
12
|
+
@test = "#{@files}/test.cash"
|
13
|
+
@opt = Cheeba::Defaults.options.merge(Cheeba::Indicators.options)
|
14
|
+
@hom = ENV['HOME']
|
15
|
+
@dot = "#{@hom}/.cheeba"
|
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
|
+
" cigarettes:",
|
38
|
+
" 1:",
|
39
|
+
" count: 2",
|
40
|
+
" model: Carton - Basic Ultra Menthol Box 100",
|
41
|
+
" beer:",
|
42
|
+
" 1:",
|
43
|
+
" count: 1",
|
44
|
+
" model: 24 pack - Coors Lite",
|
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
|
+
end
|
56
|
+
|
57
|
+
def test_read_hash
|
58
|
+
act = Cheeba.read("#{@files}/hashes.cash")
|
59
|
+
assert_equal @hsh, act
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_read_hash_options_symbolize_keys
|
63
|
+
hsh = ":awesome: dude"
|
64
|
+
exp = {:awesome => "dude"}
|
65
|
+
act = Cheeba.read(hsh, {:symbolize_keys => true})
|
66
|
+
assert_equal exp, act
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_read_string
|
70
|
+
str = IO.read("#{@files}/hashes.cash")
|
71
|
+
act = Cheeba.read(str)
|
72
|
+
assert_equal @hsh, act
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_parse_hash
|
76
|
+
act = Cheeba.parse(@hsh)
|
77
|
+
assert_equal @exp, act
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_write
|
81
|
+
File.delete(@test) if File.exists?(@test)
|
82
|
+
refute(File.exists?(@test), "dude!")
|
83
|
+
Cheeba.write(@hsh, @test)
|
84
|
+
act = (IO.readlines(@test).map {|x| x.chomp})
|
85
|
+
assert(File.exists?(@test))
|
86
|
+
assert_equal @exp, act
|
87
|
+
File.delete(@test)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_dotfile
|
91
|
+
File.delete(@dot) if File.exists?(@dot)
|
92
|
+
refute(File.exists?(@dot), "dude!")
|
93
|
+
Cheeba.dotfile
|
94
|
+
assert(File.exists?(@dot))
|
95
|
+
exp_keys = Cheeba::Defaults.options
|
96
|
+
act_keys = Cheeba::Reader.read(@dot, @opt.merge({:symbolize_keys => true}))
|
97
|
+
assert_equal exp_keys, act_keys
|
98
|
+
File.delete(@dot)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_dotfile_exists
|
102
|
+
len = Dir.entries(@hom).length
|
103
|
+
mv_by_test = (File.exists?(@dot) ? "#{@dot}.mv_by_test" : "")
|
104
|
+
File.rename(@dot, mv_by_test) unless mv_by_test.empty?
|
105
|
+
refute(File.exists?(@dot), "dude!")
|
106
|
+
dot = Cheeba.dotfile
|
107
|
+
assert File.exists?(@dot)
|
108
|
+
assert File.exists?(dot)
|
109
|
+
assert_equal len, (Dir.entries(@hom).length - 1)
|
110
|
+
File.delete(@dot) if File.exists?(@dot)
|
111
|
+
File.delete(dot) if File.exists?(dot)
|
112
|
+
refute(File.exists?(@dot), "dude!")
|
113
|
+
refute(File.exists?(dot), "dude!")
|
114
|
+
File.rename(mv_by_test, @dot) if File.exists?(mv_by_test)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_read_raise_on_empty_string
|
118
|
+
assert_raises(Cheeba::Reader::EmptyInputError) {Cheeba.read("")}
|
119
|
+
end
|
120
|
+
end
|
data/test/test_reader.rb
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
#!/usr/bin/env ruby -w
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'minitest/unit'
|
5
|
+
$: << 'lib' << 'test'
|
6
|
+
require 'cheeba/reader'
|
7
|
+
require 'cheeba/indicators'
|
8
|
+
require 'cheeba/defaults'
|
9
|
+
MiniTest::Unit.autorun
|
10
|
+
|
11
|
+
class TestReader < MiniTest::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
@file_com = "test/files/comments.cash"
|
14
|
+
@file_arr = "test/files/arrays.cash"
|
15
|
+
@file_hsh = "test/files/hashes.cash"
|
16
|
+
@file_mix = "test/files/mixed.cash"
|
17
|
+
@file_mal = "test/files/malformed.cash"
|
18
|
+
@file_bla = "test/files/blank.cash"
|
19
|
+
@file_spl = "test/files/split.cash"
|
20
|
+
opt = Cheeba::Indicators.options
|
21
|
+
@opt = Cheeba::Defaults.options.merge(opt)
|
22
|
+
@reader = Cheeba::Reader
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_test_files_exists
|
26
|
+
assert File.exists?(@file_com)
|
27
|
+
assert File.exists?(@file_arr)
|
28
|
+
assert File.exists?(@file_hsh)
|
29
|
+
assert File.exists?(@file_mix)
|
30
|
+
assert File.exists?(@file_mal)
|
31
|
+
assert File.exists?(@file_bla)
|
32
|
+
assert File.exists?(@file_spl)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_parse_string
|
36
|
+
exp1 = {"awesome" => "dude"}
|
37
|
+
exp2 = {1 => {"awesome" => "dude"}, :lst => {1 => "1"}}
|
38
|
+
act1 = @reader.read("awesome: dude", @opt.merge({:list => false}))
|
39
|
+
act2 = @reader.read("awesome: dude", @opt.merge({:list => true}))
|
40
|
+
assert_equal exp1, act1
|
41
|
+
assert_equal exp2, act2
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_parse_empty_string_raise_emptystringerror
|
45
|
+
assert_raises(Cheeba::Reader::EmptyInputError) {@reader.read("", @opt)}
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_parse_blank_file_raise_emptyfileerror
|
49
|
+
assert_raises(Cheeba::Reader::EmptyFileError) {@reader.read(@file_bla, @opt)}
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_parse_blank_file_raise_invalidinputerror
|
53
|
+
assert_raises(Cheeba::Reader::InvalidInputError) {@reader.read({}, @opt)}
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_parse_split_raise_rootnodeerror
|
57
|
+
assert_raises(Cheeba::Reader::Builder::RootNodeError) {
|
58
|
+
@reader.read(@file_spl, @opt.merge({:list => true}))
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_parse_file_array
|
63
|
+
exp1 = ["", 1, 2, [["", "awesome", "awesome"]], 4, 5, 6, [7, [8], "dude"], ""]
|
64
|
+
exp2 = {1 => ["", 1, 2, [["", "awesome", "awesome"]], 4, 5, 6, [7, [8], "dude"], ""],
|
65
|
+
:lst=> {
|
66
|
+
1 => "1",
|
67
|
+
2 => "1",
|
68
|
+
3 => "1",
|
69
|
+
4 => "1",
|
70
|
+
5 => "1,3",
|
71
|
+
6 => "1,3,0",
|
72
|
+
7 => "1,3,0",
|
73
|
+
8 => "1,3,0",
|
74
|
+
9 => "1",
|
75
|
+
10 => "1",
|
76
|
+
11 => "1",
|
77
|
+
12 => "1,7",
|
78
|
+
13 => "1,7,1",
|
79
|
+
14 => "1,7",
|
80
|
+
15 => "1"}}
|
81
|
+
act1 = @reader.read(@file_arr, @opt.merge({:list => false}))
|
82
|
+
act2 = @reader.read(@file_arr, @opt.merge({:list => true}))
|
83
|
+
assert_equal exp1, act1
|
84
|
+
assert_equal exp2, act2
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_parse_file_hash
|
88
|
+
exp1 = {"grocery_list" =>
|
89
|
+
{"meatsez" =>
|
90
|
+
{1 => {"count" => 5, "model" => "Spam"},
|
91
|
+
2 => {"count" => 1, "model" => "Log of ground beef"}},
|
92
|
+
"beer" => {1 => {"count" => 1, "model" => "24 pack - Coors Lite"}},
|
93
|
+
"cigarettes" =>
|
94
|
+
{1 => {"count" => 2, "model" => "Carton - Basic Ultra Menthol Box 100"}},
|
95
|
+
"other" =>
|
96
|
+
{1 => {"count" => 2, "model" => "Economy-Size Pork & Beans"},
|
97
|
+
2 => {"count" => 1, "model" => "Jumbo Miracle Whip"},
|
98
|
+
3 => {"count" => 2, "model" => "White Wonder Bread"}}}}
|
99
|
+
exp2 = {1 => {"grocery_list" =>
|
100
|
+
{"meatsez" =>
|
101
|
+
{1 => {"count" => 5, "model" => "Spam"},
|
102
|
+
2 => {"count" => 1, "model" => "Log of ground beef"}},
|
103
|
+
"beer" => {1 => {"count" => 1, "model" => "24 pack - Coors Lite"}},
|
104
|
+
"cigarettes" =>
|
105
|
+
{1 => {"count" => 2, "model" => "Carton - Basic Ultra Menthol Box 100"}},
|
106
|
+
"other" =>
|
107
|
+
{1 => {"count" => 2, "model" => "Economy-Size Pork & Beans"},
|
108
|
+
2 => {"count" => 1, "model" => "Jumbo Miracle Whip"},
|
109
|
+
3 => {"count" => 2, "model" => "White Wonder Bread"}}}},
|
110
|
+
:lst =>
|
111
|
+
{1 => "1,grocery_list",
|
112
|
+
2 => "1,grocery_list,beer",
|
113
|
+
3 => "1,grocery_list,beer,1",
|
114
|
+
4 => "1,grocery_list,beer,1",
|
115
|
+
5 => "1,grocery_list,beer,1",
|
116
|
+
6 => "1,grocery_list,meatsez",
|
117
|
+
7 => "1,grocery_list,meatsez,1",
|
118
|
+
8 => "1,grocery_list,meatsez,1",
|
119
|
+
9 => "1,grocery_list,meatsez,1",
|
120
|
+
10 => "1,grocery_list,meatsez,2",
|
121
|
+
11 => "1,grocery_list,meatsez,2",
|
122
|
+
12 => "1,grocery_list,meatsez,2",
|
123
|
+
13 => "1,grocery_list,cigarettes",
|
124
|
+
14 => "1,grocery_list,cigarettes,1",
|
125
|
+
15 => "1,grocery_list,cigarettes,1",
|
126
|
+
16 => "1,grocery_list,cigarettes,1",
|
127
|
+
17 => "1,grocery_list,other",
|
128
|
+
18 => "1,grocery_list,other,1",
|
129
|
+
19 => "1,grocery_list,other,1",
|
130
|
+
20 => "1,grocery_list,other,1",
|
131
|
+
21 => "1,grocery_list,other,2",
|
132
|
+
22 => "1,grocery_list,other,2",
|
133
|
+
23 => "1,grocery_list,other,2",
|
134
|
+
24 => "1,grocery_list,other,3",
|
135
|
+
25 => "1,grocery_list,other,3",
|
136
|
+
26 => "1,grocery_list,other,3"}}
|
137
|
+
act1 = @reader.read(@file_hsh, @opt.merge({:list => false}))
|
138
|
+
act2 = @reader.read(@file_hsh, @opt.merge({:list => true}))
|
139
|
+
assert_equal exp1, act1
|
140
|
+
assert_equal exp2, act2
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_parse_file_mixed
|
144
|
+
exp1 = {1=>1,
|
145
|
+
2 => ["", "one", "two", {5=>{6=>6, 7=>7}, 8=>{"dude"=>"dude"}}, ""],
|
146
|
+
3 =>"awesome"}
|
147
|
+
exp2 = {1=>
|
148
|
+
{1=>1,
|
149
|
+
2=>["", "one", "two", {5=>{6=>6, 7=>7}, 8=>{"dude"=>"dude"}}, ""],
|
150
|
+
3=>"awesome"},
|
151
|
+
:lst => {
|
152
|
+
1 => "1",
|
153
|
+
2 => "1,2",
|
154
|
+
3 => "1,2",
|
155
|
+
4 => "1,2",
|
156
|
+
5 => "1,2",
|
157
|
+
6 => "1,2,3,5",
|
158
|
+
7 => "1,2,3,5",
|
159
|
+
8 => "1,2,3,5",
|
160
|
+
9 => "1,2,3,8",
|
161
|
+
10 => "1,2,3,8",
|
162
|
+
11 => "1,2",
|
163
|
+
12 => "1"}}
|
164
|
+
act1 = @reader.read(@file_mix, @opt.merge({:list => false}))
|
165
|
+
act2 = @reader.read(@file_mix, @opt.merge({:list => true}))
|
166
|
+
assert_equal exp1, act1
|
167
|
+
assert_equal exp2, act2
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_parse_file_comments
|
171
|
+
exp1 = {1=>1, 2=>2, 3=>3, 4=>[1, 2, 3]}
|
172
|
+
exp2 = {1=>{1=>1, 2=>2, 3=>3, 4=>[1, 2, 3]},
|
173
|
+
:lst => {
|
174
|
+
1 => "#COMMENT: dude",
|
175
|
+
2 => "1",
|
176
|
+
3 => "#COMMENT: awesome",
|
177
|
+
4 => "1",
|
178
|
+
5 => "#COMMENT: ",
|
179
|
+
6 => "#COMMENT: camaro",
|
180
|
+
7 => "#COMMENT: ",
|
181
|
+
8 => "1",
|
182
|
+
9 => "#COMMENT: #",
|
183
|
+
10 => "#COMMENT: nice",
|
184
|
+
11 => "#COMMENT: one",
|
185
|
+
12 => "#COMMENT: bro!",
|
186
|
+
13 => "#COMMENT: ",
|
187
|
+
14 => "1,4",
|
188
|
+
15 => "1,4",
|
189
|
+
16 => "1,4",
|
190
|
+
17 => "1,4"}}
|
191
|
+
act1 = @reader.read(@file_com, @opt.merge({:list => false}))
|
192
|
+
act2 = @reader.read(@file_com, @opt.merge({:list => true}))
|
193
|
+
assert_equal exp1, act1
|
194
|
+
assert_equal exp2, act2
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_parse_file_malformed
|
198
|
+
exp1 = {}
|
199
|
+
exp2 = {
|
200
|
+
1=>{},
|
201
|
+
:lst => {
|
202
|
+
1 => "#MALFORMED: 2341234",
|
203
|
+
2 => "#MALFORMED: awesome",
|
204
|
+
3 => "#MALFORMED: dude"}}
|
205
|
+
act1 = @reader.read(@file_mal, @opt.merge({:list => false}))
|
206
|
+
act2 = @reader.read(@file_mal, @opt.merge({:list => true}))
|
207
|
+
assert_equal exp1, act1
|
208
|
+
assert_equal exp2, act2
|
209
|
+
end
|
210
|
+
end
|