-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_py2tex.py
140 lines (122 loc) ยท 6.55 KB
/
test_py2tex.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
import unittest, re
import py2tex
class Test_Pattern(unittest.TestCase):
@staticmethod
def get_matches(pytex):
matches = re.search(py2tex.PYTEX_PATTERN, pytex)
return matches.groups() if matches is not None else []
def compare_string_list(self, strlist1, strlist2):
""" Compares respective strings in two lists.
"""
for str1, str2 in zip(strlist1, strlist2):
self.assertMultiLineEqual(str1, str2)
def test_empty_input(self):
""" Verify pytex pattern works with empty input.
"""
filestring = ''
executables = self.get_matches(filestring)
self.assertEqual(len(executables), 0)
def test_no_executables(self):
""" Verify get_executable_strings() works with only non-executable input.
"""
filestring = 'this is some test LaTeX input.'
executables = self.get_matches(filestring)
self.assertEqual(len(executables), 0)
filestring = 'this is some test LaTeX input.\nThis time, multiline!'
executables = self.get_matches(filestring)
self.assertEqual(len(executables), 0)
def test_only_single_executable(self):
""" Verify get_executable_strings() works with executable input (single).
"""
executables = self.get_matches('๐๐')
self.compare_string_list(executables, [''])
def test_repeated_executable(self):
""" Verify get_executable_strings() works with executable input (multiple).
"""
executables = self.get_matches('๐๐๐๐๐๐')
self.compare_string_list(executables, ['','',''])
executables = self.get_matches('๐some python code๐')
self.compare_string_list(executables, ['some python code'])
executables = self.get_matches('๐some python code๐๐more python code๐')
self.compare_string_list(executables, ['some python code','more python code'])
def test_start_with_executable(self):
""" Verify get_executable_strings() works when starting with an executable.
"""
executables = self.get_matches('๐๐ and some LaTeX')
self.compare_string_list(executables, [''])
executables = self.get_matches('๐some python๐ and some LaTeX')
self.compare_string_list(executables, ['some python'])
executables = self.get_matches('๐some python๐ and some LaTeX ๐and more python๐ and more LaTeX')
self.compare_string_list(executables, ['some python','and more python'])
def test_end_with_executable(self):
""" Verify get_executable_strings() works when ending with an executable.
"""
executables = self.get_matches('Some LaTeX ๐๐')
self.compare_string_list(executables, [''])
executables = self.get_matches('Some LaTeX ๐and some python๐')
self.compare_string_list(executables, ['and some python'])
executables = self.get_matches('Some LaTeX ๐and some python๐ and some LaTeX ๐and more python๐')
self.compare_string_list(executables, ['and some python','and more python'])
class Test_Run_Executable(unittest.TestCase):
def test_collect_print_statements(self):
""" Verify that print statements are collected.
"""
self.assertMultiLineEqual('printed\n',
py2tex.collect_stdout_from_executable('print("printed")'))
self.assertMultiLineEqual('printed\nprinted again\n',
py2tex.collect_stdout_from_executable('print("printed")\nprint("printed again")'))
def test_import_between_execs(self):
""" Verify that imported packages are available between calls to exec.
"""
local_scope = global_scope = {}
py2tex.collect_stdout_from_executable('import sys', local_scope, global_scope)
out = py2tex.collect_stdout_from_executable('sys.stdout.write("sys still exists")', local_scope, global_scope)
self.assertMultiLineEqual(out, "sys still exists")
def test_exception_passed_through(self):
""" Verify that exceptions are passed through.
"""
self.assertRaises(Exception, py2tex.collect_stdout_from_executable, 'raise Exception')
class Test_Pytex_Conversion(unittest.TestCase):
def test_empty_input(self):
""" Verify conversion works with empty input.
"""
self.assertMultiLineEqual('', py2tex.pytex_to_tex(''))
def test_no_executables(self):
""" Verify conversion works with only non-executable input.
"""
self.assertMultiLineEqual('this is some test LaTeX input.',
py2tex.pytex_to_tex('this is some test LaTeX input.'))
self.assertMultiLineEqual('this is some test LaTeX input.\nThis time, multiline!',
py2tex.pytex_to_tex('this is some test LaTeX input.\nThis time, multiline!'))
def test_only_single_executable(self):
""" Verify conversion works with executable input (single).
"""
self.assertMultiLineEqual('', py2tex.pytex_to_tex('๐๐'))
def test_repeated_executable(self):
""" Verify conversion works with executable input (multiple).
"""
self.assertMultiLineEqual('', py2tex.pytex_to_tex('๐๐๐๐๐๐'))
self.assertMultiLineEqual('test\n',
py2tex.pytex_to_tex('๐print("test")๐'))
self.assertMultiLineEqual('test\ntest again\n',
py2tex.pytex_to_tex('๐print("test")๐๐print("test again")๐'))
self.assertMultiLineEqual('sys still exists',
py2tex.pytex_to_tex('๐import sys๐๐sys.stdout.write("sys still exists")๐'))
def test_start_with_executable(self):
""" Verify conversion works when starting with an executable.
"""
self.assertMultiLineEqual(' and some LaTeX',
py2tex.pytex_to_tex('๐๐ and some LaTeX'))
self.assertMultiLineEqual('test\n and some LaTeX',
py2tex.pytex_to_tex('๐print("test")๐ and some LaTeX'))
self.assertMultiLineEqual('some python\n and some LaTeX and more python\n and more LaTeX',
py2tex.pytex_to_tex('๐print("some python")๐ and some LaTeX ๐print("and more python")๐ and more LaTeX'))
def test_end_with_executable(self):
""" Verify conversion works when ending with an executable.
"""
self.assertMultiLineEqual('and some LaTeX ',
py2tex.pytex_to_tex('and some LaTeX ๐๐'))
self.assertMultiLineEqual('Some LaTeX test\n',
py2tex.pytex_to_tex('Some LaTeX ๐print("test")๐'))
self.assertMultiLineEqual('Some LaTeX and more python\n and more LaTeX some other python\n',
py2tex.pytex_to_tex('Some LaTeX ๐print("and more python")๐ and more LaTeX ๐print("some other python")๐'))