Removed assertRegexps and assertResponses, added RFE #836316 (target keyword) and frm keyword.

This commit is contained in:
Jeremy Fincher 2003-12-18 08:39:29 +00:00
parent bddac5c382
commit e3661e3ba5

View File

@ -146,12 +146,16 @@ class PluginTestCase(unittest.TestCase):
self.irc.die() self.irc.die()
gc.collect() gc.collect()
def _feedMsg(self, query, timeout=None): def _feedMsg(self, query, timeout=None, to=None, frm=None):
if to is None:
to = self.irc.nick
if frm is None:
frm = self.prefix
if timeout is None: if timeout is None:
timeout = self.timeout timeout = self.timeout
if self.myVerbose: if self.myVerbose:
print # Extra newline, so it's pretty. print # Extra newline, so it's pretty.
msg = ircmsgs.privmsg(self.irc.nick, query, prefix=self.prefix) msg = ircmsgs.privmsg(to, query, prefix=frm)
if self.myVerbose: if self.myVerbose:
print 'Feeding: %r' % msg print 'Feeding: %r' % msg
self.irc.feedMsg(msg) self.irc.feedMsg(msg)
@ -168,99 +172,85 @@ class PluginTestCase(unittest.TestCase):
def getMsg(self, query, timeout=None): def getMsg(self, query, timeout=None):
return self._feedMsg(query, timeout=timeout) return self._feedMsg(query, timeout=timeout)
def feedMsg(self, query): def feedMsg(self, query, to=None, frm=None):
"""Just feeds it a message, that's all.""" """Just feeds it a message, that's all."""
self.irc.feedMsg(ircmsgs.privmsg(self.irc.nick, query, if to is None:
prefix=self.prefix)) to = self.irc.nick
if frm is None:
frm = self.prefix
self.irc.feedMsg(ircmsgs.privmsg(to, query, prefix=frm))
# 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, to=None, frm=None):
m = self._feedMsg(query) m = self._feedMsg(query, to=to, frm=frm)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
if lastGetHelp not in m.args[1]: if lastGetHelp not in m.args[1]:
self.failUnless(m.args[1].startswith('Error:'), self.failUnless(m.args[1].startswith('Error:'),
'%r did not error: %s' % (query, m.args[1])) '%r did not error: %s' % (query, m.args[1]))
return m
def assertNotError(self, query): def assertNotError(self, query, to=None, frm=None):
m = self._feedMsg(query) m = self._feedMsg(query, to=to, frm=frm)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
self.failIf(m.args[1].startswith('Error:'), self.failIf(m.args[1].startswith('Error:'),
'%r errored: %s' % (query, m.args[1])) '%r errored: %s' % (query, m.args[1]))
self.failIf(lastGetHelp in m.args[1], self.failIf(lastGetHelp in m.args[1],
'%r returned the help string.' % query) '%r returned the help string.' % query)
return m
def assertHelp(self, query): def assertHelp(self, query, to=None, frm=None):
m = self._feedMsg(query) m = self._feedMsg(query, to=to, frm=frm)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
self.failUnless(lastGetHelp in m.args[1], self.failUnless(lastGetHelp in m.args[1],
'%s is not the help (%s)' % (m.args[1], lastGetHelp)) '%s is not the help (%s)' % (m.args[1], lastGetHelp))
return m
def assertNoResponse(self, query, timeout=None): def assertNoResponse(self, query, timeout=None, to=None, frm=None):
m = self._feedMsg(query, timeout) m = self._feedMsg(query, timeout=timeout, to=to, frm=frm)
self.failIf(m, 'Unexpected response: %r' % m) self.failIf(m, 'Unexpected response: %r' % m)
return m
def assertResponse(self, query, expectedResponse): def assertResponse(self, query, expectedResponse, to=None, frm=None):
m = self._feedMsg(query) m = self._feedMsg(query, to=to, frm=frm)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
self.assertEqual(m.args[1], expectedResponse, self.assertEqual(m.args[1], expectedResponse,
'%r != %r' % (expectedResponse, m.args[1])) '%r != %r' % (expectedResponse, m.args[1]))
return m
def assertRegexp(self, query, regexp, flags=re.I): def assertRegexp(self, query, regexp, flags=re.I, to=None, frm=None):
m = self._feedMsg(query) m = self._feedMsg(query, to=to, frm=frm)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
self.failUnless(re.search(regexp, m.args[1], flags), self.failUnless(re.search(regexp, m.args[1], flags),
'%r does not match %r' % (m.args[1], regexp)) '%r does not match %r' % (m.args[1], regexp))
return m
def assertNotRegexp(self, query, regexp, flags=re.I): def assertNotRegexp(self, query, regexp, flags=re.I, to=None, frm=None):
m = self._feedMsg(query) m = self._feedMsg(query, to=to, frm=frm)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
self.failUnless(re.search(regexp, m.args[1], flags) is None, self.failUnless(re.search(regexp, m.args[1], flags) is None,
'%r matched %r' % (m.args[1], regexp)) '%r matched %r' % (m.args[1], regexp))
return m
def assertRegexps(self, query, regexps): def assertAction(self, query, expectedResponse=None, to=None, frm=None):
started = time.time() m = self._feedMsg(query, to=to, frm=frm)
total = len(regexps)*self.timeout
while regexps and time.time() - started < total:
m = self._feedMsg(query)
if m is None:
raise TimeoutError, query
regexp = regexps.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):
responses = []
started = time.time()
while len(responses) < len(expectedResponses) and \
time.time() - started > len(expectedResponses)*self.timeout:
m = self._feedMsg(query)
if m is None:
raise TimeoutError, query
responses.append(m)
self.assertEqual(len(expectedResponses), len(responses))
for (m, expected) in zip(responses, expectedResponses):
self.assertEqual(m.args[1], expected)
def assertAction(self, query, expectedResponse=None):
m = self._feedMsg(query)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
self.failUnless(ircmsgs.isAction(m)) self.failUnless(ircmsgs.isAction(m))
if expectedResponse is not None: if expectedResponse is not None:
self.assertEqual(ircmsgs.unAction(m), expectedResponse) self.assertEqual(ircmsgs.unAction(m), expectedResponse)
return m
def assertActionRegexp(self, query, regexp, flags=re.I): def assertActionRegexp(self, query, regexp, flags=re.I, to=None, frm=None):
m = self._feedMsg(query) m = self._feedMsg(query, to=to, frm=frm)
if m is None: if m is None:
raise TimeoutError, query raise TimeoutError, query
self.failUnless(ircmsgs.isAction(m)) self.failUnless(ircmsgs.isAction(m))
@ -294,14 +284,18 @@ class ChannelPluginTestCase(PluginTestCase):
PluginTestCase.setUp(self) PluginTestCase.setUp(self)
self.irc.feedMsg(ircmsgs.join(self.channel, prefix=self.prefix)) self.irc.feedMsg(ircmsgs.join(self.channel, prefix=self.prefix))
def _feedMsg(self, query, timeout=None): def _feedMsg(self, query, timeout=None, to=None, frm=None):
if to is None:
to = self.channel
if frm is None:
frm = self.prefix
if timeout is None: if timeout is None:
timeout = self.timeout timeout = self.timeout
if self.myVerbose: if self.myVerbose:
print # Newline, just like PluginTestCase. print # Newline, just like PluginTestCase.
if query[0] not in conf.prefixChars: if query[0] not in conf.prefixChars:
query = conf.prefixChars[0] + query query = conf.prefixChars[0] + query
msg = ircmsgs.privmsg(self.channel, query, prefix=self.prefix) msg = ircmsgs.privmsg(to, query, prefix=frm)
if self.myVerbose: if self.myVerbose:
print 'Feeding: %r' % msg print 'Feeding: %r' % msg
self.irc.feedMsg(msg) self.irc.feedMsg(msg)