Wrote some more tests for FunCommands; found some bugs in the process.

This commit is contained in:
Jeremy Fincher 2003-08-17 02:02:53 +00:00
parent 074268ffe6
commit b78cabaed5
6 changed files with 106 additions and 33 deletions

View File

@ -136,7 +136,10 @@ class shlex:
elif self.state in self.quotes:
self.token = self.token + nextchar
if nextchar == '\\':
self.backslash = True
if self.backslash:
self.backslash = False
else:
self.backslash = True
else:
if not self.backslash and nextchar == self.state:
self.state = ' '

View File

@ -425,11 +425,11 @@ class FunCommands(callbacks.Privmsg):
i = long(literal, 8)
return '%s%s.0' % (previous, i)
text = self._mathHex.sub(hex2float, text)
debug.printf('After unhexing: %r' % text)
#debug.printf('After unhexing: %r' % text)
text = self._mathOctal.sub(oct2float, text)
debug.printf('After unocting: %r' % text)
#debug.printf('After unocting: %r' % text)
text = self._mathInt.sub(r'\1.0', text)
debug.printf('After uninting: %r' % text)
#debug.printf('After uninting: %r' % text)
try:
x = complex(eval(text, self._mathEnv, self._mathEnv))
irc.reply(msg, self._complexToString(x))
@ -611,9 +611,11 @@ class FunCommands(callbacks.Privmsg):
s = s.replace('\n\n', '. ')
s = ' '.join(s.split())
except NameError:
s = 'No such function exists.'
irc.error(msg, 'No such function exists.')
return
except AttributeError:
s = 'That function has no documentation.'
irc.error(msg, 'That function has no documentation.')
return
irc.reply(msg, s)

View File

@ -195,7 +195,7 @@ class Tokenizer:
ret = []
while True:
token = lexer.get_token()
if token == '':
if not token:
raise SyntaxError, 'Missing "]"'
elif token == ']':
return ret
@ -213,7 +213,8 @@ class Tokenizer:
args = []
while True:
token = lexer.get_token()
if token == '':
#debug.printf(repr(token))
if not token:
break
elif token == '[':
args.append(self.insideBrackets(lexer))

View File

