Skip to content

Commit 7450e2c

Browse files
authored
allow testing api to check for min changes (#717)
1 parent 067e7ed commit 7450e2c

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/codemodder/codemods/test/utils.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def run_and_assert(
5858
input_code,
5959
expected,
6060
num_changes: int = 1,
61+
min_num_changes: int | None = None,
6162
root: Path | None = None,
6263
files: list[Path] | None = None,
6364
lines_to_exclude: list[int] | None = None,
@@ -90,12 +91,7 @@ def run_and_assert(
9091
assert not changes
9192
return
9293

93-
assert len(changes) == 1
94-
assert (
95-
actual_num := len(changes[0].changes)
96-
) == num_changes, (
97-
f"Expected {num_changes} changes but {actual_num} were created."
98-
)
94+
self.assert_num_changes(changes, num_changes, min_num_changes)
9995

10096
self.assert_changes(
10197
tmpdir,
@@ -105,6 +101,20 @@ def run_and_assert(
105101
changes[0],
106102
)
107103

104+
def assert_num_changes(self, changes, expected_num_changes, min_num_changes):
105+
assert len(changes) == 1
106+
107+
actual_num = len(changes[0].changes)
108+
109+
if min_num_changes is not None:
110+
assert (
111+
actual_num >= min_num_changes
112+
), f"Expected at least {min_num_changes} changes but {actual_num} were created."
113+
else:
114+
assert (
115+
actual_num == expected_num_changes
116+
), f"Expected {expected_num_changes} changes but {actual_num} were created."
117+
108118
def assert_changes(self, root, file_path, input_code, expected, changes):
109119
assert os.path.relpath(file_path, root) == changes.path
110120
assert all(change.description for change in changes.changes)
@@ -133,12 +143,14 @@ def run_and_assert_filepath(
133143
input_code: str,
134144
expected: str,
135145
num_changes: int = 1,
146+
min_num_changes: int | None = None,
136147
):
137148
self.run_and_assert(
138149
tmpdir=root,
139150
input_code=input_code,
140151
expected=expected,
141152
num_changes=num_changes,
153+
min_num_changes=min_num_changes,
142154
files=[file_path],
143155
)
144156

@@ -160,6 +172,7 @@ def run_and_assert(
160172
input_code,
161173
expected,
162174
num_changes: int = 1,
175+
min_num_changes: int | None = None,
163176
root: Path | None = None,
164177
files: list[Path] | None = None,
165178
lines_to_exclude: list[int] | None = None,
@@ -195,12 +208,7 @@ def run_and_assert(
195208
assert not changes
196209
return
197210

198-
assert len(changes) == 1
199-
assert (
200-
actual_num := len(changes[0].changes)
201-
) == num_changes, (
202-
f"Expected {num_changes} changes but {actual_num} were created."
203-
)
211+
self.assert_num_changes(changes, num_changes, min_num_changes)
204212

205213
self.assert_changes(
206214
tmpdir,

0 commit comments

Comments
 (0)