Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona checagem de CNPJ para saber se ele foi previamente formatado #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/test_CNPJ.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def test_mask(self):
masked_cnpj = self.cnpj.mask('11222333444455')
self.assertEqual(masked_cnpj, '11.222.333/4444-55')

def test_mask_on_already_masked_cnpj(self):
"""Verifica se o método mask funciona corretamente."""
masked_cnpj = self.cnpj.mask('11.222.333/4444-55')
self.assertEqual(masked_cnpj, '11.222.333/4444-55')

def test_special_case(self):
"""Verifica os casos especiais de CNPJ."""
cases = [
Expand Down
4 changes: 4 additions & 0 deletions validate_docbr/CNPJ.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def generate(self, mask: bool = False) -> str:

def mask(self, doc: str = '') -> str:
"""Coloca a máscara de CNPJ na variável doc."""
if not self._validate_input(doc, ['.', '/', '-']):
# Documento previamente formatado
return doc
Comment on lines +43 to +45
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O _validate_input irá retornar True em casos como '1-.22/.33./44.4-55', pois ele verifica apenas os caracteres. Creio que o ideal seria aplicar um RegEx para verificar se doc atende ao padrão de CNPJ, algo como [0-9]{2}\.[0-9]{3}\.[0-9]{3}\/[0-9]{4}\-[0-9]{2}.


return f"{doc[:2]}.{doc[2:5]}.{doc[5:8]}/{doc[8:12]}-{doc[-2:]}"

def _generate_first_digit(self, doc: Union[str, list]) -> str:
Expand Down