From 251b01e00008387d8ff93201ed91b82eaa1dd009 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Tue, 19 Jun 2012 18:04:00 -0700 Subject: [PATCH] calc: fix handling and add tests for empty result --- modules/calc.py | 11 ++++++++--- modules/test/test_calc.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/modules/calc.py b/modules/calc.py index 5acb171..4725e80 100644 --- a/modules/calc.py +++ b/modules/calc.py @@ -48,12 +48,12 @@ def c(phenny, input): answer = answer.replace('', ')') answer = web.decode(answer) phenny.say(answer) - else: phenny.say('Sorry, no result.') + else: phenny.reply('Sorry, no result.') c.commands = ['c'] c.example = '.c 5 + 3' def py(phenny, input): - query = input.group(2) + query = input.group(2) or "" uri = 'http://tumbolia.appspot.com/py/' answer = web.get(uri + web.quote(query)) if answer: @@ -66,8 +66,13 @@ def wa(phenny, input): return phenny.reply("No search term.") query = input.group(2) uri = 'http://tumbolia.appspot.com/wa/' + answer = web.get(uri + web.quote(query.replace('+', '%2B'))) - answer = answer.split(';')[1] + try: + answer = answer.split(';')[1] + except IndexError: + answer = "" + 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 index 7b61e5f..34be034 100644 --- a/modules/test/test_calc.py +++ b/modules/test/test_calc.py @@ -24,14 +24,30 @@ class TestCalc(unittest.TestCase): self.phenny.say.assert_called_once_with('1.84467441 * 10^(19)') + def test_c_none(self): + input = Mock(group=lambda x: 'aif') + c(self.phenny, input) + + self.phenny.reply.assert_called_once_with('Sorry, no result.') + 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_py_none(self): + input = Mock(group=lambda x: "") + py(self.phenny, input) + 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)') + + def test_wa_none(self): + input = Mock(group=lambda x: "jajoajaj ojewphjqo I!tj") + wa(self.phenny, input) + + self.phenny.reply.assert_called_once_with('Sorry, no result.')