Skip to content

Commit 6210029

Browse files
authored
Fix/mech5003 (#245)
* Added tests for MECH5003 issue * Added pre-processing of the reserved keyword 'as' * Fixed preview tests * Fixed tests * Updated test names
1 parent aca2ebd commit 6210029

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

app/evaluation_tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,30 @@ def test_mu_preview_evaluate(self):
215215
result = evaluation_function(response, answer, params)
216216
assert result["is_correct"] is True
217217

218+
@pytest.mark.parametrize(
219+
"response, is_latex, is_correct", [
220+
("A/s(e**(-a*s)-e**(-b*s))", False, True),
221+
("A/s(e**(-as)-e**(-bs))", False, True),
222+
("(A/s)( e^{-a*s}-e^{-b*s})", True, True),
223+
("(A/s)( e^{-as}-e^{-bs})", True, True),
224+
(r"\frac{A}{s} (e^{-a*s}-e^{-b*s})", True, True),
225+
(r"\frac{A}{s} (e^{-a s}-e^{-b s})", True, True)
226+
]
227+
)
228+
def test_laplace_transforms(self, response, is_latex, is_correct):
229+
230+
params = {
231+
"is_latex": is_latex,
232+
"strict_syntax": False,
233+
"elementary_functions": True,
234+
"convention": "implicit_higher_precedence",
235+
}
236+
answer = "A/s(e**(-a*s)-e**(-b*s))"
237+
238+
result = evaluation_function(response, answer, params)
239+
assert result["is_correct"] == is_correct
240+
241+
218242

219243
if __name__ == "__main__":
220244
pytest.main(['-xk not slow', '--tb=short', '--durations=10', os.path.abspath(__file__)])

app/preview_tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,27 @@ def test_multi_character_implicit_multi_variable(self):
227227
assert result["preview"]["latex"] =='\\frac{a}{bc \\cdot d}'
228228
assert result["preview"]["sympy"] == "a/bcd"
229229

230+
@pytest.mark.parametrize(
231+
"response, is_latex, latex, sympy", [
232+
("A/s(e**(-a*s)-e**(-b*s))", False, r"\frac{A \cdot \left(e^{- a \cdot s} - e^{- b \cdot s}\right)}{s}", "A/s( E**(-a*s)- E**(-b*s))"),
233+
("A/s(e**(-as)-e**(-bs))", False, r"\frac{A \cdot \left(e^{- a \cdot s} - e^{- b \cdot s}\right)}{s}", "A/s( E**(-a*s)- E**(-bs))"),
234+
("(A/s)( e^{-a*s}-e^{-b*s})", True, r"(A/s)( e^{-a*s}-e^{-b*s})", "A*(-exp(-b*s) + exp(-a*s))/s"),
235+
("(A/s)( e^{-as}-e^{-bs})", True, r"(A/s)( e^{-as}-e^{-bs})", "A*(-exp(-b*s) + exp(-a*s))/s"),
236+
]
237+
)
238+
def test_laplace_transforms(self, response, is_latex, latex, sympy):
239+
params = {
240+
"is_latex": is_latex,
241+
"strict_syntax": False,
242+
"elementary_functions": True,
243+
"convention": "implicit_higher_precedence",
244+
}
245+
246+
result = preview_function(response, params)
247+
assert result["preview"]["latex"] == latex
248+
assert result["preview"]["sympy"] == sympy
249+
250+
230251

231252
if __name__ == "__main__":
232253
pytest.main(['-xk not slow', "--tb=line", os.path.abspath(__file__)])

app/utility/expression_utilities.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ def substitute_input_symbols(exprs, params):
412412
# we need to make sure it is not substituted back in
413413
substitutions = [(original, subs.replace("lambda", "lamda")) for (original, subs) in substitutions]
414414

415+
# Since 'as' is a reserved keyword in python, we add a subsitution of 'as' to 'a*s' if 'as' is not a defined symbol
416+
if 'as' not in input_symbols.keys():
417+
substitutions += [('as', 'a*s')]
418+
415419
substitutions = list(set(substitutions))
416420
if len(substitutions) > 0:
417421
substitutions.sort(key=substitutions_sort_key)

0 commit comments

Comments
 (0)