csv2hash 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,103 +1,107 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Csv2hash::Validator::Mapping do
3
+ module Csv2hash
4
+ describe Validator::Mapping do
4
5
 
5
- let(:definition) do
6
- Csv2hash::Definition.new([ { position: [0,0], key: 'name' } ], Csv2hash::Definition::MAPPING).tap do |definition|
7
- definition.validate!
8
- definition.default!
6
+ let(:definition) do
7
+ Main.generate_definition :foo do
8
+ set_type { Definition::MAPPING }
9
+ mapping { cell position: [0,0], key: 'name' }
10
+ end.tap { |d| d.validate! ; d.default! }
9
11
  end
10
- end
11
12
 
12
- subject do
13
- Csv2hash::Main.new(definition, data_source, ignore_blank_line: false)
14
- end
13
+ subject do
14
+ Main.new(definition, data_source, ignore_blank_line: false)
15
+ end
15
16
 
16
- before do
17
- allow(subject).to receive(:break_on_failure) { true }
18
- end
17
+ before do
18
+ allow(subject).to receive(:break_on_failure) { true }
19
+ end
19
20
 
20
- context 'with valid data' do
21
- let(:data_source) { [ [ 'John Doe' ] ]}
22
- it { expect { subject.validate_data! }.to_not raise_error }
23
- end
21
+ context 'with valid data' do
22
+ let(:data_source) { [ [ 'John Doe' ] ]}
23
+ it { expect { subject.validate_data! }.to_not raise_error }
24
+ end
24
25
 
25
- context 'with invalid data' do
26
- let(:data_source) { [ [ ] ]}
27
- it { expect { subject.validate_data! }.to raise_error('undefined name on [0, 0]') }
28
- end
26
+ context 'with invalid data' do
27
+ let(:data_source) { [ [ ] ]}
28
+ it { expect { subject.validate_data! }.to raise_error('undefined name on [0, 0]') }
29
+ end
29
30
 
30
- context 'wihtout exception' do
31
- let(:data_source) { [ [ ] ] }
31
+ context 'wihtout exception' do
32
+ let(:data_source) { [ [ ] ] }
32
33
 
33
- before do
34
- allow(subject).to receive(:break_on_failure) { false }
35
- end
34
+ before do
35
+ allow(subject).to receive(:break_on_failure) { false }
36
+ end
36
37
 
37
- it { expect(subject.parse.errors.to_csv).to eql ",\"undefined name on [0, 0]\"\n" }
38
+ it { expect(subject.parse.errors.to_csv).to eql ",\"undefined name on [0, 0]\"\n" }
38
39
 
39
- context 'errors should be filled' do
40
- before { subject.parse }
41
- its(:errors) { should eql [{x: 0, y: 0, message: 'undefined name on [0, 0]', key: 'name'}] }
42
- end
40
+ context 'errors should be filled' do
41
+ before { subject.parse }
42
+ its(:errors) { should eql [{x: 0, y: 0, message: 'undefined name on [0, 0]', key: 'name'}] }
43
+ end
43
44
 
44
- context 'original csv + errors should be returned' do
45
- let(:definition) do
46
- Csv2hash::Definition.new(rules, Csv2hash::Definition::MAPPING).tap do |d|
47
- d.validate!; d.default!
45
+ context 'original csv + errors should be returned' do
46
+ let(:definition) do
47
+ Main.generate_definition :foo do
48
+ set_type { Definition::MAPPING }
49
+ mapping do
50
+ cell position: [0,0], key: 'agree', values: ['yes', 'no']
51
+ cell position: [1,0], key: 'agree', values: ['yes', 'no']
52
+ cell position: [2,0], key: 'agree', values: ['yes', 'no']
53
+ end
54
+ end.tap { |d| d.validate! ; d.default! }
48
55
  end
49
- end
50
- context 'string values' do
51
- let(:rules) do
52
- [
53
- { position: [0,0], key: 'agree', values: ['yes', 'no'] },
54
- { position: [1,0], key: 'agree', values: ['yes', 'no'] },
55
- { position: [2,0], key: 'agree', values: ['yes', 'no'] }
56
- ]
56
+ context 'string values' do
57
+ let(:data_source) { [ [ 'what?' ], [ 'yes', 'what?' ], [ 'yes', 'what?', 'no' ] ] }
58
+ it { expect(subject.parse.errors.to_csv).to eql(
59
+ "what?,\"agree not supported, please use one of [\"\"yes\"\", \"\"no\"\"]\"\n") }
57
60
  end
