]> git.madduck.net Git - etc/vim.git/blobdiff - tests/test_black.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Support --diff for both files and stdin
[etc/vim.git] / tests / test_black.py
index 30ecaf693a4346f0a487074d38969c29ece034d1..226a11913e8f85bc6d5563420572581060b34d06 100644 (file)
@@ -26,7 +26,7 @@ def dump_to_stderr(*output: str) -> str:
 
 def read_data(name: str) -> Tuple[str, str]:
     """read_data('test_name') -> 'input', 'output'"""
-    if not name.endswith((".py", ".out")):
+    if not name.endswith((".py", ".out", ".diff")):
         name += ".py"
     _input: List[str] = []
     _output: List[str] = []
@@ -92,7 +92,9 @@ class BlackTestCase(unittest.TestCase):
         try:
             sys.stdin, sys.stdout = StringIO(source), StringIO()
             sys.stdin.name = "<stdin>"
-            black.format_stdin_to_stdout(line_length=ll, fast=True, write_back=True)
+            black.format_stdin_to_stdout(
+                line_length=ll, fast=True, write_back=black.WriteBack.YES
+            )
             sys.stdout.seek(0)
             actual = sys.stdout.read()
         finally:
@@ -101,6 +103,23 @@ class BlackTestCase(unittest.TestCase):
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, line_length=ll)
 
+    def test_piping_diff(self) -> None:
+        source, _ = read_data("expression.py")
+        expected, _ = read_data("expression.diff")
+        hold_stdin, hold_stdout = sys.stdin, sys.stdout
+        try:
+            sys.stdin, sys.stdout = StringIO(source), StringIO()
+            sys.stdin.name = "<stdin>"
+            black.format_stdin_to_stdout(
+                line_length=ll, fast=True, write_back=black.WriteBack.DIFF
+            )
+            sys.stdout.seek(0)
+            actual = sys.stdout.read()
+        finally:
+            sys.stdin, sys.stdout = hold_stdin, hold_stdout
+        actual = actual.rstrip() + "\n"  # the diff output has a trailing space
+        self.assertEqual(expected, actual)
+
     @patch("black.dump_to_file", dump_to_stderr)
     def test_setup(self) -> None:
         source, expected = read_data("../setup")
@@ -126,6 +145,37 @@ class BlackTestCase(unittest.TestCase):
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, line_length=ll)
 
+    def test_expression_ff(self) -> None:
+        source, expected = read_data("expression")
+        tmp_file = Path(black.dump_to_file(source))
+        try:
+            self.assertTrue(ff(tmp_file, write_back=black.WriteBack.YES))
+            with open(tmp_file) as f:
+                actual = f.read()
+        finally:
+            os.unlink(tmp_file)
+        self.assertFormatEqual(expected, actual)
+        with patch("black.dump_to_file", dump_to_stderr):
+            black.assert_equivalent(source, actual)
+            black.assert_stable(source, actual, line_length=ll)
+
+    def test_expression_diff(self) -> None:
+        source, _ = read_data("expression.py")
+        expected, _ = read_data("expression.diff")
+        tmp_file = Path(black.dump_to_file(source))
+        hold_stdout = sys.stdout
+        try:
+            sys.stdout = StringIO()
+            self.assertTrue(ff(tmp_file, write_back=black.WriteBack.DIFF))
+            sys.stdout.seek(0)
+            actual = sys.stdout.read()
+            actual = actual.replace(tmp_file.name, "<stdin>")
+        finally:
+            sys.stdout = hold_stdout
+            os.unlink(tmp_file)
+        actual = actual.rstrip() + "\n"  # the diff output has a trailing space
+        self.assertEqual(expected, actual)
+
     @patch("black.dump_to_file", dump_to_stderr)
     def test_fstring(self) -> None:
         source, expected = read_data("fstring")