mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-30 14:14:37 +01:00
Wrote some more tests for FunCommands; found some bugs in the process.
This commit is contained in:
parent
074268ffe6
commit
b78cabaed5
@ -136,7 +136,10 @@ class shlex:
|
|||||||
elif self.state in self.quotes:
|
elif self.state in self.quotes:
|
||||||
self.token = self.token + nextchar
|
self.token = self.token + nextchar
|
||||||
if nextchar == '\\':
|
if nextchar == '\\':
|
||||||
self.backslash = True
|
if self.backslash:
|
||||||
|
self.backslash = False
|
||||||
|
else:
|
||||||
|
self.backslash = True
|
||||||
else:
|
else:
|
||||||
if not self.backslash and nextchar == self.state:
|
if not self.backslash and nextchar == self.state:
|
||||||
self.state = ' '
|
self.state = ' '
|
||||||
|
@ -425,11 +425,11 @@ class FunCommands(callbacks.Privmsg):
|
|||||||
i = long(literal, 8)
|
i = long(literal, 8)
|
||||||
return '%s%s.0' % (previous, i)
|
return '%s%s.0' % (previous, i)
|
||||||
text = self._mathHex.sub(hex2float, text)
|
text = self._mathHex.sub(hex2float, text)
|
||||||
debug.printf('After unhexing: %r' % text)
|
#debug.printf('After unhexing: %r' % text)
|
||||||
text = self._mathOctal.sub(oct2float, 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)
|
text = self._mathInt.sub(r'\1.0', text)
|
||||||
debug.printf('After uninting: %r' % text)
|
#debug.printf('After uninting: %r' % text)
|
||||||
try:
|
try:
|
||||||
x = complex(eval(text, self._mathEnv, self._mathEnv))
|
x = complex(eval(text, self._mathEnv, self._mathEnv))
|
||||||
irc.reply(msg, self._complexToString(x))
|
irc.reply(msg, self._complexToString(x))
|
||||||
@ -611,9 +611,11 @@ class FunCommands(callbacks.Privmsg):
|
|||||||
s = s.replace('\n\n', '. ')
|
s = s.replace('\n\n', '. ')
|
||||||
s = ' '.join(s.split())
|
s = ' '.join(s.split())
|
||||||
except NameError:
|
except NameError:
|
||||||
s = 'No such function exists.'
|
irc.error(msg, 'No such function exists.')
|
||||||
|
return
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
s = 'That function has no documentation.'
|
irc.error(msg, 'That function has no documentation.')
|
||||||
|
return
|
||||||
irc.reply(msg, s)
|
irc.reply(msg, s)
|
||||||
|
|
||||||
|
|
||||||
|
@ -195,7 +195,7 @@ class Tokenizer:
|
|||||||
ret = []
|
ret = []
|
||||||
while True:
|
while True:
|
||||||
token = lexer.get_token()
|
token = lexer.get_token()
|
||||||
if token == '':
|
if not token:
|
||||||
raise SyntaxError, 'Missing "]"'
|
raise SyntaxError, 'Missing "]"'
|
||||||
elif token == ']':
|
elif token == ']':
|
||||||
return ret
|
return ret
|
||||||
@ -213,7 +213,8 @@ class Tokenizer:
|
|||||||
args = []
|
args = []
|
||||||
while True:
|
while True:
|
||||||
token = lexer.get_token()
|
token = lexer.get_token()
|
||||||
if token == '':
|
#debug.printf(repr(token))
|
||||||
|
if not token:
|
||||||
break
|
break
|
||||||
elif token == '[':
|
elif token == '[':
|
||||||
args.append(self.insideBrackets(lexer))
|
args.append(self.insideBrackets(lexer))
|
||||||
|
51
test/test.py
51
test/test.py
@ -36,6 +36,7 @@ sys.path.insert(0, 'plugins')
|
|||||||
|
|
||||||
from fix import *
|
from fix import *
|
||||||
|
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
import glob
|
import glob
|
||||||
import time
|
import time
|
||||||
@ -53,8 +54,10 @@ fd.close()
|
|||||||
|
|
||||||
msgs = []
|
msgs = []
|
||||||
for s in rawmsgs:
|
for s in rawmsgs:
|
||||||
print s
|
try:
|
||||||
msgs.append(ircmsgs.IrcMsg(s))
|
msgs.append(ircmsgs.IrcMsg(s))
|
||||||
|
except:
|
||||||
|
print 'IrcMsg constructor failed: %r' % s
|
||||||
|
|
||||||
nicks = ['fatjim','scn','moshez','LordVan','MetaCosm','pythong','fishfart',
|
nicks = ['fatjim','scn','moshez','LordVan','MetaCosm','pythong','fishfart',
|
||||||
'alb','d0rt','jemfinch','StyxAlso','fors','deltab','gd',
|
'alb','d0rt','jemfinch','StyxAlso','fors','deltab','gd',
|
||||||
@ -104,35 +107,47 @@ class PluginTestCase(unittest.TestCase):
|
|||||||
response = self.irc.takeMsg()
|
response = self.irc.takeMsg()
|
||||||
while response is None and time.time() - fed < self.timeout:
|
while response is None and time.time() - fed < self.timeout:
|
||||||
response = self.irc.takeMsg()
|
response = self.irc.takeMsg()
|
||||||
self.failUnless(response)
|
|
||||||
return 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
|
# 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.
|
# 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
|
# But that would be hard, so I don't bother. When this breaks, it'll get
|
||||||
# fixed, but not until then.
|
# fixed, but not until then.
|
||||||
def assertError(self, query):
|
def assertError(self, query):
|
||||||
msg = self._feedMsg(query)
|
m = self._feedMsg(query)
|
||||||
self.failUnless(msg.args[1].startswith('Error:'))
|
self.failUnless(m, msg)
|
||||||
|
self.failUnless(m.args[1].startswith('Error:'), '%r errored' % query)
|
||||||
|
|
||||||
def assertNotError(self, query):
|
def assertNotError(self, query):
|
||||||
msg = self._feedMsg(query)
|
m = self._feedMsg(query)
|
||||||
self.failIf(msg.args[1].startswith('Error:'))
|
self.failUnless(m, msg)
|
||||||
|
self.failIf(m.args[1].startswith('Error:'), '%r errored' % query)
|
||||||
|
|
||||||
def assertResponse(self, query, expectedResponse):
|
def assertResponse(self, query, expectedResponse):
|
||||||
msg = self._feedMsg(query)
|
m = self._feedMsg(query)
|
||||||
self.assertEqual(msg.args[1], expectedResponse)
|
self.failUnless(m, msg)
|
||||||
|
self.assertEqual(m.args[1], expectedResponse,
|
||||||
|
'%r != %r' % (expectedResponse, m.args[1]))
|
||||||
|
|
||||||
def assertRegexp(self, query, regexp):
|
def assertRegexp(self, query, regexp):
|
||||||
msg = self._feedMsg(query)
|
m = self._feedMsg(query)
|
||||||
self.failUnless(re.search(regexp, msg.args[1]))
|
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):
|
def assertRegexps(self, query, regexps):
|
||||||
started = time.time()
|
started = time.time()
|
||||||
total = len(expectedResponses)*self.timeout
|
total = len(expectedResponses)*self.timeout
|
||||||
while expectedResponses and time.time() - started < total:
|
while expectedResponses and time.time() - started < total:
|
||||||
msg = self._feedMsg(query)
|
m = self._feedMsg(query)
|
||||||
self.failUnless(re.search(expectedResponses.pop(0), msg.args[1]))
|
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)
|
self.failIf(time.time() - started > total)
|
||||||
|
|
||||||
def assertResponses(self, query, expectedResponses):
|
def assertResponses(self, query, expectedResponses):
|
||||||
@ -140,12 +155,12 @@ class PluginTestCase(unittest.TestCase):
|
|||||||
started = time.time()
|
started = time.time()
|
||||||
while len(responses) < len(expectedResponses) and \
|
while len(responses) < len(expectedResponses) and \
|
||||||
time.time() - started > len(expectedResponses)*self.timeout:
|
time.time() - started > len(expectedResponses)*self.timeout:
|
||||||
msg = self._feedMsg(query)
|
m = self._feedMsg(query)
|
||||||
self.failUnless(msg)
|
self.failUnless(m, 'query %r timed out' % query)
|
||||||
responses.append(msg)
|
responses.append(m)
|
||||||
self.assertEqual(len(expectedResponses), len(responses))
|
self.assertEqual(len(expectedResponses), len(responses))
|
||||||
for (msg, expected) in zip(responses, expectedResponses):
|
for (m, expected) in zip(responses, expectedResponses):
|
||||||
self.assertEqual(msg.args[1], expected)
|
self.assertEqual(m.args[1], expected)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -31,14 +31,56 @@
|
|||||||
|
|
||||||
from test import *
|
from test import *
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
import utils
|
||||||
|
|
||||||
class FunCommandsTest(PluginTestCase):
|
class FunCommandsTest(PluginTestCase):
|
||||||
plugins = ('FunCommands',)
|
plugins = ('FunCommands',)
|
||||||
def testBinary(self):
|
## def testNoErrors(self):
|
||||||
self.assertResponse('binary A', '01000001')
|
## 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):
|
## def testBinary(self):
|
||||||
for s in nicks[:10]: # 10 is probably enough.
|
## self.assertResponse('binary A', '01000001')
|
||||||
self.assertResponse('rot13 [rot13 %s]' % s, s)
|
|
||||||
|
## 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))
|
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
from test import *
|
from test import *
|
||||||
|
|
||||||
import conf
|
import conf
|
||||||
|
import utils
|
||||||
import ircmsgs
|
import ircmsgs
|
||||||
import callbacks
|
import callbacks
|
||||||
|
|
||||||
@ -42,6 +43,15 @@ class TokenizerTestCase(unittest.TestCase):
|
|||||||
def testEmpty(self):
|
def testEmpty(self):
|
||||||
self.assertEqual(tokenize(''), [])
|
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):
|
def testSingleWord(self):
|
||||||
self.assertEqual(tokenize('foo'), ['foo'])
|
self.assertEqual(tokenize('foo'), ['foo'])
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user