-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_generate_bibtex.py
More file actions
150 lines (119 loc) · 4.26 KB
/
test_generate_bibtex.py
File metadata and controls
150 lines (119 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from pathlib import Path
from unittest.mock import patch
import pytest
from pep_sphinx_extensions.generate_bibtex import (
_escape_bibtex,
_format_authors,
_generate_bibtex_entry,
_parse_created,
create_bibtex_files,
)
MOCK_TARGET = "pep_sphinx_extensions.generate_bibtex.get_from_doctree"
PEP_8_HEADERS = {
"PEP": "8",
"Title": "Style Guide for Python Code",
"Author": "Guido van Rossum, Barry Warsaw, Alyssa Coghlan",
"Created": "05-Jul-2001",
}
def _mock_doctree(headers: dict[str, str]):
"""Return a mock get_from_doctree that returns values from headers dict."""
return lambda full_path, text: headers.get(text, "")
@pytest.mark.parametrize(
("text", "expected"),
[
("Hello World", "Hello World"),
("Tom & Jerry", r"Tom \& Jerry"),
("100%", r"100\%"),
("$x$", r"\$x\$"),
("C#", r"C\#"),
("snake_case", r"snake\_case"),
("{}", r"\{\}"),
("~tilde", r"\~tilde"),
("no specials", "no specials"),
],
)
def test_escape_bibtex(text: str, expected: str) -> None:
assert _escape_bibtex(text) == expected
@pytest.mark.parametrize(
("created", "expected"),
[
("01-Jan-1990", ("1990", "jan")),
("15-Sep-2021", ("2021", "sep")),
("28-Feb-2000", ("2000", "feb")),
],
)
def test_parse_created(created: str, expected: tuple[str, str]) -> None:
assert _parse_created(created) == expected
@pytest.mark.parametrize(
("author_header", "expected"),
[
("Cardinal Ximénez", "Cardinal Ximénez"),
(
"Cardinal Ximénez <Cardinal.Ximenez@spanish.inquisition>,"
" Cardinal Biggles <Cardinal.Biggles@spanish.inquisition>",
"Cardinal Ximénez and Cardinal Biggles",
),
(
"Cardinal Ximénez,\n Cardinal Biggles",
"Cardinal Ximénez and Cardinal Biggles",
),
(
"Cardinal Ximénez, Cardinal Biggles, Cardinal Fang",
"Cardinal Ximénez and Cardinal Biggles and Cardinal Fang",
),
],
)
def test_format_authors(author_header: str, expected: str) -> None:
assert _format_authors(author_header) == expected
def test_generate_bibtex_entry() -> None:
# Arrange / Act
with patch(MOCK_TARGET, _mock_doctree(PEP_8_HEADERS)):
result = _generate_bibtex_entry(Path("pep-0008.doctree"))
# Assert
assert "@techreport{pep8," in result
assert 'author = "Guido van Rossum and Barry Warsaw and Alyssa Coghlan"' in result
assert 'title = "PEP 8 --- Style Guide for Python Code"' in result
assert 'year = "2001"' in result
assert "month = jul," in result
assert 'number = "8"' in result
assert 'url = "https://peps.python.org/pep-0008/"' in result
def test_generate_bibtex_entry_title_escaped() -> None:
# Arrange
headers = {**PEP_8_HEADERS, "PEP": "999", "Title": "Use of $ & % in PEPs"}
# Act
with patch(MOCK_TARGET, _mock_doctree(headers)):
result = _generate_bibtex_entry(Path("pep-0999.doctree"))
# Assert
assert r"Use of \$ \& \% in PEPs" in result
def test_generate_bibtex_entry_author_escaped() -> None:
# Arrange
headers = {**PEP_8_HEADERS, "Author": "Tom & Jerry <tj@example.com>"}
# Act
with patch(MOCK_TARGET, _mock_doctree(headers)):
result = _generate_bibtex_entry(Path("pep-0008.doctree"))
# Assert
assert r"Tom \& Jerry" in result
def test_create_bibtex_files(tmp_path: Path) -> None:
# Arrange
doctree_dir = tmp_path / "doctrees"
doctree_dir.mkdir()
output_dir = tmp_path / "output"
output_dir.mkdir()
(doctree_dir / "pep-0008.doctree").touch()
# Act
with patch(MOCK_TARGET, _mock_doctree(PEP_8_HEADERS)):
create_bibtex_files(str(doctree_dir), str(output_dir))
# Assert
bib = (output_dir / "pep-0008.bib").read_text()
assert "@techreport{pep8," in bib
assert 'author = "Guido van Rossum and Barry Warsaw and Alyssa Coghlan"' in bib
def test_create_bibtex_files_no_doctrees(tmp_path: Path) -> None:
# Arrange
doctree_dir = tmp_path / "doctrees"
doctree_dir.mkdir()
output_dir = tmp_path / "output"
output_dir.mkdir()
# Act
create_bibtex_files(str(doctree_dir), str(output_dir))
# Assert
assert list(output_dir.glob("*.bib")) == []