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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
# vim:fileencoding=utf-8
# Copyright (c) 2006-2021 Andrey Golovizin
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Test routines for the BibTeX .bib parser.
Some tests were adopted from Babybib - another BibTeX parser by Matthew Brett.
https://github.com/matthew-brett/babybib
"""
from __future__ import absolute_import, unicode_literals
from unittest import TestCase
from itertools import zip_longest
from pybtex.database import BibliographyData, Entry, Person
from pybtex.database.input.bibtex import Parser
class _TestParser(Parser):
def __init__(self, *args, **kwargs):
super(_TestParser, self).__init__(*args, **kwargs)
self.errors = []
def handle_error(self, error):
self.errors.append(error)
class ParserTest(object):
input_string = None
input_strings = []
correct_result = None
parser_options = {}
errors = []
def setUp(self):
if not self.input_strings:
self.input_strings = [self.input_string]
def test_parser(self):
parser = _TestParser(encoding='UTF-8', **self.parser_options)
for input_string in self.input_strings:
parser.parse_string(input_string)
result = parser.data
correct_result = self.correct_result
assert result == correct_result
for error, correct_error in zip_longest(parser.errors, self.errors):
actual_error = str(error)
assert actual_error == correct_error
class EmptyDataTest(ParserTest, TestCase):
input_string = u''
correct_result = BibliographyData()
class BracesTest(ParserTest, TestCase):
input_string = u"""@ARTICLE{
test,
title={Polluted
with {DDT}.
},
}"""
correct_result = BibliographyData([(u'test', Entry('article', [(u'title', 'Polluted with {DDT}.')]))])
class BracesAndQuotesTest(ParserTest, TestCase):
input_string = u'''@ARTICLE{
test,
title="Nested braces and {"quotes"}",
}'''
correct_result = BibliographyData([(u'test', Entry('article', [(u'title', 'Nested braces and {"quotes"}')]))])
class EntryInStringTest(ParserTest, TestCase):
input_string = u"""
@article{Me2010, author="Brett, Matthew", title="An article
@article{something, author={Name, Another}, title={not really an article}}
"}
@article{Me2009,author={Nom de Plume, My}, title="A short story"}
"""
correct_result = BibliographyData(
entries=[
(u'Me2010', Entry(u'article',
fields=[
(u'title', u'An article @article{something, author={Name, Another}, title={not really an article}}'),
],
persons=[(u'author', [Person(u'Brett, Matthew')])]
)),
(u'Me2009', Entry(u'article',
fields=[(u'title', u'A short story')],
persons={u'author': [Person(u'Nom de Plume, My')]}
)),
]
)
class EntryInCommentTest(ParserTest, TestCase):
input_string = u"""
Both the articles register despite the comment block
@Comment{
@article{Me2010, title="An article"}
@article{Me2009, title="A short story"}
}
These all work OK without errors
@Comment{and more stuff}
Last article to show we can get here
@article{Me2011, }
"""
correct_result = BibliographyData([
('Me2010', Entry('article', fields=[('title', 'An article')])),
('Me2009', Entry('article', fields=[('title', 'A short story')])),
('Me2011', Entry('article')),
])
class AtTest(ParserTest, TestCase):
# FIXME: check warnings
input_string = u"""
The @ here parses fine in both cases
@article{Me2010,
title={An @tey article}}
@article{Me2009, title="A @tey short story"}
"""
correct_result = BibliographyData([
('Me2010', Entry('article', [('title', 'An @tey article')])),
('Me2009', Entry('article', [('title', 'A @tey short story')])),
])
errors = [
"syntax error in line 2: '(' or '{' expected",
]
class EntryTypesTest(ParserTest, TestCase):
input_string = u"""
Testing what are allowed for entry types
These are OK
@somename{an_id,}
@t2{another_id,}
@t@{again_id,}
@t+{aa1_id,}
@_t{aa2_id,}
These ones not
@2thou{further_id,}
@some name{id3,}
@some#{id4,}
@some%{id4,}
"""
correct_result = BibliographyData([
('an_id', Entry('somename')),
('another_id', Entry('t2')),
('again_id', Entry('t@')),
('aa1_id', Entry('t+')),
('aa2_id', Entry('_t')),
])
errors = [
"syntax error in line 12: a valid name expected",
"syntax error in line 13: '(' or '{' expected",
"syntax error in line 14: '(' or '{' expected",
"syntax error in line 15: '(' or '{' expected",
]
class FieldNamesTest(ParserTest, TestCase):
input_string = u"""
Check for characters allowed in field names
Here the cite key is fine, but the field name is not allowed:
``You are missing a field name``
@article{2010, 0author="Me"}
Underscores allowed (no error)
@article{2011, _author="Me"}
Not so for spaces obviously (``expecting an '='``)
@article{2012, author name = "Myself"}
Or hashes (``missing a field name``)
@article{2013, #name = "Myself"}
But field names can start with +-.
@article{2014, .name = "Myself"}
@article{2015, +name = "Myself"}
@article{2016, -name = "Myself"}
@article{2017, @name = "Myself"}
"""
correct_result = BibliographyData([
('2010', Entry('article')),
('2011', Entry('article', [('_author', 'Me')])),
('2012', Entry('article')),
('2013', Entry('article')),
('2014', Entry('article', [('.name', 'Myself')])),
('2015', Entry('article', [('+name', 'Myself')])),
('2016', Entry('article', [('-name', 'Myself')])),
('2017', Entry('article', [('@name', 'Myself')])),
])
errors = [
"syntax error in line 5: '}' expected",
"syntax error in line 11: '=' expected",
'syntax error in line 14: \'}\' expected',
]
class InlineCommentTest(ParserTest, TestCase):
input_string = u"""
"some text" causes an error like this
``You're missing a field name---line 6 of file bibs/inline_comment.bib``
for all 3 of the % some text occurences below; in each case the parser keeps
what it has up till that point and skips, so that it correctly gets the last
entry.
@article{Me2010,}
@article{Me2011,
author="Brett-like, Matthew",
% some text
title="Another article"}
@article{Me2012, % some text
author="Real Brett"}
This one correctly read
@article{Me2013,}
"""
correct_result = BibliographyData([
('Me2010', Entry('article')),
('Me2011', Entry('article', persons={'author': [
Person(first='Matthew', last='Brett-like'),
]})),
('Me2012', Entry('article')),
('Me2013', Entry('article')),
])
errors = [
"syntax error in line 10: '}' expected",
"syntax error in line 12: '}' expected",
]
class SimpleEntryTest(ParserTest, TestCase):
input_string = u"""
% maybe the simplest possible
% just a comment and one reference
@ARTICLE{Brett2002marsbar,
author = {Matthew Brett and Jean-Luc Anton and Romain Valabregue and Jean-Baptise
Poline},
title = {{Region of interest analysis using an SPM toolbox}},
journal = {Neuroimage},
institution = {},
year = {2002},
volume = {16},
pages = {1140--1141},
number = {2}
}
"""
correct_result = BibliographyData({
'Brett2002marsbar': Entry('article',
fields=[
('title', '{Region of interest analysis using an SPM toolbox}'),
('journal', 'Neuroimage'),
('institution', ''),
('year', '2002'),
('volume', '16'),
('pages', '1140--1141'),
('number', '2'),
],
persons={
'author': [
Person(first='Matthew', last='Brett'),
Person(first='Jean-Luc', last='Anton'),
Person(first='Romain', last='Valabregue'),
Person(first='Jean-Baptise', last='Poline'),
],
},
)
})
class KeyParsingTest(ParserTest, TestCase):
input_string = u"""
# will not work as expected
@article(test(parens1))
# works fine
@article(test(parens2),)
# works fine
@article{test(braces1)}
# also works
@article{test(braces2),}
"""
correct_result = BibliographyData([
('test(parens1))', Entry('article')),
('test(parens2)', Entry('article')),
('test(braces1)', Entry('article')),
('test(braces2)', Entry('article')),
])
errors = [
"syntax error in line 5: ')' expected",
]
class KeylessEntriesTest(ParserTest, TestCase):
parser_options = {'keyless_entries': True}
input_string = u"""
@BOOK(
title="I Am Jackie Chan: My Life in Action",
year=1999
)
@BOOK()
@BOOK{}
@BOOK{
title = "Der deutsche Jackie Chan Filmführer",
}
"""
correct_result = BibliographyData([
('unnamed-1', Entry('book', [('title', 'I Am Jackie Chan: My Life in Action'), ('year', '1999')])),
('unnamed-2', Entry('book')),
('unnamed-3', Entry('book')),
('unnamed-4', Entry('book', [('title', u'Der deutsche Jackie Chan Filmführer')])),
])
class MacrosTest(ParserTest, TestCase):
input_string = u"""
@String{and = { and }}
@String{etal = and # { {et al.}}}
@Article(
unknown,
author = nobody,
)
@Article(
gsl,
author = "Gough, Brian"#etal,
)
"""
correct_result = BibliographyData([
('unknown', Entry('article')),
('gsl', Entry('article', persons={u'author': [Person(u'Gough, Brian'), Person(u'{et al.}')]})),
])
errors = [
'undefined string in line 6: nobody',
]
class WantedEntriesTest(ParserTest, TestCase):
parser_options = {'wanted_entries': ['GSL']}
input_string = u"""
@Article(
gsl,
)
"""
correct_result = BibliographyData(entries={
'GSL': Entry('article'),
})
class CrossrefTest(ParserTest, TestCase):
parser_options = {'wanted_entries': ['GSL', 'GSL2']}
input_string = u"""
@Article(gsl, crossref="the_journal")
@Article(gsl2, crossref="The_Journal")
@Journal{the_journal,}
"""
correct_result = BibliographyData(entries=[
('GSL', Entry('article', [('crossref', 'the_journal')])),
('GSL2', Entry('article', [('crossref', 'The_Journal')])),
('the_journal', Entry('journal')),
])
class CrossrefWantedTest(ParserTest, TestCase):
"""When cross-referencing an explicitly cited, the key from .aux file should be used."""
parser_options = {'wanted_entries': ['GSL', 'GSL2', 'The_Journal']}
input_string = u"""
@Article(gsl, crossref="the_journal")
@Article(gsl2, crossref="The_Journal")
@Journal{the_journal,}
"""
correct_result = BibliographyData(entries=[
('GSL', Entry('article', [('crossref', 'the_journal')])),
('GSL2', Entry('article', [('crossref', 'The_Journal')])),
('The_Journal', Entry('journal')),
])
class UnusedEntryTest(ParserTest, TestCase):
parser_options = {'wanted_entries': []}
input_string = u"""
@Article(
gsl,
author = nobody,
)
"""
correct_result = BibliographyData()
class CrossFileMacrosTest(ParserTest, TestCase):
input_strings = [
u'@string{jackie = "Jackie Chan"}',
u""",
@Book{
i_am_jackie,
author = jackie,
title = "I Am " # jackie # ": My Life in Action",
}
""",
]
correct_result = BibliographyData({
'i_am_jackie': Entry('book',
fields=[('title', 'I Am Jackie Chan: My Life in Action')],
persons={'author': [Person(u'Chan, Jackie')]}
),
})
class AtCharacterTest(ParserTest, TestCase):
input_strings = [
r""",
@proceedings{acc,
title = {Proc.\@ of the American Control Conference},
notes = "acc@example.org"
}
""",
]
correct_result = BibliographyData({
'acc': Entry('proceedings',
fields=[
('title', r'Proc.\@ of the American Control Conference'),
('notes', 'acc@example.org'),
],
),
})
class AtCharacterInUnwantedEntryTest(ParserTest, TestCase):
parser_options = {'wanted_entries': []}
input_strings = [
r""",
@proceedings{acc,
title = {Proc.\@ of the American Control Conference},
notes = "acc@example.org"
}
""",
]
correct_result = BibliographyData()
class CaseSensitivityTest(ParserTest, TestCase):
input_strings = [
r""",
@Article{CamelCase,
Title = {To CamelCase or Under score},
year = 2009,
NOTES = "none"
}
""",
]
correct_result = BibliographyData({
'CamelCase': Entry('article',
fields=[
('Title', 'To CamelCase or Under score'),
('year', '2009'),
('NOTES', 'none'),
],
),
})
class WindowsNewlineTest(ParserTest, TestCase):
input_strings = [
u"""'@Article\r\n\r\n\r\n}\r\n'""",
]
correct_result = BibliographyData()
errors = ["syntax error in line 4: '(' or '{' expected"]
class DuplicateFieldTest(ParserTest, TestCase):
input_strings = [
r"""
@MASTERSTHESIS{
Mastering,
year = 1364,
title = "Mastering Thesis Writing",
school = "Charles University in Prague",
TITLE = "No One Reads Master's Theses Anyway LOL",
TiTlE = "Well seriously, lol.",
}
"""
]
correct_result = BibliographyData({
'Mastering': Entry('mastersthesis',
fields=[
('year', '1364'),
('title', 'Mastering Thesis Writing'),
('school', 'Charles University in Prague'),
],
),
})
errors = [
'entry with key Mastering has a duplicate TITLE field',
'entry with key Mastering has a duplicate TiTlE field',
]
class DuplicatePersonFieldTest(ParserTest, TestCase):
input_string = u"""
@article{Me2009,author={Nom de Plume, My}, title="A short story", AUTHoR = {Foo}}
"""
correct_result = BibliographyData(
entries=[
(u'Me2009', Entry(u'article',
fields=[(u'title', u'A short story')],
persons={u'author': [Person(u'Nom de Plume, My')]}
)),
]
)
errors = [
'entry with key Me2009 has a duplicate AUTHoR field',
]
|