Skip to content

Commit cea87d8

Browse files
committed
feat(issue63):complete cpf with zeros with not 11 digits and test it
this commit allow validate cpf starts with zeros
1 parent 3ad01b9 commit cea87d8

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

tests/test_CPF.py

+14
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ def test_special_case(self):
5656
]
5757
for cpf, is_valid in cases:
5858
self.assertEqual(self.cpf.validate(cpf), is_valid)
59+
60+
def test_add_leading_zeros(self):
61+
"""Verifica se o método de adicionar zeros à esquerda funciona corretamente."""
62+
cases = [
63+
('123456789', '00123456789'), # 9 digits
64+
('12345678901', '12345678901'), # 11 digits
65+
('1234567', '00001234567'), # 7 digits
66+
]
67+
for cpf_input, expected_output in cases:
68+
self.assertEqual(self.cpf._complete_with_zeros(cpf_input), expected_output)
69+
70+
# Test if the input is already correct length, it should not add zeros
71+
self.assertEqual(self.cpf._complete_with_zeros('00123456789'), '00123456789')
72+
self.assertEqual(self.cpf._complete_with_zeros('23456789012'), '23456789012')

validate_docbr/CPF.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ def validate(self, doc: str = '') -> bool:
1919
doc = list(self._only_digits(doc))
2020

2121
if len(doc) != 11:
22-
return False
22+
doc = self._complete_with_zeros(doc)
23+
if len(doc) != 11:
24+
return False
2325

2426
if not self.repeated_digits and self._check_repeated_digits(doc):
2527
return False
2628

2729
return self._generate_first_digit(doc) == doc[9] \
28-
and self._generate_second_digit(doc) == doc[10]
30+
and self._generate_second_digit(doc) == doc[10]
2931

3032
def generate(self, mask: bool = False) -> str:
3133
"""Gerar CPF."""
@@ -76,3 +78,8 @@ def _check_repeated_digits(self, doc: List[str]) -> bool:
7678
"""Verifica se é um CPF com números repetidos.
7779
Exemplo: 111.111.111-11"""
7880
return len(set(doc)) == 1
81+
82+
def _complete_with_zeros(self, doc: list[str]) -> list[str]:
83+
"""Adiciona zeros a esquerda para completar o CPF."""
84+
85+
return '0' * (11 - len(doc)) + str(doc)

0 commit comments

Comments
 (0)