-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_tests.py
More file actions
258 lines (223 loc) · 10.8 KB
/
preview_tests.py
File metadata and controls
258 lines (223 loc) · 10.8 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
import os
import pytest
from .utility.preview_utilities import Params
from .preview import preview_function
class TestPreviewFunction():
"""
TestCase Class used to test the algorithm.
---
Tests are used here to check that the algorithm written
is working as it should.
It's best practise to write these tests first to get a
kind of 'specification' for how your algorithm should
work, and you should run these tests before committing
your code to AWS.
Read the docs on how to use unittest here:
https://docs.python.org/3/library/unittest.html
Use preview_function() to check your algorithm works
as it should.
"""
# Import tests that makes sure that mathematical expression comparison works as expected
from .tests.symbolic_preview_tests import TestPreviewFunction as TestSymbolicComparison
# Import tests that makes sure that physical quantities are handled as expected
from .tests.physical_quantity_preview_tests import TestPreviewFunction as TestQuantityComparison
def test_empty_latex_expression(self):
response = ""
params = Params(is_latex=True)
result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]
assert preview["latex"] == ""
def test_empty_non_latex_expression(self):
response = ""
params = Params(is_latex=False)
result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]
assert preview["sympy"] == ""
def test_latex_and_sympy_are_returned(self):
response = "x+1"
params = Params(is_latex=True)
result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]
assert "latex" in preview.keys()
assert "sympy" in preview.keys()
response = "x+1"
params = Params(is_latex=False)
result = preview_function(response, params)
assert "preview" in result
preview = result["preview"]
assert "latex" in preview
assert "sympy" in preview
def test_natural_logarithm_notation(self):
response = "ln(x)"
params = Params(is_latex=False)
result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]
assert preview["latex"] == r"\ln{\left(x \right)}"
@pytest.mark.parametrize(
"response, is_latex, elementary_functions, response_latex, response_sympy", [
("e", False, True, "e", "E",),
("oo", False, True, "\\infty", "oo"),
("ex", False, True, "e \\cdot x", "Ex"),
("e * x", False, True, "e \\cdot x", "E * x"),
("E", False, True, "e", "E", ),
("e", False, False, "e", "e",),
("ex", False, False, "e \\cdot x", "ex"),
("e * x", False, False, "e \\cdot x", "e * x"),
("E", False, False, "E", "E",),
("E", True, True, "E", "E",),
("e", True, False, "e", "E",),
("ex", True, False, "ex", "E*x"),
("e * x", True, False, "e * x", "E*x"),
("E", True, False, "E", "E",),
("ER_2", True, False, "ER_2", "E*R_2",),
("exp(1)", False, True, "e^{1}", "exp(1)"),
("e**1", False, True, "e^{1}", "E**1"),
("e^{1}", True, True, "e^{1}", "E"),
("exp(0)", False, True, "e^{0}", "exp(0)"),
("e**0", False, True, "e^{0}", "E**0"),
("e^{0}", True, True, "e^{0}", "1"),
("exp(2)", False, True, "e^{2}", "exp(2)"),
("e**2", False, True, "e^{2}", "E**2"),
("e^{2}", True, True, "e^{2}", "exp(2)"),
("exp(x)", False, True, "e^{x}", "exp(x)"),
("e**x", False, True, "e^{x}", "E**x"),
("e^{x}", True, True, "e^{x}", "exp(x)")
]
)
def test_eulers_number_notation(self, response, is_latex, elementary_functions, response_latex, response_sympy):
params = Params(is_latex=is_latex, elementary_functions=elementary_functions, strict_syntax=False)
result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]
assert preview["latex"] == response_latex
assert preview["sympy"] == response_sympy
@pytest.mark.parametrize(
"response, is_latex, response_latex, response_sympy, symbols", [
("e**ea", False, "e^{ea}", "E**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
("e**Ea", False, "e^{ea}", "E**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
("e^{ea}", True, "e^{ea}", "exp(ea)", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
("e^{Ea}", True, "e^{Ea}", "e**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
("e**aea", False, "e^{aea}", "E**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
("e**aEa", False, "e^{aea}", "E**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
("e^{aea}", True, "e^{aea}", "exp(aea)", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
("e^{aEa}", True, "e^{aEa}", "e**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
]
)
def test_e_latex(self, response, is_latex, response_latex, response_sympy, symbols):
params = {
"is_latex": is_latex,
"strict_syntax": False,
"elementary_functions": True,
"symbols": symbols,
}
result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]
assert preview["latex"] == response_latex, "latex_error"
assert preview["sympy"] == response_sympy, "sympy_error"
@pytest.mark.parametrize(
"response, is_latex, response_latex, response_sympy, symbols", [
("ab", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("Ab", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("aB", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("AB", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("ab", True, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("Ab", True, "Ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("aB", True, "aB", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("AB", True, "AB", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
]
)
def test_alias(self, response, is_latex, response_latex, response_sympy, symbols):
params = {
"is_latex": is_latex,
"strict_syntax": False,
"elementary_functions": True,
"symbols": symbols,
}
result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]
assert preview["latex"] == response_latex, "latex error"
assert preview["sympy"] == response_sympy, "sympy error"
@pytest.mark.parametrize(
"response, is_latex, response_latex, response_sympy",
[
("plus_minus x", False, '\\left\\{- x,~x\\right\\}', "plus_minus x"),
("\\pm x", True, '\\pm x', '{-x, x}'),
(r"\pm x^{2}+\mp y^{2}", True, "\pm x^{2}+\mp y^{2}", "{-x**2 + y**2, x**2 - y**2}"),
("plus_minus x**2 + minus_plus y**2", False, r"\left\{- x^{2} + y^{2},~x^{2} - y^{2}\right\}", "plus_minus x**2 + minus_plus y**2"),
("- minus_plus x^2 minus_plus y^2", False, r"\left\{- x^{2} + y^{2},~x^{2} - y^{2}\right\}", "- minus_plus x^2 minus_plus y^2"),
("- minus_plus x^2 - plus_minus y^2", False, r"\left\{- x^{2} - - y^{2},~x^{2} - y^{2}\right\}", "- minus_plus x^2 - plus_minus y^2"),
("pm x**2 + mp y**2", False, r"\left\{- x^{2} + y^{2},~x^{2} - y^{2}\right\}", "plus_minus x**2 + minus_plus y**2"),
("+- x**2 + -+ y**2", False, r"\left\{- x^{2} + y^{2},~x^{2} - y^{2}\right\}", "plus_minus x**2 + minus_plus y**2"),
]
)
def test_using_plus_minus_symbols(self, response, is_latex, response_latex, response_sympy):
params = {
"strict_syntax": False,
"elementary_functions": True,
"is_latex": is_latex,
"symbols": {
"plus_minus": {
"latex": r"\(\pm\)",
"aliases": ["pm", "+-"],
},
"minus_plus": {
"latex": r"\(\mp\)",
"aliases": ["mp", "-+"],
},
},
}
params = Params(**params)
result = preview_function(response, params)
assert result["preview"]["latex"] == response_latex
assert result["preview"]["sympy"] == response_sympy
def test_lh_rh_response(self):
params = {
"strict_syntax": False,
"elementary_functions": True,
"is_latex": False,
"symbols": {
"plus_minus": {
"latex": r"\(\pm\)",
"aliases": ["pm", "+-"],
},
"minus_plus": {
"latex": r"\(\mp\)",
"aliases": ["mp", "-+"],
},
},
}
params = Params(**params)
result = preview_function("x + y = y + x", params)
assert result["preview"]["latex"] == "x + y=x + y"
assert result["preview"]["sympy"] == "x + y=y + x"
def test_multi_character_implicit_multi_variable(self):
params = {
"strict_syntax": False,
"elementary_functions": True,
"convention": "implicit_higher_precedence",
"symbols": {
"a": {"aliases": ["a"], "latex": "a"},
"bc": {"aliases": ["bc"], "latex": "bc"},
"d": {"aliases": ["d"], "latex": "d"}
},
}
response_full = "a/(bc*d)"
response_implicit_bracket = "a/(bcd)"
response_implicit_no_bracket = "a/bcd"
result = preview_function(response_full, params)
assert result["preview"]["latex"] == '\\frac{a}{bc \\cdot d}'
assert result["preview"]["sympy"] == "a/(bc*d)"
result = preview_function(response_implicit_bracket, params)
assert result["preview"]["latex"] == '\\frac{a}{bc \\cdot d}'
assert result["preview"]["sympy"] == "a/(bcd)"
result = preview_function(response_implicit_no_bracket, params)
assert result["preview"]["latex"] =='\\frac{a}{bc \\cdot d}'
assert result["preview"]["sympy"] == "a/bcd"
if __name__ == "__main__":
pytest.main(['-xk not slow', "--tb=line", os.path.abspath(__file__)])