parse_me 0.0.2

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.
@@ -0,0 +1,389 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/parse_me"
3
+
4
+ describe 'FixedWidthParser' do
5
+ subject {ParseMe::FixedWidthParser}
6
+
7
+ describe 'InputSplitting' do
8
+ subject {ParseMe::InputSplitting}
9
+
10
+ it 'should be defined' do
11
+ should be
12
+ end
13
+
14
+ describe 'Fixed' do
15
+ subject {ParseMe::InputSplitting::Fixed}
16
+
17
+ before :each do
18
+ class Dummy; include ParseMe::InputSplitting::Fixed; end
19
+ @dummy = Dummy.new
20
+
21
+ subject.public_instance_methods.each do |pim|
22
+ subject.module_eval do
23
+ module_function pim
24
+ public pim
25
+ end
26
+ end
27
+ end
28
+
29
+ it 'should be defined' do
30
+ should be
31
+ end
32
+
33
+ describe 'parse' do
34
+
35
+ it 'should define the parse method' do
36
+ subject.method(:parse).should be
37
+ end
38
+
39
+ describe 'test case 1' do
40
+ before :each do
41
+ @source = 'Lorem Ipsum Dolor sit amet Foo Bar '
42
+ @rules = {
43
+ name: {required: true, length: 15},
44
+ last_name: {required: true, length: 20}
45
+ }
46
+ test_rules = @rules
47
+ test_source = @source
48
+ @dummy.instance_eval do
49
+ @source = test_source
50
+ @rules = test_rules
51
+ @options = {}
52
+ end
53
+ end
54
+
55
+ it 'should have a 70 characters long source' do
56
+ @source.length.should eq(70)
57
+ end
58
+
59
+ it 'should properly get length rules' do
60
+ @dummy.get_length_rules.should eq([15,20])
61
+ end
62
+
63
+ it 'should acknowledge valid length rules' do
64
+ @dummy.valid_length_rules?.should be_true
65
+ end
66
+
67
+ it 'should have NO labeled rows' do
68
+ @dummy.labeled_rows?.should be_false
69
+ end
70
+
71
+ it 'should have a width of 35' do
72
+ @dummy.file_width.should eq(35)
73
+ end
74
+
75
+ it 'should properly split the input' do
76
+ @dummy.split_input.should eq([
77
+ 'Lorem Ipsum Dolor sit amet ',
78
+ 'Foo Bar '
79
+ ])
80
+ end
81
+
82
+ it 'should create a proper unpack pattern' do
83
+ @dummy.unpack_pattern.should eq('A15A20')
84
+ end
85
+
86
+ it 'should parse valid attributes' do
87
+ @dummy.attributes.should eq([:name, :last_name])
88
+ end
89
+
90
+ describe 'member' do
91
+ it 'should create a valid attributes hash for first string' do
92
+ @dummy.member(@dummy.split_input[0]).should eq({
93
+ name: 'Lorem Ipsum',
94
+ last_name: 'Dolor sit amet'
95
+ })
96
+ end
97
+
98
+ it 'should create a valid attributes hash for second string' do
99
+ @dummy.member(@dummy.split_input[1]).should eq({
100
+ name: 'Foo',
101
+ last_name: 'Bar'
102
+ })
103
+ end
104
+ end
105
+
106
+ describe 'collection' do
107
+ it 'should map a valid collection of members' do
108
+ @dummy.collection.should eq([
109
+ {
110
+ name: 'Lorem Ipsum',
111
+ last_name: 'Dolor sit amet'
112
+ },
113
+ {
114
+ name: 'Foo',
115
+ last_name: 'Bar'
116
+ }
117
+ ])
118
+ end
119
+ end
120
+ end
121
+
122
+ describe 'test case 2 (invalid length)' do
123
+ before :each do
124
+ @source = 'Lorem Ipsum Dolor sit amet Foo Bar '
125
+ @rules = {
126
+ name: {required: true},
127
+ last_name: {required: true, length: 20}
128
+ }
129
+ test_rules = @rules
130
+ @dummy.instance_eval do
131
+ @rules = test_rules
132
+ @options = {}
133
+ end
134
+ end
135
+
136
+ it 'should have a 70 characters long source' do
137
+ @source.length.should eq(70)
138
+ end
139
+
140
+ it 'should properly get length rules' do
141
+ @dummy.get_length_rules.should eq([nil,20])
142
+ end
143
+
144
+ it 'should NOT acknowledge valid length rules' do
145
+ @dummy.valid_length_rules?.should be_false
146
+ end
147
+
148
+ it 'should have NO labeled rows' do
149
+ @dummy.labeled_rows?.should be_false
150
+ end
151
+
152
+ it 'should have a width of 35' do
153
+ expect { @dummy.file_width }.to raise_error(Exception)
154
+ end
155
+
156
+ it 'should raise an exception when split is invoked' do
157
+ expect { @dummy.split_input }.to raise_error(Exception)
158
+ end
159
+
160
+ it 'should raise an exception when parse is invoked' do
161
+ expect { @dummy.parse(@source, @rules) }.to raise_error(Exception)
162
+ end
163
+
164
+ it 'should parse valid attributes' do
165
+ @dummy.attributes.should eq([:name, :last_name])
166
+ end
167
+ end
168
+
169
+ describe 'test case 3 (labeled rows)' do
170
+ before :each do
171
+ @source = '0Lorem Ipsum Dolor sit amet '
172
+ @source += '1Foo Bar Baz '
173
+ @rules = {
174
+ "0" => {
175
+ name: {required: true, length: 15},
176
+ last_name: {required: true, length: 20}
177
+ },
178
+ "1" => {
179
+ first: {required: true, length: 15},
180
+ second: {required: true, length: 10},
181
+ third: {required: true, length: 10}
182
+ }
183
+ }
184
+ test_rules = @rules
185
+ test_source = @source
186
+ @dummy.instance_eval do
187
+ @source = test_source
188
+ @rules = test_rules
189
+ @options = {labeled_rows: true}
190
+ end
191
+ end
192
+
193
+ it 'should have a 72 characters long source' do
194
+ @source.length.should eq(72)
195
+ end
196
+
197
+ it 'should properly get length rules' do
198
+ @dummy.get_length_rules.should eq([15,20,15,10,10])
199
+ end
200
+
201
+ it 'should acknowledge valid length rules' do
202
+ @dummy.valid_length_rules?.should be
203
+ end
204
+
205
+ it 'should have labeled rows' do
206
+ @dummy.labeled_rows?.should be
207
+ end
208
+
209
+ it 'should have labels 0 and 1' do
210
+ @dummy.labels.should eq(['0','1'])
211
+ end
212
+
213
+ it 'should have label_lengths equal to 1 and 1' do
214
+ @dummy.label_lengths.should eq([1,1])
215
+ end
216
+
217
+ it 'should have label_length equal to 1' do
218
+ @dummy.label_length.should eq(1)
219
+ end
220
+
221
+ it 'should have a width of 36' do
222
+ @dummy.file_width.should eq(36)
223
+ end
224
+
225
+ it 'should properly split the input' do
226
+ @dummy.split_input.should eq([
227
+ '0Lorem Ipsum Dolor sit amet ',
228
+ '1Foo Bar Baz '
229
+ ])
230
+ end
231
+
232
+ it 'should create a proper unpack pattern for label "0"' do
233
+ @dummy.unpack_pattern('0').should eq('A1A15A20')
234
+ end
235
+
236
+ it 'should create a proper unpack pattern for label "1"' do
237
+ @dummy.unpack_pattern('1').should eq('A1A15A10A10')
238
+ end
239
+
240
+ describe 'label_for' do
241
+ it 'should fetch the label of the first string' do
242
+ @dummy.label_for(@dummy.split_input[0]).should eq('0')
243
+ end
244
+
245
+ it 'should fetch the label of the second string' do
246
+ @dummy.label_for(@dummy.split_input[1]).should eq('1')
247
+ end
248
+ end
249
+
250
+ describe 'labeled_member' do
251
+ it 'should create a proper attributes hash for first string' do
252
+ str = @dummy.split_input[0]
253
+ label = @dummy.label_for str
254
+ @dummy.labeled_member(str, label).should eq({
255
+ name: 'Lorem Ipsum',
256
+ last_name: 'Dolor sit amet',
257
+ __label: '0'
258
+ })
259
+ end
260
+
261
+ it 'should create a proper attributes hash for second string' do
262
+ str = @dummy.split_input[1]
263
+ label = @dummy.label_for str
264
+ @dummy.labeled_member(str, label).should eq({
265
+ first: 'Foo',
266
+ second: 'Bar',
267
+ third: 'Baz',
268
+ __label: '1'
269
+ })
270
+ end
271
+ end
272
+
273
+ describe 'attributes' do
274
+ it 'should parse valid attributes for first label' do
275
+ @dummy.attributes('0').should eq([:name, :last_name])
276
+ end
277
+
278
+ it 'should parse valid attributes for second label' do
279
+ @dummy.attributes('1').should eq([:first, :second, :third])
280
+ end
281
+ end
282
+
283
+ describe 'collection' do
284
+ it 'should map a valid collection of members' do
285
+ @dummy.collection.should eq([
286
+ {
287
+ name: 'Lorem Ipsum',
288
+ last_name: 'Dolor sit amet',
289
+ __label: '0'
290
+ },
291
+ {
292
+ first: 'Foo',
293
+ second: 'Bar',
294
+ third: 'Baz',
295
+ __label: '1'
296
+ }
297
+ ])
298
+ end
299
+ end
300
+
301
+ end
302
+
303
+ describe 'test case 4' do
304
+ before :each do
305
+ @source = "Lorem Ipsum Dolor sit amet \nFoo Bar \n"
306
+ @rules = {
307
+ name: {required: true, length: 15},
308
+ last_name: {required: true, length: 20}
309
+ }
310
+ test_rules = @rules
311
+ test_source = @source
312
+ @dummy.instance_eval do
313
+ @source = test_source
314
+ @rules = test_rules
315
+ @options = {}
316
+ end
317
+ end
318
+
319
+ it 'should have a 72 characters long source' do
320
+ @source.length.should eq(72)
321
+ end
322
+
323
+ it 'should properly get length rules' do
324
+ @dummy.get_length_rules.should eq([15,20])
325
+ end
326
+
327
+ it 'should acknowledge valid length rules' do
328
+ @dummy.valid_length_rules?.should be_true
329
+ end
330
+
331
+ it 'should have NO labeled rows' do
332
+ @dummy.labeled_rows?.should be_false
333
+ end
334
+
335
+ it 'should have a width of 35' do
336
+ @dummy.file_width.should eq(35)
337
+ end
338
+
339
+ it 'should properly split the input' do
340
+ @dummy.split_input.should eq([
341
+ 'Lorem Ipsum Dolor sit amet ',
342
+ 'Foo Bar '
343
+ ])
344
+ end
345
+
346
+ it 'should create a proper unpack pattern' do
347
+ @dummy.unpack_pattern.should eq('A15A20')
348
+ end
349
+
350
+ it 'should parse valid attributes' do
351
+ @dummy.attributes.should eq([:name, :last_name])
352
+ end
353
+
354
+ describe 'member' do
355
+ it 'should create a valid attributes hash for first string' do
356
+ @dummy.member(@dummy.split_input[0]).should eq({
357
+ name: 'Lorem Ipsum',
358
+ last_name: 'Dolor sit amet'
359
+ })
360
+ end
361
+
362
+ it 'should create a valid attributes hash for second string' do
363
+ @dummy.member(@dummy.split_input[1]).should eq({
364
+ name: 'Foo',
365
+ last_name: 'Bar'
366
+ })
367
+ end
368
+ end
369
+
370
+ describe 'collection' do
371
+ it 'should map a valid collection of members' do
372
+ @dummy.collection.should eq([
373
+ {
374
+ name: 'Lorem Ipsum',
375
+ last_name: 'Dolor sit amet'
376
+ },
377
+ {
378
+ name: 'Foo',
379
+ last_name: 'Bar'
380
+ }
381
+ ])
382
+ end
383
+ end
384
+ end
385
+ end
386
+ end
387
+
388
+ end
389
+ end
@@ -0,0 +1,35 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/parse_me"
3
+
4
+ describe 'FixedWidthParser' do
5
+ subject {ParseMe::FixedWidthParser}
6
+
7
+ describe 'Test case 1' do
8
+ before :each do
9
+ @source = 'Lorem Ipsum Dolor sit amet Foo Bar '
10
+ @rules = {
11
+ name: {required: true, length: 15},
12
+ last_name: {required: true, length: 20}
13
+ }
14
+ @attributes = [
15
+ {name: 'Lorem Ipsum', last_name: 'Dolor sit amet'},
16
+ {name: 'Foo', last_name: 'Bar'}
17
+ ]
18
+ @result = subject.parse(@source, @rules)
19
+ end
20
+
21
+ it 'should return an array' do
22
+ @result.class.should eq(Array)
23
+ end
24
+
25
+ it 'should properly parse attributes for record with index 0' do
26
+ @result[0].name.should eq(@attributes[0][:name])
27
+ @result[0].last_name.should eq(@attributes[0][:last_name])
28
+ end
29
+
30
+ it 'should properly parse attributes for record with index 1' do
31
+ @result[1].name.should eq(@attributes[1][:name])
32
+ @result[1].last_name.should eq(@attributes[1][:last_name])
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/parse_me"
3
+
4
+ describe ParseMe do
5
+ it 'should be defined' do
6
+ should be
7
+ end
8
+
9
+ it 'should define FixedWidthParser' do
10
+ ParseMe::FixedWidthParser.should be
11
+ end
12
+
13
+ it 'should define VarWidthParser' do
14
+ ParseMe::VarWidthParser.should be
15
+ end
16
+
17
+ it 'should define ParsedObject' do
18
+ ParseMe::ParsedObject.should be
19
+ end
20
+ end
@@ -0,0 +1,165 @@
1
+ require './spec/spec_helper'
2
+ require './lib/parse_me'
3
+
4
+ describe 'ParsedObject' do
5
+ subject{ParseMe::ParsedObject}
6
+
7
+ it 'should be defined' do
8
+ should be
9
+ end
10
+
11
+ it 'should create an instance properly' do
12
+ po = subject.new({})
13
+ po.should be
14
+ end
15
+
16
+ describe 'attribute reading' do
17
+ before :each do
18
+ attrs = {
19
+ name: 'Foo',
20
+ last_name: 'Bar',
21
+ age: 15
22
+ }
23
+ @po = subject.new attrs
24
+ end
25
+
26
+ it 'should be able to read an attribute' do
27
+ @po.name.should eq('Foo')
28
+ end
29
+
30
+ it 'should raise NoMethodError when reading an undefined attribute' do
31
+ expect{@po.foo}.to raise_error(NoMethodError)
32
+ end
33
+ end
34
+
35
+ describe 'not labeled rows' do
36
+ before :each do
37
+ @source = 'Lorem Ipsum Dolor sit amet 1050000'
38
+ @source += 'Foo Bar Baz 1234567'
39
+ @source += 'John Doe Dong 34000'
40
+ @rules = {
41
+ first: {required: true, length: 15},
42
+ second: {required: true, length: 10},
43
+ third: {required: true, length: 10},
44
+ income: {required: true, length: 7, ghost_decimal_scale: 2}
45
+ }
46
+ @parsed = ParseMe::FixedWidthParser.new.parse(@source, @rules)
47
+ end
48
+
49
+ it 'should return a 3 elements long Array' do
50
+ @parsed.length.should eq(3)
51
+ end
52
+
53
+ it 'should have valid records(0)' do
54
+ @parsed[0].valid?.should be(true)
55
+ end
56
+
57
+ it 'should have valid records(1)' do
58
+ @parsed[1].valid?.should be(true)
59
+ end
60
+
61
+ it 'should have valid records(2)' do
62
+ @parsed[2].valid?.should be(true)
63
+ end
64
+
65
+ it 'should have no errors(0)' do
66
+ @parsed[0].errors.empty?.should be(true)
67
+ end
68
+
69
+ it 'should have no errors(1)' do
70
+ @parsed[1].errors.empty?.should be(true)
71
+ end
72
+
73
+ it 'should have no errors(2)' do
74
+ @parsed[2].errors.empty?.should be(true)
75
+ end
76
+
77
+ describe 'income attribute' do
78
+ it 'should properly transform the income attribute to 10500' do
79
+ @parsed[0].income.should eq(10500)
80
+ end
81
+
82
+ it 'should properly transform the income attribute to 12345.67' do
83
+ @parsed[1].income.should eq(12345.67)
84
+ end
85
+
86
+ it 'should properly transform the income attribute to 340' do
87
+ @parsed[2].income.should eq(340)
88
+ end
89
+ end
90
+
91
+ describe 'invalid record' do
92
+ before :each do
93
+ @inv_source = 'Lorem Ipsum Dolor sit amet 1050000'
94
+ @inv_source += 'Foo Bar Baz 1234567'
95
+ @inv_source += 'John Doe 34000'
96
+ @inv = ParseMe::FixedWidthParser.new.parse(@inv_source, @rules)
97
+ end
98
+
99
+ it "should have it's fourth record invalid" do
100
+ @inv[2].valid?.should be(false)
101
+ end
102
+
103
+ it "should have an error" do
104
+ @inv[2].errors.length.should eq(1)
105
+ end
106
+ end
107
+ end
108
+
109
+ describe 'labeled rows' do
110
+ before :each do
111
+ @source = '0This is a header This is a desc. '
112
+ @source += '1Lorem Ipsum Dolor sit amet 1050000'
113
+ @source += '1Foo Bar Baz 1234567'
114
+ @source += '1John Doe Dong 0034000'
115
+ @rules = {
116
+ "0" => {
117
+ header: {required: true, length: 20},
118
+ description: {required: true, length: 15},
119
+ filler: {length: 7}
120
+ },
121
+ "1" => {
122
+ first: {required: true, length: 15},
123
+ second: {required: true, length: 10},
124
+ third: {required: true, length: 10},
125
+ income: {required: true, length: 7, ghost_decimal_scale: 2}
126
+ }
127
+ }
128
+ @parsed = ParseMe::FixedWidthParser.new.parse(@source, @rules, labeled_rows: true)
129
+ end
130
+
131
+ it 'should return a 4 elements long Array' do
132
+ @parsed.length.should eq(4)
133
+ end
134
+
135
+ describe 'header record' do
136
+ it 'should be labeled with "0"' do
137
+ @parsed[0].record_label.should eq("0")
138
+ end
139
+ end
140
+
141
+ describe 'body record' do
142
+ it 'should be labeled with "1"' do
143
+ @parsed[1].record_label.should eq("1")
144
+ end
145
+
146
+ it 'should be labeled with "1"' do
147
+ @parsed[2].record_label.should eq("1")
148
+ end
149
+ end
150
+
151
+ describe 'income attribute' do
152
+ it 'should properly transform the income attribute to 10500' do
153
+ @parsed[1].income.should eq(10500)
154
+ end
155
+
156
+ it 'should properly transform the income attribute to 12345.67' do
157
+ @parsed[2].income.should eq(12345.67)
158
+ end
159
+
160
+ it 'should properly transform the income attribute to 340' do
161
+ @parsed[3].income.should eq(340)
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,4 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ minimum_coverage 90
4
+ end