Skip to content

Commit

Permalink
allow testing api to check for min changes (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna authored Jul 17, 2024
1 parent 067e7ed commit 7450e2c
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/codemodder/codemods/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def run_and_assert(
input_code,
expected,
num_changes: int = 1,
min_num_changes: int | None = None,
root: Path | None = None,
files: list[Path] | None = None,
lines_to_exclude: list[int] | None = None,
Expand Down Expand Up @@ -90,12 +91,7 @@ def run_and_assert(
assert not changes
return

assert len(changes) == 1
assert (
actual_num := len(changes[0].changes)
) == num_changes, (
f"Expected {num_changes} changes but {actual_num} were created."
)
self.assert_num_changes(changes, num_changes, min_num_changes)

self.assert_changes(
tmpdir,
Expand All @@ -105,6 +101,20 @@ def run_and_assert(
changes[0],
)

def assert_num_changes(self, changes, expected_num_changes, min_num_changes):
assert len(changes) == 1

actual_num = len(changes[0].changes)

if min_num_changes is not None:
assert (
actual_num >= min_num_changes
), f"Expected at least {min_num_changes} changes but {actual_num} were created."
else:
assert (
actual_num == expected_num_changes
), f"Expected {expected_num_changes} changes but {actual_num} were created."

def assert_changes(self, root, file_path, input_code, expected, changes):
assert os.path.relpath(file_path, root) == changes.path
assert all(change.description for change in changes.changes)
Expand Down Expand Up @@ -133,12 +143,14 @@ def run_and_assert_filepath(
input_code: str,
expected: str,
num_changes: int = 1,
min_num_changes: int | None = None,
):
self.run_and_assert(
tmpdir=root,
input_code=input_code,
expected=expected,
num_changes=num_changes,
min_num_changes=min_num_changes,
files=[file_path],
)

Expand All @@ -160,6 +172,7 @@ def run_and_assert(
input_code,
expected,
num_changes: int = 1,
min_num_changes: int | None = None,
root: Path | None = None,
files: list[Path] | None = None,
lines_to_exclude: list[int] | None = None,
Expand Down Expand Up @@ -195,12 +208,7 @@ def run_and_assert(
assert not changes
return

assert len(changes) == 1
assert (
actual_num := len(changes[0].changes)
) == num_changes, (
f"Expected {num_changes} changes but {actual_num} were created."
)
self.assert_num_changes(changes, num_changes, min_num_changes)

self.assert_changes(
tmpdir,
Expand Down

0 comments on commit 7450e2c

Please sign in to comment.