|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | from bibtexparser import parse_file |
| 10 | +from bibtexparser import parse_string |
10 | 11 | from bibtexparser import write_file |
11 | 12 | from bibtexparser import write_string |
12 | 13 | from bibtexparser.library import Library |
| 14 | +from bibtexparser.model import DuplicateBlockKeyBlock |
13 | 15 | from bibtexparser.model import Entry |
14 | 16 | from bibtexparser.model import Field |
| 17 | +from bibtexparser.model import String |
15 | 18 |
|
16 | 19 |
|
17 | 20 | def test_gbk(): |
@@ -241,3 +244,160 @@ def test_write_string_unexpected_keyword_argument_raises_error(): |
241 | 244 | write_string(library, unknown_param="value") |
242 | 245 | assert "unexpected keyword arguments" in str(excinfo.value) |
243 | 246 | assert "unknown_param" in str(excinfo.value) |
| 247 | + |
| 248 | + |
| 249 | +FIRST_BIBTEX = """@string{me = "My Name"} |
| 250 | +
|
| 251 | +@article{first, |
| 252 | + title = {Hello World}, |
| 253 | + author = me, |
| 254 | + year = 2023 |
| 255 | +}""" |
| 256 | + |
| 257 | + |
| 258 | +def test_parse_string_into_existing_library_returns_the_passed_library(): |
| 259 | + """The passed library is mutated and returned, no new instance is created.""" |
| 260 | + library = parse_string(FIRST_BIBTEX) |
| 261 | + returned = parse_string("@article{second, title = {Second}}", library=library) |
| 262 | + assert returned is library |
| 263 | + |
| 264 | + |
| 265 | +def test_parse_string_into_existing_library_keeps_previous_blocks_untouched(): |
| 266 | + """Blocks already in the library must not be transformed a second time.""" |
| 267 | + library = parse_string(FIRST_BIBTEX) |
| 268 | + first_entry = library.entries_dict["first"] |
| 269 | + first_string = library.strings_dict["me"] |
| 270 | + |
| 271 | + parse_string("@article{second, title = {Second}}", library=library) |
| 272 | + |
| 273 | + assert library.entries_dict["first"] is first_entry |
| 274 | + assert library.strings_dict["me"] is first_string |
| 275 | + assert first_entry["title"] == "Hello World" |
| 276 | + assert first_entry["author"] == "My Name" |
| 277 | + assert first_entry["year"] == "2023" |
| 278 | + assert first_string.value == "My Name" |
| 279 | + assert all(field.enclosing is None for field in first_entry.fields) |
| 280 | + assert first_string.enclosing is None |
| 281 | + |
| 282 | + |
| 283 | +def test_parse_string_into_existing_library_does_not_warn(): |
| 284 | + """The parse stack must not be re-applied, hence no enclosing-order warning.""" |
| 285 | + library = parse_string(FIRST_BIBTEX) |
| 286 | + with warnings.catch_warnings(record=True) as w: |
| 287 | + warnings.simplefilter("always") |
| 288 | + parse_string("@article{second, title = {Second}, author = me}", library=library) |
| 289 | + assert [str(warning.message) for warning in w] == [] |
| 290 | + |
| 291 | + |
| 292 | +def test_parse_string_into_existing_library_roundtrips(): |
| 293 | + """The resulting library must still serialize to valid (re-parsable) bibtex.""" |
| 294 | + library = parse_string(FIRST_BIBTEX) |
| 295 | + parse_string("@article{second, title = {Second}, author = me}", library=library) |
| 296 | + |
| 297 | + written = write_string(library) |
| 298 | + assert "title = {Hello World}" in written |
| 299 | + assert "author = {My Name}" in written |
| 300 | + assert "@string{me = {My Name}}" in written |
| 301 | + |
| 302 | + reparsed = parse_string(written) |
| 303 | + assert reparsed.failed_blocks == [] |
| 304 | + assert reparsed.entries_dict["first"]["title"] == "Hello World" |
| 305 | + assert reparsed.entries_dict["first"]["author"] == "My Name" |
| 306 | + assert reparsed.entries_dict["second"]["author"] == "My Name" |
| 307 | + assert reparsed.strings_dict["me"].value == "My Name" |
| 308 | + |
| 309 | + |
| 310 | +def test_parse_string_into_existing_library_is_equivalent_to_parsing_at_once(): |
| 311 | + """Parsing in two steps must yield the same bibtex as parsing everything at once.""" |
| 312 | + second_bibtex = "@article{second, title = {Second}, author = me}" |
| 313 | + |
| 314 | + stepwise = parse_string(FIRST_BIBTEX) |
| 315 | + parse_string(second_bibtex, library=stepwise) |
| 316 | + at_once = parse_string(FIRST_BIBTEX + "\n\n" + second_bibtex) |
| 317 | + |
| 318 | + assert write_string(stepwise) == write_string(at_once) |
| 319 | + |
| 320 | + |
| 321 | +def test_parse_string_into_existing_library_resolves_previously_defined_strings(): |
| 322 | + """String references in the new content resolve against earlier @string blocks.""" |
| 323 | + library = parse_string(FIRST_BIBTEX) |
| 324 | + parse_string("@article{second, author = me}", library=library) |
| 325 | + |
| 326 | + assert library.entries_dict["second"]["author"] == "My Name" |
| 327 | + assert len(library.strings) == 1 |
| 328 | + assert [type(block) for block in library.blocks].count(String) == 1 |
| 329 | + |
| 330 | + |
| 331 | +def test_parse_string_into_existing_library_resolves_strings_case_insensitively(): |
| 332 | + """As within a single parse, string references are case-insensitive.""" |
| 333 | + library = parse_string(FIRST_BIBTEX) |
| 334 | + parse_string("@article{second, author = ME}", library=library) |
| 335 | + assert library.entries_dict["second"]["author"] == "My Name" |
| 336 | + |
| 337 | + |
| 338 | +def test_parse_string_repeatedly_into_existing_library(): |
| 339 | + """More than two consecutive calls keep working (no accumulating corruption).""" |
| 340 | + library = parse_string(FIRST_BIBTEX) |
| 341 | + for key in ("second", "third", "fourth"): |
| 342 | + parse_string(f"@article{{{key}, author = me}}", library=library) |
| 343 | + |
| 344 | + assert len(library.entries) == 4 |
| 345 | + assert library.failed_blocks == [] |
| 346 | + assert all(entry["author"] == "My Name" for entry in library.entries) |
| 347 | + assert write_string(library).count("author = {My Name}") == 4 |
| 348 | + |
| 349 | + |
| 350 | +def test_parse_string_into_existing_library_duplicate_entry_key(): |
| 351 | + """A duplicate entry key across two calls yields a DuplicateBlockKeyBlock.""" |
| 352 | + library = parse_string("@article{duplicate, title = {First}}") |
| 353 | + parse_string("@article{duplicate, title = {Second}}", library=library) |
| 354 | + |
| 355 | + assert len(library.failed_blocks) == 1 |
| 356 | + failed = library.failed_blocks[0] |
| 357 | + assert isinstance(failed, DuplicateBlockKeyBlock) |
| 358 | + assert failed.key == "duplicate" |
| 359 | + assert library.entries_dict["duplicate"]["title"] == "First" |
| 360 | + |
| 361 | + |
| 362 | +def test_parse_string_into_existing_library_redefined_string_key(): |
| 363 | + """A @string redefined by the new content becomes a DuplicateBlockKeyBlock, |
| 364 | + but is used to resolve references within the newly parsed content.""" |
| 365 | + library = parse_string('@string{me = "Old"}\n@article{first, author = me}') |
| 366 | + parse_string('@string{me = "New"}\n@article{second, author = me}', library=library) |
| 367 | + |
| 368 | + assert library.strings_dict["me"].value == "Old" |
| 369 | + assert len(library.failed_blocks) == 1 |
| 370 | + assert isinstance(library.failed_blocks[0], DuplicateBlockKeyBlock) |
| 371 | + assert library.failed_blocks[0].key == "me" |
| 372 | + |
| 373 | + assert library.entries_dict["first"]["author"] == "Old" |
| 374 | + assert library.entries_dict["second"]["author"] == "New" |
| 375 | + |
| 376 | + |
| 377 | +def test_parse_string_into_manually_created_library(): |
| 378 | + """Strings of a hand-built (never parsed) library are usable as references.""" |
| 379 | + library = Library( |
| 380 | + [ |
| 381 | + Entry("article", "manual", [Field("title", "Manual")]), |
| 382 | + String("manual_string", "Some Value"), |
| 383 | + ] |
| 384 | + ) |
| 385 | + parse_string("@article{parsed, author = manual_string}", library=library) |
| 386 | + |
| 387 | + assert library.entries_dict["parsed"]["author"] == "Some Value" |
| 388 | + assert library.entries_dict["manual"]["title"] == "Manual" |
| 389 | + assert "author = {Some Value}" in write_string(library) |
| 390 | + |
| 391 | + |
| 392 | +def test_parse_string_into_existing_library_keeps_block_order(): |
| 393 | + """Newly parsed blocks are appended after the pre-existing ones.""" |
| 394 | + library = parse_string("% first comment\n@article{first, title = {First}}") |
| 395 | + parse_string("% second comment\n@article{second, title = {Second}}", library=library) |
| 396 | + |
| 397 | + assert [block.__class__.__name__ for block in library.blocks] == [ |
| 398 | + "ImplicitComment", |
| 399 | + "Entry", |
| 400 | + "ImplicitComment", |
| 401 | + "Entry", |
| 402 | + ] |
| 403 | + assert [entry.key for entry in library.entries] == ["first", "second"] |
0 commit comments