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

View File

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