Skip to content

Commit caadab5

Browse files
committed
Refactor: Improve handling of multiple common content sections and add tests
This commit improves the `exclude_common_contents` function to handle multiple common content sections within a single README file. It also adds a new test, `test__exclude_common_contents__double`, to verify this functionality. The following changes were made: - Updated `exclude_common_contents`: - The function now uses `re.findall` to find all occurrences of common content markers and removes them from the input string. - Improved logging to provide more context when common content markers are not found. - Added `test__exclude_common_contents__double`: - This new test verifies that the `exclude_common_contents` function correctly handles multiple common content sections. - It uses new fixtures, `common_lines_2` and `specific_lines_2`, to provide additional test data. - The test asserts that the specific lines are preserved and the common lines are removed from the output. - Added `create_common_block` helper function: - This function encapsulates the logic for creating a common content block, making the fixtures more readable. These changes improve the robustness and functionality of the `exclude_common_contents` function and provide better test coverage. add // before specific-2 lines between common content blocks to make it easier to understand the test message
1 parent 2e59a30 commit caadab5

2 files changed

Lines changed: 81 additions & 11 deletions

File tree

ai_tutor.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,17 @@ def exclude_common_contents(
283283
A string with the common content removed.
284284
"""
285285
# Include the markers in the pattern itself
286-
pattern = rf"(.*?){common_content_start_marker}\s*(.*?)\s*{common_content_end_marker}(.*?)"
287-
match = re.search(pattern, readme_content, re.DOTALL | re.IGNORECASE)
288-
289-
if match:
290-
# Extract the parts before and after the common content
291-
before_common = match.group(1).strip()
292-
after_common = match.group(3).strip()
293-
# Combine them to get the final instructions
294-
instruction = (before_common + after_common).strip()
295-
else:
286+
pattern = rf"({common_content_start_marker}\s*.*?\s*{common_content_end_marker})"
287+
found_list = re.findall(pattern, readme_content, re.DOTALL | re.IGNORECASE)
288+
289+
instruction = str(readme_content)
290+
291+
if not found_list:
296292
logging.warning(f"Common content markers '{common_content_start_marker}' and '{common_content_end_marker}' not found in README.md. Returning entire file.")
297-
instruction = readme_content.strip() # Single exit point
293+
else:
294+
for found in found_list:
295+
# Remove the common content
296+
instruction = instruction.replace(found, "")
298297

299298
return instruction
300299

tests/test_ai_tutor.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ def common_content_single(
312312
start_marker:str,
313313
common_lines:Tuple[str],
314314
end_marker:str
315+
) -> str:
316+
return create_common_block(start_marker, common_lines, end_marker)
317+
318+
319+
def create_common_block(
320+
start_marker:str,
321+
common_lines:Tuple[str],
322+
end_marker:str
315323
) -> str:
316324
return (
317325
'\n'.join((start_marker,) + common_lines + (end_marker,))
@@ -360,5 +368,68 @@ def test__exclude_common_contents__single(
360368
assert end_marker.strip() not in result
361369

362370

371+
@pytest.fixture
372+
def specific_lines_2() -> Tuple[str]:
373+
"""Provides a tuple of specific lines to be inserted between common content blocks."""
374+
return (
375+
"// This is specific content between the common blocks.",
376+
"// It should be preserved in the output."
377+
)
378+
379+
380+
@pytest.fixture
381+
def common_lines_2() -> Tuple[str]:
382+
return (
383+
"def div(a, b):",
384+
" return a / b"
385+
)
386+
387+
388+
@pytest.fixture
389+
def readme_content_double(
390+
readme_content_single:str,
391+
specific_lines_2:Tuple[str],
392+
start_marker:str,
393+
common_lines_2:Tuple[str],
394+
end_marker:str
395+
) -> str:
396+
return (
397+
readme_content_single
398+
+ '\n'.join(specific_lines_2) + '\n'
399+
+ create_common_block(start_marker, common_lines_2, end_marker)
400+
)
401+
402+
403+
def test__exclude_common_contents__double(
404+
readme_content_double:str,
405+
start_marker:str,
406+
end_marker:str,
407+
specific_lines:Tuple[str],
408+
specific_lines_2:Tuple[str],
409+
common_lines:Tuple[str],
410+
common_lines_2:Tuple[str],
411+
):
412+
result = ai_tutor.exclude_common_contents(
413+
readme_content=readme_content_double,
414+
common_content_start_marker=start_marker,
415+
common_content_end_marker=end_marker,
416+
)
417+
418+
for line in (tuple(specific_lines) + tuple(specific_lines_2)):
419+
assert line.strip() in result, ("\n"
420+
f"Could not find line: {line}\n"
421+
f"in result: {result}."
422+
)
423+
424+
for line in (tuple(common_lines) + tuple(common_lines_2)):
425+
assert line.strip() not in result, ("\n"
426+
f"Found line: {line}\n"
427+
f"in result: {result}."
428+
)
429+
430+
assert start_marker.strip() not in result
431+
assert end_marker.strip() not in result
432+
433+
363434
if '__main__' == __name__:
364435
pytest.main([__file__])

0 commit comments

Comments
 (0)