| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
import os |
| 4 |
|
| 5 |
from io import StringIO |
| 6 |
from unittest import TestCase |
| 7 |
|
| 8 |
from citeproc.source.bibtex.bibparse import BibTeXParser, BibTeXDecodeError |
| 9 |
|
| 10 |
|
| 11 |
TEST_BIB = os.path.join(os.path.dirname(__file__), 'test.bib') |
| 12 |
|
| 13 |
|
| 14 |
class TestBibTeXParser(TestCase): |
| 15 |
def test_parse(self): |
| 16 |
file = StringIO(sample) |
| 17 |
bib = BibTeXParser(file) |
| 18 |
self.print_entries(bib) |
| 19 |
# TODO: perform useful checks |
| 20 |
|
| 21 |
def test_parse_file(self): |
| 22 |
test_bib = BibTeXParser(TEST_BIB, encoding='utf-8') |
| 23 |
self.print_entries(test_bib) |
| 24 |
|
| 25 |
def test_parse_file_bad_encoding(self): |
| 26 |
with self.assertRaises(BibTeXDecodeError): |
| 27 |
BibTeXParser(TEST_BIB) |
| 28 |
|
| 29 |
@staticmethod |
| 30 |
def print_entries(bib): |
| 31 |
for key, entry in bib.items(): |
| 32 |
print(key) |
| 33 |
for name, value in entry.items(): |
| 34 |
print(' {}: {}'.format(name, value)) |
| 35 |
|
| 36 |
|
| 37 |
# based on the sample BibTeX database by Xavier Décoret |
| 38 |
# http://artis.imag.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html |
| 39 |
|
| 40 |
sample = r""" |
| 41 |
@Article(py03, |
| 42 |
author = {Xavier D\'ecoret}, |
| 43 |
title = "PyBiTex", |
| 44 |
year = 2003 |
| 45 |
) |
| 46 |
|
| 47 |
@Article{key03, |
| 48 |
title = "A {bunch {of} braces {in}} title" |
| 49 |
} |
| 50 |
|
| 51 |
@Article{key01a, |
| 52 |
author = "Simon {"}the {saint"} Templar", |
| 53 |
} |
| 54 |
|
| 55 |
@Article{key01b, |
| 56 |
title = "The history of @ sign" |
| 57 |
} |
| 58 |
|
| 59 |
Some {{comments} with unbalanced braces |
| 60 |
....and a "commented" entry... |
| 61 |
|
| 62 |
Book{landru21, |
| 63 |
author = {Landru, Henri D\'esir\'e}, |
| 64 |
title = {A hundred recipes for you wife}, |
| 65 |
publisher = {Culinary Expert Series}, |
| 66 |
year = 1921 |
| 67 |
} |
| 68 |
|
| 69 |
..some other comments..before a valid entry... |
| 70 |
|
| 71 |
@Book{steward03a, |
| 72 |
author = { Martha Steward }, |
| 73 |
title = {Cooking behind bars}, |
| 74 |
publisher = {Culinary Expert Series}, |
| 75 |
year = 2003 |
| 76 |
} |
| 77 |
|
| 78 |
...and finally an entry commented by the use of the special @Comment entry type. |
| 79 |
|
| 80 |
@Comment{steward03b, |
| 81 |
author = {Martha Steward}, |
| 82 |
title = {Cooking behind bars}, |
| 83 |
publisher = {Culinary Expert Series}, |
| 84 |
year = 2003 |
| 85 |
} |
| 86 |
|
| 87 |
@Comment{ |
| 88 |
@Book{steward03c, |
| 89 |
author = {Martha Steward}, |
| 90 |
title = {Cooking behind bars}, |
| 91 |
publisher = {Culinary Expert Series}, |
| 92 |
year = 2003 |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@String(mar = "march") |
| 97 |
|
| 98 |
@Book{sweig42, |
| 99 |
Author = { Stefan Sweig }, |
| 100 |
title = { The impossible book }, |
| 101 |
publisher = { Dead Poet Society}, |
| 102 |
year = 1942, |
| 103 |
month = mar |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
@String {firstname = "Xavier"} |
| 108 |
@String {lastname = "Decoret"} |
| 109 |
@String {email = firstname # "." # lastname # "@imag.fr"} |
| 110 |
|
| 111 |
@preamble{ "\newcommand{\noopsort}[1]{} " |
| 112 |
# "\newcommand{\printfirst}[2]{#1} " |
| 113 |
# "\newcommand{\singleletter}[1]{#1} " |
| 114 |
# "\newcommand{\switchargs}[2]{#2#1} " } |
| 115 |
|
| 116 |
@INBOOK{inbook-minimal, |
| 117 |
author = "Donald E. Knuth", |
| 118 |
title = "Fundamental Algorithms", |
| 119 |
publisher = "Addison-Wesley", |
| 120 |
year = "{\noopsort{1973b}}1973", |
| 121 |
chapter = "1.2", |
| 122 |
} |
| 123 |
""" |