Skip to content

Commit

Permalink
Add two tests
Browse files Browse the repository at this point in the history
  • Loading branch information
firefly-cpp committed Jan 12, 2025
1 parent 6f0b86d commit adf9d15
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_scenario1.py
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions tests/test_scenario2.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit adf9d15

Please sign in to comment.