diff --git a/tests/test_scenario1.py b/tests/test_scenario1.py new file mode 100644 index 0000000..9ae4915 --- /dev/null +++ b/tests/test_scenario1.py @@ -0,0 +1,25 @@ +import tempfile +import os +from latex_clean_fig.clean import extract_included_images + +def test_extract_included_images(): + latex_content = """ + \\documentclass{article} + \\usepackage{graphicx} + \\begin{document} + \\includegraphics{figure1} + \\includegraphics[width=0.5\\textwidth]{figure2} + \\end{document} + """ + + # Create a temporary LaTeX file + with tempfile.NamedTemporaryFile(delete=False, suffix=".tex") as temp_file: + temp_file.write(latex_content.encode("utf-8")) + temp_file_path = temp_file.name + + try: + # Extract included images + images = extract_included_images(temp_file_path) + assert images == {"figure1", "figure2"}, f"Expected {{'figure1', 'figure2'}}, got {images}" + finally: + os.remove(temp_file_path) diff --git a/tests/test_scenario2.py b/tests/test_scenario2.py new file mode 100644 index 0000000..becf8ae --- /dev/null +++ b/tests/test_scenario2.py @@ -0,0 +1,31 @@ +import tempfile +import os +from latex_clean_fig.clean import remove_unused_images + +def test_remove_unused_images(): + # Simulate LaTeX file + latex_content = """ + \\includegraphics{figure1} + \\includegraphics{figure2} + """ + + # Create a temporary LaTeX file + with tempfile.NamedTemporaryFile(delete=False, suffix=".tex") as temp_tex_file: + temp_tex_file.write(latex_content.encode("utf-8")) + tex_file_path = temp_tex_file.name + + # Create a temporary directory with images + with tempfile.TemporaryDirectory() as temp_dir: + # Create image files + included_images = ["figure1.png", "figure2.jpg"] + unused_images = ["unused1.png", "unused2.jpg"] + for image in included_images + unused_images: + open(os.path.join(temp_dir, image), "w").close() + + # Call remove_unused_images + included, removed, total_files = remove_unused_images(temp_dir, tex_file_path) + assert included == {"figure1", "figure2"}, f"Expected {{'figure1', 'figure2'}}, got {included}" + assert sorted(removed) == sorted(unused_images), f"Expected {unused_images}, got {removed}" + assert total_files == len(included_images + unused_images), f"Expected {len(included_images + unused_images)}, got {total_files}" + + os.remove(tex_file_path)