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