From 46a15a26ddbc6039e53f7f76bf1590ced70d19b6 Mon Sep 17 00:00:00 2001 From: Vinicius Mesel Date: Wed, 24 Jul 2024 15:58:04 -0300 Subject: [PATCH] Adds new check for CNPJ --- tests/test_CNPJ.py | 5 +++++ validate_docbr/CNPJ.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/tests/test_CNPJ.py b/tests/test_CNPJ.py index cfcfe9e..87b6e00 100644 --- a/tests/test_CNPJ.py +++ b/tests/test_CNPJ.py @@ -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 = [ diff --git a/validate_docbr/CNPJ.py b/validate_docbr/CNPJ.py index 004679b..d8d86e3 100644 --- a/validate_docbr/CNPJ.py +++ b/validate_docbr/CNPJ.py @@ -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 + 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: