]> git.madduck.net Git - etc/vim.git/commitdiff

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:

Remove hard coded test cases (#3062)
authorSagi Shadur <saroad2@gmail.com>
Wed, 18 May 2022 19:11:37 +0000 (22:11 +0300)
committerGitHub <noreply@github.com>
Wed, 18 May 2022 19:11:37 +0000 (12:11 -0700)
64 files changed:
tests/data/fast/pep_572_do_not_remove_parens.py [moved from tests/data/pep_572_do_not_remove_parens.py with 100% similarity]
tests/data/jupyter/non_python_notebook.ipynb [moved from tests/data/non_python_notebook.ipynb with 100% similarity]
tests/data/jupyter/notebook_empty_metadata.ipynb [moved from tests/data/notebook_empty_metadata.ipynb with 100% similarity]
tests/data/jupyter/notebook_no_trailing_newline.ipynb [moved from tests/data/notebook_no_trailing_newline.ipynb with 100% similarity]
tests/data/jupyter/notebook_trailing_newline.ipynb [moved from tests/data/notebook_trailing_newline.ipynb with 100% similarity]
tests/data/jupyter/notebook_which_cant_be_parsed.ipynb [moved from tests/data/notebook_which_cant_be_parsed.ipynb with 100% similarity]
tests/data/jupyter/notebook_without_changes.ipynb [moved from tests/data/notebook_without_changes.ipynb with 100% similarity]
tests/data/miscellaneous/async_as_identifier.py [moved from tests/data/async_as_identifier.py with 100% similarity]
tests/data/miscellaneous/blackd_diff.diff [moved from tests/data/blackd_diff.diff with 100% similarity]
tests/data/miscellaneous/blackd_diff.py [moved from tests/data/blackd_diff.py with 100% similarity]
tests/data/miscellaneous/debug_visitor.out [moved from tests/data/debug_visitor.out with 100% similarity]
tests/data/miscellaneous/debug_visitor.py [moved from tests/data/debug_visitor.py with 100% similarity]
tests/data/miscellaneous/decorators.py [moved from tests/data/decorators.py with 100% similarity]
tests/data/miscellaneous/docstring_no_string_normalization.py [moved from tests/data/docstring_no_string_normalization.py with 100% similarity]
tests/data/miscellaneous/expression_skip_magic_trailing_comma.diff [moved from tests/data/expression_skip_magic_trailing_comma.diff with 100% similarity]
tests/data/miscellaneous/force_py36.py [moved from tests/data/force_py36.py with 100% similarity]
tests/data/miscellaneous/force_pyi.py [moved from tests/data/force_pyi.py with 100% similarity]
tests/data/miscellaneous/long_strings_flag_disabled.py [moved from tests/data/long_strings_flag_disabled.py with 100% similarity]
tests/data/miscellaneous/missing_final_newline.diff [moved from tests/data/missing_final_newline.diff with 100% similarity]
tests/data/miscellaneous/missing_final_newline.py [moved from tests/data/missing_final_newline.py with 100% similarity]
tests/data/miscellaneous/pattern_matching_invalid.py [moved from tests/data/pattern_matching_invalid.py with 100% similarity]
tests/data/miscellaneous/power_op_newline.py [moved from tests/data/power_op_newline.py with 100% similarity]
tests/data/miscellaneous/python2_detection.py [moved from tests/data/python2_detection.py with 100% similarity]
tests/data/miscellaneous/string_quotes.py [moved from tests/data/string_quotes.py with 100% similarity]
tests/data/miscellaneous/stub.pyi [moved from tests/data/stub.pyi with 100% similarity]
tests/data/preview/cantfit.py [moved from tests/data/cantfit.py with 100% similarity]
tests/data/preview/comments7.py [moved from tests/data/comments7.py with 100% similarity]
tests/data/preview/comments8.py [moved from tests/data/comments8.py with 100% similarity]
tests/data/preview/docstring_preview.py [moved from tests/data/docstring_preview.py with 100% similarity]
tests/data/preview/long_strings.py [moved from tests/data/long_strings.py with 100% similarity]
tests/data/preview/long_strings__edge_case.py [moved from tests/data/long_strings__edge_case.py with 100% similarity]
tests/data/preview/long_strings__regression.py [moved from tests/data/long_strings__regression.py with 100% similarity]
tests/data/preview/one_element_subscript.py [moved from tests/data/one_element_subscript.py with 100% similarity]
tests/data/preview/percent_precedence.py [moved from tests/data/percent_precedence.py with 100% similarity]
tests/data/preview/remove_await_parens.py [moved from tests/data/remove_await_parens.py with 100% similarity]
tests/data/preview/remove_except_parens.py [moved from tests/data/remove_except_parens.py with 100% similarity]
tests/data/preview/remove_for_brackets.py [moved from tests/data/remove_for_brackets.py with 100% similarity]
tests/data/preview/return_annotation_brackets.py [moved from tests/data/return_annotation_brackets.py with 100% similarity]
tests/data/preview_39/remove_with_brackets.py [moved from tests/data/remove_with_brackets.py with 100% similarity]
tests/data/py_310/parenthesized_context_managers.py [moved from tests/data/parenthesized_context_managers.py with 100% similarity]
tests/data/py_310/pattern_matching_complex.py [moved from tests/data/pattern_matching_complex.py with 100% similarity]
tests/data/py_310/pattern_matching_extras.py [moved from tests/data/pattern_matching_extras.py with 100% similarity]
tests/data/py_310/pattern_matching_generic.py [moved from tests/data/pattern_matching_generic.py with 100% similarity]
tests/data/py_310/pattern_matching_simple.py [moved from tests/data/pattern_matching_simple.py with 100% similarity]
tests/data/py_310/pattern_matching_style.py [moved from tests/data/pattern_matching_style.py with 100% similarity]
tests/data/py_310/pep_572_py310.py [moved from tests/data/pep_572_py310.py with 100% similarity]
tests/data/py_310/starred_for_target.py [moved from tests/data/starred_for_target.py with 100% similarity]
tests/data/py_311/pep_654.py [moved from tests/data/pep_654.py with 100% similarity]
tests/data/py_311/pep_654_style.py [moved from tests/data/pep_654_style.py with 100% similarity]
tests/data/py_36/numeric_literals.py [moved from tests/data/numeric_literals.py with 100% similarity]
tests/data/py_36/numeric_literals_skip_underscores.py [moved from tests/data/numeric_literals_skip_underscores.py with 100% similarity]
tests/data/py_37/python37.py [moved from tests/data/python37.py with 100% similarity]
tests/data/py_38/pep_570.py [moved from tests/data/pep_570.py with 100% similarity]
tests/data/py_38/pep_572.py [moved from tests/data/pep_572.py with 100% similarity]
tests/data/py_38/pep_572_remove_parens.py [moved from tests/data/pep_572_remove_parens.py with 100% similarity]
tests/data/py_38/python38.py [moved from tests/data/python38.py with 100% similarity]
tests/data/py_39/pep_572_py39.py [moved from tests/data/pep_572_py39.py with 100% similarity]
tests/data/py_39/python39.py [moved from tests/data/python39.py with 100% similarity]
tests/test_black.py
tests/test_blackd.py
tests/test_format.py
tests/test_ipynb.py
tests/test_no_ipynb.py
tests/util.py

index 281019a0bfa6d90252b42d630e4b12d429f18109..a633e678dd700d70f17ad622bd21cb0cc602ade0 100644 (file)
@@ -60,6 +60,8 @@ from tests.util import (
     ff,
     fs,
     read_data,
+    get_case_path,
+    read_data_from_file,
 )
 
 THIS_FILE = Path(__file__)
@@ -157,7 +159,7 @@ class BlackTestCase(BlackBaseTestCase):
         )
 
     def test_piping(self) -> None:
