[CI][RFC] Replace black-jupyter with ruff-format (#37210)
Co-authored-by: Alison Shao <a.shao@wustl.edu>
This commit is contained in:
co-authored by
Alison Shao
parent
2641e427be
commit
28262c20df
@@ -12,7 +12,7 @@ class TestApplyEdits:
|
||||
"""Tests for the apply_edits() source text transformation function."""
|
||||
|
||||
def test_single_line_match_to_multiline_replacement(self) -> None:
|
||||
source = "def foo():\n" " x = compute()\n" " return x\n"
|
||||
source = "def foo():\n x = compute()\n return x\n"
|
||||
edits = [
|
||||
EditSpec(
|
||||
match="x = compute()",
|
||||
@@ -20,12 +20,10 @@ class TestApplyEdits:
|
||||
)
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == (
|
||||
"def foo():\n" " x = compute()\n" " print(x)\n" " return x\n"
|
||||
)
|
||||
assert result == ("def foo():\n x = compute()\n print(x)\n return x\n")
|
||||
|
||||
def test_pure_insertion(self) -> None:
|
||||
source = "def foo():\n" " a = 1\n" " b = 2\n"
|
||||
source = "def foo():\n a = 1\n b = 2\n"
|
||||
edits = [
|
||||
EditSpec(
|
||||
match="a = 1",
|
||||
@@ -33,10 +31,10 @@ class TestApplyEdits:
|
||||
)
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == ("def foo():\n" " a = 1\n" " print(a)\n" " b = 2\n")
|
||||
assert result == ("def foo():\n a = 1\n print(a)\n b = 2\n")
|
||||
|
||||
def test_pure_deletion_via_empty_replacement(self) -> None:
|
||||
source = "def foo():\n" " debug_log()\n" " return 42\n"
|
||||
source = "def foo():\n debug_log()\n return 42\n"
|
||||
edits = [
|
||||
EditSpec(
|
||||
match="debug_log()",
|
||||
@@ -44,10 +42,10 @@ class TestApplyEdits:
|
||||
)
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == ("def foo():\n" " return 42\n")
|
||||
assert result == ("def foo():\n return 42\n")
|
||||
|
||||
def test_deletion_fewer_lines(self) -> None:
|
||||
source = "def foo():\n" " a = 1\n" " b = 2\n" " c = 3\n"
|
||||
source = "def foo():\n a = 1\n b = 2\n c = 3\n"
|
||||
edits = [
|
||||
EditSpec(
|
||||
match="a = 1\nb = 2",
|
||||
@@ -55,7 +53,7 @@ class TestApplyEdits:
|
||||
)
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == ("def foo():\n" " ab = 3\n" " c = 3\n")
|
||||
assert result == ("def foo():\n ab = 3\n c = 3\n")
|
||||
|
||||
def test_multiline_match_to_multiline_replacement(self) -> None:
|
||||
source = (
|
||||
@@ -133,15 +131,7 @@ class TestApplyEdits:
|
||||
|
||||
def test_not_found_diagnostic_single_window_with_marker(self) -> None:
|
||||
"""first line is present once but full match doesn't fit: one window with '>' on the match-region line."""
|
||||
source = (
|
||||
"line0\n"
|
||||
"line1\n"
|
||||
"line2\n"
|
||||
"anchor()\n"
|
||||
"wrong_next()\n"
|
||||
"line5\n"
|
||||
"line6\n"
|
||||
)
|
||||
source = "line0\nline1\nline2\nanchor()\nwrong_next()\nline5\nline6\n"
|
||||
edits = [EditSpec(match="anchor()\nright_next()", replacement="x")]
|
||||
with pytest.raises(PatchApplicationError) as exc_info:
|
||||
apply_edits(source=source, edits=edits)
|
||||
@@ -232,24 +222,22 @@ class TestApplyEdits:
|
||||
assert "filler9" not in msg
|
||||
|
||||
def test_match_found_multiple_times_raises(self) -> None:
|
||||
source = "def foo():\n" " print(1)\n" " print(1)\n"
|
||||
source = "def foo():\n print(1)\n print(1)\n"
|
||||
edits = [EditSpec(match="print(1)", replacement="print(2)")]
|
||||
with pytest.raises(PatchApplicationError, match="multiple"):
|
||||
apply_edits(source=source, edits=edits)
|
||||
|
||||
def test_multiple_edits_applied_sequentially(self) -> None:
|
||||
source = "def foo():\n" " a = 1\n" " b = 2\n" " return a + b\n"
|
||||
source = "def foo():\n a = 1\n b = 2\n return a + b\n"
|
||||
edits = [
|
||||
EditSpec(match="a = 1", replacement="a = 10"),
|
||||
EditSpec(match="b = 2", replacement="b = 20"),
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == (
|
||||
"def foo():\n" " a = 10\n" " b = 20\n" " return a + b\n"
|
||||
)
|
||||
assert result == ("def foo():\n a = 10\n b = 20\n return a + b\n")
|
||||
|
||||
def test_strip_matching_ignores_leading_trailing_whitespace(self) -> None:
|
||||
source = "def foo():\n" " x = compute()\n" " return x\n"
|
||||
source = "def foo():\n x = compute()\n return x\n"
|
||||
edits = [
|
||||
EditSpec(
|
||||
match=" x = compute() ",
|
||||
@@ -257,11 +245,11 @@ class TestApplyEdits:
|
||||
)
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == ("def foo():\n" " x = replaced()\n" " return x\n")
|
||||
assert result == ("def foo():\n x = replaced()\n return x\n")
|
||||
|
||||
def test_replacement_indented_text_realigned(self) -> None:
|
||||
"""replacement text with its own indentation gets realigned to match source."""
|
||||
source = "def foo():\n" " x = compute()\n" " return x\n"
|
||||
source = "def foo():\n x = compute()\n return x\n"
|
||||
edits = [
|
||||
EditSpec(
|
||||
match="x = compute()",
|
||||
@@ -270,15 +258,12 @@ class TestApplyEdits:
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == (
|
||||
"def foo():\n"
|
||||
" x = compute()\n"
|
||||
" print(x)\n"
|
||||
" return x\n"
|
||||
"def foo():\n x = compute()\n print(x)\n return x\n"
|
||||
)
|
||||
|
||||
def test_replacement_with_existing_indent_realigned(self) -> None:
|
||||
"""replacement text already has indentation that should be rebased."""
|
||||
source = "def foo():\n" " if True:\n" " x = 1\n" " return x\n"
|
||||
source = "def foo():\n if True:\n x = 1\n return x\n"
|
||||
edits = [
|
||||
EditSpec(
|
||||
match="x = 1",
|
||||
@@ -296,12 +281,10 @@ class TestApplyEdits:
|
||||
)
|
||||
|
||||
def test_append_keeps_match_and_adds_after(self) -> None:
|
||||
source = "def foo():\n" " x = compute()\n" " return x\n"
|
||||
source = "def foo():\n x = compute()\n return x\n"
|
||||
edits = [EditSpec(match="x = compute()", append="print(x)")]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == (
|
||||
"def foo():\n" " x = compute()\n" " print(x)\n" " return x\n"
|
||||
)
|
||||
assert result == ("def foo():\n x = compute()\n print(x)\n return x\n")
|
||||
|
||||
def test_append_multiline_match(self) -> None:
|
||||
source = (
|
||||
@@ -330,21 +313,18 @@ class TestApplyEdits:
|
||||
)
|
||||
|
||||
def test_prepend_adds_before_match(self) -> None:
|
||||
source = "def foo():\n" " x = compute()\n" " return x\n"
|
||||
source = "def foo():\n x = compute()\n return x\n"
|
||||
edits = [EditSpec(match="x = compute()", prepend="print('before')")]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == (
|
||||
"def foo():\n"
|
||||
" print('before')\n"
|
||||
" x = compute()\n"
|
||||
" return x\n"
|
||||
"def foo():\n print('before')\n x = compute()\n return x\n"
|
||||
)
|
||||
|
||||
def test_prepend_multiline(self) -> None:
|
||||
source = "def foo():\n" " return x\n"
|
||||
source = "def foo():\n return x\n"
|
||||
edits = [EditSpec(match="return x", prepend="a = 1\nb = 2")]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == ("def foo():\n" " a = 1\n" " b = 2\n" " return x\n")
|
||||
assert result == ("def foo():\n a = 1\n b = 2\n return x\n")
|
||||
|
||||
def test_prepend_deep_indent(self) -> None:
|
||||
source = (
|
||||
@@ -365,11 +345,7 @@ class TestApplyEdits:
|
||||
|
||||
def test_prepend_multiline_match(self) -> None:
|
||||
source = (
|
||||
"def foo():\n"
|
||||
" result = call(\n"
|
||||
" a=1,\n"
|
||||
" )\n"
|
||||
" return result\n"
|
||||
"def foo():\n result = call(\n a=1,\n )\n return result\n"
|
||||
)
|
||||
edits = [
|
||||
EditSpec(
|
||||
@@ -401,13 +377,13 @@ class TestApplyEdits:
|
||||
|
||||
def test_second_edit_sees_result_of_first(self) -> None:
|
||||
"""Edits are applied sequentially; second edit matches modified source."""
|
||||
source = "def foo():\n" " x = 1\n" " return x\n"
|
||||
source = "def foo():\n x = 1\n return x\n"
|
||||
edits = [
|
||||
EditSpec(match="x = 1", replacement="x = 1\ny = 2"),
|
||||
EditSpec(match="y = 2", replacement="y = 20"),
|
||||
]
|
||||
result = apply_edits(source=source, edits=edits)
|
||||
assert result == ("def foo():\n" " x = 1\n" " y = 20\n" " return x\n")
|
||||
assert result == ("def foo():\n x = 1\n y = 20\n return x\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user