br_documents 0.0.15 → 0.2.0
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.
- checksums.yaml +5 -5
- data/.github/workflows/config.yml +54 -0
- data/.gitignore +1 -1
- data/.rubocop.yml +65 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +100 -0
- data/README.md +58 -1
- data/Rakefile +3 -3
- data/br_documents.gemspec +23 -19
- data/lib/br_documents.rb +3 -3
- data/lib/br_documents/cnpj_cpf/cnpj.rb +2 -2
- data/lib/br_documents/cnpj_cpf/cpf.rb +2 -2
- data/lib/br_documents/cnpj_cpf/document.rb +9 -9
- data/lib/br_documents/cnpj_validator.rb +4 -4
- data/lib/br_documents/commons/mod11.rb +1 -2
- data/lib/br_documents/commons/mod14.rb +20 -0
- data/lib/br_documents/cpf_validator.rb +4 -4
- data/lib/br_documents/gtin/gtin.rb +28 -0
- data/lib/br_documents/gtin_validator.rb +8 -0
- data/lib/br_documents/ie/ac.rb +3 -2
- data/lib/br_documents/ie/al.rb +2 -1
- data/lib/br_documents/ie/am.rb +3 -2
- data/lib/br_documents/ie/ap.rb +20 -16
- data/lib/br_documents/ie/ba.rb +28 -25
- data/lib/br_documents/ie/base.rb +1 -1
- data/lib/br_documents/ie/ce.rb +3 -2
- data/lib/br_documents/ie/df.rb +3 -2
- data/lib/br_documents/ie/es.rb +2 -1
- data/lib/br_documents/ie/factory.rb +3 -3
- data/lib/br_documents/ie/go.rb +3 -2
- data/lib/br_documents/ie/ma.rb +2 -1
- data/lib/br_documents/ie/mg.rb +14 -17
- data/lib/br_documents/ie/ms.rb +2 -1
- data/lib/br_documents/ie/mt.rb +6 -5
- data/lib/br_documents/ie/pa.rb +3 -2
- data/lib/br_documents/ie/pattern1.rb +10 -7
- data/lib/br_documents/ie/pattern2.rb +9 -8
- data/lib/br_documents/ie/pb.rb +3 -2
- data/lib/br_documents/ie/pe.rb +10 -10
- data/lib/br_documents/ie/pi.rb +2 -1
- data/lib/br_documents/ie/pr.rb +3 -2
- data/lib/br_documents/ie/rj.rb +3 -2
- data/lib/br_documents/ie/rn.rb +19 -16
- data/lib/br_documents/ie/ro.rb +17 -14
- data/lib/br_documents/ie/rr.rb +4 -3
- data/lib/br_documents/ie/rs.rb +3 -2
- data/lib/br_documents/ie/sc.rb +3 -2
- data/lib/br_documents/ie/se.rb +3 -2
- data/lib/br_documents/ie/sp.rb +17 -14
- data/lib/br_documents/ie/to.rb +6 -5
- data/lib/br_documents/ie_validator.rb +16 -15
- data/lib/br_documents/suframa/suframa.rb +60 -0
- data/lib/br_documents/suframa_validator.rb +8 -0
- data/lib/br_documents/version.rb +1 -1
- data/spec/cnpj_cpf/cnpj_spec.rb +21 -21
- data/spec/cnpj_cpf/cpf_spec.rb +21 -21
- data/spec/cnpj_validator_spec.rb +12 -12
- data/spec/cpf_validator_spec.rb +12 -12
- data/spec/gtin/gtin_spec.rb +49 -0
- data/spec/gtin_validator_spec.rb +47 -0
- data/spec/ie/ac_spec.rb +16 -16
- data/spec/ie/al_spec.rb +14 -14
- data/spec/ie/am_spec.rb +16 -16
- data/spec/ie/ap_spec.rb +14 -14
- data/spec/ie/ba_spec.rb +28 -28
- data/spec/ie/ce_spec.rb +9 -9
- data/spec/ie/df_spec.rb +16 -16
- data/spec/ie/es_spec.rb +9 -9
- data/spec/ie/factory_spec.rb +88 -89
- data/spec/ie/go_spec.rb +16 -16
- data/spec/ie/ma_spec.rb +9 -9
- data/spec/ie/mg_spec.rb +16 -16
- data/spec/ie/ms_spec.rb +9 -9
- data/spec/ie/mt_spec.rb +17 -17
- data/spec/ie/pa_spec.rb +16 -16
- data/spec/ie/pb_spec.rb +9 -9
- data/spec/ie/pe_spec.rb +27 -27
- data/spec/ie/pi_spec.rb +9 -9
- data/spec/ie/pr_spec.rb +14 -14
- data/spec/ie/rj_spec.rb +16 -16
- data/spec/ie/rn_spec.rb +27 -27
- data/spec/ie/ro_spec.rb +27 -27
- data/spec/ie/rr_spec.rb +16 -16
- data/spec/ie/rs_spec.rb +16 -16
- data/spec/ie/sc_spec.rb +16 -16
- data/spec/ie/se_spec.rb +9 -9
- data/spec/ie/shared_examples_for_pattern1.rb +11 -11
- data/spec/ie/shared_examples_for_to_remove_all_masks.rb +7 -8
- data/spec/ie/sp_spec.rb +19 -19
- data/spec/ie/to_spec.rb +35 -35
- data/spec/ie_validator_spec.rb +43 -31
- data/spec/spec_helper.rb +9 -6
- data/spec/suframa/suframa_spec.rb +109 -0
- data/spec/suframa_validator_spec.rb +36 -0
- data/spec/support/utilities.rb +2 -2
- metadata +94 -25
- data/.travis.yml +0 -6
- data/circle.yml +0 -3
data/spec/ie/sp_spec.rb
CHANGED
@@ -1,45 +1,45 @@
|
|
1
|
-
require
|
2
|
-
require_relative
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'shared_examples_for_to_remove_all_masks'
|
3
3
|
|
4
4
|
describe BrDocuments::IE::SP do
|
5
|
-
describe
|
6
|
-
context
|
7
|
-
it
|
8
|
-
ie = described_class.new(
|
9
|
-
expect(ie.formatted).to eq
|
5
|
+
describe '#formatted' do
|
6
|
+
context 'when having 12 digits' do
|
7
|
+
it 'returns a formatted ie' do
|
8
|
+
ie = described_class.new('110042490114')
|
9
|
+
expect(ie.formatted).to eq '110.042.490.114'
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
context
|
14
|
-
it
|
15
|
-
ie = described_class.new(
|
16
|
-
expect(ie.formatted).to eq
|
13
|
+
context 'when having 13 digits' do
|
14
|
+
it 'returns a formatted ie' do
|
15
|
+
ie = described_class.new('P011004243002')
|
16
|
+
expect(ie.formatted).to eq 'P-01100424.3/002'
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
describe
|
22
|
-
it
|
23
|
-
[
|
21
|
+
describe '#valid?' do
|
22
|
+
it 'is invalid with malformed number' do
|
23
|
+
['36.9107.395114', '527.08212.8110'].each do |number|
|
24
24
|
ie = described_class.new(number)
|
25
25
|
expect(ie).to_not be_valid
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
30
|
-
[
|
29
|
+
it 'is invalid with invalid check number' do
|
30
|
+
['110042490113', '110.042.491.114', 'P-01100424.4/002'].each do |number|
|
31
31
|
ie = described_class.new(number)
|
32
32
|
expect(ie).to_not be_valid
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
37
|
-
[
|
36
|
+
it 'is valid with valid number' do
|
37
|
+
['110042490114', '110.042.490.114', 'P-01100424.3/002'].each do |number|
|
38
38
|
ie = described_class.new(number)
|
39
39
|
expect(ie).to be_valid
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
include_examples
|
43
|
+
include_examples 'for to remove all masks', 'P-01100424.3/002'
|
44
44
|
end
|
45
45
|
end
|
data/spec/ie/to_spec.rb
CHANGED
@@ -1,100 +1,100 @@
|
|
1
|
-
require
|
2
|
-
require_relative
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'shared_examples_for_to_remove_all_masks'
|
3
3
|
|
4
4
|
describe BrDocuments::IE::TO do
|
5
|
-
describe
|
6
|
-
context
|
7
|
-
subject { described_class.new(
|
5
|
+
describe '#formatted' do
|
6
|
+
context 'when having 9 digits' do
|
7
|
+
subject { described_class.new('123456789') }
|
8
8
|
|
9
|
-
it
|
10
|
-
expect(subject.formatted).to eq
|
9
|
+
it 'returns a formatted ie' do
|
10
|
+
expect(subject.formatted).to eq '12.345678-9'
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
context
|
15
|
-
subject { described_class.new(
|
14
|
+
context 'when having 11 digits' do
|
15
|
+
subject { described_class.new('12345678901') }
|
16
16
|
|
17
|
-
it
|
18
|
-
expect(subject.formatted).to eq
|
17
|
+
it 'returns a formatted ie' do
|
18
|
+
expect(subject.formatted).to eq '12.34.567890-1'
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
describe
|
24
|
-
it
|
25
|
-
[
|
23
|
+
describe '#valid?' do
|
24
|
+
it 'is invalid with malformed number' do
|
25
|
+
['2.903.593.362-3', '29.02.79268-45', '29.02.5AB420-5'].each do |number|
|
26
26
|
ie = described_class.new(number)
|
27
27
|
expect(ie).to_not be_valid
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
it
|
32
|
-
[
|
31
|
+
it 'is invalid with length different of 9 or 11' do
|
32
|
+
['12345678', '1234567890', '123456789012'].each do |number|
|
33
33
|
ie = described_class.new(number)
|
34
34
|
expect(ie).to_not be_valid
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
context
|
39
|
-
it
|
40
|
-
[
|
38
|
+
context 'when having 9 digits' do
|
39
|
+
it 'is invalid with invalid check number' do
|
40
|
+
['290227835', '294083153'].each do |number|
|
41
41
|
ie = described_class.new(number)
|
42
42
|
expect(ie).to_not be_valid
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
47
|
-
[
|
46
|
+
it 'is invalid with correct check number but invalid mask' do
|
47
|
+
['29-022783-6', '29.408315.4'].each do |number|
|
48
48
|
ie = described_class.new(number)
|
49
49
|
expect(ie).to_not be_valid
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
54
|
-
[
|
53
|
+
it 'is valid with valid number' do
|
54
|
+
['290227836', '294083154'].each do |number|
|
55
55
|
ie = described_class.new(number)
|
56
56
|
expect(ie).to be_valid
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
it
|
61
|
-
[
|
60
|
+
it 'is valid with valid masked number' do
|
61
|
+
['29.022783-6', '29.408315-4'].each do |number|
|
62
62
|
ie = described_class.new(number)
|
63
63
|
expect(ie).to be_valid
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
-
context
|
69
|
-
it
|
70
|
-
[
|
68
|
+
context 'when having 11 digits' do
|
69
|
+
it 'is invalid with invalid check number' do
|
70
|
+
['29023864406', '29023864409'].each do |number|
|
71
71
|
ie = described_class.new(number)
|
72
72
|
expect(ie).to_not be_valid
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
it
|
77
|
-
[
|
76
|
+
it 'is invalid with correct check number but invalid mask' do
|
77
|
+
['29-99.200143-6', '29.99.231741.7'].each do |number|
|
78
78
|
ie = described_class.new(number)
|
79
79
|
expect(ie).to_not be_valid
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
it
|
84
|
-
[
|
83
|
+
it 'is valid with valid number' do
|
84
|
+
['29992001436', '29992317417'].each do |number|
|
85
85
|
ie = described_class.new(number)
|
86
86
|
expect(ie).to be_valid
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
it
|
91
|
-
[
|
90
|
+
it 'is valid with valid masked number' do
|
91
|
+
['29.99.200143-6', '29.99.231741-7'].each do |number|
|
92
92
|
ie = described_class.new(number)
|
93
93
|
expect(ie).to be_valid
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
-
include_examples
|
98
|
+
include_examples 'for to remove all masks', '29.99.231741-7'
|
99
99
|
end
|
100
100
|
end
|
data/spec/ie_validator_spec.rb
CHANGED
@@ -1,73 +1,85 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe IeValidator do
|
4
|
-
let(:record) { double(
|
3
|
+
RSpec.describe IeValidator do
|
4
|
+
let(:record) { double('model') }
|
5
5
|
|
6
6
|
before do
|
7
|
-
allow(record).to receive(:uf) {
|
8
|
-
allow(record).to receive(:errors).and_return(
|
7
|
+
allow(record).to receive(:uf) { 'SC' }
|
8
|
+
allow(record).to receive(:errors).and_return({})
|
9
9
|
allow(record.errors).to receive(:messages).and_return({})
|
10
|
-
allow(record.errors).to receive(:add) do |
|
11
|
-
record.errors
|
10
|
+
allow(record.errors).to receive(:add) do |attribute, error|
|
11
|
+
record.errors[attribute] ||= []
|
12
|
+
record.errors[attribute] << error
|
13
|
+
record.errors.messages[attribute] = record.errors[attribute]
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
|
-
subject { IeValidator.new(attributes:
|
17
|
+
subject { IeValidator.new(attributes: 'ie', uf: 'uf') }
|
16
18
|
|
17
19
|
context "when IE's number is valid" do
|
18
|
-
before { subject.validate_each(record,
|
20
|
+
before { subject.validate_each(record, 'ie', '253667852') }
|
19
21
|
|
20
|
-
it
|
22
|
+
it 'does not add errors in model' do
|
21
23
|
expect(record.errors.messages).to be_empty
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
|
-
context
|
26
|
-
before { subject.validate_each(record,
|
27
|
+
context 'when IE is blank' do
|
28
|
+
before { subject.validate_each(record, 'ie', '') }
|
27
29
|
|
28
|
-
it
|
30
|
+
it 'does not add errors in model' do
|
29
31
|
expect(record.errors.messages).to be_empty
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
|
-
context
|
34
|
-
|
35
|
+
context 'when IE is invalid' do
|
36
|
+
it 'adds :invalid error in the model' do
|
37
|
+
subject.validate_each(record, 'ie', '253667853')
|
35
38
|
|
36
|
-
|
37
|
-
|
39
|
+
expect(record.errors.messages['ie']).to include(:invalid)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'does not override the previous message' do
|
43
|
+
record.errors.add('ie', 'another error message')
|
44
|
+
subject.validate_each(record, 'ie', '253667853')
|
45
|
+
|
46
|
+
expect(record.errors.messages['ie']).to include('another error message')
|
47
|
+
expect(record.errors.messages['ie']).to include(:invalid)
|
38
48
|
end
|
39
49
|
end
|
40
50
|
|
41
|
-
context
|
51
|
+
context 'when UF is invalid' do
|
42
52
|
before do
|
43
|
-
allow(record).to receive(:uf){
|
44
|
-
subject.validate_each(record,
|
53
|
+
allow(record).to receive(:uf) { 'XX' }
|
54
|
+
subject.validate_each(record, 'ie', '253667852')
|
45
55
|
end
|
46
56
|
|
47
|
-
it
|
48
|
-
expect(record.errors.messages[
|
57
|
+
it 'adds error in model' do
|
58
|
+
expect(record.errors.messages['ie']).to include t('validator.ie.uf.invalid')
|
49
59
|
end
|
50
60
|
end
|
51
61
|
|
52
|
-
context
|
62
|
+
context 'when cannot be found attribute uf in model' do
|
53
63
|
before do
|
54
64
|
allow(record).to receive(:uf).and_raise(NoMethodError)
|
55
|
-
subject.validate_each(record,
|
65
|
+
subject.validate_each(record, 'ie', '253667852')
|
56
66
|
end
|
57
67
|
|
58
|
-
it
|
68
|
+
it 'adds error in model' do
|
59
69
|
expect(record.errors.messages[:base]).to eql [
|
60
|
-
t(
|
70
|
+
t('validator.ie.uf.no_present', uf: 'uf')
|
71
|
+
]
|
61
72
|
end
|
62
73
|
end
|
63
74
|
|
64
|
-
context
|
65
|
-
subject { IeValidator.new(attributes:
|
66
|
-
before { subject.validate_each(record,
|
75
|
+
context 'when attribute uf is not defined' do
|
76
|
+
subject { IeValidator.new(attributes: 'ie') }
|
77
|
+
before { subject.validate_each(record, 'ie', '253667852') }
|
67
78
|
|
68
|
-
it
|
79
|
+
it 'adds error in model' do
|
69
80
|
expect(record.errors.messages[:base]).to eql [
|
70
|
-
t(
|
81
|
+
t('validator.ie.uf.no_configured')
|
82
|
+
]
|
71
83
|
end
|
72
84
|
end
|
73
85
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,19 +1,22 @@
|
|
1
|
-
require
|
1
|
+
require 'simplecov'
|
2
2
|
SimpleCov.start
|
3
3
|
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
5
|
Bundler.require(:default, :development)
|
6
6
|
|
7
|
-
require
|
7
|
+
require 'br_documents'
|
8
8
|
|
9
|
-
Dir[File.dirname(__FILE__)
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].sort.each { |f| require f }
|
10
10
|
|
11
|
-
I18n.default_locale =
|
11
|
+
I18n.default_locale = 'pt-BR'
|
12
12
|
|
13
13
|
RSpec.configure do |config|
|
14
14
|
config.expect_with :rspec do |c|
|
15
15
|
c.syntax = :expect
|
16
16
|
end
|
17
17
|
|
18
|
-
config.
|
18
|
+
config.filter_run focus: true
|
19
|
+
config.run_all_when_everything_filtered = true
|
20
|
+
|
21
|
+
config.order = 'random'
|
19
22
|
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BrDocuments::Suframa do
|
4
|
+
subject { described_class.new }
|
5
|
+
|
6
|
+
describe '#formatted' do
|
7
|
+
it 'returns the formatted number' do
|
8
|
+
value = '100698107'
|
9
|
+
|
10
|
+
subject = described_class.new(value)
|
11
|
+
|
12
|
+
expect(subject.formatted).to eql '10.0698.107'
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when number is invalid' do
|
16
|
+
it 'returns a empty string' do
|
17
|
+
value = '123'
|
18
|
+
|
19
|
+
subject = described_class.new(value)
|
20
|
+
|
21
|
+
expect(subject.formatted).to eql ''
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'is invalid if number is malformed' do
|
26
|
+
['121.23.4123', '5.50.309.012', '5.503.090.12'].each do |number|
|
27
|
+
suframa = BrDocuments::Suframa.new(number)
|
28
|
+
|
29
|
+
expect(suframa).to_not be_valid
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'is invalid with a number longer than 9' do
|
34
|
+
['1234567890', '12.3456.7890'].each do |number|
|
35
|
+
suframa = BrDocuments::Suframa.new(number)
|
36
|
+
|
37
|
+
expect(suframa).to_not be_valid
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'is invalid with a number shorter than 9' do
|
42
|
+
['12345678', '12.3456.78'].each do |number|
|
43
|
+
suframa = BrDocuments::Suframa.new(number)
|
44
|
+
|
45
|
+
expect(suframa).to_not be_valid
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'is invalid with a sequence of the same number' do
|
50
|
+
['11.1111.111', '111111111'].each do |number|
|
51
|
+
suframa = BrDocuments::Suframa.new(number)
|
52
|
+
|
53
|
+
expect(suframa).to_not be_valid
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'is invalid with invalid verifying digit' do
|
58
|
+
['55.0309.010', '550309010'].each do |number|
|
59
|
+
suframa = BrDocuments::Suframa.new(number)
|
60
|
+
|
61
|
+
expect(suframa).to_not be_valid
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'is valid with correct number' do
|
66
|
+
['11.1279.100', '10.1040.105', '100628109', '100698107'].each do |number|
|
67
|
+
suframa = BrDocuments::Suframa.new(number)
|
68
|
+
|
69
|
+
expect(suframa).to be_valid
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'activity sector' do
|
74
|
+
it 'is valid when activity_sector is in 01, 02, 10, 11, 20, 60' do
|
75
|
+
['11.1279.100', '10.1040.105'].each do |number|
|
76
|
+
suframa = BrDocuments::Suframa.new(number)
|
77
|
+
|
78
|
+
expect(suframa).to be_valid
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'is invalid when activity_sector is not in 01, 02, 10, 11, 20, 60' do
|
83
|
+
['17.1279.107', '141161108'].each do |number|
|
84
|
+
suframa = BrDocuments::Suframa.new(number)
|
85
|
+
|
86
|
+
expect(suframa).to_not be_valid
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'administration unit' do
|
92
|
+
it 'is valid when adm_unit is in 01, 10, 30' do
|
93
|
+
['11.1279.100', '101160100'].each do |number|
|
94
|
+
suframa = BrDocuments::Suframa.new(number)
|
95
|
+
|
96
|
+
expect(suframa).to be_valid
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'is invalid when adm_unit is not in 01, 10, 30' do
|
101
|
+
['11.1279.124', '101160160'].each do |number|
|
102
|
+
suframa = BrDocuments::Suframa.new(number)
|
103
|
+
|
104
|
+
expect(suframa).to_not be_valid
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|