From 0e675fb71381ce3142b445b0f2732f7db877efc7 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sat, 2 Jun 2012 14:23:26 -0700 Subject: [PATCH] calc: remove deprecated, fix wa output, add test --- modules/calc.py | 41 +-------------------------------------- modules/test/test_calc.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 40 deletions(-) create mode 100644 modules/test/test_calc.py diff --git a/modules/calc.py b/modules/calc.py index 3ad8013..c3e9b01 100644 --- a/modules/calc.py +++ b/modules/calc.py @@ -27,46 +27,6 @@ subs = [ ('mbps', '(megabits / second)') ] -def calc(phenny, input): - """Use the Frink online calculator.""" - q = input.group(2) - if not q: - return phenny.say('0?') - - query = q[:] - for a, b in subs: - query = re.sub(a, b, query) - query = query.rstrip(' \t') - - precision = 5 - if query[-3:] in ('GBP', 'USD', 'EUR', 'NOK'): - precision = 2 - query = web.quote(query) - - uri = 'http://futureboy.us/fsp/frink.fsp?fromVal=' - bytes = web.get(uri + query) - m = r_result.search(bytes) - if m: - result = m.group(1) - result = r_tag.sub('', result) # strip span.warning tags - result = result.replace('>', '>') - result = result.replace('(undefined symbol)', '(?) ') - - if '.' in result: - try: result = str(round(float(result), precision)) - except ValueError: pass - - if not result.strip(): - result = '?' - elif ' in ' in q: - result += ' ' + q.split(' in ', 1)[1] - - phenny.say(q + ' = ' + result[:350]) - else: phenny.reply("Sorry, can't calculate that.") - phenny.say('Note that .calc is deprecated, consider using .c') -calc.commands = ['calc'] -calc.example = '.calc 5 + 3' - def c(phenny, input): """Google calculator.""" if not input.group(2): @@ -105,6 +65,7 @@ def wa(phenny, input): query = input.group(2) uri = 'http://tumbolia.appspot.com/wa/' answer = web.get(uri + web.quote(query.replace('+', '%2B'))) + answer = answer.split(';')[1] if answer: phenny.say(answer) else: phenny.reply('Sorry, no result.') diff --git a/modules/test/test_calc.py b/modules/test/test_calc.py new file mode 100644 index 0000000..9f62918 --- /dev/null +++ b/modules/test/test_calc.py @@ -0,0 +1,31 @@ +""" +test_calc.py - tests for the calc module +author: mutantmonkey +""" + +import unittest +from mock import MagicMock, Mock +from modules.calc import c, py, wa + + +class TestCalc(unittest.TestCase): + def setUp(self): + self.phenny = MagicMock() + + def test_c(self): + input = Mock(group=lambda x: '5*5') + c(self.phenny, input) + + self.phenny.say.assert_called_once_with('25') + + def test_py(self): + input = Mock(group=lambda x: "'test'*3") + py(self.phenny, input) + + self.phenny.say.assert_called_once_with('testtesttest\n') + + def test_wa(self): + 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)')