Repository: brazilian-utils/brutils-python (python)

Issue:
Melhorando a função format_cpf
Chama a função is_valid para checar o CPF, se True retorna o número com os símbolos, senão, retorna None. 

Pull request:
Melhorando a função format_cpf 
Closes #88 

Patch:
diff --git a/README.md b/README.md
index aeaa7b2..51a03ab 100644
--- a/README.md
+++ b/README.md
@@ -46,12 +46,12 @@ False
 
 ## format_cpf
 
-Formata o CPF.
+Formata o CPF. Retorna None se o CPF for inválido.
 
 ```python
 >>> from brutils import format_cpf
->>> format_cpf('00011122233')
-'000.111.222-33'
+>>> format_cpf('11144477735')
+'111.444.777-35'
 ```
 
 ## parse_cpf
diff --git a/README_EN.md b/README_EN.md
index 718d631..733d24d 100644
--- a/README_EN.md
+++ b/README_EN.md
@@ -46,12 +46,12 @@ False
 
 ## format_cpf
 
-Format CPF.
+Format CPF. Returns None if CPF is invalid.
 
 ```python
 >>> from brutils import format_cpf
->>> format_cpf('00011122233')
-'000.111.222-33'
+>>> format_cpf('11144477735')
+'111.444.777-35'
 ```
 
 ## parse_cpf
diff --git a/brutils/cpf.py b/brutils/cpf.py
index 218a674..74aa83d 100644
--- a/brutils/cpf.py
+++ b/brutils/cpf.py
@@ -39,10 +39,14 @@ def display(cpf):  # type: (str) -> str
 
 def format_cpf(cpf):  # type: (str) -> str
     """
-    Will format an adequately formatted numbers-only CPF string,
-    adding in standard formatting visual aid symbols for display.
+    Format an adequately formatted numbers-only CPF string,
+    Returns a cpf formatted with standard visual aid symbols.
+    Returns None if cpf is invalid.
     """
-    return display(cpf)
+
+    if not is_valid(cpf):
+        return None
+    return "{}.{}.{}-{}".format(cpf[:3], cpf[3:6], cpf[6:9], cpf[9:])
 
 
 # CALCULATORS


Test patch:
diff --git a/tests/test_cpf.py b/tests/test_cpf.py
index 8359055..f1e1550 100644
--- a/tests/test_cpf.py
+++ b/tests/test_cpf.py
@@ -2,6 +2,7 @@ from os import pardir
 from os.path import abspath, join, dirname
 from sys import path, version_info, dont_write_bytecode
 from inspect import getsourcefile
+from unittest.mock import patch
 
 dont_write_bytecode = True
 range = range if version_info.major >= 3 else xrange
@@ -46,10 +47,16 @@ class CPF(TestCase):
         assert display("000000000000") is None
 
     def test_format_cpf(self):
-        assert format_cpf("00000000011") == "000.000.000-11"
-        assert format_cpf("00000000000") is None
-        assert format_cpf("0000000000a") is None
-        assert format_cpf("000000000000") is None
+        with patch("brutils.cpf.is_valid", return_value=True) as mock_is_valid:
+            # When cpf is_valid, returns formatted cpf
+            assert format_cpf("11144477735") == "111.444.777-35"
+
+        # Checks if function is_valid_cpf is called
+        mock_is_valid.assert_called_once_with("11144477735")
+
+        with patch("brutils.cpf.is_valid", return_value=False) as mock_is_valid:
+            # When cpf isn't valid, returns None
+            assert format_cpf("11144477735") is None
 
     def test_hashdigit(self):
         assert hashdigit("000000000", 10) == 0