calc: remove deprecated, fix wa output, add test
parent
6979d538d6
commit
0e675fb713
|
@ -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.')
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
"""
|
||||
test_calc.py - tests for the calc module
|
||||
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
|
||||
"""
|
||||
|
||||
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)')
|
Loading…
Reference in New Issue