58
- let(:data_source) { [ [ 'what?' ], [ 'yes', 'what?' ], [ 'yes', 'what?', 'no' ] ] }
59
- it { expect(subject.parse.errors.to_csv).to eql(
60
- "what?,\"agree not supported, please use one of [\"\"yes\"\", \"\"no\"\"]\"\n") }
61
- end
62
- context 'range values' do
63
- let(:rules) do
64
- [
65
- { position: [0,0], key: 'score', values: 1..10 },
66
- { position: [1,0], key: 'score', values: 1..10 },
67
- { position: [2,0], key: 'score', values: 1..10 }
68
- ]
61
+ context 'range values' do
62
+ let(:definition) do
63
+ Main.generate_definition :foo do
64
+ set_type { Definition::MAPPING }
65
+ mapping do
66
+ cell position: [0,0], key: 'score', values: 1..10
67
+ cell position: [1,0], key: 'score', values: 1..10
68
+ cell position: [2,0], key: 'score', values: 1..10
69
+ end
70
+ end.tap { |d| d.validate! ; d.default! }
71
+ end
72
+ let(:data_source) { [ [ 12 ], [ 2, 12 ], [ 3, 12, 1 ] ] }
73
+ it { expect(subject.parse.errors.to_csv).to eql("12,\"score not supported, please use one of 1..10\"\n") }
69
74
  end
70
- let(:data_source) { [ [ 12 ], [ 2, 12 ], [ 3, 12, 1 ] ] }
71
- it { expect(subject.parse.errors.to_csv).to eql("12,\"score not supported, please use one of 1..10\"\n") }
72
75
  end
73
- end
74
76
 
75
- context 'with extra_validator' do
76
- let(:definition) do
77
- Csv2hash::Definition.new([
78
- { position: [0,0], key: 'name', extra_validator: DowncaseValidator.new,
79
- message: 'your data should be writting in downcase only'
80
- }
81
- ],
82
- Csv2hash::Definition::MAPPING).tap do |definition|
83
- definition.validate!; definition.default!
77
+ context 'with extra_validator' do
78
+ let(:definition) do
79
+ Main.generate_definition :foo do
80
+ set_type { Definition::MAPPING }
81
+ mapping do
82
+ cell position: [0,0], key: 'name', extra_validator: DowncaseValidator.new,
83
+ message: 'your data should be writting in downcase only'
84
+ end
85
+ end.tap { |d| d.validate! ; d.default! }
86
+ end
87
+ before { subject.parse }
88
+ context 'not valid data' do
89
+ let(:data_source) { [ [ 'Foo' ] ] }
90
+ its(:errors) do
91
+ should eql [{x: 0, y: 0, message: 'your data should be writting in downcase only', key: 'name'}]
92
+ end
93
+ end
94
+ context 'valid data' do
95
+ let(:data_source) { [ [ 'foo' ] ] }
96
+ its(:errors) { should be_empty }
84
97
  end
85
- end
86
- before { subject.parse }
87
- context 'not valid data' do
88
- let(:data_source) { [ [ 'Foo' ] ] }
89
- its(:errors) { should eql [{x: 0, y: 0, message: 'your data should be writting in downcase only', key: 'name'}]}
90
- end
91
- context 'valid data' do
92
- let(:data_source) { [ [ 'foo' ] ] }
93
- its(:errors) { should be_empty }
94
98
  end
95
99
  end
96
100
  end
97
- end
98
101
 
99
- class DowncaseValidator < Csv2hash::ExtraValidator
100
- def valid? rule, value
101
- !!(value.match /^[a-z]+$/)
102
+ class DowncaseValidator < ExtraValidator
103
+ def valid? rule, value
104
+ !!(value.match /^[a-z]+$/)
105
+ end
102
106
  end
103
107
  end
@@ -1,40 +1,41 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Csv2hash::Validator do
3
+ module Csv2hash
4
+ describe 'Validator' do
4
5
 
5
- let(:definition) do
6
- Csv2hash::Definition.new([ { position: [0,0], key: 'name' } ], Csv2hash::Definition::MAPPING).tap do |definition|
7
- definition.validate!
8
- definition.default!
9
- end
10
- end
6
+ describe '#message' do
7
+ let(:cell) { double(:cell, rules: rules) }
11
8
 