-        source, expected = read_data("src/black/__init__", data=False)
+        source, expected = read_data_from_file(PROJECT_ROOT / "src/black/__init__.py")
         result = BlackRunner().invoke(
             black.main,
             [
@@ -179,8 +181,8 @@ class BlackTestCase(BlackBaseTestCase):
             r"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d "
             r"\+\d\d\d\d"
         )
-        source, _ = read_data("simple_cases/expression.py")
-        expected, _ = read_data("simple_cases/expression.diff")
+        source, _ = read_data("simple_cases", "expression.py")
+        expected, _ = read_data("simple_cases", "expression.diff")
         args = [
             "-",
             "--fast",
@@ -197,7 +199,7 @@ class BlackTestCase(BlackBaseTestCase):
         self.assertEqual(expected, actual)
 
     def test_piping_diff_with_color(self) -> None:
-        source, _ = read_data("simple_cases/expression.py")
+        source, _ = read_data("simple_cases", "expression.py")
         args = [
             "-",
             "--fast",
@@ -219,7 +221,7 @@ class BlackTestCase(BlackBaseTestCase):
 
     @patch("black.dump_to_file", dump_to_stderr)
     def _test_wip(self) -> None:
-        source, expected = read_data("wip")
+        source, expected = read_data("miscellaneous", "wip")
         sys.settrace(tracefunc)
         mode = replace(
             DEFAULT_MODE,
@@ -233,7 +235,7 @@ class BlackTestCase(BlackBaseTestCase):
         black.assert_stable(source, actual, black.FileMode())
 
     def test_pep_572_version_detection(self) -> None:
-        source, _ = read_data("pep_572")
+        source, _ = read_data("py_38", "pep_572")
         root = black.lib2to3_parse(source)
         features = black.get_features_used(root)
         self.assertIn(black.Feature.ASSIGNMENT_EXPRESSIONS, features)
@@ -241,7 +243,7 @@ class BlackTestCase(BlackBaseTestCase):
         self.assertIn(black.TargetVersion.PY38, versions)
 
     def test_expression_ff(self) -> None:
-        source, expected = read_data("simple_cases/expression.py")
+        source, expected = read_data("simple_cases", "expression.py")
         tmp_file = Path(black.dump_to_file(source))
         try:
             self.assertTrue(ff(tmp_file, write_back=black.WriteBack.YES))
@@ -255,8 +257,8 @@ class BlackTestCase(BlackBaseTestCase):
             black.assert_stable(source, actual, DEFAULT_MODE)
 
     def test_expression_diff(self) -> None:
-        source, _ = read_data("simple_cases/expression.py")
-        expected, _ = read_data("simple_cases/expression.diff")
+        source, _ = read_data("simple_cases", "expression.py")
+        expected, _ = read_data("simple_cases", "expression.diff")
         tmp_file = Path(black.dump_to_file(source))
         diff_header = re.compile(
             rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
@@ -281,8 +283,8 @@ class BlackTestCase(BlackBaseTestCase):
             self.assertEqual(expected, actual, msg)
 
     def test_expression_diff_with_color(self) -> None:
-        source, _ = read_data("simple_cases/expression.py")
-        expected, _ = read_data("simple_cases/expression.diff")
+        source, _ = read_data("simple_cases", "expression.py")
+        expected, _ = read_data("simple_cases", "expression.diff")
         tmp_file = Path(black.dump_to_file(source))
         try:
             result = BlackRunner().invoke(
@@ -301,7 +303,7 @@ class BlackTestCase(BlackBaseTestCase):
         self.assertIn("\033[0m", actual)
 
     def test_detect_pos_only_arguments(self) -> None:
-        source, _ = read_data("pep_570")
+        source, _ = read_data("py_38", "pep_570")
         root = black.lib2to3_parse(source)
         features = black.get_features_used(root)
         self.assertIn(black.Feature.POS_ONLY_ARGUMENTS, features)
@@ -310,7 +312,7 @@ class BlackTestCase(BlackBaseTestCase):
 
     @patch("black.dump_to_file", dump_to_stderr)
     def test_string_quotes(self) -> None:
-        source, expected = read_data("string_quotes")
+        source, expected = read_data("miscellaneous", "string_quotes")
         mode = black.Mode(preview=True)
         assert_format(source, expected, mode)
         mode = replace(mode, string_normalization=False)
@@ -320,8 +322,10 @@ class BlackTestCase(BlackBaseTestCase):
         black.assert_stable(source, not_normalized, mode=mode)
 
     def test_skip_magic_trailing_comma(self) -> None:
-        source, _ = read_data("simple_cases/expression.py")
-        expected, _ = read_data("expression_skip_magic_trailing_comma.diff")
+        source, _ = read_data("simple_cases", "expression")
+        expected, _ = read_data(
+            "miscellaneous", "expression_skip_magic_trailing_comma.diff"
+        )
         tmp_file = Path(black.dump_to_file(source))
         diff_header = re.compile(
             rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
@@ -348,8 +352,8 @@ class BlackTestCase(BlackBaseTestCase):
 
     @patch("black.dump_to_file", dump_to_stderr)
     def test_async_as_identifier(self) -> None:
-        source_path = (THIS_DIR / "data" / "async_as_identifier.py").resolve()
-        source, expected = read_data("async_as_identifier")
+        source_path = get_case_path("miscellaneous", "async_as_identifier")
+        source, expected = read_data_from_file(source_path)
         actual = fs(source)
         self.assertFormatEqual(expected, actual)
         major, minor = sys.version_info[:2]
@@ -363,8 +367,8 @@ class BlackTestCase(BlackBaseTestCase):
 
     @patch("black.dump_to_file", dump_to_stderr)
     def test_python37(self) -> None:
-        source_path = (THIS_DIR / "data" / "python37.py").resolve()
-        source, expected = read_data("python37")
+        source_path = get_case_path("py_37", "python37")
+        source, expected = read_data_from_file(source_path)
         actual = fs(source)
         self.assertFormatEqual(expected, actual)
         major, minor = sys.version_info[:2]
@@ -712,7 +716,7 @@ class BlackTestCase(BlackBaseTestCase):
         # since this makes some test cases of test_get_features_used()
         # fails if it fails, this is tested first so that a useful case
         # is identified
-        simples, relaxed = read_data("decorators")
+        simples, relaxed = read_data("miscellaneous", "decorators")
         # skip explanation comments at the top of the file
         for simple_test in simples.split("##")[1:]:
             node = black.lib2to3_parse(simple_test)
@@ -755,7 +759,7 @@ class BlackTestCase(BlackBaseTestCase):
         self.assertEqual(black.get_features_used(node), {Feature.NUMERIC_UNDERSCORES})
         node = black.lib2to3_parse("123456\n")
         self.assertEqual(black.get_features_used(node), set())
-        source, expected = read_data("simple_cases/function.py")
+        source, expected = read_data("simple_cases", "function")
         node = black.lib2to3_parse(source)
         expected_features = {
             Feature.TRAILING_COMMA_IN_CALL,
@@ -765,7 +769,7 @@ class BlackTestCase(BlackBaseTestCase):
         self.assertEqual(black.get_features_used(node), expected_features)
         node = black.lib2to3_parse(expected)
         self.assertEqual(black.get_features_used(node), expected_features)
-        source, expected = read_data("simple_cases/expression.py")
+        source, expected = read_data("simple_cases", "expression")
         node = black.lib2to3_parse(source)
         self.assertEqual(black.get_features_used(node), set())
         node = black.lib2to3_parse(expected)
@@ -851,8 +855,8 @@ class BlackTestCase(BlackBaseTestCase):
 
     @pytest.mark.incompatible_with_mypyc
     def test_debug_visitor(self) -> None:
-        source, _ = read_data("debug_visitor.py")
-        expected, _ = read_data("debug_visitor.out")
+        source, _ = read_data("miscellaneous", "debug_visitor")
+        expected, _ = read_data("miscellaneous", "debug_visitor.out")
         out_lines = []
         err_lines = []
 
@@ -936,10 +940,10 @@ class BlackTestCase(BlackBaseTestCase):
     def test_check_diff_use_together(self) -> None:
         with cache_dir():
             # Files which will be reformatted.
-            src1 = (THIS_DIR / "data" / "string_quotes.py").resolve()
+            src1 = get_case_path("miscellaneous", "string_quotes")
             self.invokeBlack([str(src1), "--diff", "--check"], exit_code=1)
             # Files which will not be reformatted.
-            src2 = (THIS_DIR / "data" / "simple_cases" / "composition.py").resolve()
+            src2 = get_case_path("simple_cases", "composition")
             self.invokeBlack([str(src2), "--diff", "--check"])
             # Multi file command.
             self.invokeBlack([str(src1), str(src2), "--diff", "--check"], exit_code=1)
@@ -963,7 +967,7 @@ class BlackTestCase(BlackBaseTestCase):
 
     def test_single_file_force_pyi(self) -> None:
         pyi_mode = replace(DEFAULT_MODE, is_pyi=True)
-        contents, expected = read_data("force_pyi")
+        contents, expected = read_data("miscellaneous", "force_pyi")
         with cache_dir() as workspace:
             path = (workspace / "file.py").resolve()
             with open(path, "w") as fh:
@@ -984,7 +988,7 @@ class BlackTestCase(BlackBaseTestCase):
     def test_multi_file_force_pyi(self) -> None:
         reg_mode = DEFAULT_MODE
         pyi_mode = replace(DEFAULT_MODE, is_pyi=True)
-        contents, expected = read_data("force_pyi")
+        contents, expected = read_data("miscellaneous", "force_pyi")
         with cache_dir() as workspace:
             paths = [
                 (workspace / "file1.py").resolve(),
@@ -1006,7 +1010,7 @@ class BlackTestCase(BlackBaseTestCase):
                 self.assertNotIn(str(path), normal_cache)
 
     def test_pipe_force_pyi(self) -> None:
-        source, expected = read_data("force_pyi")
+        source, expected = read_data("miscellaneous", "force_pyi")
         result = CliRunner().invoke(
             black.main, ["-", "-q", "--pyi"], input=BytesIO(source.encode("utf8"))
         )
@@ -1017,7 +1021,7 @@ class BlackTestCase(BlackBaseTestCase):
     def test_single_file_force_py36(self) -> None:
         reg_mode = DEFAULT_MODE
         py36_mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
-        source, expected = read_data("force_py36")
+        source, expected = read_data("miscellaneous", "force_py36")
         with cache_dir() as workspace:
             path = (workspace / "file.py").resolve()
             with open(path, "w") as fh:
@@ -1036,7 +1040,7 @@ class BlackTestCase(BlackBaseTestCase):
     def test_multi_file_force_py36(self) -> None:
         reg_mode = DEFAULT_MODE
         py36_mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
-        source, expected = read_data("force_py36")
+        source, expected = read_data("miscellaneous", "force_py36")
         with cache_dir() as workspace:
             paths = [
                 (workspace / "file1.py").resolve(),
@@ -1058,7 +1062,7 @@ class BlackTestCase(BlackBaseTestCase):
                 self.assertNotIn(str(path), normal_cache)
 
     def test_pipe_force_py36(self) -> None:
-        source, expected = read_data("force_py36")
+        source, expected = read_data("miscellaneous", "force_py36")
         result = CliRunner().invoke(
             black.main,
             ["-", "-q", "--target-version=py36"],
@@ -1454,10 +1458,10 @@ class BlackTestCase(BlackBaseTestCase):
 
         # https://bugs.python.org/issue2142
 
-        source, _ = read_data("missing_final_newline.py")
+        source, _ = read_data("miscellaneous", "missing_final_newline")
         # read_data adds a trailing newline
         source = source.rstrip()
-        expected, _ = read_data("missing_final_newline.diff")
+        expected, _ = read_data("miscellaneous", "missing_final_newline.diff")
         tmp_file = Path(black.dump_to_file(source, ensure_final_newline=False))
         diff_header = re.compile(
             rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
index 6174c4538b94d7438d513383f9e6977885855ac6..75d756705be7847a4371e9c09c3c0fc088e5101a 100644 (file)
@@ -95,7 +95,7 @@ class BlackDTestCase(AioHTTPTestCase):
 
     @unittest_run_loop
     async def test_blackd_pyi(self) -> None:
-        source, expected = read_data("stub.pyi")
+        source, expected = read_data("miscellaneous", "stub.pyi")
         response = await self.client.post(
             "/", data=source, headers={blackd.PYTHON_VARIANT_HEADER: "pyi"}
         )
@@ -108,8 +108,8 @@ class BlackDTestCase(AioHTTPTestCase):
             r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
         )
 
-        source, _ = read_data("blackd_diff.py")
-        expected, _ = read_data("blackd_diff.diff")
+        source, _ = read_data("miscellaneous", "blackd_diff")
+        expected, _ = read_data("miscellaneous", "blackd_diff.diff")
 
         response = await self.client.post(
             "/", data=source, headers={blackd.DIFF_HEADER: "true"}
index 003f5bbe18863513bf6b9328526e9ffceab32d87..005a5771c2b644c9feb9bb881adfefbaa5be7547 100644 (file)
@@ -8,45 +8,12 @@ import black
 from tests.util import (
     DEFAULT_MODE,
     PY36_VERSIONS,
-    THIS_DIR,
     assert_format,
     dump_to_stderr,
     read_data,
     all_data_cases,
 )
 
-PY310_CASES: List[str] = [
-    "starred_for_target",
-    "pattern_matching_simple",
-    "pattern_matching_complex",
-    "pattern_matching_extras",
-    "pattern_matching_style",
-    "pattern_matching_generic",
-    "parenthesized_context_managers",
-]
-
-PY311_CASES: List[str] = [
-    "pep_654",
-    "pep_654_style",
-]
-
-PREVIEW_CASES: List[str] = [
-    # string processing
-    "cantfit",
-    "comments7",
-    "comments8",
-    "long_strings",
-    "long_strings__edge_case",
-    "long_strings__regression",
-    "percent_precedence",
-    "remove_except_parens",
-    "remove_for_brackets",
-    "one_element_subscript",
-    "remove_await_parens",
-    "return_annotation_brackets",
-    "docstring_preview",
-]
-
 SOURCES: List[str] = [
     "src/black/__init__.py",
     "src/black/__main__.py",
@@ -95,25 +62,33 @@ def patch_dump_to_file(request: Any) -> Iterator[None]:
         yield
 
 
-def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None:
-    source, expected = read_data(filename, data=data)
+def check_file(
+    subdir: str, filename: str, mode: black.Mode, *, data: bool = True
+) -> None:
+    source, expected = read_data(subdir, filename, data=data)
     assert_format(source, expected, mode, fast=False)
 
 
 @pytest.mark.parametrize("filename", all_data_cases("simple_cases"))
 def test_simple_format(filename: str) -> None:
-    check_file(filename, DEFAULT_MODE)
+    check_file("simple_cases", filename, DEFAULT_MODE)
 
 
-@pytest.mark.parametrize("filename", PREVIEW_CASES)
+@pytest.mark.parametrize("filename", all_data_cases("preview"))
 def test_preview_format(filename: str) -> None:
-    check_file(filename, black.Mode(preview=True))
+    check_file("preview", filename, black.Mode(preview=True))
+
+
+@pytest.mark.parametrize("filename", all_data_cases("preview_39"))
+def test_preview_minimum_python_39_format(filename: str) -> None:
+    source, expected = read_data("preview_39", filename)
+    mode = black.Mode(preview=True)
+    assert_format(source, expected, mode, minimum_version=(3, 9))
 
 
 @pytest.mark.parametrize("filename", SOURCES)
 def test_source_is_formatted(filename: str) -> None:
-    path = THIS_DIR.parent / filename
-    check_file(str(path), DEFAULT_MODE, data=False)
+    check_file("", filename, DEFAULT_MODE, data=False)
 
 
 # =============== #
@@ -126,59 +101,50 @@ def test_empty() -> None:
     assert_format(source, expected)
 
 
-def test_pep_572() -> None:
-    source, expected = read_data("pep_572")
-    assert_format(source, expected, minimum_version=(3, 8))
-
-
-def test_pep_572_remove_parens() -> None:
-    source, expected = read_data("pep_572_remove_parens")
-    assert_format(source, expected, minimum_version=(3, 8))
-
-
-def test_pep_572_do_not_remove_parens() -> None:
-    source, expected = read_data("pep_572_do_not_remove_parens")
-    # the AST safety checks will fail, but that's expected, just make sure no
-    # parentheses are touched
-    assert_format(source, expected, fast=True)
+@pytest.mark.parametrize("filename", all_data_cases("py_36"))
+def test_python_36(filename: str) -> None:
+    source, expected = read_data("py_36", filename)
+    mode = black.Mode(target_versions=PY36_VERSIONS)
+    assert_format(source, expected, mode, minimum_version=(3, 6))
 
 
-@pytest.mark.parametrize("major, minor", [(3, 9), (3, 10)])
-def test_pep_572_newer_syntax(major: int, minor: int) -> None:
-    source, expected = read_data(f"pep_572_py{major}{minor}")
-    assert_format(source, expected, minimum_version=(major, minor))
+@pytest.mark.parametrize("filename", all_data_cases("py_37"))
+def test_python_37(filename: str) -> None:
+    source, expected = read_data("py_37", filename)
+    mode = black.Mode(target_versions={black.TargetVersion.PY37})
+    assert_format(source, expected, mode, minimum_version=(3, 7))
 
 
-def test_pep_570() -> None:
-    source, expected = read_data("pep_570")
-    assert_format(source, expected, minimum_version=(3, 8))
+@pytest.mark.parametrize("filename", all_data_cases("py_38"))
+def test_python_38(filename: str) -> None:
+    source, expected = read_data("py_38", filename)
+    mode = black.Mode(target_versions={black.TargetVersion.PY38})
+    assert_format(source, expected, mode, minimum_version=(3, 8))
 
 
-def test_remove_with_brackets() -> None:
-    source, expected = read_data("remove_with_brackets")
-    assert_format(
-        source,
-        expected,
-        black.Mode(preview=True),
-        minimum_version=(3, 9),
-    )
+@pytest.mark.parametrize("filename", all_data_cases("py_39"))
+def test_python_39(filename: str) -> None:
+    source, expected = read_data("py_39", filename)
+    mode = black.Mode(target_versions={black.TargetVersion.PY39})
+    assert_format(source, expected, mode, minimum_version=(3, 9))
 
 
-@pytest.mark.parametrize("filename", PY310_CASES)
+@pytest.mark.parametrize("filename", all_data_cases("py_310"))
 def test_python_310(filename: str) -> None:
-    source, expected = read_data(filename)
+    source, expected = read_data("py_310", filename)
     mode = black.Mode(target_versions={black.TargetVersion.PY310})
     assert_format(source, expected, mode, minimum_version=(3, 10))
 
 
-def test_python_310_without_target_version() -> None:
-    source, expected = read_data("pattern_matching_simple")
+@pytest.mark.parametrize("filename", all_data_cases("py_310"))
+def test_python_310_without_target_version(filename: str) -> None:
+    source, expected = read_data("py_310", filename)
     mode = black.Mode()
     assert_format(source, expected, mode, minimum_version=(3, 10))
 
 
 def test_patma_invalid() -> None:
-    source, expected = read_data("pattern_matching_invalid")
+    source, expected = read_data("miscellaneous", "pattern_matching_invalid")
     mode = black.Mode(target_versions={black.TargetVersion.PY310})
     with pytest.raises(black.parsing.InvalidInput) as exc_info:
         assert_format(source, expected, mode, minimum_version=(3, 10))
@@ -186,13 +152,19 @@ def test_patma_invalid() -> None:
     exc_info.match("Cannot parse: 10:11")
 
 
-@pytest.mark.parametrize("filename", PY311_CASES)
+@pytest.mark.parametrize("filename", all_data_cases("py_311"))
 def test_python_311(filename: str) -> None:
-    source, expected = read_data(filename)
+    source, expected = read_data("py_311", filename)
     mode = black.Mode(target_versions={black.TargetVersion.PY311})
     assert_format(source, expected, mode, minimum_version=(3, 11))
 
 
+@pytest.mark.parametrize("filename", all_data_cases("fast"))
+def test_fast_cases(filename: str) -> None:
+    source, expected = read_data("fast", filename)
+    assert_format(source, expected, fast=True)
+
+
 def test_python_2_hint() -> None:
     with pytest.raises(black.parsing.InvalidInput) as exc_info:
         assert_format("print 'daylily'", "print 'daylily'")
@@ -201,47 +173,25 @@ def test_python_2_hint() -> None:
 
 def test_docstring_no_string_normalization() -> None:
     """Like test_docstring but with string normalization off."""
-    source, expected = read_data("docstring_no_string_normalization")
+    source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
     mode = replace(DEFAULT_MODE, string_normalization=False)
     assert_format(source, expected, mode)
 
 
 def test_long_strings_flag_disabled() -> None:
     """Tests for turning off the string processing logic."""
-    source, expected = read_data("long_strings_flag_disabled")
+    source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
     mode = replace(DEFAULT_MODE, experimental_string_processing=False)
     assert_format(source, expected, mode)
 
 
-def test_numeric_literals() -> None:
-    source, expected = read_data("numeric_literals")
-    mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
-    assert_format(source, expected, mode)
-
-
-def test_numeric_literals_ignoring_underscores() -> None:
-    source, expected = read_data("numeric_literals_skip_underscores")
-    mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
-    assert_format(source, expected, mode)
-
-
 def test_stub() -> None:
     mode = replace(DEFAULT_MODE, is_pyi=True)
-    source, expected = read_data("stub.pyi")
+    source, expected = read_data("miscellaneous", "stub.pyi")
     assert_format(source, expected, mode)
 
 
-def test_python38() -> None:
-    source, expected = read_data("python38")
-    assert_format(source, expected, minimum_version=(3, 8))
-
-
-def test_python39() -> None:
-    source, expected = read_data("python39")
-    assert_format(source, expected, minimum_version=(3, 9))
-
-
 def test_power_op_newline() -> None:
     # requires line_length=0
-    source, expected = read_data("power_op_newline")
+    source, expected = read_data("miscellaneous", "power_op_newline")
     assert_format(source, expected, mode=black.Mode(line_length=0))
index 6f6b3090cd1d73cde9f8c99a91045efeddc5162e..e1d7dd88dcba7398af62e8ded7006d760dbf365a 100644 (file)
@@ -17,7 +17,7 @@ from black import (
 import pytest
 from black import Mode
 from _pytest.monkeypatch import MonkeyPatch
-from tests.util import DATA_DIR
+from tests.util import DATA_DIR, read_jupyter_notebook, get_case_path
 
 with contextlib.suppress(ModuleNotFoundError):
     import IPython
@@ -252,9 +252,7 @@ def test_empty_cell() -> None:
 
 
 def test_entire_notebook_empty_metadata() -> None:
-    with open(DATA_DIR / "notebook_empty_metadata.ipynb", "rb") as fd:
-        content_bytes = fd.read()
-    content = content_bytes.decode()
+    content = read_jupyter_notebook("jupyter", "notebook_empty_metadata")
     result = format_file_contents(content, fast=True, mode=JUPYTER_MODE)
     expected = (
         "{\n"
@@ -289,9 +287,7 @@ def test_entire_notebook_empty_metadata() -> None:
 
 
 def test_entire_notebook_trailing_newline() -> None:
-    with open(DATA_DIR / "notebook_trailing_newline.ipynb", "rb") as fd:
-        content_bytes = fd.read()
-    content = content_bytes.decode()
+    content = read_jupyter_notebook("jupyter", "notebook_trailing_newline")
     result = format_file_contents(content, fast=True, mode=JUPYTER_MODE)
     expected = (
         "{\n"
@@ -338,9 +334,7 @@ def test_entire_notebook_trailing_newline() -> None:
 
 
 def test_entire_notebook_no_trailing_newline() -> None:
-    with open(DATA_DIR / "notebook_no_trailing_newline.ipynb", "rb") as fd:
-        content_bytes = fd.read()
-    content = content_bytes.decode()
+    content = read_jupyter_notebook("jupyter", "notebook_no_trailing_newline")
     result = format_file_contents(content, fast=True, mode=JUPYTER_MODE)
     expected = (
         "{\n"
@@ -387,17 +381,14 @@ def test_entire_notebook_no_trailing_newline() -> None:
 
 
 def test_entire_notebook_without_changes() -> None:
-    with open(DATA_DIR / "notebook_without_changes.ipynb", "rb") as fd:
-        content_bytes = fd.read()
-    content = content_bytes.decode()
+    content = read_jupyter_notebook("jupyter", "notebook_without_changes")
     with pytest.raises(NothingChanged):
         format_file_contents(content, fast=True, mode=JUPYTER_MODE)
 
 
 def test_non_python_notebook() -> None:
-    with open(DATA_DIR / "non_python_notebook.ipynb", "rb") as fd:
-        content_bytes = fd.read()
-    content = content_bytes.decode()
+    content = read_jupyter_notebook("jupyter", "non_python_notebook")
+
     with pytest.raises(NothingChanged):
         format_file_contents(content, fast=True, mode=JUPYTER_MODE)
 
@@ -408,7 +399,7 @@ def test_empty_string() -> None:
 
 
 def test_unparseable_notebook() -> None:
-    path = DATA_DIR / "notebook_which_cant_be_parsed.ipynb"
+    path = get_case_path("jupyter", "notebook_which_cant_be_parsed.ipynb")
     msg = rf"File '{re.escape(str(path))}' cannot be parsed as valid Jupyter notebook\."
     with pytest.raises(ValueError, match=msg):
         format_file_in_place(path, fast=True, mode=JUPYTER_MODE)
@@ -418,7 +409,7 @@ def test_ipynb_diff_with_change() -> None:
     result = runner.invoke(
         main,
         [
-            str(DATA_DIR / "notebook_trailing_newline.ipynb"),
+            str(get_case_path("jupyter", "notebook_trailing_newline.ipynb")),
             "--diff",
             f"--config={EMPTY_CONFIG}",
         ],
@@ -431,7 +422,7 @@ def test_ipynb_diff_with_no_change() -> None:
     result = runner.invoke(
         main,
         [
-            str(DATA_DIR / "notebook_without_changes.ipynb"),
+            str(get_case_path("jupyter", "notebook_without_changes.ipynb")),
             "--diff",
             f"--config={EMPTY_CONFIG}",
         ],
@@ -445,7 +436,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_single(
 ) -> None:
     # Check that the cache isn't written to if Jupyter dependencies aren't installed.
     jupyter_dependencies_are_installed.cache_clear()
-    nb = DATA_DIR / "notebook_trailing_newline.ipynb"
+    nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
     tmp_nb = tmp_path / "notebook.ipynb"
     with open(nb) as src, open(tmp_nb, "w") as dst:
         dst.write(src.read())
@@ -471,7 +462,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_dir(
 ) -> None:
     # Check that the cache isn't written to if Jupyter dependencies aren't installed.
     jupyter_dependencies_are_installed.cache_clear()
-    nb = DATA_DIR / "notebook_trailing_newline.ipynb"
+    nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
     tmp_nb = tmp_path / "notebook.ipynb"
     with open(nb) as src, open(tmp_nb, "w") as dst:
         dst.write(src.read())
@@ -489,7 +480,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_dir(
 
 
 def test_ipynb_flag(tmp_path: pathlib.Path) -> None:
-    nb = DATA_DIR / "notebook_trailing_newline.ipynb"
+    nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
     tmp_nb = tmp_path / "notebook.a_file_extension_which_is_definitely_not_ipynb"
     with open(nb) as src, open(tmp_nb, "w") as dst:
         dst.write(src.read())
@@ -507,7 +498,7 @@ def test_ipynb_flag(tmp_path: pathlib.Path) -> None:
 
 
 def test_ipynb_and_pyi_flags() -> None:
-    nb = DATA_DIR / "notebook_trailing_newline.ipynb"
+    nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
     result = runner.invoke(
         main,
         [
index b03b8e13f149794f9785c51d9ba0815501672528..a3c897760fb874a8909a78f2e0c3a665893544ff 100644 (file)
@@ -1,8 +1,7 @@
 import pytest
-import os
 import pathlib
 
-from tests.util import THIS_DIR
+from tests.util import get_case_path
 from black import main, jupyter_dependencies_are_installed
 from click.testing import CliRunner
 
@@ -13,7 +12,7 @@ runner = CliRunner()
 
 def test_ipynb_diff_with_no_change_single() -> None:
     jupyter_dependencies_are_installed.cache_clear()
-    path = THIS_DIR / "data/notebook_trailing_newline.ipynb"
+    path = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
     result = runner.invoke(main, [str(path)])
     expected_output = (
         "Skipping .ipynb files as Jupyter dependencies are not installed.\n"
@@ -25,7 +24,7 @@ def test_ipynb_diff_with_no_change_single() -> None:
 def test_ipynb_diff_with_no_change_dir(tmp_path: pathlib.Path) -> None:
     jupyter_dependencies_are_installed.cache_clear()
     runner = CliRunner()
-    nb = os.path.join("tests", "data", "notebook_trailing_newline.ipynb")
+    nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
     tmp_nb = tmp_path / "notebook.ipynb"
     with open(nb) as src, open(tmp_nb, "w") as dst:
         dst.write(src.read())
index 1d76681dbeaa8c922402a1cfb223d33e33fa9ab2..d65c2e651ae9552c6670ad8964d79b821e10d285 100644 (file)
@@ -11,6 +11,9 @@ from black.debug import DebugVisitor
 from black.mode import TargetVersion
 from black.output import diff, err, out
 
+PYTHON_SUFFIX = ".py"
+ALLOWED_SUFFIXES = (PYTHON_SUFFIX, ".pyi", ".out", ".diff", ".ipynb")
+
 THIS_DIR = Path(__file__).parent
 DATA_DIR = THIS_DIR / "data"
 PROJECT_ROOT = THIS_DIR.parent
@@ -90,21 +93,30 @@ class BlackBaseTestCase(unittest.TestCase):
         _assert_format_equal(expected, actual)
 
 
-def all_data_cases(dir_name: str, data: bool = True) -> List[str]:
-    base_dir = DATA_DIR if data else PROJECT_ROOT
-    cases_dir = base_dir / dir_name
+def get_base_dir(data: bool) -> Path:
+    return DATA_DIR if data else PROJECT_ROOT
+
+
+def all_data_cases(subdir_name: str, data: bool = True) -> List[str]:
+    cases_dir = get_base_dir(data) / subdir_name
     assert cases_dir.is_dir()
-    return [f"{dir_name}/{case_path.stem}" for case_path in cases_dir.iterdir()]
+    return [case_path.stem for case_path in cases_dir.iterdir()]
 
 
-def read_data(name: str, data: bool = True) -> Tuple[str, str]:
-    """read_data('test_name') -> 'input', 'output'"""
-    if not name.endswith((".py", ".pyi", ".out", ".diff")):
-        name += ".py"
-    base_dir = DATA_DIR if data else PROJECT_ROOT
-    case_path = base_dir / name
+def get_case_path(
+    subdir_name: str, name: str, data: bool = True, suffix: str = PYTHON_SUFFIX
+) -> Path:
+    """Get case path from name"""
+    case_path = get_base_dir(data) / subdir_name / name
+    if not name.endswith(ALLOWED_SUFFIXES):
+        case_path = case_path.with_suffix(suffix)
     assert case_path.is_file(), f"{case_path} is not a file."
-    return read_data_from_file(case_path)
+    return case_path
+
+
+def read_data(subdir_name: str, name: str, data: bool = True) -> Tuple[str, str]:
+    """read_data('test_name') -> 'input', 'output'"""
+    return read_data_from_file(get_case_path(subdir_name, name, data))
 
 
 def read_data_from_file(file_name: Path) -> Tuple[str, str]:
@@ -126,6 +138,18 @@ def read_data_from_file(file_name: Path) -> Tuple[str, str]:
     return "".join(_input).strip() + "\n", "".join(_output).strip() + "\n"
 
 
+def read_jupyter_notebook(subdir_name: str, name: str, data: bool = True) -> str:
+    return read_jupyter_notebook_from_file(
+        get_case_path(subdir_name, name, data, suffix=".ipynb")
+    )
+
+
+def read_jupyter_notebook_from_file(file_name: Path) -> str:
+    with open(file_name, mode="rb") as fd:
+        content_bytes = fd.read()
+    return content_bytes.decode()
+
+
 @contextmanager
 def change_directory(path: Path) -> Iterator[None]:
     """Context manager to temporarily chdir to a different directory."""