Moved base command to Math (from Fun) and added a test for no escaping ValueError.

This commit is contained in:
Jeremy Fincher 2004-01-09 00:13:44 +00:00
parent 626958bef9
commit 69aaea218f
3 changed files with 21 additions and 8 deletions

View File

@ -119,14 +119,6 @@ class Fun(callbacks.Privmsg):
except ValueError: except ValueError:
irc.error('That number doesn\'t map to an 8-bit character.') irc.error('That number doesn\'t map to an 8-bit character.')
def base(self, irc, msg, args):
"""<base> <number>
Converts from base <base> the number <number>
"""
(base, number) = privmsgs.getArgs(args, required=2)
irc.reply(str(long(number, int(base))))
def encode(self, irc, msg, args): def encode(self, irc, msg, args):
"""<encoding> <text> """<encoding> <text>

View File

@ -69,6 +69,24 @@ class Math(callbacks.Privmsg):
# Then we delete all square brackets, underscores, and whitespace, so no # Then we delete all square brackets, underscores, and whitespace, so no
# one can do list comprehensions or call __...__ functions. # one can do list comprehensions or call __...__ functions.
### ###
def base(self, irc, msg, args):
"""<base> <number>
Converts from base <base> the number <number>
"""
(base, number) = privmsgs.getArgs(args, required=2)
try:
base = int(base)
if not (2 <= base <= 36):
raise ValueError
except ValueError:
irc.error('<base> must be a number between 2 and 36.')
return
try:
irc.reply(str(long(number, int(base))))
except ValueError:
irc.error('Invalid <number> for base %s: %s' % (base, number))
_mathEnv = {'__builtins__': types.ModuleType('__builtins__'), 'i': 1j} _mathEnv = {'__builtins__': types.ModuleType('__builtins__'), 'i': 1j}
_mathEnv.update(math.__dict__) _mathEnv.update(math.__dict__)
_mathEnv.update(cmath.__dict__) _mathEnv.update(cmath.__dict__)

View File

@ -33,6 +33,9 @@ from testsupport import *
class MathTestCase(PluginTestCase, PluginDocumentation): class MathTestCase(PluginTestCase, PluginDocumentation):
plugins = ('Math',) plugins = ('Math',)
def testBase(self):
self.assertNotRegexp('base 56 asdflkj', 'ValueError')
def testCalc(self): def testCalc(self):
self.assertResponse('calc 5*0.06', str(5*0.06)) self.assertResponse('calc 5*0.06', str(5*0.06))
self.assertResponse('calc 2.0-7.0', str(2-7)) self.assertResponse('calc 2.0-7.0', str(2-7))