@ -36,6 +36,7 @@ sys.path.insert(0, 'plugins')
from fix import *
import re
import sys
import glob
import time
@ -53,8 +54,10 @@ fd.close()
msgs = []
for s in rawmsgs:
print s
msgs.append(ircmsgs.IrcMsg(s))
try:
msgs.append(ircmsgs.IrcMsg(s))
except:
print 'IrcMsg constructor failed: %r' % s
nicks = ['fatjim','scn','moshez','LordVan','MetaCosm','pythong','fishfart',
'alb','d0rt','jemfinch','StyxAlso','fors','deltab','gd',
@ -104,35 +107,47 @@ class PluginTestCase(unittest.TestCase):
response = self.irc.takeMsg()
while response is None and time.time() - fed < self.timeout:
response = self.irc.takeMsg()
self.failUnless(response)
return response
def feedMsg(self, query):
"""Just feeds it a message, that's all."""
self.irc.feedMsg(ircmsgs.privmsg(self.nick, query, prefix=self.prefix))
# These assertError/assertNoError are somewhat fragile. The proper way to
# do them would be to use a proxy for the irc object and intercept .error.
# But that would be hard, so I don't bother. When this breaks, it'll get
# fixed, but not until then.
def assertError(self, query):
msg = self._feedMsg(query)
self.failUnless(msg.args[1].startswith('Error:'))
m = self._feedMsg(query)
self.failUnless(m, msg)
self.failUnless(m.args[1].startswith('Error:'), '%r errored' % query)
def assertNotError(self, query):
msg = self._feedMsg(query)
self.failIf(msg.args[1].startswith('Error:'))
m = self._feedMsg(query)
self.failUnless(m, msg)
self.failIf(m.args[1].startswith('Error:'), '%r errored' % query)
def assertResponse(self, query, expectedResponse):
msg = self._feedMsg(query)
self.assertEqual(msg.args[1], expectedResponse)
m = self._feedMsg(query)
self.failUnless(m, msg)
self.assertEqual(m.args[1], expectedResponse,
'%r != %r' % (expectedResponse, m.args[1]))
def assertRegexp(self, query, regexp):
msg = self._feedMsg(query)
self.failUnless(re.search(regexp, msg.args[1]))
m = self._feedMsg(query)
self.failUnless(m, msg)
self.failUnless(re.search(regexp, m.args[1]),
'%r does not match %r' % (m.args[1], regexp))
def assertRegexps(self, query, regexps):
started = time.time()
total = len(expectedResponses)*self.timeout
while expectedResponses and time.time() - started < total:
msg = self._feedMsg(query)
self.failUnless(re.search(expectedResponses.pop(0), msg.args[1]))
m = self._feedMsg(query)
self.failUnless(m, msg)
regepx = expectedResponses.pop(0)
self.failUnless(re.search(regexp, m.args[1]),
'%r does not match %r' % (m.args[1], regexp))
self.failIf(time.time() - started > total)
def assertResponses(self, query, expectedResponses):
@ -140,12 +155,12 @@ class PluginTestCase(unittest.TestCase):
started = time.time()
while len(responses) < len(expectedResponses) and \
time.time() - started > len(expectedResponses)*self.timeout:
msg = self._feedMsg(query)
self.failUnless(msg)
responses.append(msg)
m = self._feedMsg(query)
self.failUnless(m, 'query %r timed out' % query)
responses.append(m)
self.assertEqual(len(expectedResponses), len(responses))
for (msg, expected) in zip(responses, expectedResponses):
self.assertEqual(msg.args[1], expected)
for (m, expected) in zip(responses, expectedResponses):
self.assertEqual(m.args[1], expected)
if __name__ == '__main__':

View File

@ -31,14 +31,56 @@
from test import *
import re
import utils
class FunCommandsTest(PluginTestCase):
plugins = ('FunCommands',)
def testBinary(self):
self.assertResponse('binary A', '01000001')
## def testNoErrors(self):
## self.assertNotError('netstats')
## self.assertNotError('cpustats')
## self.assertNotError('uptime')
## self.assertNotError('leet foobar')
## self.assertNotError('lithp meghan sweeney')
## self.assertNotError('objects')
## self.assertNotError('levenshtein Python Perl')
## self.assertNotError('soundex jemfinch')
def testRot13(self):
for s in nicks[:10]: # 10 is probably enough.
self.assertResponse('rot13 [rot13 %s]' % s, s)
## def testBinary(self):
## self.assertResponse('binary A', '01000001')
## def testRot13(self):
## for s in nicks[:10]: # 10 is probably enough.
## self.assertResponse('rot13 [rot13 %s]' % s, s)
## def testCalc(self):
## self.assertResponse('calc 5*0.06', str(5*0.06))
## def testChr(self):
## for i in range(256):
## c = chr(i)
## regexp = r'%s|%s' % (re.escape(c), re.escape(repr(c)))
## self.assertRegexp('chr %s' % i, regexp)
## def testHexlifyUnhexlify(self):
## for s in nicks[:10]: # 10, again, is probably enough.
## self.assertResponse('unhexlify [hexlify %s]' % s, s)
## def testXor(self):
## for s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 in group(nicks, 10):
## data = '%s%s%s%s%s%s%s%s%s' % (s0, s1, s2, s3, s4, s5, s6, s7, s8)
## self.assertResponse('xor %s [xor %s %s]' % (s9, s9, data), data)
## def testUrlquoteUrlunquote(self):
## self.assertResponse('urlunquote [urlquote ~jfincher]', '~jfincher')
## def testPydoc(self):
## self.assertNotError('pydoc str')
## self.assertError('pydoc foobar')
def testOrd(self):
for c in map(chr, range(256)):
i = ord(c)
self.assertResponse('ord %s' % utils.dqrepr(c), str(i))
def testCalc(self):
self.assertResponse('calc 5*0.06', str(5*0.06))

View File

@ -32,6 +32,7 @@
from test import *
import conf
import utils
import ircmsgs
import callbacks
@ -42,6 +43,15 @@ class TokenizerTestCase(unittest.TestCase):
def testEmpty(self):
self.assertEqual(tokenize(''), [])
def testNullCharacter(self):
self.assertEqual(tokenize(utils.dqrepr('\0')), ['\0'])
def testSingleDQInDQString(self):
self.assertEqual(tokenize('"\\""'), ['"'])
def testDQsWithBackslash(self):
self.assertEqual(tokenize('"\\\\"'), ["\\"])
def testSingleWord(self):
self.assertEqual(tokenize('foo'), ['foo'])