Skip to content

Commit 99209aa

Browse files
author
Dariush Wahdany
committed
feat: tests for bib filter
1 parent 43fcda0 commit 99209aa

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed

tests/test_bib_processor.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""Test cases for BibTeX processing functionality."""
2+
3+
import pytest
4+
from pathlib import Path
5+
from tex_minify.bib_processor import extract_citations, filter_bib_file
6+
7+
8+
def test_extract_single_citation():
9+
content = r"""
10+
This is a test document with a single citation \cite{key1}.
11+
"""
12+
citations = extract_citations(content)
13+
assert citations == {"key1"}
14+
15+
16+
def test_extract_multiple_citations_single_cite():
17+
content = r"""
18+
This is a test document with multiple citations \cite{key1,key2, key3}.
19+
"""
20+
citations = extract_citations(content)
21+
assert citations == {"key1", "key2", "key3"}
22+
23+
24+
def test_extract_multiple_cite_commands():
25+
content = r"""
26+
Different types of citations:
27+
\cite{key1}
28+
\citep{key2}
29+
\citet{key3}
30+
"""
31+
citations = extract_citations(content)
32+
assert citations == {"key1", "key2", "key3"}
33+
34+
35+
def test_extract_duplicate_citations():
36+
content = r"""
37+
Duplicate citations:
38+
\cite{key1}
39+
\cite{key2,key1}
40+
\citep{key1,key3}
41+
"""
42+
citations = extract_citations(content)
43+
assert citations == {"key1", "key2", "key3"}
44+
45+
46+
def test_extract_no_citations():
47+
content = "This is a test document with no citations."
48+
citations = extract_citations(content)
49+
assert citations == set()
50+
51+
52+
def test_filter_bib_file(tmp_path):
53+
# Create a temporary BibTeX file
54+
bib_content = """
55+
@article{key1,
56+
title = {Title 1},
57+
author = {Author 1},
58+
}
59+
60+
@book{key2,
61+
title = {Title 2},
62+
author = {Author 2},
63+
}
64+
65+
@inproceedings{key3,
66+
title = {Title 3},
67+
author = {Author 3},
68+
}
69+
"""
70+
bib_file = tmp_path / "test.bib"
71+
bib_file.write_text(bib_content)
72+
73+
# Test filtering with different citation sets
74+
used_citations = {"key1", "key3"}
75+
filtered = filter_bib_file(bib_file, used_citations)
76+
77+
# Check that only key1 and key3 entries are present
78+
assert "@article{key1," in filtered
79+
assert "@inproceedings{key3," in filtered
80+
assert "@book{key2," not in filtered
81+
82+
83+
def test_filter_bib_file_no_matches(tmp_path):
84+
bib_content = """
85+
@article{key1,
86+
title = {Title 1},
87+
author = {Author 1},
88+
}
89+
"""
90+
bib_file = tmp_path / "test.bib"
91+
bib_file.write_text(bib_content)
92+
93+
used_citations = {"key2"}
94+
filtered = filter_bib_file(bib_file, used_citations)
95+
assert filtered.strip() == ""
96+
97+
98+
def test_filter_bib_file_empty_citations(tmp_path):
99+
bib_content = """
100+
@article{key1,
101+
title = {Title 1},
102+
author = {Author 1},
103+
}
104+
"""
105+
bib_file = tmp_path / "test.bib"
106+
bib_file.write_text(bib_content)
107+
108+
used_citations = set()
109+
filtered = filter_bib_file(bib_file, used_citations)
110+
assert filtered.strip() == ""
111+
112+
113+
def test_filter_bib_file_with_comments(tmp_path):
114+
bib_content = """
115+
% This is a comment
116+
@article{key1,
117+
title = {Title 1},
118+
author = {Author 1},
119+
}
120+
121+
% Another comment
122+
@book{key2,
123+
title = {Title 2},
124+
author = {Author 2},
125+
}
126+
"""
127+
bib_file = tmp_path / "test.bib"
128+
bib_file.write_text(bib_content)
129+
130+
used_citations = {"key1"}
131+
filtered = filter_bib_file(bib_file, used_citations)
132+
assert "@article{key1," in filtered
133+
assert "@book{key2," not in filtered

tests/test_cli.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""Test cases for CLI functionality."""
2+
3+
import pytest
4+
from click.testing import CliRunner
5+
from pathlib import Path
6+
from tex_minify.cli import cli
7+
8+
9+
def test_minify_command(tmp_path):
10+
runner = CliRunner()
11+
12+
# Create a test input file
13+
input_file = tmp_path / "test.tex"
14+
input_file.write_text(r"\input{chapter1}")
15+
16+
# Create the input file that will be referenced
17+
chapter_file = tmp_path / "chapter1.tex"
18+
chapter_file.write_text("Chapter 1 content")
19+
20+
# Test with output to file
21+
output_file = tmp_path / "output.tex"
22+
result = runner.invoke(cli, ["minify", str(input_file), "-o", str(output_file)])
23+
assert result.exit_code == 0
24+
assert output_file.read_text() == "Chapter 1 content"
25+
26+
# Test with output to stdout
27+
result = runner.invoke(cli, ["minify", str(input_file)])
28+
assert result.exit_code == 0
29+
assert result.output == "Chapter 1 content\n"
30+
31+
32+
def test_filter_bib_command(tmp_path):
33+
runner = CliRunner()
34+
35+
# Create test input files
36+
tex_file = tmp_path / "test.tex"
37+
tex_file.write_text(r"""
38+
This is a test document.
39+
\cite{key1,key2}
40+
\citep{key3}
41+
""")
42+
43+
bib_file = tmp_path / "test.bib"
44+
bib_file.write_text("""
45+
@article{key1,
46+
title = {Title 1},
47+
author = {Author 1},
48+
}
49+
50+
@book{key2,
51+
title = {Title 2},
52+
author = {Author 2},
53+
}
54+
55+
@inproceedings{key3,
56+
title = {Title 3},
57+
author = {Author 3},
58+
}
59+
60+
@misc{key4,
61+
title = {Title 4},
62+
author = {Author 4},
63+
}
64+
""")
65+
66+
# Test with output to file
67+
output_file = tmp_path / "filtered.bib"
68+
result = runner.invoke(cli, ["filter-bib", str(tex_file), str(bib_file), "-o", str(output_file)])
69+
assert result.exit_code == 0
70+
71+
filtered_content = output_file.read_text()
72+
assert "@article{key1," in filtered_content
73+
assert "@book{key2," in filtered_content
74+
assert "@inproceedings{key3," in filtered_content
75+
assert "@misc{key4," not in filtered_content
76+
77+
# Test with output to stdout
78+
result = runner.invoke(cli, ["filter-bib", str(tex_file), str(bib_file)])
79+
assert result.exit_code == 0
80+
assert "@article{key1," in result.output
81+
assert "@book{key2," in result.output
82+
assert "@inproceedings{key3," in result.output
83+
assert "@misc{key4," not in result.output
84+
85+
86+
def test_filter_bib_command_no_citations(tmp_path):
87+
runner = CliRunner()
88+
89+
# Create test input files with no citations
90+
tex_file = tmp_path / "test.tex"
91+
tex_file.write_text("This is a test document with no citations.")
92+
93+
bib_file = tmp_path / "test.bib"
94+
bib_file.write_text("""
95+
@article{key1,
96+
title = {Title 1},
97+
author = {Author 1},
98+
}
99+
""")
100+
101+
# Test with output to file
102+
output_file = tmp_path / "filtered.bib"
103+
result = runner.invoke(cli, ["filter-bib", str(tex_file), str(bib_file), "-o", str(output_file)])
104+
assert result.exit_code == 0
105+
assert output_file.read_text().strip() == ""
106+
107+
108+
def test_filter_bib_command_missing_files(tmp_path):
109+
runner = CliRunner()
110+
111+
# Test with non-existent TeX file
112+
result = runner.invoke(cli, ["filter-bib", "nonexistent.tex", "test.bib"])
113+
assert result.exit_code != 0
114+
115+
# Test with non-existent BibTeX file
116+
tex_file = tmp_path / "test.tex"
117+
tex_file.write_text(r"\cite{key1}")
118+
result = runner.invoke(cli, ["filter-bib", str(tex_file), "nonexistent.bib"])
119+
assert result.exit_code != 0

0 commit comments

Comments
 (0)