br_documents 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/config.yml +54 -0
- data/.rubocop.yml +65 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +76 -42
- data/README.md +33 -0
- 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 +1 -2
- data/lib/br_documents/cpf_validator.rb +4 -4
- data/lib/br_documents/gtin/gtin.rb +1 -1
- data/lib/br_documents/gtin_validator.rb +6 -6
- 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 +16 -13
- 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 +7 -7
- 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 +2 -2
- data/spec/gtin_validator_spec.rb +13 -13
- 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 +7 -5
- data/spec/spec_helper.rb +6 -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 +86 -23
- data/.circleci/config.yml +0 -42
data/lib/br_documents/ie/sp.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative 'base'
|
2
|
+
require_relative '../commons/mod11'
|
3
3
|
|
4
4
|
module BrDocuments
|
5
5
|
module IE
|
6
6
|
class SP < Base
|
7
7
|
include Commons::Mod11
|
8
8
|
|
9
|
-
|
9
|
+
protected
|
10
|
+
|
10
11
|
def format_ie(number)
|
11
|
-
if number.gsub(/(\-)|(\.)|(\/)/,
|
12
|
-
number.sub(/(\w{1})(\d{8})(\d{1})(\d{3})/,
|
12
|
+
if number.gsub(/(\-)|(\.)|(\/)/, '').length == 13
|
13
|
+
number.sub(/(\w{1})(\d{8})(\d{1})(\d{3})/, '\\1-\\2.\\3/\\4')
|
13
14
|
else
|
14
|
-
number.sub(/(\d{3})(\d{3})(\d{3})(\d{3})/,
|
15
|
+
number.sub(/(\d{3})(\d{3})(\d{3})(\d{3})/, '\\1.\\2.\\3.\\4')
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
@@ -20,33 +21,35 @@ module BrDocuments
|
|
20
21
|
regex.match(@number).present?
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
-
if @number[0]
|
24
|
+
def valid_check_digit?
|
25
|
+
if @number[0] == 'P'
|
25
26
|
valid_rural_producer_digital_check?
|
26
27
|
else
|
27
28
|
valid_standard_digital_check?
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
32
|
+
private
|
33
|
+
|
31
34
|
def valid_standard_digital_check?
|
32
|
-
@number.gsub!(/[\.\/P-]/,
|
35
|
+
@number.gsub!(/[\.\/P-]/, '')
|
33
36
|
|
34
37
|
weight1 = [1, 3, 4, 5, 6, 7, 8, 10]
|
35
38
|
weight2 = [3, 2, 10, 9, 8, 7, 6, 5, 4, 3, 2]
|
36
39
|
|
37
|
-
dc1 =
|
38
|
-
dc2 =
|
40
|
+
dc1 = generate_check_digit(weight1)
|
41
|
+
dc2 = generate_check_digit(weight2)
|
39
42
|
|
40
43
|
(@number[8] == dc1) and (@number[11] == dc2)
|
41
44
|
end
|
42
45
|
|
43
46
|
def valid_rural_producer_digital_check?
|
44
|
-
@number.gsub!(/[\.\/P-]/,
|
47
|
+
@number.gsub!(/[\.\/P-]/, '')
|
45
48
|
weight9 = [1, 3, 4, 5, 6, 7, 8, 10]
|
46
|
-
@number[8] ==
|
49
|
+
@number[8] == generate_check_digit(weight9)
|
47
50
|
end
|
48
51
|
|
49
|
-
def
|
52
|
+
def generate_check_digit(weights)
|
50
53
|
sum = reduce_weights(@number, weights)
|
51
54
|
mod = sum % 11
|
52
55
|
mod.to_s[-1]
|
data/lib/br_documents/ie/to.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
5
5
|
class TO < Pattern1
|
6
6
|
def initialize(number)
|
7
7
|
super
|
8
|
-
if number.gsub(/(\.)|(\-)/,
|
8
|
+
if number.gsub(/(\.)|(\-)/, '').length == 9
|
9
9
|
@mask = /^(\d{2}\.\d{6}\-\d{1})$|^(\d{9})$/
|
10
10
|
@weight = [9, 8, 7, 6, 5, 4, 3, 2]
|
11
11
|
else
|
@@ -15,11 +15,12 @@ module BrDocuments
|
|
15
15
|
end
|
16
16
|
|
17
17
|
private
|
18
|
+
|
18
19
|
def format_ie(number)
|
19
|
-
if number.gsub(/(\.)|(\-)/,
|
20
|
-
number.sub(/(\d{2})(\d{6})(\d{1})/,
|
20
|
+
if number.gsub(/(\.)|(\-)/, '').length == 9
|
21
|
+
number.sub(/(\d{2})(\d{6})(\d{1})/, '\\1.\\2-\\3')
|
21
22
|
else
|
22
|
-
number.sub(/(\d{2})(\d{2})(\d{6})(\d{1})/,
|
23
|
+
number.sub(/(\d{2})(\d{2})(\d{6})(\d{1})/, '\\1.\\2.\\3-\\4')
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
@@ -7,13 +7,13 @@ class IeValidator < ActiveModel::EachValidator
|
|
7
7
|
end
|
8
8
|
|
9
9
|
private
|
10
|
+
|
10
11
|
def ie_present?(value)
|
11
12
|
value.present?
|
12
13
|
end
|
13
14
|
|
14
15
|
def attribute_uf_was_configured_at_validator?(record)
|
15
|
-
record.errors.add(:base,
|
16
|
-
I18n.t("validator.ie.uf.no_configured")) unless options[:uf].present?
|
16
|
+
record.errors.add(:base, I18n.t('validator.ie.uf.no_configured')) unless options[:uf].present?
|
17
17
|
|
18
18
|
options[:uf].present?
|
19
19
|
end
|
@@ -22,8 +22,8 @@ class IeValidator < ActiveModel::EachValidator
|
|
22
22
|
begin
|
23
23
|
uf = read_uf(record)
|
24
24
|
rescue NoMethodError
|
25
|
-
record.errors.add(
|
26
|
-
uf: options[:uf])
|
25
|
+
record.errors.add(
|
26
|
+
:base, I18n.t('validator.ie.uf.no_present', uf: options[:uf])
|
27
27
|
)
|
28
28
|
end
|
29
29
|
|
@@ -33,8 +33,8 @@ class IeValidator < ActiveModel::EachValidator
|
|
33
33
|
def ie_valid?(record, attribute, value)
|
34
34
|
begin
|
35
35
|
record.errors.add(attribute, :invalid) unless number_valid?(record, value)
|
36
|
-
rescue ArgumentError =>
|
37
|
-
record.errors.add(attribute,
|
36
|
+
rescue ArgumentError => e
|
37
|
+
record.errors.add(attribute, e.message)
|
38
38
|
end
|
39
39
|
record.errors.messages.empty?
|
40
40
|
end
|
@@ -47,7 +47,7 @@ class IeValidator < ActiveModel::EachValidator
|
|
47
47
|
|
48
48
|
def read_uf(record)
|
49
49
|
attribute = record
|
50
|
-
options[:uf].split(
|
50
|
+
options[:uf].split('#').each do |field|
|
51
51
|
attribute = attribute.send(field)
|
52
52
|
end
|
53
53
|
attribute
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require_relative '../commons/mod11'
|
2
|
+
|
3
|
+
# rubocop:disable Style/ClassAndModuleChildren
|
4
|
+
class BrDocuments::Suframa
|
5
|
+
include BrDocuments::Commons::Mod11
|
6
|
+
|
7
|
+
def initialize(number)
|
8
|
+
@number = number
|
9
|
+
end
|
10
|
+
|
11
|
+
def formatted
|
12
|
+
valid? ? format_number : ''
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
valid_pattern? &&
|
17
|
+
!sequence_of_equal_numbers? &&
|
18
|
+
valid_check_digit? &&
|
19
|
+
valid_activity_sector? &&
|
20
|
+
valid_administrative_unit?
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def valid_pattern?
|
26
|
+
/^(\d{2}\.\d{4}\.\d{3})$|^(\d{9})$/.match(@number).present?
|
27
|
+
end
|
28
|
+
|
29
|
+
def sequence_of_equal_numbers?
|
30
|
+
number_without_mask.split('').uniq.length == 1
|
31
|
+
end
|
32
|
+
|
33
|
+
def number_without_mask
|
34
|
+
@number_without_mask ||= @number.gsub('.', '')
|
35
|
+
end
|
36
|
+
|
37
|
+
def valid_check_digit?
|
38
|
+
weights = [9, 8, 7, 6, 5, 4, 3, 2]
|
39
|
+
|
40
|
+
check_digit = generate_check_digit(number_without_mask, weights)
|
41
|
+
number_without_mask[8] == check_digit.to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_activity_sector?
|
45
|
+
activity_sector = number_without_mask[0] + number_without_mask[1]
|
46
|
+
|
47
|
+
['01', '02', '10', '11', '20', '60'].include?(activity_sector)
|
48
|
+
end
|
49
|
+
|
50
|
+
def valid_administrative_unit?
|
51
|
+
administrative_unit = number_without_mask[6] + number_without_mask[7]
|
52
|
+
|
53
|
+
['01', '10', '30'].include?(administrative_unit)
|
54
|
+
end
|
55
|
+
|
56
|
+
def format_number
|
57
|
+
@number.sub(/(\d{2})(\d{4})(\d{3})/, '\\1.\\2.\\3')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
# rubocop:enable Style/ClassAndModuleChildren
|
data/lib/br_documents/version.rb
CHANGED
data/spec/cnpj_cpf/cnpj_spec.rb
CHANGED
@@ -1,57 +1,57 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe BrDocuments::CnpjCpf::Cnpj do
|
4
|
-
describe
|
5
|
-
it
|
6
|
-
cnpj = BrDocuments::CnpjCpf::Cnpj.new(
|
7
|
-
expect(cnpj.formatted).to eq
|
4
|
+
describe '#formatted' do
|
5
|
+
it 'returns a formatted number' do
|
6
|
+
cnpj = BrDocuments::CnpjCpf::Cnpj.new('04001155000101')
|
7
|
+
expect(cnpj.formatted).to eq '04.001.155/0001-01'
|
8
8
|
end
|
9
9
|
|
10
|
-
context
|
11
|
-
it
|
12
|
-
cnpj = BrDocuments::CnpjCpf::Cnpj.new(
|
13
|
-
expect(cnpj.formatted).to eq
|
10
|
+
context 'when is invalid' do
|
11
|
+
it 'returns an empty string' do
|
12
|
+
cnpj = BrDocuments::CnpjCpf::Cnpj.new('123')
|
13
|
+
expect(cnpj.formatted).to eq ''
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
19
|
-
[
|
18
|
+
it 'is invalid with malformed number' do
|
19
|
+
['242.2818/50001-54', '35.611.011/000192'].each do |number|
|
20
20
|
cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
|
21
21
|
expect(cnpj).to_not be_valid
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
26
|
-
[
|
25
|
+
it 'is invalid with a number longer than 14' do
|
26
|
+
['24.228.185/0001-544', '936710360001881'].each do |number|
|
27
27
|
cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
|
28
28
|
expect(cnpj).to_not be_valid
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
33
|
-
[
|
32
|
+
it 'is invalid with a number shorter than 14' do
|
33
|
+
['24.228.185/0001-5', '9367103600018'].each do |number|
|
34
34
|
cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
|
35
35
|
expect(cnpj).to_not be_valid
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
40
|
-
[
|
39
|
+
it 'is invalid with a sequence of the same number' do
|
40
|
+
['11.111.111/1111-11', '22222222222222'].each do |number|
|
41
41
|
cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
|
42
42
|
expect(cnpj).to_not be_valid
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
47
|
-
[
|
46
|
+
it 'is invalid with invalid check number' do
|
47
|
+
['04.001.155/0001-03', '85961757000101'].each do |number|
|
48
48
|
cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
|
49
49
|
expect(cnpj).to_not be_valid
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
54
|
-
[
|
53
|
+
it 'is valid with correct number' do
|
54
|
+
['04.001.155/0001-01', '85961757000102'].each do |number|
|
55
55
|
cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
|
56
56
|
expect(cnpj).to be_valid
|
57
57
|
end
|
data/spec/cnpj_cpf/cpf_spec.rb
CHANGED
@@ -1,57 +1,57 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe BrDocuments::CnpjCpf::Cpf do
|
4
|
-
describe
|
5
|
-
it
|
6
|
-
cpf = BrDocuments::CnpjCpf::Cpf.new(
|
7
|
-
expect(cpf.formatted).to eq
|
4
|
+
describe '#formatted' do
|
5
|
+
it 'returns a formatted number' do
|
6
|
+
cpf = BrDocuments::CnpjCpf::Cpf.new('11144477735')
|
7
|
+
expect(cpf.formatted).to eq '111.444.777-35'
|
8
8
|
end
|
9
9
|
|
10
|
-
context
|
11
|
-
it
|
12
|
-
cpf = BrDocuments::CnpjCpf::Cpf.new(
|
13
|
-
expect(cpf.formatted).to eq
|
10
|
+
context 'when is invalid' do
|
11
|
+
it 'returns an empty string' do
|
12
|
+
cpf = BrDocuments::CnpjCpf::Cpf.new('123')
|
13
|
+
expect(cpf.formatted).to eq ''
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
19
|
-
[
|
18
|
+
it 'is invalid with malformed number' do
|
19
|
+
['111.126.491.07', '482.261.15-663', '111444.777-35'].each do |number|
|
20
20
|
cpf = BrDocuments::CnpjCpf::Cpf.new(number)
|
21
21
|
expect(cpf).to_not be_valid
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
26
|
-
[
|
25
|
+
it 'is invalid with a number longer than 11' do
|
26
|
+
['045.769.421-150', '308514217766'].each do |number|
|
27
27
|
cpf = BrDocuments::CnpjCpf::Cpf.new(number)
|
28
28
|
expect(cpf).to_not be_valid
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
33
|
-
[
|
32
|
+
it 'is invalid with a number shorter than 11' do
|
33
|
+
['111.444.777-3', '1234567890'].each do |number|
|
34
34
|
cpf = BrDocuments::CnpjCpf::Cpf.new(number)
|
35
35
|
expect(cpf).to_not be_valid
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
40
|
-
[
|
39
|
+
it 'is invalid with a sequence of the same number' do
|
40
|
+
['111.111.111-11', '22222222222'].each do |number|
|
41
41
|
cpf = BrDocuments::CnpjCpf::Cpf.new(number)
|
42
42
|
expect(cpf).to_not be_valid
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
47
|
-
[
|
46
|
+
it 'is invalid with invalid check number' do
|
47
|
+
['111.111.111-22', '2222222233'].each do |number|
|
48
48
|
cpf = BrDocuments::CnpjCpf::Cpf.new(number)
|
49
49
|
expect(cpf).to_not be_valid
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
54
|
-
[
|
53
|
+
it 'is valid with correct number' do
|
54
|
+
['111.444.777-35', '01233254120'].each do |number|
|
55
55
|
cpf = BrDocuments::CnpjCpf::Cpf.new(number)
|
56
56
|
expect(cpf).to be_valid
|
57
57
|
end
|
data/spec/cnpj_validator_spec.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe CnpjValidator do
|
4
4
|
before(:each) do
|
5
|
-
@validator = CnpjValidator.new(attributes:
|
6
|
-
@mock = double(
|
5
|
+
@validator = CnpjValidator.new(attributes: 'cnpj')
|
6
|
+
@mock = double('model')
|
7
7
|
allow(@mock).to receive(:errors).and_return([])
|
8
8
|
allow(@mock.errors).to receive(:messages).and_return({})
|
9
|
-
allow(@mock.errors).to receive(:add) do |
|
10
|
-
|
9
|
+
allow(@mock.errors).to receive(:add) do |attribute, error|
|
10
|
+
@mock.errors.messages[attribute] = [error]
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
subject { @validator }
|
15
15
|
|
16
|
-
context
|
16
|
+
context 'when Cnpj is valid' do
|
17
17
|
it "doesn't add errors in model" do
|
18
|
-
subject.validate_each(@mock,
|
18
|
+
subject.validate_each(@mock, 'cnpj', '85961757000102')
|
19
19
|
expect(@mock.errors.messages).to be_empty
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
context
|
23
|
+
context 'when Cnpj is blank' do
|
24
24
|
it "doesn't add errors in model" do
|
25
|
-
subject.validate_each(@mock,
|
25
|
+
subject.validate_each(@mock, 'cnpj', '')
|
26
26
|
expect(@mock.errors.messages).to be_empty
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
context
|
31
|
-
it
|
32
|
-
subject.validate_each(@mock,
|
30
|
+
context 'when Cnpj is invalid' do
|
31
|
+
it 'adds errors in model' do
|
32
|
+
subject.validate_each(@mock, 'cnpj', '85961757000103')
|
33
33
|
expect(@mock.errors.messages).to_not be_empty
|
34
34
|
end
|
35
35
|
end
|
data/spec/cpf_validator_spec.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe CpfValidator do
|
4
4
|
before(:each) do
|
5
|
-
@validator = CpfValidator.new(attributes:
|
6
|
-
@mock = double(
|
5
|
+
@validator = CpfValidator.new(attributes: 'cpf')
|
6
|
+
@mock = double('model')
|
7
7
|
allow(@mock).to receive(:errors).and_return([])
|
8
8
|
allow(@mock.errors).to receive(:messages).and_return({})
|
9
|
-
allow(@mock.errors).to receive(:add) do |
|
10
|
-
|
9
|
+
allow(@mock.errors).to receive(:add) do |attribute, error|
|
10
|
+
@mock.errors.messages[attribute] = [error]
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
subject { @validator }
|
15
15
|
|
16
|
-
context
|
16
|
+
context 'when Cpf is valid' do
|
17
17
|
it "doesn't add errors in model" do
|
18
|
-
subject.validate_each(@mock,
|
18
|
+
subject.validate_each(@mock, 'cpf', '01233254120')
|
19
19
|
expect(@mock.errors.messages).to be_empty
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
context
|
23
|
+
context 'when Cpf is blank' do
|
24
24
|
it "doesn't add errors in model" do
|
25
|
-
subject.validate_each(@mock,
|
25
|
+
subject.validate_each(@mock, 'cpf', '')
|
26
26
|
expect(@mock.errors.messages).to be_empty
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
context
|
31
|
-
it
|
32
|
-
subject.validate_each(@mock,
|
30
|
+
context 'when Cpf is invalid' do
|
31
|
+
it 'adds errors in model' do
|
32
|
+
subject.validate_each(@mock, 'cpf', '01233254121')
|
33
33
|
expect(@mock.errors.messages).to_not be_empty
|
34
34
|
end
|
35
35
|
end
|