Commit cf7b2c61 authored by Oliver Rümpelein's avatar Oliver Rümpelein
Browse files

Initial commit, taken from old repo.

parents
# F3LBot – ErrBot Plugins for use with f3l
# Copyright (C) 2015 The F3L-Team,
# Oliver Rümpelein <oli_r(at)fg4f.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
[Core]
Name = Give
Module = give
[Python]
Version = 3
[Documentation]
Description = "'Give' things to others"
from errbot import BotPlugin, botcmd
# region > Utility functions
def peer_account_name(msg):
"""Returns the Nick of the sender of a message"""
assert msg
if msg.is_group:
return msg.frm.resource # pragma: no cover
else:
return msg.frm.nick
# endregion
class Give(BotPlugin):
"""Example: Hand out Beer"""
@staticmethod
def __evalto(args):
"""Count number of given names, separated by 'and'
Args:
args: List of strings
Returns:
Integer with number of names
"""
num = 1
for i in range(1, len(args), 2):
if args[i] == "and":
num += 1
else:
break
return num
@staticmethod
def __printargs(args):
"""Print args as string if given
Args:
args: List of strings
Returns:
String for in-sentence use.
"""
if args:
return " " + " ".join(args) + " "
else:
return " "
@staticmethod
def __printto(args, num):
"""Print user string, with users separated by 'and'.
Args:
args: List of strings
num: Numbers of names
Returns:
String for end-of-sentence-use
"""
answer_string = args[0]
for i in range(2, 2*num, 2):
answer_string += " and " + args[i]
return answer_string
@botcmd(split_args_with=None)
def beer(self, msg, args):
"""Get beer from the cellar, optional specify properties"""
return "/me goes to the cellar and returns, carrying a{}beer \
for {}.".format(self.__printargs(args),
peer_account_name(msg))
# noinspection PyUnusedLocal
@botcmd(split_args_with=None)
def beer_for(self, msg, args):
"""Get beer from the cellar, hand to someone else"""
num = self.__evalto(args)
return "/me goes to the cellar and returns, \
carrying a{}beer for {}.".format(
self.__printargs(args[2*num-1:]),
self.__printto(args, num),
)
@botcmd(split_args_with=None)
def give(self, msg, args):
"""Give 'something' to yourself"""
return "/me gives a{}to {}.".format(
self.__printargs(args),
peer_account_name(msg))
# noinspection PyUnusedLocal
@botcmd(split_args_with=None)
def give_to(self, msg, args):
"""Give 'something' to 'someone'"""
num = self.__evalto(args)
return "/me gives{}to {}.".format(
self.__printargs(args[2*num-1:]),
self.__printto(args, num)
)
# noinspection PyUnresolvedReferences
from errbot.backends.test import testbot
# region Utility Functions
def get_plugin(bot, plugin_name):
plugin = bot.bot.plugin_manager.get_plugin_obj_by_name(plugin_name)
if plugin is None: # pragma: no cover
raise ValueError("""Type of Plugin {} is None! \
Have you forgotten 'extra_plugin_dir="."'?""".format(plugin_name))
else:
return plugin
def dialog_test(bot, msg, exp):
"""Easy interface for tests of the form msg => reply
Arguments:
bot: A testbot instance
msg: The message to pass to bot
exp: Expected outcome
"""
bot.push_message(msg)
result = bot.pop_message()
print(result)
assert result == exp
# endregion
class TestGive:
extra_plugin_dir = "."
def test_evalto(self, testbot):
plugin = get_plugin(testbot, 'Give')
expected = [1, 3]
result = [
plugin._Give__evalto(['asdil1991']),
plugin._Give__evalto(['asdil1991', 'and', 'pheerai',
'and', 'asdil12', 'cool', 'büchenbacher'])
]
assert result == expected
def test_printargs(self, testbot):
plugin = get_plugin(testbot, 'Give')
expected = [" ", " 1 2 3 "]
result = [plugin._Give__printargs([]),
plugin._Give__printargs(["1", "2", "3"])]
assert result == expected
def test_printto(self, testbot):
plugin = get_plugin(testbot, 'Give')
expected = ['asdil1991', 'asdil1991 and pheerai and asdil12']
result = [plugin._Give__printto(["asdil1991"], 1),
plugin._Give__printto(["asdil1991", "and", "pheerai",
"and", "asdil12"], 3)]
assert expected == result
def test_beer_1(self, testbot):
dialog_test(
testbot,
'!beer',
'/me goes to the cellar and returns, carrying a beer \
for None.'
)
dialog_test(
testbot,
'!beer cool büchenbacher',
'/me goes to the cellar and returns, carrying a cool \
büchenbacher beer for None.'
)
def test_beer_for_1(self, testbot):
dialog_test(
testbot,
'!beer for asdil1991',
'/me goes to the cellar and returns, carrying a beer \
for asdil1991.'
)
def test_beer_for_2(self, testbot):
dialog_test(
testbot,
'!beer for asdil1991 and pheerai and asdil12 \
cool Büchenbacher',
'/me goes to the cellar and returns, carrying a \
cool Büchenbacher beer for asdil1991 and pheerai and asdil12.'
)
def test_give(self, testbot):
dialog_test(
testbot,
'!give',
'/me gives a to None.'
)
dialog_test(
testbot,
'!give sweet chocolate',
'/me gives a sweet chocolate to None.'
)
def test_give_to(self, testbot):
# give to 1 person
dialog_test(
testbot,
'!give to asdil1991',
'/me gives to asdil1991.'
)
# Give to multiple persons
dialog_test(
testbot,
'!give to asdil1991 and pheerai and asdil12 \
salty gulasch',
'/me gives salty gulasch to asdil1991 and pheerai \
and asdil12.'
)
Copyright (c) 2015-2017 Oliver Rümpelein, F3L Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Give #
## About ##
A simple 'give <foo> to <bar>'-plugin, with english texts and a historically grown interface (sorry for that).
Part of the F3LBot-Suite.
## Test ##
Run `test.py Give` with an interpreter that knows about Err.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment