Commit 343d9019 by Peter Baratta

Merge pull request #15 from edx/pbaratta/fix/trailing-period

Add test and functionality for trailing period in [Numerical/Formula]Response
parents aae3380a ff3997e7
......@@ -182,8 +182,8 @@ def evaluator(variables, functions, string, cs=False):
number_part = Word(nums)
# 0.33 or 7 or .34
inner_number = (number_part + Optional("." + number_part)) | ("." + number_part)
# 0.33 or 7 or .34 or 16.
inner_number = (number_part + Optional("." + Optional(number_part))) | ("." + number_part)
# 0.33k or -17
number = (Optional(minus | plus) + inner_number
......
......@@ -5,6 +5,7 @@ Unit tests for calc.py
import unittest
import numpy
import calc
from pyparsing import ParseException
class EvaluatorTest(unittest.TestCase):
......@@ -20,6 +21,11 @@ class EvaluatorTest(unittest.TestCase):
def test_number_input(self):
"""
Test different kinds of float inputs
See also
test_trailing_period (slightly different)
test_exponential_answer
test_si_suffix
"""
easy_eval = lambda x: calc.evaluator({}, {}, x)
......@@ -30,7 +36,22 @@ class EvaluatorTest(unittest.TestCase):
self.assertEqual(easy_eval("-13"), -13)
self.assertEqual(easy_eval("-3.14"), -3.14)
self.assertEqual(easy_eval("-.618033989"), -0.618033989)
# See also test_exponential_answer and test_si_suffix
def test_period(self):
"""
The string '.' should not evaluate to anything.
"""
self.assertRaises(ParseException, calc.evaluator, {}, {}, '.')
self.assertRaises(ParseException, calc.evaluator, {}, {}, '1+.')
def test_trailing_period(self):
"""
Test that things like '4.' will be 4 and not throw an error
"""
try:
self.assertEqual(4.0, calc.evaluator({}, {}, '4.'))
except ParseException:
self.fail("'4.' is a valid input, but threw an exception")
def test_exponential_answer(self):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment