-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathtest_splitter_basic.py
More file actions
394 lines (343 loc) · 13.7 KB
/
Copy pathtest_splitter_basic.py
File metadata and controls
394 lines (343 loc) · 13.7 KB
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
"""Basic splitter tests.
These tests are not exhaustive, but they should cover the most common cases.
More exhaustive and atomic tests are provided in separate modules."""
from typing import Any
import pytest as pytest
from bibtexparser.library import Library
from bibtexparser.splitter import Splitter
# flake8: noqa
example_bibstr = """
@string{goossens = "Goossens, Michel"}
This line is an implicit comment.
@article{FuMetalhalideperovskite2019,
author = "Yongping Fu and Haiming Zhu and Jie Chen and Matthew P. Hautzinger and X.-Y. Zhu and Song Jin",
doi = {10.1038/s41578-019-0080-9},
journal = {Nature Reviews Materials},
month = {feb},
number = {3},
pages = {169-188},
publisher = {Springer Science and Business Media {LLC}},
title = {Metal halide perovskite nanostructures for optoelectronic applications and the study of physical properties},
url = {https://www.nature.com/articles/s41578-019-0080-9},
volume = {4},
year = {2019}
}
@comment{
This is a comment.
Spanning over two lines.
}
@preamble{e = mc^2}
@article{SunEnablingSiliconSolar2014,
author = {Ke Sun and Shaohua Shen and Yongqi Liang and Paul E. Burrows and Samuel S. Mao and Deli Wang},
doi = {10.1021/cr300459q},
journal = {Chemical Reviews},
month = {aug},
number = {17},
pages = {8662-8719},
publisher = {American Chemical Society ({ACS})},
title = "This title is missing a closing quote,
url = {http://pubs.acs.org/doi/10.1021/cr300459q},
volume = {114},
year = {2014}
}
@string{mittelbach="Mittelbach, Franck"}
@inproceedings{LiuPhotocatalytichydrogenproduction2016,
author = {Maochang Liu and Yubin Chen and Jinzhan Su and Jinwen Shi and Xixi Wang and Liejin Guo},
doi = {10.1038/nenergy.2016.151},
impactfactor = {54.000},
journal = {Nature Energy},
month = {sep},
number = {11},
pages = {16151},
publisher = {Springer Science and Business Media {LLC}},
title = {Photocatalytic hydrogen production using twinned nanocrystals and an unanchored {NiSx} co-catalyst},
url = {http://www.nature.com/articles/nenergy2016151},
volume = {1},
year = {2016}
}
@Comment{This is another comment}
"""
def _split() -> Library:
return Splitter(example_bibstr).split()
@pytest.mark.parametrize(
"expected",
[
# First valid entry
{
"key": "FuMetalhalideperovskite2019",
"type": "article",
"start_line": 6,
"overall_position": 2,
"entries_position": 0,
"fields": {
"author": '"Yongping Fu and Haiming Zhu and Jie Chen '
"and Matthew P. Hautzinger and X.-Y. Zhu "
'and Song Jin"',
"doi": "{10.1038/s41578-019-0080-9}",
"journal": "{Nature Reviews Materials}",
"month": "{feb}",
"number": "{3}",
"pages": "{169-188}",
"publisher": "{Springer Science and Business Media {LLC}}",
"title": "{Metal halide perovskite nanostructures for "
"optoelectronic applications and the study of "
"physical properties}",
"url": "{https://www.nature.com/articles/s41578-019-0080-9}",
"volume": "{4}",
"year": "{2019}",
},
},
# Second valid entry
{
"key": "LiuPhotocatalytichydrogenproduction2016",
"type": "inproceedings",
"start_line": 44,
"overall_position": 7,
"entries_position": 1,
"fields": {
"author": "{Maochang Liu and Yubin Chen and Jinzhan Su "
"and Jinwen Shi and Xixi Wang and Liejin Guo}",
"doi": "{10.1038/nenergy.2016.151}",
"impactfactor": "{54.000}",
"journal": "{Nature Energy}",
"month": "{sep}",
"number": "{11}",
"pages": "{16151}",
"publisher": "{Springer Science and Business Media {LLC}}",
"title": "{Photocatalytic hydrogen production using twinned "
"nanocrystals and an unanchored {NiSx} co-catalyst}",
"url": "{http://www.nature.com/articles/nenergy2016151}",
"volume": "{1}",
"year": "{2016}",
},
},
],
)
def test_entry(expected: dict[str, Any]):
library = _split()
assert len(library.entries) == 2
entry_by_key = library.entries_dict
assert len(entry_by_key) == 2
entry = entry_by_key[expected["key"]]
assert entry.key == expected["key"]
assert entry.entry_type == expected["type"]
assert len(entry.fields) == len(expected["fields"])
for field_name, field_value in expected["fields"].items():
field = entry.fields_dict[field_name]
assert field.key == field_name
assert field.value == field_value
assert entry.start_line == expected["start_line"]
# Assert overall position
assert library.blocks.index(entry) == expected["overall_position"]
# Assert position in entries
assert library.entries.index(entry) == expected["entries_position"]
@pytest.mark.parametrize(
"expected",
[
{
"position": 0,
"str_position": 0,
"key": "goossens",
"value": '"Goossens, Michel"',
"raw": '@string{goossens = "Goossens, Michel"}',
"line": 2,
},
{
"position": 6,
"str_position": 1,
"key": "mittelbach",
"value": '"Mittelbach, Franck"',
"raw": '@string{mittelbach="Mittelbach, Franck"}',
"line": 42,
},
],
)
def test_strings(expected: dict[str, any]) -> None:
library = _split()
assert len(library.strings) == 2
# Raise KeyError if not found
tested_string = library.strings_dict[expected["key"]]
# Test parsed conent
assert tested_string is not None
assert tested_string.value == expected["value"]
assert tested_string.key == expected["key"]
assert tested_string.raw == expected["raw"]
assert tested_string.start_line == expected["line"]
# Make sure list and dict entries are the same, and position in list matches
assert library.strings[expected["str_position"]] == tested_string
# Make sure position in overall blocks matches
assert library.blocks[expected["position"]] == tested_string
def test_preamble():
library = _split()
assert len(library.preambles) == 1
preamble = library.preambles[0]
assert preamble.raw == "@preamble{e = mc^2}"
assert preamble.value == "e = mc^2"
assert preamble.start_line == 25
@pytest.mark.parametrize(
"expected",
[
# The first (implicit comment)
{
"position": 1,
"comment_position": 0,
"type": "ImplicitComment",
"raw": "This line is an implicit comment.",
"line": 4,
"comment": "This line is an implicit comment.",
},
# The second (explicit comment)
{
"position": 3,
"comment_position": 1,
"type": "ExplicitComment",
"raw": "@comment{\n This is a comment.\n Spanning over two lines.\n}",
"line": 20,
"comment": "This is a comment.\n Spanning over two lines.",
},
# The third (explicit comment)
{
"position": 8,
"comment_position": 2,
"type": "ExplicitComment",
"raw": "@Comment{This is another comment}",
"line": 60,
"comment": "This is another comment",
},
],
)
def test_comments(expected: dict[str, any]) -> None:
library = _split()
comments = library.comments
assert len(comments) == 3
# Raise KeyError if not found
tested_comment = comments[expected["comment_position"]]
# Test parsed comment
assert tested_comment is not None
assert expected["type"] in str(type(tested_comment))
assert tested_comment.raw == expected["raw"]
assert tested_comment.start_line == expected["line"]
assert tested_comment.comment == expected["comment"]
# Make sure position in overall blocks matches
assert library.blocks[expected["position"]] == tested_comment
# Make sure position in comment blocks matches
assert library.comments[expected["comment_position"]] == tested_comment
def test_failed_block():
library = _split()
assert len(library.failed_blocks) == 1
failed_block = library.failed_blocks[0]
expected_raw = "\n".join(example_bibstr.splitlines()[27:40])
assert failed_block.raw.strip() == expected_raw.strip()
assert failed_block.start_line == 27
assert 'Was still looking for field-value closing `"`' in failed_block.error.abort_reason
duplicate_bibtex_entry_keys = """
@article{duplicate,
author = {Duplicate, A.},
title = {Duplicate article 1},
year = {2021},
}
@article{duplicate,
author = {Duplicate, B.},
title = {Duplicate article 2},
year = {2022},
}"""
def test_handles_duplicate_entries():
"""Makes sure that duplicate keys are handled correctly."""
import bibtexparser
lib = bibtexparser.parse_string(duplicate_bibtex_entry_keys)
assert len(lib.blocks) == 2
assert len(lib.entries) == 1
assert len(lib.entries_dict) == 1
assert len(lib.failed_blocks) == 1
assert lib.entries[0]["title"] == "Duplicate article 1"
assert isinstance(lib.failed_blocks[0], bibtexparser.model.DuplicateBlockKeyBlock)
assert lib.failed_blocks[0].previous_block["title"] == "Duplicate article 1"
assert lib.failed_blocks[0].ignore_error_block["title"] == "{Duplicate article 2}"
assert isinstance(lib.failed_blocks[0].ignore_error_block, bibtexparser.model.Entry)
duplicate_bibtex_string_keys = """
@string{duplicate = "Duplicate string 1"}
@string{duplicate = "Duplicate string 2"}
@string{duplicate = "Duplicate string 3"}
"""
def test_handles_duplicate_strings():
"""Makes sure that duplicate string keys are handled correctly."""
import bibtexparser
lib = bibtexparser.parse_string(duplicate_bibtex_string_keys)
assert len(lib.blocks) == 3
assert len(lib.strings) == 1
assert len(lib.strings_dict) == 1
assert len(lib.failed_blocks) == 2
assert lib.strings[0].value == "Duplicate string 1"
assert isinstance(lib.failed_blocks[0], bibtexparser.model.DuplicateBlockKeyBlock)
assert lib.failed_blocks[0].previous_block.value == "Duplicate string 1"
assert lib.failed_blocks[0].ignore_error_block.value == '"Duplicate string 2"'
assert isinstance(lib.failed_blocks[0].ignore_error_block, bibtexparser.model.String)
blocks_not_starting_on_new_lines = """\
@article{KUEH2023S98,
title = {Myocardial Characterisation Using Delayed Dual-Energy Cardiac Computed Tomography},
author = {S.-H. Kueh and J. Benatar and R. Stewart}
}@INPROCEEDINGS{9837531,
author={Hassan, Mona Bakri and Saeed, Rashid A. and Khalifa, Othman and Ali, Elmustafa Sayed and Mokhtar, Rania A. and Hashim, Aisha A.},
title={Green Machine Learning for Green Cloud Energy Efficiency},
doi={10.1109/MI-STA54861.2022.9837531}}@ARTICLE{9372936,
author={Hu, Ning and Tian, Zhihong and Du, Xiaojiang and Guizani, Nadra and Zhu, Zhihan},
title={Deep-Green: A Dispersed Energy-Efficiency Computing Paradigm for Green Industrial IoT},
doi={10.1109/TGCN.2021.3064683}}@ARTICLE{5445167,
author={Kumar, Karthik and Lu, Yung-Hsiang},
title={Cloud Computing for Mobile Users: Can Offloading Computation Save Energy?},
doi={10.1109/MC.2010.98}}
"""
@pytest.mark.parametrize(
"expected",
[
{
"key": "KUEH2023S98",
"type": "article",
"raw": "@article{KUEH2023S98,\n"
"title = {Myocardial Characterisation Using Delayed Dual-Energy Cardiac Computed Tomography},\n"
"author = {S.-H. Kueh and J. Benatar and R. Stewart}\n}",
"start_line": 0,
},
{
"key": "9837531",
"type": "INPROCEEDINGS",
"raw": "@INPROCEEDINGS{9837531,\n"
" author={Hassan, Mona Bakri and Saeed, Rashid A. and Khalifa, Othman and Ali, Elmustafa Sayed and Mokhtar, Rania A. and Hashim, Aisha A.},\n"
" title={Green Machine Learning for Green Cloud Energy Efficiency},\n"
" doi={10.1109/MI-STA54861.2022.9837531}}",
"start_line": 3,
},
{
"key": "9372936",
"type": "ARTICLE",
"raw": "@ARTICLE{9372936,\n"
" author={Hu, Ning and Tian, Zhihong and Du, Xiaojiang and Guizani, Nadra and Zhu, Zhihan},\n"
" title={Deep-Green: A Dispersed Energy-Efficiency Computing Paradigm for Green Industrial IoT},\n"
" doi={10.1109/TGCN.2021.3064683}}",
"start_line": 6,
},
{
"key": "5445167",
"type": "ARTICLE",
"raw": "@ARTICLE{5445167,\n"
" author={Kumar, Karthik and Lu, Yung-Hsiang},\n"
" title={Cloud Computing for Mobile Users: Can Offloading Computation Save Energy?},\n"
" doi={10.1109/MC.2010.98}}",
"start_line": 9,
},
],
)
def test_blocks_not_starting_on_new_lines(expected: dict[str, Any]):
"""Test the new blocks that are not on new lines.
Discussed at https://github.com/sciunto-org/python-bibtexparser/issues/411.
"""
import bibtexparser
lib = bibtexparser.parse_string(blocks_not_starting_on_new_lines)
assert len(lib.entries) == 4
entry_by_key = lib.entries_dict
assert len(entry_by_key) == 4
entry = entry_by_key[expected["key"]]
assert entry.key == expected["key"]
assert entry.entry_type == expected["type"]
assert entry.raw == expected["raw"]
assert entry.start_line == expected["start_line"]