From 80a27f939147d92d606b003f717394dc91d10db0 Mon Sep 17 00:00:00 2001 From: Peter Baratta Date: Fri, 31 May 2013 10:41:08 -0400 Subject: [PATCH 1/3] Add test and functionality for trailing period --- common/lib/calc/calc.py | 2 +- common/lib/calc/tests/test_calc.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/common/lib/calc/calc.py b/common/lib/calc/calc.py index 2f33b66bfd..e5cddf4017 100644 --- a/common/lib/calc/calc.py +++ b/common/lib/calc/calc.py @@ -183,7 +183,7 @@ 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) + inner_number = (number_part + Optional("." + Optional(number_part))) | ("." + number_part) # 0.33k or -17 number = (Optional(minus | plus) + inner_number diff --git a/common/lib/calc/tests/test_calc.py b/common/lib/calc/tests/test_calc.py index 58d0860af6..967166121c 100644 --- a/common/lib/calc/tests/test_calc.py +++ b/common/lib/calc/tests/test_calc.py @@ -5,7 +5,7 @@ Unit tests for calc.py import unittest import numpy import calc - +from pyparsing import ParseException class EvaluatorTest(unittest.TestCase): """ @@ -20,6 +20,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 +35,15 @@ 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_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): """ From 087834337a0e850e1117664e2e4938e0e6c455bd Mon Sep 17 00:00:00 2001 From: Peter Baratta Date: Fri, 31 May 2013 11:05:21 -0400 Subject: [PATCH 2/3] Pep8 newline --- common/lib/calc/tests/test_calc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/common/lib/calc/tests/test_calc.py b/common/lib/calc/tests/test_calc.py index 967166121c..24f4f7d1e4 100644 --- a/common/lib/calc/tests/test_calc.py +++ b/common/lib/calc/tests/test_calc.py @@ -7,6 +7,7 @@ import numpy import calc from pyparsing import ParseException + class EvaluatorTest(unittest.TestCase): """ Run tests for calc.evaluator From ff3997e72e164ce000738cb5881647438ddb704c Mon Sep 17 00:00:00 2001 From: Peter Baratta Date: Fri, 31 May 2013 12:37:14 -0400 Subject: [PATCH 3/3] Add test to verify a single period is not accepted as input --- common/lib/calc/calc.py | 2 +- common/lib/calc/tests/test_calc.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/common/lib/calc/calc.py b/common/lib/calc/calc.py index e5cddf4017..2ee82e2fb4 100644 --- a/common/lib/calc/calc.py +++ b/common/lib/calc/calc.py @@ -182,7 +182,7 @@ def evaluator(variables, functions, string, cs=False): number_part = Word(nums) - # 0.33 or 7 or .34 + # 0.33 or 7 or .34 or 16. inner_number = (number_part + Optional("." + Optional(number_part))) | ("." + number_part) # 0.33k or -17 diff --git a/common/lib/calc/tests/test_calc.py b/common/lib/calc/tests/test_calc.py index 24f4f7d1e4..cfa1b7525d 100644 --- a/common/lib/calc/tests/test_calc.py +++ b/common/lib/calc/tests/test_calc.py @@ -37,6 +37,13 @@ class EvaluatorTest(unittest.TestCase): self.assertEqual(easy_eval("-3.14"), -3.14) self.assertEqual(easy_eval("-.618033989"), -0.618033989) + 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