12
- describe '#message' do
13
- subject { Csv2hash::Main.new double('definition', type: Csv2hash::Definition::COLLECTION), nil }
9
+ subject do
10
+ Class.new do
11
+ include Validator
12
+ end.new
13
+ end
14
14
 
15
- context 'string value' do
16
- let(:rule) { { foo: 'bar', message: ':foo are value of foo key' } }
15
+ context 'string value' do
16
+ let(:rules) {{ foo: 'bar', message: ':foo are value of foo key' }}
17
17
 
18
- it 'substitue value of key' do
19
- expect(subject.send(:message, rule, nil, nil)).to eql 'bar are value of foo key'
18
+ it 'substitue value of key' do
19
+ expect(subject.send(:message, cell, nil, nil)).to eql 'bar are value of foo key'
20
+ end
20
21
  end
21
- end
22
22
 
23
- context 'array value' do
24
- let(:rule) { { foo: ['bar', 'zone'], message: ':foo are values of foo key' } }
23
+ context 'array value' do
24
+ let(:rules) { { foo: ['bar', 'zone'], message: ':foo are values of foo key' } }
25
25
 
26
- it 'substitue value of key' do
27
- expect(subject.send(:message, rule, nil, nil)).to eql '["bar", "zone"] are values of foo key'
26
+ it 'substitue value of key' do
27
+ expect(subject.send(:message, cell, nil, nil)).to eql '["bar", "zone"] are values of foo key'
28
+ end
28
29
  end
29
- end
30
30
 
31
- context 'with position' do
32
- let(:rule) { { message: 'value not found on :position' } }
31
+ context 'with position' do
32
+ let(:rules) { { message: 'value not found on :position' } }
33
33
 
34
- it 'substitue value of key' do
35
- expect(subject.send(:message, rule, 0, 2)).to eql 'value not found on [0, 2]'
34
+ it 'substitue value of key' do
35
+ expect(subject.send(:message, cell, 0, 2)).to eql 'value not found on [0, 2]'
36
+ end
36
37
  end
37
38
  end
38
- end
39
39
 
40
+ end
40
41
  end
@@ -1,3 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Csv2hash
3
+ describe Csv2hash do
4
+ let(:definition) { double('definition', type: nil) }
5
+ let(:data_source) { nil }
6
+
7
+ subject do
8
+ Csv2hash::Main.new(definition, data_source, ignore_blank_line: false)
9
+ end
10
+
11
+ context '...' do
12
+ specify do
13
+ expect(subject.options.fetch(:ignore_blank_line)).to eql(false)
14
+ end
15
+ end
16
+
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv2hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel AZEMAR
@@ -83,6 +83,7 @@ dependencies:
83
83
  description: DSL to map a CSV to a Ruby Hash
84
84
  email: joel.azemar@gmail.com
85
85
  executables:
86
+ - launch_irb
86
87
  - load_rvm
87
88
  extensions: []
88
89
  extra_rdoc_files: []
@@ -97,6 +98,7 @@ files:
97
98
  - README.md
98
99
  - Rakefile
99
100
  - ReleaseNotes.md
101
+ - bin/launch_irb
100
102
  - bin/load_rvm
101
103
  - coverage/.resultset.json.lock
102
104
  - csv2hash.gemspec
@@ -105,14 +107,17 @@ files:
105
107
  - lib/csv2hash/adapters/base.rb
106
108
  - lib/csv2hash/adapters/csv_adapter.rb
107
109
  - lib/csv2hash/adapters/memory_adapter.rb
110
+ - lib/csv2hash/cell.rb
108
111
  - lib/csv2hash/csv_array.rb
109
112
  - lib/csv2hash/data_wrapper.rb
110
113
  - lib/csv2hash/definition.rb
114
+ - lib/csv2hash/expectation.rb
111
115
  - lib/csv2hash/extra_validator.rb
112
116
  - lib/csv2hash/notifier.rb
113
117
  - lib/csv2hash/parser.rb
114
118
  - lib/csv2hash/parser/collection.rb
115
119
  - lib/csv2hash/parser/mapping.rb
120
+ - lib/csv2hash/registry.rb
116
121
  - lib/csv2hash/structure_validator.rb
117
122
  - lib/csv2hash/structure_validator/max_columns.rb
118
123
  - lib/csv2hash/structure_validator/min_columns.rb