mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-23 02:24:12 +01:00
Fixed some bugs and cleaned up the test suite.
This commit is contained in:
parent
084fe1854f
commit
c2f179cd0d
@ -67,6 +67,9 @@ class Topic(callbacks.Privmsg):
|
||||
topicSeparator = ' || '
|
||||
topicFormatter = '%s (%s)'
|
||||
topicUnformatter = re.compile('(.*) \((\S+)\)')
|
||||
def _splitTopic(self, topic):
|
||||
return filter(None, topic.split(self.topicSeparator))
|
||||
|
||||
def add(self, irc, msg, args, channel):
|
||||
"""[<channel>] <topic>
|
||||
|
||||
@ -99,10 +102,17 @@ class Topic(callbacks.Privmsg):
|
||||
message isn't sent in the channel itself.
|
||||
"""
|
||||
newtopic = irc.state.getTopic(channel)
|
||||
topics = newtopic.split(self.topicSeparator)
|
||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
||||
if len(topics) == 0 or len(topics) == 1:
|
||||
irc.error(msg, 'I can\'t shuffle 1 or fewer topics.')
|
||||
return
|
||||
elif len(topics) == 2:
|
||||
topics.reverse()
|
||||
newtopic = self.topicSeparator.join(topics)
|
||||
else:
|
||||
random.shuffle(topics)
|
||||
newtopic = self.topicSeparator.join(topics)
|
||||
while len(topics) > 1 and newtopic == irc.state.getTopic(channel):
|
||||
while newtopic == irc.state.getTopic(channel):
|
||||
random.shuffle(topics)
|
||||
newtopic = self.topicSeparator.join(topics)
|
||||
irc.queueMsg(ircmsgs.topic(channel, newtopic))
|
||||
@ -118,17 +128,22 @@ class Topic(callbacks.Privmsg):
|
||||
number = privmsgs.getArgs(args)
|
||||
try:
|
||||
number = int(number)
|
||||
if number >= 0:
|
||||
if number > 0:
|
||||
number -= 1
|
||||
elif number == 0:
|
||||
irc.error(msg, 'That\'s not a valid topic number.')
|
||||
return
|
||||
except ValueError:
|
||||
irc.error(msg, 'The argument must be a valid integer.')
|
||||
return
|
||||
topics = irc.state.getTopic(channel).split(self.topicSeparator)
|
||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
||||
if topics:
|
||||
try:
|
||||
irc.reply(msg, topics[number])
|
||||
except IndexError:
|
||||
irc.error(msg, 'That\'s not a valid topic.')
|
||||
return
|
||||
else:
|
||||
irc.error(msg, 'There are no topics to get.')
|
||||
get = privmsgs.channel(get)
|
||||
|
||||
def change(self, irc, msg, args, channel):
|
||||
@ -143,8 +158,11 @@ class Topic(callbacks.Privmsg):
|
||||
(number, regexp) = privmsgs.getArgs(args, needed=2)
|
||||
try:
|
||||
number = int(number)
|
||||
if number >= 0:
|
||||
if number > 0:
|
||||
number -= 1
|
||||
elif number == 0:
|
||||
irc.error(msg, 'That\'s not a valid topic number.')
|
||||
return
|
||||
except ValueError:
|
||||
irc.error(msg, 'The <number> argument must be a number.')
|
||||
return
|
||||
@ -156,7 +174,10 @@ class Topic(callbacks.Privmsg):
|
||||
except re.error, e:
|
||||
irc.error(msg, debug.exnToString(e))
|
||||
return
|
||||
topics = irc.state.getTopic(channel).split(self.topicSeparator)
|
||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
||||
if not topics:
|
||||
irc.error(msg, 'There are no topics to change.')
|
||||
return
|
||||
topic = topics.pop(number)
|
||||
match = self.topicUnformatter.match(topic)
|
||||
if match is None:
|
||||
@ -190,12 +211,15 @@ class Topic(callbacks.Privmsg):
|
||||
"""
|
||||
try:
|
||||
number = int(privmsgs.getArgs(args))
|
||||
if number >= 0:
|
||||
if number > 0:
|
||||
number -= 1
|
||||
elif number == 0:
|
||||
irc.error(msg, 'That\'s not a valid topic number.')
|
||||
return
|
||||
except ValueError:
|
||||
irc.error(msg, 'The argument must be a number.')
|
||||
return
|
||||
topics = irc.state.getTopic(channel).split(self.topicSeparator)
|
||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
||||
try:
|
||||
topic = topics.pop(number)
|
||||
except IndexError:
|
||||
|
@ -31,33 +31,51 @@
|
||||
|
||||
from test import *
|
||||
|
||||
class TopicTestCase(PluginTestCase, PluginDocumentation):
|
||||
plugins = ('Topic', 'Admin')
|
||||
def testAddtopic(self):
|
||||
_ = self.getMsg('join #foo')
|
||||
_ = self.getMsg(' ') # Get the WHO.
|
||||
m = self.getMsg('topic add #foo foo')
|
||||
self.assertEqual(m.command, 'TOPIC')
|
||||
self.assertEqual(m.args[0], '#foo')
|
||||
self.assertEqual(m.args[1], 'foo (test)')
|
||||
m = self.getMsg('topic add #foo bar')
|
||||
self.assertEqual(m.command, 'TOPIC')
|
||||
self.assertEqual(m.args[0], '#foo')
|
||||
class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
||||
plugins = ('Topic',)
|
||||
def testTopicRemove(self):
|
||||
self.assertError('topic remove 1')
|
||||
_ = self.getMsg('topic add foo')
|
||||
_ = self.getMsg('topic add bar')
|
||||
_ = self.getMsg('topic add baz')
|
||||
self.assertError('topic remove 0')
|
||||
self.assertNotError('topic remove 3')
|
||||
self.assertNotError('topic remove 2')
|
||||
self.assertNotError('topic remove 1')
|
||||
self.assertError('topic remove 1')
|
||||
|
||||
def testChangetopic(self):
|
||||
_ = self.getMsg('join #foo')
|
||||
_ = self.getMsg(' ')
|
||||
_ = self.getMsg('topic add #foo foo')
|
||||
_ = self.getMsg('topic add #foo bar')
|
||||
_ = self.getMsg('topic add #foo baz')
|
||||
self.assertRegexp('topic change #foo -1 s/baz/biff/',
|
||||
def testTopicGet(self):
|
||||
self.assertError('topic get 1')
|
||||
_ = self.getMsg('topic add foo')
|
||||
_ = self.getMsg('topic add bar')
|
||||
_ = self.getMsg('topic add baz')
|
||||
self.assertRegexp('topic get 1', '^foo')
|
||||
self.assertRegexp('topic get 2', '^bar')
|
||||
self.assertRegexp('topic get 3', '^baz')
|
||||
self.assertError('topic get 0')
|
||||
|
||||
def testTopicAdd(self):
|
||||
m = self.getMsg('topic add foo')
|
||||
self.assertEqual(m.command, 'TOPIC')
|
||||
self.assertEqual(m.args[0], self.channel)
|
||||
self.assertEqual(m.args[1], 'foo (test)')
|
||||
m = self.getMsg('topic add bar')
|
||||
self.assertEqual(m.command, 'TOPIC')
|
||||
self.assertEqual(m.args[0], self.channel)
|
||||
|
||||
def testTopicChange(self):
|
||||
_ = self.getMsg('topic add foo')
|
||||
_ = self.getMsg('topic add bar')
|
||||
_ = self.getMsg('topic add baz')
|
||||
self.assertRegexp('topic change -1 s/baz/biff/',
|
||||
r'foo.*bar.*biff')
|
||||
self.assertRegexp('topic change #foo 2 s/bar/baz/',
|
||||
self.assertRegexp('topic change 2 s/bar/baz/',
|
||||
r'foo.*baz.*biff')
|
||||
self.assertRegexp('topic change #foo 1 s/foo/bar/',
|
||||
self.assertRegexp('topic change 1 s/foo/bar/',
|
||||
r'bar.*baz.*biff')
|
||||
self.assertRegexp('topic change #foo -2 s/baz/bazz/',
|
||||
self.assertRegexp('topic change -2 s/baz/bazz/',
|
||||
r'bar.*bazz.*biff')
|
||||
self.assertError('topic change 0 s/baz/biff/')
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||
|
Loading…
Reference in New Issue
Block a user