Debian Codegraph @

tests/test_bibliography.py

raw
1 # coding: utf-8
2 from citeproc import (
3 Citation,
4 CitationItem,
5 CitationStylesBibliography,
6 CitationStylesStyle,
7 formatter,
8 source,
9 )
10 from unittest import TestCase
11 from citeproc.source.bibtex.bibparse import BibTeXParser
12
13 template = {
14 "type": "book",
15 "title": "La Rettorica",
16 "author": [{"literal": "Brunetto Latini"}],
17 "issued": {"date-parts": [[1968]]},
18 "editor": [{"literal": "Francesco Maggini"}],
19 "publisher": "Le Monnier",
20 "location": "Firenze",
21 }
22
23
24 def _pp(string):
25 # The edition renders as an ordinal (e.g. "2nd edn"); its word position
26 # varies by locale, but it is the only token that starts with a digit (the
27 # year is parenthesized), so pick that token out.
28 return next(word for word in string.split(" ") if word[:1].isdigit())
29
30
31 class TestBibliographyGeneration(TestCase):
32 def test_generate(self):
33 expected_ordinals = {
34 "en-US": {0: "1st", 1: "2nd", 3: "4th", 19: "20th"},
35 "de-DE": {0: "1.", 9: "10."},
36 "es-ES": {0: "1.ª"},
37 "fr-FR": {0: "1ʳᵉ", 9: "10ᵉ"},
38 "hi-IN": {0: "1वाँ"},
39 "it-IT": {0: "1º"}
40 }
41
42 for lg, ordinals in expected_ordinals.items():
43 bib_style = CitationStylesStyle("harvard-cite-them-right", lg)
44 entries = []
45 for edition in range(1, 26):
46 template["id"] = str(edition)
47 template["edition"] = edition
48 entries.append(template.copy())
49 bib = source.json.CiteProcJSON(entries)
50 bibliography = CitationStylesBibliography(bib_style, bib, formatter.plain)
51 citations = [CitationItem(str(x)) for x in range(1, 26)]
52 bibliography.register(Citation(citations))
53 generated_ordinals = [_pp(x) for x in bibliography.style.render_bibliography(citations)]
54
55 for index, expected_value in ordinals.items():
56 assert generated_ordinals[index] == expected_value, f"Failed for {lg} at index {index}. Expected: {expected_value}, Got: {generated_ordinals[index]}"
57
58 def test_ordinals_with_embedded_locale(self):
59 """chicago-author-date has an embedded <locale xml:lang="en"> that defines
60 editor/translator terms but no ordinal terms. Ordinals must still be
61 resolved from the file-based en-US locale data.
62 Regression test for https://github.com/citeproc-py/citeproc-py/issues/183."""
63 bib_style = CitationStylesStyle("chicago-author-date", "en-US")
64
65 entries = [
66 {
67 "id": str(n),
68 "type": "book",
69 "title": "Some Book",
70 "author": [{"family": "Smith", "given": "John"}],
71 "issued": {"date-parts": [[2020]]},
72 "publisher": "Publisher",
73 "publisher-place": "New York",
74 "edition": n,
75 }
76 for n in range(1, 5)
77 ]
78
79 bib = source.json.CiteProcJSON(entries)
80 bibliography = CitationStylesBibliography(bib_style, bib, formatter.plain)
81 citations = [CitationItem(str(x)) for x in range(1, 5)]
82 bibliography.register(Citation(citations))
83 rendered = bibliography.style.render_bibliography(citations)
84
85 # The ordinal edition token is the 6th whitespace-delimited word, e.g.:
86 # "Smith, John. 2020. Some Book. 2nd ed. New York: Publisher."
87 ordinals = [r.split()[5] for r in rendered]
88 assert ordinals[0] == "1st", f"Expected '1st', got '{ordinals[0]}'"
89 assert ordinals[1] == "2nd", f"Expected '2nd', got '{ordinals[1]}'"
90 assert ordinals[2] == "3rd", f"Expected '3rd', got '{ordinals[2]}'"
91 assert ordinals[3] == "4th", f"Expected '4th', got '{ordinals[3]}'"