/
test_main.py
453 lines (380 loc) · 15.3 KB
/
test_main.py
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import argparse
import copy
import pytest
import json
import sys
import os
import io
from pretex import pretex
from pretex.Transformer import Transformer, get_document_contents, strip_comments, get_default_config, \
get_transformed_math
from pretex.Transformer import get_inside_str
def silent_remove(filename):
try:
os.remove(filename)
except OSError:
pass
@pytest.fixture(scope="module")
def trans(request):
ttt = Transformer()
return ttt
class TestClass(object):
def test_get_document_contents_full(self, trans):
test_string = get_inside_str(r'''
header
\begin{document}
%comment1
text
%comment2a
%comment2b
$math$
\end{document}
after document
''')
expected = ('header\n\\begin{document}',
'\n%comment1\ntext\n%comment2a\n%comment2b\n$math$\n',
'\\end{document}\nafter document')
assert get_document_contents(test_string) == expected
def test_get_document_contents_bare(self, trans):
test_string = get_inside_str(r'''
%comment1
text
%comment2a
%comment2b
$math$
''')
expected = ("", '%comment1\ntext\n%comment2a\n%comment2b\n$math$', "")
assert get_document_contents(test_string) == expected
def test_strip_comments(self, trans):
test_string = get_inside_str(r'''
header
\begin{document}
%comment1
text
%comment2a
%comment2b
$math$
abc %comm
\end{document}
after document
''')
expected = get_inside_str(r'''
header
\begin{document}
text
$math$
abc
\end{document}
after document
''')
assert strip_comments(test_string) == expected
def test_get_pretextec_tree_inline(self, trans):
test_str = get_inside_str(r'''
text $x^2$ bar
''')
result = trans.get_pretextec_tree(test_str)
expected = [{'type': 'text', 'content': 'text $'},
{'type': 'math_env', 'pretexes': [], 'content': 'x^2'},
{'type': 'text', 'content': '$ bar'}]
assert result == expected
def test_get_pretextec_tree_env(self, trans):
test_str = get_inside_str(r'''
text
\begin{align}
a*b
\end{align}
''')
result = trans.get_pretextec_tree(test_str)
expected = [{'content': 'text\n\\begin{align}', 'type': 'text'},
{'content': '\na\\cdot b\n', 'pretexes':
[{'start': 2, 'end': 8, 'type': 'cdot'}],
'type': 'math_env'},
{'content': '\\end{align}', 'type': 'text'}]
assert result == expected
def test_get_transformed_str_basic(self, trans):
test_str = get_inside_str(r'''
a\begin{document}
text %comm 1
\begin{align}
a*b %comm 2
\end{align}
\end{document}b
''')
test_str_expected = get_inside_str(r'''
a\begin{document}
text
\begin{align}
a\cdot b
\end{align}
\end{document}b
''')
result = trans.get_transformed_str(test_str)
assert result == test_str_expected
def test_parse_filenames(self, trans):
default_config = get_default_config()
with pytest.raises(SystemExit):
pretex.parse_cmd_arguments(default_config, [])
with pytest.raises(ValueError):
pretex.parse_cmd_arguments(default_config, "same_filename.tex -o same_filename.tex".split())
with pytest.raises(ValueError):
pretex.parse_cmd_arguments(default_config, "test.tex --set unknown_command=disabled".split())
assert pretex.parse_cmd_arguments(default_config, "in.tex -o out.tex".split()) == (
"in.tex", "out.tex", default_config)
assert pretex.parse_cmd_arguments(default_config, ["in.tex"]) == ("in.tex", "in_t.tex", default_config)
config_expected = copy.deepcopy(default_config)
config_expected["cdot"] = "disabled"
assert pretex.parse_cmd_arguments(default_config, "test.tex --set cdot=disabled".split()) == (
"test.tex", "test_t.tex", config_expected)
config_expected = copy.deepcopy(default_config)
config_expected["cdot"] = "disabled"
config_expected["geq"] = "disabled"
assert pretex.parse_cmd_arguments(default_config, "in.tex --set cdot=disabled --set geq=disabled".split()) == (
"in.tex", "in_t.tex", config_expected)
config_expected = copy.deepcopy(default_config)
config_expected["html"] = "enabled"
assert pretex.parse_cmd_arguments(default_config, "in.tex --html".split()) == (
"in.tex", "in_t.tex", config_expected)
def test_re_sub_superscript(self, trans):
trans.config["sub_superscript"] = "enabled"
test_cases = [
(r"a_abc", r"a_{abc}"),
(r"a_ abc b", r"a_ {abc} b"),
(r"e^a+b", r"e^{a+b}"),
(r"\tau_1+\tau_2", ""),
(r"\tau_\alpha", "")
]
for test_input, expected_output in test_cases:
output = get_transformed_math(test_input, trans.config)
assert output[0] == (expected_output or test_input)
def test_re_sub_superscript_agg(self, trans):
trans.config["sub_superscript"] = "aggressive"
test_cases = [
(r"a_abc", r"a_{abc}"),
(r"a_ abc b", r"a_ {abc} b"),
(r"e^a+b", r"e^{a+b}"),
(r"\tau_1+\tau_2", ""),
(r"\tau_\alpha", ""),
(r"a_i=0,j=0 ", r"a_{i=0,j=0} "),
(r"\int_n=1 ^42+x ", r"\int_{n=1} ^{42+x} ")
]
for test_input, expected_output in test_cases:
output = get_transformed_math(test_input, trans.config)
assert output[0] == (expected_output or test_input)
def test_cdot(self, trans):
trans.config["cdot"] = "enabled"
test_cases = [
(r"a*b", r"a\cdot b"),
(r"a*b*c", r"a\cdot b\cdot c"),
(r"a^*b", "")
]
for test_input, expected_output in test_cases:
assert get_transformed_math(test_input, trans.config)[0] == (expected_output or test_input)
def test_dots(self, trans):
trans.config["dots"] = "enabled"
test_cases = [
(r"1,...,...b", r"1,\dots ,\dots b"),
(r"a....b", r"a....b")]
for test_input, expected_output in test_cases:
output = get_transformed_math(test_input, trans.config)
assert output[0] == expected_output
def test_frac(self, trans):
trans.config["frac"] = "enabled"
test_cases = [
(r"\frac a+b c+d", r"\frac{a+b}{c+d}"),
(r"\frac a+b 2", r"\frac{a+b}{2}"),
(r"\frac {a+b 2", r"\frac {a+b 2"),
(r"\frac aa bb \frac aa bb", r"\frac{aa}{bb} \frac{aa}{bb}")
]
for test_input, expected_output in test_cases:
assert get_transformed_math(test_input, trans.config)[0] == expected_output
def test_dot_normal(self, trans):
trans.config["dot"] = "enabled"
test_cases = [(r"a b. c", r"a \dot{b} c")]
for test_input, expected_output in test_cases:
assert get_transformed_math(test_input, trans.config)[0] == expected_output
def test_re_ddot_easy(self, trans):
trans.config["dot"] = "enabled"
testcases = [
(r"$a \phi.. b$", r"$a \ddot{\phi} b$"),
(r"b.f", r"b.f"),
(r"b. f", r"\dot{b} f"),
(r"b. f", r"\dot{b} f"),
(r"ab.. f", r"\ddot{ab} f"),
(r"f=f(x., x.., t)", r"f=f(\dot{x}, \ddot{x}, t)")
]
for test_input, test_output in testcases:
result = get_transformed_math(test_input, trans.config)
assert result[0] == test_output
def test_re_ddot_compl(self, trans):
trans.config["dot"] = "enabled"
invalid_testcases = [r"$\phi..b$", r"$a\vec x..b$", r"$a\vec{abc}..b$"]
for test_input in invalid_testcases:
assert get_transformed_math(test_input, trans.config)[0] == test_input
testcases = [
(r"a \phi.. b", r"a \ddot{\phi} b"),
(r"a \phi..", r"a \ddot{\phi}"),
(r"\varphi. ", r"\dot{\varphi} "),
(r"a \vec x.. b", r"a \ddot{\vec x} b"),
(r"{a \vec{abc}.)", r"{a \dot{\vec{abc}})"),
(r"a q_i.. b", r"a \ddot{q_i} b")
]
for test_input, expected_output in testcases:
assert get_transformed_math(test_input, trans.config)[0] == expected_output
def test_braket(self, trans):
trans.config["braket"] = "enabled"
testcases = [
(r"foo bar <a|b|c>", r"foo bar \braket{a|b|c}"),
(r"foo bar <a|b>", r"foo bar \braket{a|b}"),
(r"|ket>", r"\ket{ket}"),
(r"|ket><bra|", r"\ket{ket}\bra{bra}"),
(r"x|ket> <bra| x", r"x\ket{ket} \bra{bra} x"),
(r"|ket>x", r"|ket>x"),
(r"|ke t>", r"|ke t>"),
(r"= { x | x>0 }", r"= { x | x>0 }")
]
for test_input, test_output in testcases:
result = get_transformed_math(test_input, trans.config)
assert result[0] == test_output
def test_arrow(self, trans):
testcases = [
(r"a -> b", r"a \to b"),
(r"a ->^{1+1} b", r"a \xrightarrow{1+1} b"),
]
for test_input, test_output in testcases:
result = get_transformed_math(test_input, trans.config)
assert result[0] == test_output
trans.config["arrow"] = "conservative"
testcases = [
(r"a -> b", r"a \to b"),
(r"a ->^{1+1} b", ""),
]
for test_input, test_output in testcases:
result = get_transformed_math(test_input, trans.config)
assert result[0] == test_output or test_input
def test_substack(self, trans):
testcases = [
(r"\sum_{i<m \\ j<n}", r"\sum_{\substack{i<m \\ j<n}}"),
(r"\sum_{\substack{i<m \\ j<n}}", r""),
]
for test_input, test_output in testcases:
result = get_transformed_math(test_input, trans.config)
assert result[0] == test_output or test_input
def test_simple(self, trans):
test_cases = [
("arrow", r"a -> b", r"a \to b"),
("arrow", r"a -> b", r"a \to b"),
("approx", r"a~=b", r"a\approx b"),
("approx", r"a~= b", r"a\approx b"),
("approx", r"a~= b", r"a\approx b"),
("leq", r"a<=b", r"a\leq b"),
("geq", r"a>=b", r"a\geq b"),
("ll", r"a<<b", r"a\ll b"),
("gg", r"a>>b", r"a\gg b"),
("neq", r"a != b", r"a \neq b")
]
for name, test_input, test_output in test_cases:
trans.config[name] = "enabled"
result = get_transformed_math(test_input, trans.config)
assert result[0] == test_output
def test_auto_align(self, trans):
test_string_1 = r'''
a = b \\
x = y
'''
test_string_1_expected = r'''
a &= b \\
x &= y
'''
test_string_2 = r'''
a = x = b \\
x = y
'''
test_string_3 = r'''
a = b \\
x &= y
'''
test_string_4 = r'''
a = b
x = y
'''
test_string_4_expected = r'''
a &= b \\
x &= y
'''
test_config = get_default_config()
test_config["auto_align"] = "enabled"
assert get_transformed_math(test_string_1, test_config, "align")[0] == test_string_1_expected
assert get_transformed_math(test_string_2, test_config, "align")[0] == test_string_2
assert get_transformed_math(test_string_3, test_config, "align")[0] == test_string_3
assert get_transformed_math(test_string_4, test_config, "align")[0] == test_string_4_expected
test_config = get_default_config()
test_config["auto_align"] = "disabled"
assert get_transformed_math(test_string_1, test_config, "align")[0] == test_string_1
def test_skip(self, trans):
invariant_inputs = [
(r"$a.$", ["dot"]),
(r"$a..$", ["dot"]),
(r"foo ${\oint\limits_ a^b-2 x^2$", ["sub_superscript"]),
(r"foo $\frac a+b c+d x$", ["frac"]),
(r"foo $ bar a*b$", ["cdot"]),
(r"foo $ bar a, b, ..., n$", ["dots"]),
(r"foo $ bar <a|b|c>$", ["braket"]),
(r"foo $ a. <a|b|c>$", ["braket", "dot"]),
(r"u_tt", ["sub_superscript"])
]
for test_input, exclude_cmds in invariant_inputs:
for cmd in exclude_cmds:
trans.config[cmd] = "disabled"
assert get_transformed_math(test_input, trans.config)[0] == test_input
@pytest.fixture(scope="module")
def mock_testfile(self, request):
with io.open("test_simple.tex", 'w', encoding='utf-8') as file_out:
file_out.write(r"$\frac aa bb$")
def cleanup():
silent_remove("test_simple.tex")
silent_remove("test_simple_t.tex")
request.addfinalizer(cleanup)
def test_main_simple(self, monkeypatch, mock_testfile):
monkeypatch.setattr(sys, 'argv', "xxx test_simple.tex".split())
pretex.main()
with io.open("test_simple_t.tex", 'r', encoding='utf-8') as file_read:
test_file_content = file_read.read()
assert test_file_content == r"$\frac{aa}{bb}$"
def test_main_complex(self, monkeypatch):
monkeypatch.setattr(sys, 'argv', "xxx tests/test_file.tex --html --set auto_align=enabled --set brackets=enabled".split())
pretex.main()
with io.open("tests/test_file_t.tex", 'r', encoding='utf-8') as file_read:
test_file_content = file_read.read()
with io.open("tests/test_file_expected.tex", 'r', encoding='utf-8') as file_read:
test_expected_content = file_read.read()
assert test_file_content == test_expected_content
silent_remove("tests/test_file_t.tex")
#
#
# def test_arxiv(self, monkeypatch):
# arxiv_files = [
# # "arxiv_temp.tex",
# "arxiv_astro-ph.tex",
# "arxiv_hep-ex.tex",
# "arxiv_hep-th.tex",
# "arxiv_math-ph.tex",
# "arxiv_physics.tex",
# "arxiv_quant-ph.tex",
# "arxiv_math.tex"
# ]
# for filename in arxiv_files:
# input_path = "tests/" + filename
#
# dot_position = input_path.rfind(".")
# output_path = input_path[:dot_position] + "_t" + input_path[dot_position:]
#
# monkeypatch.setattr(sys, 'argv', ['xxx', input_path])
# pretex.main()
# with io.open(input_path, 'r', encoding='utf-8') as file_read:
# test_file_content = file_read.read()
# with io.open(output_path, 'r', encoding='utf-8') as file_read:
# test_expected_content = file_read.read()
# assert test_file_content == test_expected_content
# silent_remove(output_path)