Changed the name of some variables ('whore' doesn't partse as 'who re{gexp}') and added tests.

This commit is contained in:
Jeremy Fincher 2003-11-02 16:45:38 +00:00
parent 0adde0c117
commit 31ef30d961
2 changed files with 59 additions and 20 deletions

View File

@ -309,29 +309,32 @@ class Toggleable(object):
irc.error(msg, '%r isn\'t a valid name to toggle. '
'Valid names are %s' % (name, self._toggleNames()))
randomnickre = re.compile ("\$randomnick", re.I)
randomdatere = re.compile ("\$randomdate", re.I)
randomintre = re.compile ("\$randomint", re.I)
whore = re.compile ("\$who", re.I)
botnickre = re.compile("\$botnick", re.I)
todayre = re.compile("\$today", re.I)
_randomnickRe = re.compile ("\$randomnick", re.I)
_randomdateRe = re.compile ("\$randomdate", re.I)
_randomintRe = re.compile ("\$randomint", re.I)
_whoRe = re.compile ("\$who", re.I)
_botnickRe = re.compile("\$botnick", re.I)
_todayRe = re.compile("\$today", re.I)
_nowRe = re.compile("\$now", re.I)
def standardSubstitute(irc, msg, text):
"""Do the standard set of substitutions on text, and return it"""
nochannel = False
try:
channel = privmsgs.getChannel(msg, None)
except:
nochannel = True
if nochannel:
text = randomnickre.sub('anyone', text)
if ircutils.isChannel(msg.args[0]):
channel = msg.args[0]
else:
text = randomnickre.sub(random.choice(irc.state.channels[channel].users._data.keys()),
text)
channel = None
if channel:
text = _randomnickRe.sub('anyone', text)
else:
user = random.choice(list(irc.state.channels[channel].users))
text = _randomnickRe.sub(user, text)
t = pow(2,30)*random.random()+time.time()/4.0
text = randomdatere.sub(time.ctime(t), text)
text = randomintre.sub(str(random.randint(-1000, 1000)), text)
text = whore.sub(msg.nick, text)
text = botnickre.sub(irc.nick, text)
text = todayre.sub(time.ctime(), text)
text = _randomdateRe.sub(time.ctime(t), text)
text = _randomintRe.sub(str(random.randint(-1000, 1000)), text)
text = _whoRe.sub(msg.nick, text)
text = _botnickRe.sub(irc.nick, text)
text = _todayRe.sub(time.ctime(), text)
text = _nowRe.sub(time.ctime(), text)
return text
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -31,6 +31,9 @@
from test import *
import sets
import irclib
import plugins
class ToggleDictionaryTestCase(unittest.TestCase):
@ -85,4 +88,37 @@ class ToggleDictionaryTestCase(unittest.TestCase):
self.assertEqual(t.toString(), '(bar: On; foo: On)')
self.assertEqual(t.toString(channel='#foo'),
'(bar: Off; foo: Off)')
class FunctionsTestCase(unittest.TestCase):
class irc:
class state:
users = sets.Set(['foo', 'bar', 'baz'])
nick = 'foobar'
pass
def testStandardSubstitute(self):
msg = ircmsgs.privmsg('#foo', 'filler', prefix='biff!quux@xyzzy')
s = plugins.standardSubstitute(self.irc, msg, '$randomint')
try:
int(s)
except ValueError:
self.fail('$randomnick wasn\'t an int.')
s = plugins.standardSubstitute(self.irc, msg, '$randomInt')
try:
int(s)
except ValueError:
self.fail('$randomnick wasn\'t an int.')
self.assertEqual(plugins.standardSubstitute(self.irc, msg, '$botnick'),
self.irc.nick)
self.assertEqual(plugins.standardSubstitute(self.irc, msg, '$who'),
msg.nick)
self.assert_(plugins.standardSubstitute(self.irc, msg, '$randomdate'))
self.assert_(plugins.standardSubstitute(self.irc, msg, '$today'))
self.assert_(plugins.standardSubstitute(self.irc, msg, '$now'))