| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
from unittest import TestCase |
| 4 |
|
| 5 |
from citeproc.source.bibtex.latex import parse_latex, substitute_ligatures |
| 6 |
|
| 7 |
|
| 8 |
class TestLatex(TestCase): |
| 9 |
# from "A TEX Primer for Scientists" (sections 5.2 and 5.3) |
| 10 |
# by Stanley A. Sawyer, Steven G. Krantz |
| 11 |
MACROS = [("Æsop's fables", r"\AE sop's fables"), |
| 12 |
('Æ is a Latin ligature', r"\AE\ is a Latin ligature"), |
| 13 |
('A TeX expert is a TeXnician.', |
| 14 |
r"A \TeX\ expert is a \TeX nician."), |
| 15 |
('This book is ©ed.', r"This book is \copyright ed."), |
| 16 |
('Umeå, Sweden', r"Ume\aa, Sweden"), |
| 17 |
('Your serial number is Å1102.', |
| 18 |
r"Your serial number is \AA1102."), |
| 19 |
('Your serial number is Å1102.', |
| 20 |
r"Your serial number is \AA 1102."), |
| 21 |
('$22.50 plus $ 4.07 tax.', r"\$22.50 plus \$ 4.07 tax."), |
| 22 |
('Crève Cœur, Missouri', r"Cr\`eve C\oe ur, Missouri"), |
| 23 |
('Pëtr, meet Françoise', r"P\"etr, meet Fran\c coise"), |
| 24 |
|
| 25 |
('Pëtr Pëtr Pëtr', r'P\" etr P\" etr ' 'P\\"\tetr'), |
| 26 |
] |
| 27 |
|
| 28 |
ACCENT_MACROS = [('très élevé', r"tr\`es \'elev\'e"), |
| 29 |
('übel Föhn', r'\"ubel F\"ohn'), |
| 30 |
('Andō Tokutarō', r"And\=o Tokutar\=o"), |
| 31 |
('García Lorca', r"Garc\'\i a Lorca"), |
| 32 |
('naïve', r'na\"\i ve'), |
| 33 |
|
| 34 |
('hát hàt hȧt hät hāt', r"h\'at h\`at h\.at h\"at h\=at"), |
| 35 |
('hãt hât ha̧t ha̱t', r"h\~at h\^at h\c at h\b at"), |
| 36 |
('hạt hǎt ha̋t', "h\\d at h\\v at h\\H at"), |
| 37 |
('hăt ha͡at', "h\\u at h\\t aat"), |
| 38 |
|
| 39 |
('øre', r"\o re"), |
| 40 |
('Øre', r"\O re"), |
| 41 |
('łódka', r"\l\'odka"), |
| 42 |
('Łodz', r"\L odz"), |
| 43 |
('Altstraße', r"Altstra\ss e"), |
| 44 |
('æsthete', r"\ae sthete"), |
| 45 |
('Ålm', r"\AA lm"), |
| 46 |
# ('', r"{\it\$}5"), |
| 47 |
('Œuvre', r"\OE uvre"), |
| 48 |
] |
| 49 |
|
| 50 |
# assorted string with macros |
| 51 |
ASSORTED = [('Escobar, María José', r"Escobar, Mar{\'\i}a Jos{\'e}"), |
| 52 |
('Escobar, María-José', r"Escobar, Mar\'{\i}a-Jos\'{e}")] |
| 53 |
|
| 54 |
WARNINGS = [('Åke José Édouard Gödel', |
| 55 |
r"\AA{ke} {Jos{\'{e}} {\'{E}douard} G{\"o}del", |
| 56 |
"Unbalanced parenthesis")] |
| 57 |
|
| 58 |
MATH = [(r'An $O(n \log n / \! \log\log n)$ Sorting Algorithm', |
| 59 |
r"An $O(n \log n / \! \log\log n)$ Sorting Algorithm")] |
| 60 |
|
| 61 |
def test_parse_latex(self): |
| 62 |
for reference, string in self.MACROS: |
| 63 |
self.assertEqual(reference, parse_latex(string)) |
| 64 |
for reference, string in self.ACCENT_MACROS: |
| 65 |
self.assertEqual(reference, parse_latex(string)) |
| 66 |
for reference, string in self.ASSORTED: |
| 67 |
self.assertEqual(reference, parse_latex(string)) |
| 68 |
for reference, string, warning in self.WARNINGS: |
| 69 |
with self.assertWarns(UserWarning) as record: |
| 70 |
self.assertEqual(reference, parse_latex(string)) |
| 71 |
message, = (rec.message.args[0] for rec in record.warnings) |
| 72 |
assert warning in message |
| 73 |
for reference, string in self.MATH: |
| 74 |
self.assertEqual(reference, parse_latex(string)) |
| 75 |
|
| 76 |
LIGATURES = [('¿Que pasa?', "?`Que pasa?"), |
| 77 |
('¡Que!', "!`Que!")] |
| 78 |
|
| 79 |
def test_substitute_ligatures(self): |
| 80 |
for reference, string in self.LIGATURES: |
| 81 |
self.assertEqual(reference, substitute_ligatures(string)) |