Skip to content

Commit 2e59a30

Browse files
committed
Test: Refactor test__exclude_common_contents__single with fixtures
This commit refactors the `test__exclude_common_contents__single` test to use pytest fixtures for improved organization, readability, and maintainability. The following fixtures are introduced: - `start_marker`: Provides the start marker string. - `end_marker`: Provides the end marker string. - `common_lines`: Provides a tuple of common code lines. - `specific_lines`: Provides a tuple of specific code lines. - `common_content_single`: Generates the common content string using the `start_marker`, `common_lines`, and `end_marker` fixtures. - `readme_content_single`: Generates the complete README content using the `specific_lines` and `common_content_single` fixtures. These fixtures make the test setup more modular and easier to understand. They also promote code reuse and make it easier to modify the test data if needed. The test function now takes these fixtures as arguments, making the test logic more concise and focused. The assertions are also updated to use the fixtures, improving readability and maintainability. add typehints for test__exclude_common_contents__single()
1 parent b7fef4a commit 2e59a30

1 file changed

Lines changed: 71 additions & 30 deletions

File tree

tests/test_ai_tutor.py

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -280,43 +280,84 @@ def test_load_locale(explanation_in:str, homework:Tuple[str]):
280280
)
281281

282282

283-
def test__exclude_common_contents__single():
284-
start_marker = '``From here is common to all assignments.``'
285-
end_marker = '``Until here is common to all assignments.``'
286-
287-
common_line_0 = "def subtract(a, b):"
288-
common_line_1 = "return a - b"
289-
290-
common_content = start_marker + (
291-
'``\n'
292-
"\n"
293-
f"{common_line_0}\n"
294-
f" {common_line_1}\n"
295-
) + end_marker + '\n'
296-
297-
specific_line_0 = "def add(a, b):"
298-
specific_line_1 = "return a + b"
299-
300-
content = (
301-
"Write a function that returns the sum of two numbers.\n"
302-
"\n"
303-
f"{specific_line_0}\n"
304-
f" {specific_line_1}\n"
305-
) + common_content
283+
@pytest.fixture
284+
def start_marker() -> str:
285+
return r"``From here is common to all assignments.``"
286+
287+
288+
@pytest.fixture
289+
def end_marker() -> str:
290+
return r"``Until here is common to all assignments.``"
291+
292+
293+
@pytest.fixture
294+
def common_lines() -> Tuple[str]:
295+
return (
296+
"def subtract(a, b):",
297+
" return a - b"
298+
)
299+
300+
301+
@pytest.fixture
302+
def specific_lines() -> Tuple[str]:
303+
return (
304+
"Write a function that returns the sum of two numbers.",
305+
"def add(a, b):",
306+
" return a + b"
307+
)
306308

309+
310+
@pytest.fixture
311+
def common_content_single(
312+
start_marker:str,
313+
common_lines:Tuple[str],
314+
end_marker:str
315+
) -> str:
316+
return (
317+
'\n'.join((start_marker,) + common_lines + (end_marker,))
318+
+ '\n'
319+
)
320+
321+
322+
@pytest.fixture
323+
def readme_content_single(
324+
specific_lines:Tuple[str],
325+
common_content_single:str
326+
) -> str:
327+
return (
328+
'\n'.join(specific_lines)
329+
+ '\n'
330+
+ common_content_single
331+
)
332+
333+
334+
def test__exclude_common_contents__single(
335+
readme_content_single:str,
336+
start_marker:str,
337+
end_marker:str,
338+
specific_lines:Tuple[str],
339+
common_lines:Tuple[str],
340+
):
307341
result = ai_tutor.exclude_common_contents(
308-
readme_content=content,
342+
readme_content=readme_content_single,
309343
common_content_start_marker=start_marker,
310344
common_content_end_marker=end_marker,
311345
)
312346

313-
assert specific_line_0 in result
314-
assert specific_line_1 in result
347+
for line in specific_lines:
348+
assert line.strip() in result, ("\n"
349+
f"Could not find line: {line}\n"
350+
f"in result: {result}."
351+
)
352+
353+
for line in common_lines:
354+
assert line.strip() not in result, ("\n"
355+
f"Found line: {line}\n"
356+
f"in result: {result}."
357+
)
315358

316-
assert common_line_0 not in result
317-
assert common_line_1 not in result
318-
assert start_marker not in result
319-
assert end_marker not in result
359+
assert start_marker.strip() not in result
360+
assert end_marker.strip() not in result
320361

321362

322363
if '__main__' == __name__:

0 commit comments

Comments
 (0)