Conditional: Rename @iferror to @cerror and simplify its logic to just return a boolean.

This commit is contained in:
Valentin Lorentz 2015-11-14 19:28:05 +01:00
parent 5f361ef180
commit b69e6e22bb
2 changed files with 10 additions and 29 deletions

View File

@ -299,11 +299,11 @@ class Conditional(callbacks.Plugin):
irc.reply('false') irc.reply('false')
nle = wrap(nle, ['float', 'float']) nle = wrap(nle, ['float', 'float'])
def iferror(self, irc, msg, args, testcommand, ifcommand, elsecommand): def cerror(self, irc, msg, args, testcommand):
"""<testcommand> <ifcommand> <elsecommand> """<testcommand>
Runs <ifcommand> if <testcommand> raises an error, runs <elsecommand> Runs <testcommand> and returns true if it raises an error;
otherwise. false otherwise.
""" """
tokens = callbacks.tokenize(testcommand) tokens = callbacks.tokenize(testcommand)
InvalidCommand = collections.namedtuple('InvalidCommand', InvalidCommand = collections.namedtuple('InvalidCommand',
@ -330,12 +330,10 @@ class Conditional(callbacks.Plugin):
pass pass
# TODO: do something with the results # TODO: do something with the results
if errors: if errors:
tokens = callbacks.tokenize(ifcommand) irc.reply('true')
self.Proxy(irc, msg, tokens)
else: else:
tokens = callbacks.tokenize(elsecommand) irc.reply('false')
self.Proxy(irc, msg, tokens) cerror = wrap(cerror, ['something'])
iferror = wrap(iferror, ['something', 'something', 'something'])
Condition = internationalizeDocstring(Conditional) Condition = internationalizeDocstring(Conditional)

View File

@ -147,26 +147,9 @@ class ConditionalTestCase(PluginTestCase):
self.assertError('nle 1 bla') self.assertError('nle 1 bla')
def testIferror(self): def testIferror(self):
self.assertResponse( self.assertResponse('cerror "echo hi"', 'false')
'iferror "echo hi" "foo" "bar"', self.assertResponse('cerror "foobarbaz"', 'true')
'Error: "bar" is not a valid command.') self.assertResponse('cerror "help foobarbaz"', 'true')
self.assertResponse(
'iferror "echo hi" "echo 1 error" "echo 0 errors"',
'0 errors')
self.assertResponse(
'iferror "foobarbaz" "echo 1 error" "echo 0 errors"',
'1 error')
self.assertResponse(
'iferror "help foobarbaz" "echo 1 error" "echo 0 errors"',
'1 error')
self.assertResponse(
r'iferror "iferror \"help foobarbaz\" \"echo hi\" \"foobarbaz\"" '
r'"foo" "bar"',
'Error: "bar" is not a valid command.')
self.assertResponse(
r'iferror "iferror \"help foobarbaz\" \"echo hi\" \"foobarbaz\"" '
r'"echo 0 errors" "echo 1 error"',
'1 error')
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: