2013-07-23 21:02:06 +02:00
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
from supybot.test import *
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.plugin as plugin
|
|
|
|
import supybot.registry as registry
|
|
|
|
|
|
|
|
import plugin as Aka
|
|
|
|
|
|
|
|
class FunctionsTest(SupyTestCase):
|
|
|
|
def testFindBiggestDollar(self):
|
|
|
|
self.assertEqual(Aka.findBiggestDollar(''), 0)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('foo'), 0)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('$0'), 0)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('$1'), 1)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('$2'), 2)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('$2 $10'), 10)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('$3'), 3)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('$3 $2 $1'), 3)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('foo bar $1'), 1)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('foo $2 $1'), 2)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('foo $0 $1'), 1)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('foo $1 $3'), 3)
|
|
|
|
self.assertEqual(Aka.findBiggestDollar('$10 bar $1'), 10)
|
|
|
|
|
2013-07-23 23:05:45 +02:00
|
|
|
class AkaChannelTestCase(ChannelPluginTestCase):
|
2013-07-24 18:23:51 +02:00
|
|
|
plugins = ('Aka', 'Conditional', 'Filter', 'Math', 'Utilities',
|
|
|
|
'Format', 'Reply')
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
def testDoesNotOverwriteCommands(self):
|
|
|
|
# We don't have dispatcher commands anymore
|
|
|
|
#self.assertError('aka add aka "echo foo bar baz"')
|
|
|
|
self.assertError('aka add add "echo foo bar baz"')
|
|
|
|
self.assertError('aka add remove "echo foo bar baz"')
|
|
|
|
self.assertError('aka add lock "echo foo bar baz"')
|
|
|
|
self.assertError('aka add unlock "echo foo bar baz"')
|
|
|
|
|
|
|
|
def testAkaHelp(self):
|
|
|
|
self.assertNotError('aka add slashdot foo')
|
|
|
|
self.assertRegexp('help slashdot', "Alias for .*foo")
|
|
|
|
self.assertNotError('aka add nonascii echo éé')
|
|
|
|
self.assertRegexp('help nonascii', "Alias for .*echo éé")
|
|
|
|
|
2014-07-13 21:19:48 +02:00
|
|
|
def testShow(self):
|
|
|
|
self.assertNotError('aka add foo bar')
|
|
|
|
self.assertResponse('show foo', 'bar $*')
|
|
|
|
self.assertNotError('aka add "foo bar" baz')
|
|
|
|
self.assertResponse('show "foo bar"', 'baz $*')
|
|
|
|
|
2013-07-23 21:02:06 +02:00
|
|
|
def testRemove(self):
|
|
|
|
self.assertNotError('aka add foo echo bar')
|
|
|
|
self.assertResponse('foo', 'bar')
|
|
|
|
self.assertNotError('aka remove foo')
|
|
|
|
self.assertError('foo')
|
|
|
|
|
|
|
|
def testDollars(self):
|
|
|
|
self.assertNotError('aka add rot26 "rot13 [rot13 $1]"')
|
|
|
|
self.assertResponse('rot26 foobar', 'foobar')
|
|
|
|
|
|
|
|
def testMoreDollars(self):
|
|
|
|
self.assertNotError('aka add rev "echo $3 $2 $1"')
|
|
|
|
self.assertResponse('rev foo bar baz', 'baz bar foo')
|
|
|
|
|
|
|
|
def testAllArgs(self):
|
|
|
|
self.assertNotError('aka add swap "echo $2 $1 $*"')
|
|
|
|
self.assertResponse('swap 1 2 3 4 5', '2 1 3 4 5')
|
2013-12-23 17:41:33 +01:00
|
|
|
self.assertError('aka add foo "echo $1 @1 $*"')
|
2013-07-23 21:02:06 +02:00
|
|
|
self.assertNotError('aka add moo echo $1 $*')
|
|
|
|
self.assertError('moo')
|
|
|
|
self.assertResponse('moo foo', 'foo')
|
|
|
|
self.assertResponse('moo foo bar', 'foo bar')
|
2013-08-11 12:02:00 +02:00
|
|
|
|
|
|
|
self.assertNotError('aka add spam "echo [echo $*]"')
|
|
|
|
self.assertResponse('spam egg', 'egg')
|
|
|
|
self.assertResponse('spam egg bacon', 'egg bacon')
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
def testChannel(self):
|
|
|
|
self.assertNotError('aka add channel echo $channel')
|
|
|
|
self.assertResponse('aka channel', self.channel)
|
|
|
|
|
|
|
|
def testAddRemoveAka(self):
|
|
|
|
cb = self.irc.getCallback('Aka')
|
2013-07-23 22:47:50 +02:00
|
|
|
cb._add_aka('global', 'foobar', 'echo sbbone')
|
|
|
|
cb._db.lock_aka('global', 'foobar', 'evil_admin')
|
2013-07-23 21:02:06 +02:00
|
|
|
self.assertResponse('foobar', 'sbbone')
|
2014-12-17 01:36:55 +01:00
|
|
|
self.assertRegexp('aka list', 'foobar')
|
2013-07-23 22:47:50 +02:00
|
|
|
self.assertRaises(Aka.AkaError, cb._remove_aka, 'global', 'foobar')
|
|
|
|
cb._remove_aka('global', 'foobar', evenIfLocked=True)
|
2014-12-17 01:36:55 +01:00
|
|
|
self.assertNotRegexp('aka list', 'foobar')
|
2013-07-23 21:02:06 +02:00
|
|
|
self.assertError('foobar')
|
|
|
|
|
|
|
|
def testOptionalArgs(self):
|
|
|
|
self.assertNotError('aka add myrepr "repr @1"')
|
|
|
|
self.assertResponse('myrepr foo', '"foo"')
|
|
|
|
self.assertResponse('myrepr ""', '""')
|
|
|
|
|
|
|
|
def testNoExtraSpaces(self):
|
|
|
|
self.assertNotError('aka add foo "action takes $1\'s money"')
|
|
|
|
self.assertResponse('foo bar', '\x01ACTION takes bar\'s money\x01')
|
|
|
|
|
|
|
|
def testNoExtraQuotes(self):
|
|
|
|
self.assertNotError('aka add myre "echo s/$1/$2/g"')
|
|
|
|
self.assertResponse('myre foo bar', 's/foo/bar/g')
|
|
|
|
|
|
|
|
def testSimpleAkaWithoutArgsImpliesDollarStar(self):
|
|
|
|
self.assertNotError('aka add exo echo')
|
|
|
|
self.assertResponse('exo foo bar baz', 'foo bar baz')
|
|
|
|
|
|
|
|
def testChannelPriority(self):
|
|
|
|
self.assertNotError('aka add spam "echo foo"')
|
2013-07-23 21:10:26 +02:00
|
|
|
self.assertNotError('aka add --channel %s spam "echo bar"' %
|
|
|
|
self.channel)
|
2013-07-23 21:02:06 +02:00
|
|
|
self.assertResponse('spam', 'bar')
|
|
|
|
|
2013-07-23 21:10:26 +02:00
|
|
|
self.assertNotError('aka add --channel %s egg "echo baz"' %
|
|
|
|
self.channel)
|
2013-07-23 21:02:06 +02:00
|
|
|
self.assertNotError('aka add egg "echo qux"')
|
2013-07-23 21:10:26 +02:00
|
|
|
self.assertResponse('egg', 'baz')
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2013-07-24 11:28:55 +02:00
|
|
|
def testComplicatedNames(self):
|
|
|
|
self.assertNotError(u'aka add café "echo coffee"')
|
|
|
|
self.assertResponse(u'café', 'coffee')
|
|
|
|
|
2013-07-24 12:16:02 +02:00
|
|
|
self.assertNotError('aka add "foo bar" "echo spam"')
|
|
|
|
self.assertResponse('foo bar', 'spam')
|
|
|
|
self.assertNotError('aka add "foo" "echo egg"')
|
|
|
|
self.assertResponse('foo', 'egg')
|
2013-08-25 01:06:31 +02:00
|
|
|
# You could expect 'spam' here, but in fact, this is dangerous.
|
|
|
|
# Just imagine this session:
|
|
|
|
# <evil_user> aka add "echo foo" quit
|
|
|
|
# <bot> The operation succeeded.
|
|
|
|
# ...
|
|
|
|
# <owner> echo foo
|
|
|
|
# * bot has quit
|
|
|
|
self.assertResponse('foo bar', 'egg')
|
|
|
|
|
|
|
|
def testNoOverride(self):
|
|
|
|
self.assertNotError('aka add "echo foo" "echo bar"')
|
|
|
|
self.assertResponse('echo foo', 'foo')
|
2013-12-11 17:01:01 +01:00
|
|
|
self.assertNotError('aka add foo "echo baz"')
|
|
|
|
self.assertNotError('aka add "foo bar" "echo qux"')
|
|
|
|
self.assertResponse('foo bar', 'baz')
|
2013-07-24 12:16:02 +02:00
|
|
|
|
2013-07-24 18:23:51 +02:00
|
|
|
def testRecursivity(self):
|
|
|
|
self.assertNotError('aka add fact '
|
|
|
|
r'"cif [nceq $1 0] \"echo 1\" '
|
|
|
|
r'\"calc $1 * [fact [calc $1 - 1]]\""')
|
|
|
|
self.assertResponse('fact 4', '24')
|
|
|
|
self.assertRegexp('fact 50', 'more nesting')
|
|
|
|
|
2013-08-08 14:11:44 +02:00
|
|
|
def testDollarStarNesting(self):
|
|
|
|
self.assertNotError('aka add alias aka $*')
|
|
|
|
self.assertNotError('alias add a+ aka add $*')
|
|
|
|
|
2013-07-23 23:05:45 +02:00
|
|
|
class AkaTestCase(PluginTestCase):
|
2013-07-31 19:08:49 +02:00
|
|
|
plugins = ('Aka', 'Alias', 'User', 'Utilities')
|
2013-07-23 23:05:45 +02:00
|
|
|
|
2013-12-24 15:37:44 +01:00
|
|
|
def testMaximumLength(self):
|
|
|
|
self.assertNotError('aka add "foo bar baz qux quux" "echo test"')
|
|
|
|
self.assertError('aka add "foo bar baz qux quux corge" "echo test"')
|
|
|
|
|
2013-07-23 23:05:45 +02:00
|
|
|
def testAkaLockedHelp(self):
|
|
|
|
self.assertNotError('register evil_admin foo')
|
|
|
|
|
|
|
|
self.assertNotError('aka add slashdot foo')
|
2013-08-01 12:06:54 +02:00
|
|
|
self.assertRegexp('help aka slashdot', "Alias for .*foo")
|
|
|
|
self.assertNotRegexp('help aka slashdot', 'Locked by')
|
2013-07-23 23:05:45 +02:00
|
|
|
self.assertNotError('aka lock slashdot')
|
2013-08-01 12:06:54 +02:00
|
|
|
self.assertRegexp('help aka slashdot', 'Locked by evil_admin')
|
2013-07-23 23:05:45 +02:00
|
|
|
self.assertNotError('aka unlock slashdot')
|
2013-08-01 12:06:54 +02:00
|
|
|
self.assertNotRegexp('help aka slashdot', 'Locked by')
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2013-07-31 19:08:49 +02:00
|
|
|
def testAliasImport(self):
|
|
|
|
self.assertNotError('alias add foo "echo bar"')
|
|
|
|
self.assertNotError(u'alias add baz "echo café"')
|
|
|
|
self.assertNotError('aka add qux "echo quux"')
|
|
|
|
self.assertResponse('alias foo', 'bar')
|
|
|
|
self.assertResponse('alias baz', 'café')
|
|
|
|
self.assertRegexp('aka foo', 'there is no command named')
|
|
|
|
self.assertResponse('aka qux', 'quux')
|
|
|
|
|
|
|
|
self.assertNotError('aka importaliasdatabase')
|
|
|
|
|
|
|
|
self.assertRegexp('alias foo', 'there is no command named')
|
|
|
|
self.assertResponse('aka foo', 'bar')
|
|
|
|
self.assertResponse('aka baz', 'café')
|
|
|
|
self.assertResponse('aka qux', 'quux')
|
|
|
|
|
|
|
|
self.assertNotError('alias add foo "echo test"')
|
|
|
|
self.assertNotError('alias add spam "echo egg"')
|
2013-08-15 10:55:57 +02:00
|
|
|
self.assertNotError('alias lock spam')
|
2013-07-31 19:08:49 +02:00
|
|
|
|
|
|
|
self.assertRegexp('aka importaliasdatabase',
|
|
|
|
r'the 1 following command: foo \(This Aka already exists.\)$')
|
|
|
|
self.assertResponse('aka foo', 'bar')
|
|
|
|
self.assertResponse('alias foo', 'test')
|
|
|
|
self.assertRegexp('alias spam', 'there is no command named')
|
|
|
|
self.assertResponse('aka spam', 'egg')
|
|
|
|
|
2014-12-17 01:36:55 +01:00
|
|
|
def testList(self):
|
|
|
|
self.assertNotError('aka add foo bar')
|
|
|
|
self.assertRegexp('aka list', 'foo.*?bar \$\*')
|
|
|
|
self.assertNotError('aka add "foo bar" baz')
|
|
|
|
self.assertRegexp('aka list', 'foo.*?bar \$\*.*?foo bar.*?baz \$\*')
|
2013-07-31 19:08:49 +02:00
|
|
|
|
2013-07-23 21:02:06 +02:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|