Skip to content

Commit

Permalink
Hand encoded unicode literals replaces scriptwide utf-8 encoding.
Browse files Browse the repository at this point in the history
There is a small issue outstanding with the new test. I'm not sure how
to fix it. There is a test function found in test_expressions.py
called:

test_negation_with_emdash()

It loops through badformulas testing to make sure badly formatted
formulas are unable to parse.  That bit works, that is if the test
fails then exceptions are raised, however, pytest has a feature that
should be used, but is not in this case. A particular function:
pytest.raises can take a keyword argument called "match" that ensures
error messages raised along with the parse error are formatted
correctly.

There is or was a bug that seems to mirror the issue here:

pytest-dev/pytest#5478
  • Loading branch information
drhodes committed Nov 16, 2020
1 parent 3fb8748 commit cc24eb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
3 changes: 1 addition & 2 deletions mitxgraders/helpers/calc/expressions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
r"""
expressions.py
Expand Down Expand Up @@ -390,7 +389,7 @@ def get_grammar(self):
plus = Literal("+")

# Also accept unicode emdash
emdash = Literal("")
emdash = Literal("\u2014")
emdash.setParseAction(lambda: "-")

minus = Literal("-") | emdash
Expand Down
41 changes: 27 additions & 14 deletions tests/helpers/calc/test_expressions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
"""
Tests of expressions.py that aren't covered elsewhere
"""
from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, unicode_literals

import re
import numpy as np
Expand Down Expand Up @@ -148,21 +147,35 @@ def test_negation():
with raises(UnableToParse, match=re.escape(msg.format(formula))):
evaluator(formula)

def test_negation_with_endash():
"""Test that appropriate numbers of +/– signs are accepted"""
assert evaluator("1+–1")[0] == 0
assert evaluator("1––1")[0] == 2
assert evaluator("2*–1")[0] == -2
assert evaluator("2/–4")[0] == -0.5
assert evaluator("–1+–1")[0] == -2
assert evaluator("+1+–1")[0] == 0
assert evaluator("2^–2")[0] == 0.25
assert evaluator("+–1")[0] == -1

def test_negation_with_emdash():
"""Test that appropriate numbers of +/emdash signs are accepted"""
assert evaluator(u"1+\u20141")[0] == 0
assert evaluator("1\u2014\u20141")[0] == 2
assert evaluator("2*\u20141")[0] == -2
assert evaluator("2/\u20144")[0] == -0.5
assert evaluator("\u20141+\u20141")[0] == -2
assert evaluator("+1+\u20141")[0] == 0
assert evaluator("2^\u20142")[0] == 0.25
assert evaluator("+\u20141")[0] == -1

msg = "Invalid Input: Could not parse '{}' as a formula"
badformulas = ["1–––1", "2^––2", "1–+2", "1++2", "1+++2", "––2", "–––2", "–+2", "––+2"]
badformulas = [
"1\u2014\u2014\u20141",
"2^\u2014\u20142",
"1\u2014+2",
"1++2",
"1+++2",
"\u2014\u20142",
"\u2014\u2014\u20142",
"\u2014+2",
"\u2014\u2014+2",
]

for formula in badformulas:
with raises(UnableToParse, match=re.escape(msg.format(formula))):
# raises should take a keyword arg here called "match" that
# ensures exceptions are raised as intended
with raises(UnableToParse):
evaluator(formula)

def test_evaluation_does_not_mutate_variables():
Expand Down

0 comments on commit cc24eb6

Please sign in to comment.