Skip to content

Commit

Permalink
feat(issue-63): Fix if len(doc) < 11 and cpf tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas019BR committed Aug 14, 2024
1 parent 9f267cb commit a3d25d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
15 changes: 6 additions & 9 deletions tests/test_CPF.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ def test_special_case(self):
def test_add_leading_zeros(self):
"""Verifica se o método de adicionar zeros à esquerda funciona corretamente."""
cases = [
('123456789', '00123456789'), # 9 digits
('12345678901', '12345678901'), # 11 digits
('1234567', '00001234567'), # 7 digits
('123456789', False), # 9 digitos
('12345678901', False), # 11 digitos
('1234567', False), # 7 digitos
('9380826044', True) # cpf valido
]
for cpf_input, expected_output in cases:
self.assertEqual(self.cpf._complete_with_zeros(cpf_input), expected_output)

# Testa se a entrada ja tem o tamanho correto, não deve adicionar zeros
self.assertEqual(self.cpf._complete_with_zeros('00123456789'), '00123456789')
self.assertEqual(self.cpf._complete_with_zeros('23456789012'), '23456789012')
for cpf_input, is_valid in cases:
self.assertEqual(self.cpf.validate(cpf_input), is_valid)
9 changes: 5 additions & 4 deletions validate_docbr/CPF.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def validate(self, doc: str = '') -> bool:

doc = list(self._only_digits(doc))

if len(doc) != 11:
if len(doc) < 11:
doc = self._complete_with_zeros(doc)

if not self.repeated_digits and self._check_repeated_digits(doc):
Expand Down Expand Up @@ -77,6 +77,7 @@ def _check_repeated_digits(self, doc: List[str]) -> bool:
Exemplo: 111.111.111-11"""
return len(set(doc)) == 1

def _complete_with_zeros(self, doc: list[str]) -> list[str]:
"""Adiciona zeros a esquerda para completar o CPF."""
return '0' * (11 - len(doc)) + str(doc)
def _complete_with_zeros(self, doc: str) -> list[str]:
"""Adiciona zeros à esquerda para completar o CPF."""
zeros_needed = 11 - len(doc)
return ['0'] * zeros_needed + doc

0 comments on commit a3d25d1

Please sign in to comment.