add tests for appropriate behavior

Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
Daniel Folkinshteyn 2010-04-14 10:27:56 -04:00 committed by James Vega
parent 51eac7da5b
commit 8d64d08645
2 changed files with 15 additions and 13 deletions

View File

@ -62,17 +62,17 @@ def getChannel(msg, args=()):
raise callbacks.Error, 'Command must be sent in a channel or ' \ raise callbacks.Error, 'Command must be sent in a channel or ' \
'include a channel in its arguments.' 'include a channel in its arguments.'
def getArgs(args, required=1, optional=0): def getArgs(args, required=1, optional=0, wildcard=0):
if len(args) < required: if len(args) < required:
raise callbacks.ArgumentError raise callbacks.ArgumentError
if len(args) < required + optional: if len(args) < required + optional:
ret = list(args) + ([''] * (required + optional - len(args))) ret = list(args) + ([''] * (required + optional - len(args)))
elif len(args) >= required + optional: elif len(args) >= required + optional:
if not wildcard:
ret = list(args[:required + optional - 1]) ret = list(args[:required + optional - 1])
ret.append(' '.join(args[required + optional - 1:])) ret.append(' '.join(args[required + optional - 1:]))
if len(ret) == 1:
return ret[0]
else: else:
ret = list(args)
return ret return ret
class AliasError(Exception): class AliasError(Exception):
@ -119,11 +119,9 @@ def makeNewAlias(name, alias):
channel = getChannel(msg, args) channel = getChannel(msg, args)
alias = alias.replace('$channel', channel) alias = alias.replace('$channel', channel)
tokens = callbacks.tokenize(alias) tokens = callbacks.tokenize(alias)
if not wildcard and biggestDollar or biggestAt: if biggestDollar or biggestAt:
args = getArgs(args, required=biggestDollar, optional=biggestAt) args = getArgs(args, required=biggestDollar, optional=biggestAt,
# Gotta have a mutable sequence (for replace). wildcard=wildcard)
if biggestDollar + biggestAt == 1: # We got a string, no tuple.
args = [args]
def regexpReplace(m): def regexpReplace(m):
idx = int(m.group(1)) idx = int(m.group(1))
return args[idx-1] return args[idx-1]

View File

@ -85,6 +85,10 @@ class AliasTestCase(ChannelPluginTestCase):
self.assertNotError('alias add swap "echo $2 $1 $*"') self.assertNotError('alias add swap "echo $2 $1 $*"')
self.assertResponse('swap 1 2 3 4 5', '2 1 3 4 5') self.assertResponse('swap 1 2 3 4 5', '2 1 3 4 5')
self.assertError('alias add foo "echo $1 @1 $*"') self.assertError('alias add foo "echo $1 @1 $*"')
self.assertNotError('alias add moo echo $1 $*')
self.assertError('moo')
self.assertResponse('moo foo', 'foo')
self.assertResponse('moo foo bar', 'foo bar')
def testChannel(self): def testChannel(self):
self.assertNotError('alias add channel echo $channel') self.assertNotError('alias add channel echo $channel')