-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathtest_enclosing.py
More file actions
374 lines (308 loc) · 14.6 KB
/
test_enclosing.py
File metadata and controls
374 lines (308 loc) · 14.6 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
from copy import deepcopy
from typing import Union
import pytest
from bibtexparser.library import Library
from bibtexparser.middlewares.enclosing import AddEnclosingMiddleware
from bibtexparser.middlewares.enclosing import RemoveEnclosingMiddleware
from bibtexparser.model import Entry
from bibtexparser.model import Field
from bibtexparser.model import String
from tests.middleware_tests.middleware_test_util import assert_block_does_not_change
from tests.middleware_tests.middleware_test_util import assert_inplace_is_respected
from tests.middleware_tests.middleware_test_util import assert_nonfield_entry_attributes_unchanged
from tests.resources import EDGE_CASE_VALUES
from tests.resources import ENCLOSINGS
def _skip_pseudo_enclosing_value(value: str):
starts_and_ends_in_brackets = value.startswith("{") and value.endswith("}")
starts_and_ends_in_quotes = value.startswith('"') and value.endswith('"')
if starts_and_ends_in_quotes or starts_and_ends_in_brackets:
pytest.skip("No enclosing to remove")
@pytest.mark.parametrize("enclosing", ENCLOSINGS + [pytest.param("{0}", id="no_enclosing")])
@pytest.mark.parametrize("value", EDGE_CASE_VALUES)
@pytest.mark.parametrize("inplace", [True, False], ids=["inplace", "not_inplace"])
def test_removal_of_enclosing_on_string(enclosing, value, inplace):
"""Extensive Matrix-Testing of the RemoveEnclosingMiddleware on Strings.
Also covers the internals for other block types (i.e., Entry),
which thus can be tested more light-weight."""
if enclosing == "{0}":
_skip_pseudo_enclosing_value(value)
# Create test string
key = "someKey"
raw = "<--- does not matter for this unit test -->"
start_line = 5
original = String(start_line=start_line, key=key, raw=raw, value=enclosing.format(value))
middleware = RemoveEnclosingMiddleware(allow_inplace_modification=inplace)
transformed_library = middleware.transform(library=Library([original]))
# Assert correct library state
assert len(transformed_library.blocks) == 1
assert len(transformed_library.strings) == 1
# Assert correct removal of enclosing
transformed = transformed_library.strings[0]
assert transformed.value == value
expected_enclosing = enclosing.format("")[0] if enclosing != "{0}" else "no-enclosing"
assert transformed.parser_metadata["removed_enclosing"] == expected_enclosing
# Assert remaining fields are unchanged
assert transformed.start_line == start_line
assert transformed.key == key
assert transformed.raw == raw
# Assert `allow_inplace_modification` is respected
assert_inplace_is_respected(inplace, original, transformed)
@pytest.mark.parametrize("enclosing", ENCLOSINGS)
@pytest.mark.parametrize("inplace", [True, False], ids=["inplace", "not_inplace"])
def test_removal_of_enclosing_on_entry(enclosing: str, inplace: bool):
"""Test the RemoveEnclosingMiddleware on Entries."""
fields = [
# Enclosed string value
Field(value=enclosing.format("Michael Weiss"), start_line=6, key="author"),
# Unenclosed int value
Field(value="2019", start_line=7, key="year"),
# Enclosed int value
Field(value=enclosing.format("1"), start_line=8, key="month"),
]
input_entry = Entry(
start_line=5,
entry_type="article",
raw="<--- does not matter for this unit test -->",
key="someKey",
fields=fields,
)
middleware = RemoveEnclosingMiddleware(allow_inplace_modification=inplace)
transformed_library = middleware.transform(library=Library([input_entry]))
# Assert correct library state
assert len(transformed_library.blocks) == 1
assert len(transformed_library.entries) == 1
# Assert fields are transformed correctly
transformed_fields = transformed_library.entries[0].fields_dict
assert transformed_fields["author"].value == "Michael Weiss"
assert transformed_fields["year"].value == "2019"
assert transformed_fields["month"].value == "1"
# Assert remaining fields are unchanged
assert_nonfield_entry_attributes_unchanged(input_entry, transformed_library.entries[0])
# Assert `allow_inplace_modification` is respected
assert_inplace_is_respected(inplace, input_entry, transformed_library.entries[0])
@pytest.mark.parametrize("block", ["preamble", "implicit_comment", "explicit_comment"])
@pytest.mark.parametrize("inplace", [True, False], ids=["inplace", "not_inplace"])
def test_no_removal_blocktypes(block: str, inplace: bool):
assert_block_does_not_change(
block_type=block,
middleware=RemoveEnclosingMiddleware(allow_inplace_modification=inplace),
same_instance=inplace,
)
@pytest.mark.parametrize("metadata_enclosing", ["{", '"', "no-enclosing", None])
@pytest.mark.parametrize("default_enclosing", ["{", '"'])
@pytest.mark.parametrize("enclose_ints", [True, False], ids=["enclose_ints", "no_enclose_ints"])
@pytest.mark.parametrize("reuse_previous_enclosing", [True, False], ids=["reuse", "no_reuse"])
@pytest.mark.parametrize("value", EDGE_CASE_VALUES + ["1990"])
@pytest.mark.parametrize("inplace", [True, False], ids=["inplace", "not_inplace"])
def test_addition_of_enclosing_on_entry(
metadata_enclosing: str,
default_enclosing: str,
enclose_ints: bool,
reuse_previous_enclosing: bool,
value: Union[str, int],
inplace: bool,
):
"""Extensive Matrix-Testing of the AddEnclosingMiddleware on Entries.
Also covers the internals for other block types (i.e., String),
which thus can be tested more light-weight."""
# These values not matter for this unit test,
# but must not change during transformation
# (hence, they are created as variables, not directly in Entry constructor)
input_entry = Entry(
start_line=5,
entry_type="article",
raw="<--- does not matter for this unit test -->",
key="someKey",
fields=[Field(value=value, start_line=6, key="year")],
)
if metadata_enclosing is not None:
input_entry.parser_metadata["removed_enclosing"] = {"year": metadata_enclosing}
middleware = AddEnclosingMiddleware(
allow_inplace_modification=inplace,
default_enclosing=default_enclosing,
reuse_previous_enclosing=reuse_previous_enclosing,
enclose_integers=enclose_ints,
)
transformed_library = middleware.transform(library=Library([input_entry]))
# Assert correct library state
assert len(transformed_library.blocks) == 1
assert len(transformed_library.entries) == 1
# Assert correct addition of enclosing
transformed = transformed_library.entries[0]
changed_value = transformed["year"]
# Figure out which enclosing was added
used_enclosing = _figure_out_added_enclosing(changed_value, value)
# Assert correct enclosing was added
if reuse_previous_enclosing and metadata_enclosing is not None:
expected_enclosing = metadata_enclosing
elif (isinstance(value, int) or value.isdigit()) and not enclose_ints:
expected_enclosing = "no-enclosing"
else:
expected_enclosing = default_enclosing
if expected_enclosing == "no-enclosing":
_skip_pseudo_enclosing_value(value)
assert used_enclosing == expected_enclosing
# Assert remaining fields are unchanged
assert_nonfield_entry_attributes_unchanged(input_entry, transformed)
# Assert `allow_inplace_modification` is respected
assert_inplace_is_respected(inplace, input_entry, transformed)
def _figure_out_added_enclosing(changed_value, value):
if changed_value.startswith('"') and changed_value.endswith('"'):
used_enclosing = '"'
elif changed_value.startswith("{") and changed_value.endswith("}"):
used_enclosing = "{"
elif str(changed_value) == str(value):
used_enclosing = "no-enclosing"
else:
raise ValueError(f"Strange encoding: {changed_value}")
return used_enclosing
@pytest.mark.parametrize("metadata_resolving", ["", "journal"])
@pytest.mark.parametrize("metadata_enclosing", ["{", '"', "no-enclosing", None])
@pytest.mark.parametrize("default_enclosing", ["{", '"'])
@pytest.mark.parametrize("enclose_ints", [True, False], ids=["enclose_ints", "no_enclose_ints"])
@pytest.mark.parametrize(
"keep_abbr_string", [True, False], ids=["keep_abbr_string", "no_keep_abbr_string"]
)
@pytest.mark.parametrize("reuse_previous_enclosing", [True, False], ids=["reuse", "no_reuse"])
@pytest.mark.parametrize(
"value",
[
# value, is a abbreviation?
("IEEE_T_PAMI", True),
('IEEE_T_PAMI # "ieee tpami"', True),
('IEEE_T_PAMI" # ieee tpami', False),
('IEEE_T-PAMI # "ieee tpami"', False),
('IEEE_T-PAMI # "ieee # tpami"', False),
('IEEE T-PAMI # "ieee tpami"', False),
],
)
@pytest.mark.parametrize("inplace", [True, False], ids=["inplace", "not_inplace"])
def test_addition_of_enclosing_on_entry_with_abbr(
value: tuple,
metadata_resolving: str,
keep_abbr_string: bool,
metadata_enclosing: str,
default_enclosing: str,
enclose_ints: bool,
reuse_previous_enclosing: bool,
inplace: bool,
):
"""Extensive Matrix-Testing of the AddEnclosingMiddleware on Entries.
Also covers the internals for other block types (i.e., String),
which thus can be tested more light-weight."""
# These values not matter for this unit test,
# but must not change during transformation
# (hence, they are created as variables, not directly in Entry constructor)
value, is_abbr = value
input_entry = Entry(
start_line=5,
entry_type="article",
raw="<--- does not matter for this unit test -->",
key="someKey",
fields=[Field(value=value, start_line=6, key="journal")],
)
if metadata_resolving:
input_entry.parser_metadata["ResolveStringReferences"] = [metadata_resolving]
if metadata_enclosing is not None:
input_entry.parser_metadata["removed_enclosing"] = {"journal": metadata_enclosing}
middleware = AddEnclosingMiddleware(
allow_inplace_modification=inplace,
default_enclosing=default_enclosing,
reuse_previous_enclosing=reuse_previous_enclosing,
enclose_integers=enclose_ints,
keep_abbr_string=keep_abbr_string,
)
transformed_library = middleware.transform(library=Library([input_entry]))
# Assert correct library state
assert len(transformed_library.blocks) == 1
assert len(transformed_library.entries) == 1
# Assert correct addition of enclosing
transformed = transformed_library.entries[0]
changed_value = transformed["journal"]
# Assert correct enclosing was added
if reuse_previous_enclosing and metadata_enclosing is not None:
expected_enclosing = metadata_enclosing
elif (isinstance(value, int) or value.isdigit()) and not enclose_ints:
expected_enclosing = "no-enclosing"
elif not metadata_resolving and keep_abbr_string:
if is_abbr:
expected_enclosing = "no-enclosing"
else:
expected_enclosing = default_enclosing
else:
expected_enclosing = default_enclosing
if expected_enclosing == "no-enclosing":
_skip_pseudo_enclosing_value(value)
assert changed_value == middleware._enclose_value(value, expected_enclosing)
# Assert remaining fields are unchanged
assert_nonfield_entry_attributes_unchanged(input_entry, transformed)
# Assert `allow_inplace_modification` is respected
assert_inplace_is_respected(inplace, input_entry, transformed)
@pytest.mark.parametrize("metadata_enclosing", ["{", '"', None])
@pytest.mark.parametrize("default_enclosing", ["{", '"'])
@pytest.mark.parametrize("enclose_ints", [True, False], ids=["enclose_ints", "no_enclose_ints"])
@pytest.mark.parametrize("reuse_previous_enclosing", [True, False], ids=["reuse", "no_reuse"])
@pytest.mark.parametrize("inplace", [True, False], ids=["inplace", "not_inplace"])
def test_addition_of_enclosing_on_string(
metadata_enclosing: str,
default_enclosing: str,
enclose_ints: bool,
reuse_previous_enclosing: bool,
inplace: bool,
):
input_string = String(
start_line=5,
raw="<--- does not matter for this unit test -->",
key="someKey",
value="someValue", # Value edge-cases are tested in Entry test
)
input_string_copy = deepcopy(input_string)
if metadata_enclosing is not None:
input_string.parser_metadata["removed_enclosing"] = metadata_enclosing
middleware = AddEnclosingMiddleware(
allow_inplace_modification=inplace,
default_enclosing=default_enclosing,
reuse_previous_enclosing=reuse_previous_enclosing,
enclose_integers=enclose_ints, # This should not impact String
)
transformed_library = middleware.transform(library=Library([input_string]))
# Assert correct library state
assert len(transformed_library.blocks) == 1
assert len(transformed_library.strings) == 1
# Assert correct addition of enclosing
transformed = transformed_library.strings[0]
changed_value = transformed.value
# Figure out which enclosing was added
used_enclosing = _figure_out_added_enclosing(changed_value, input_string.value)
# Assert correct enclosing was added
if reuse_previous_enclosing and metadata_enclosing is not None:
expected_enclosing = metadata_enclosing
else:
# Note: `enclose_integers` param is not relevant for String
expected_enclosing = default_enclosing
assert used_enclosing == expected_enclosing
# Assert remaining fields are unchanged
assert transformed.start_line == input_string_copy.start_line
assert transformed.raw == input_string_copy.raw
assert transformed.key == input_string_copy.key
# Assert `allow_inplace_modification` is respected
assert_inplace_is_respected(inplace, input_string, transformed)
@pytest.mark.parametrize("block", ["preamble", "implicit_comment", "explicit_comment"])
@pytest.mark.parametrize("reuse_encoding", [True, False], ids=["reuse", "no_reuse"])
@pytest.mark.parametrize("enclose_int", [True, False], ids=["enclose_int", "no_enclose_int"])
@pytest.mark.parametrize("default_enc", ["{", '"'])
@pytest.mark.parametrize("inplace", [True, False], ids=["inplace", "not_inplace"])
def test_no_addition_block_types(
block: str, reuse_encoding: bool, enclose_int: bool, default_enc: str, inplace: bool
):
assert_block_does_not_change(
block_type=block,
middleware=AddEnclosingMiddleware(
reuse_previous_enclosing=reuse_encoding,
enclose_integers=enclose_int,
default_enclosing=default_enc,
allow_inplace_modification=inplace,
),
same_instance=inplace,
)
# TODO round-trip tests (removal -> addition -> removal)