From c3815c9cfe282034c968e97968be0b7cf321de09 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sun, 17 Nov 2013 12:37:14 -0800 Subject: [PATCH] improvements to calc module * .wa output cleanup (remove ugly html entities in places) * add some more test cases --- modules/calc.py | 17 ++++++----------- modules/test/test_calc.py | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/modules/calc.py b/modules/calc.py index 412a417..0b715a8 100644 --- a/modules/calc.py +++ b/modules/calc.py @@ -11,20 +11,13 @@ http://inamidst.com/phenny/ import re import web -r_result = re.compile(r'(?i)(.*?)') -r_tag = re.compile(r'<\S+.*?>') - subs = [ - (' in ', ' -> '), - (' over ', ' / '), ('£', 'GBP '), ('€', 'EUR '), ('\$', 'USD '), - (r'\bKB\b', 'kilobytes'), - (r'\bMB\b', 'megabytes'), - (r'\bGB\b', 'kilobytes'), - ('kbps', '(kilobits / second)'), - ('mbps', '(megabits / second)') + (r'\n', '; '), + ('°', '°'), + (r'\/', '/'), ] @@ -70,7 +63,9 @@ def wa(phenny, input): return answer = answers[1] - answer = answer.replace('\\n', ', ') + for sub in subs: + answer = answer.replace(sub[0], sub[1]) + phenny.say(answer) wa.commands = ['wa'] diff --git a/modules/test/test_calc.py b/modules/test/test_calc.py index 80eb61d..a271683 100644 --- a/modules/test/test_calc.py +++ b/modules/test/test_calc.py @@ -1,3 +1,4 @@ +# coding=utf-8 """ test_calc.py - tests for the calc module author: mutantmonkey @@ -18,6 +19,12 @@ class TestCalc(unittest.TestCase): self.phenny.say.assert_called_once_with('25') + def test_c_sqrt(self): + input = Mock(group=lambda x: '4^(1/2)') + c(self.phenny, input) + + self.phenny.say.assert_called_once_with('2') + @unittest.skip('Not supported with DuckDuckGo') def test_c_scientific(self): input = Mock(group=lambda x: '2^64') @@ -35,7 +42,7 @@ class TestCalc(unittest.TestCase): input = Mock(group=lambda x: 'airspeed of an unladen swallow') wa(self.phenny, input) - self.phenny.say.assert_called_once_with('25 mph (miles per hour), '\ + self.phenny.say.assert_called_once_with('25 mph (miles per hour); '\ '(asked, but not answered, about a general swallow in the '\ '1975 film Monty Python and the Holy Grail)') @@ -45,6 +52,18 @@ class TestCalc(unittest.TestCase): self.phenny.say.assert_called_once_with('4') + def test_wa_convert_deg(self): + input = Mock(group=lambda x: '30 degrees C in F') + wa(self.phenny, input) + + self.phenny.say.assert_called_once_with('86 °F (degrees Fahrenheit)') + + def test_wa_convert_bytes(self): + input = Mock(group=lambda x: '5 MB/s in Mbps') + wa(self.phenny, input) + + self.phenny.say.assert_called_once_with( + '40 Mb/s (megabits per second)') def test_wa_none(self): input = Mock(group=lambda x: "jajoajaj ojewphjqo I!tj")