From 7eabc5c13abb09f3dc72a1f81d7be8fd63a9fa4d Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Sat, 27 Jul 2024 12:32:20 +0100 Subject: [PATCH] test(8ball): Refactor to allow testing of choice logic We parametrise the class with a source of randomness so it can be fixed with a seed in the tests --- cogs/commands/baseplugin.py | 15 +++++++++++++++ cogs/commands/eightball.py | 23 ++++++++++++++--------- tests/test_eightball.py | 20 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 cogs/commands/baseplugin.py create mode 100644 tests/test_eightball.py diff --git a/cogs/commands/baseplugin.py b/cogs/commands/baseplugin.py new file mode 100644 index 0000000..d4cc3b1 --- /dev/null +++ b/cogs/commands/baseplugin.py @@ -0,0 +1,15 @@ +import string +from typing import Optional + +from discord.ext import commands +from discord.ext.commands import Bot + + +class BasePlugin(commands.Cog): + + def __init__(self, bot: Bot): + self.bot = bot + + + def execute(self, author: string, message: string) -> Optional[string]: + return None diff --git a/cogs/commands/eightball.py b/cogs/commands/eightball.py index 53d7993..88d61c2 100644 --- a/cogs/commands/eightball.py +++ b/cogs/commands/eightball.py @@ -1,7 +1,7 @@ -import random +from random import Random import shlex import string -from typing import List +from typing import List, Optional from discord.ext import commands from discord.ext.commands import Bot, Context, clean_content @@ -16,20 +16,25 @@ class EightBall(commands.Cog): - def __init__(self, bot: Bot, options: List[string]): + def __init__(self, bot: Bot, random: Random, options: List[string]): self.bot = bot + self.random = random self.options = options + async def execute(self, author: string, args: List[string]) -> Optional[string]: + if len(args) == 1: + return f"You must pose the Magic 8-ball a dilemma {author}!" + else: + return f"{author}: {(self.random.choice(self.options))}" + @commands.hybrid_command(name="8ball", help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) - async def execute(self, ctx: Context, *, args: str = ""): + async def eight_ball(self, ctx: Context, *, args: str = ""): args = await clean_content().convert(ctx, args) args = shlex.split(args) display_name = get_name_string(ctx.message) - if len(args) == 1: - await ctx.send(f"You must pose the Magic 8-ball a dilemma {display_name}!") - else: - await ctx.send(f"{display_name}: {(random.choice(self.options))}") + if response := self.execute(display_name, args): + await ctx.send(response) async def setup(bot: Bot): @@ -59,4 +64,4 @@ async def setup(bot: Bot): "Very doubtful", ] - await bot.add_cog(EightBall(bot, options)) + await bot.add_cog(EightBall(bot, Random(), options)) diff --git a/tests/test_eightball.py b/tests/test_eightball.py new file mode 100644 index 0000000..f9289ef --- /dev/null +++ b/tests/test_eightball.py @@ -0,0 +1,20 @@ +from random import Random + +from discord import Intents +from discord.ext.commands import Bot + +from cogs.commands.eightball import EightBall + + +def test_eightball_no_question(): + eightball = EightBall(Bot("/", intents=Intents()), Random(1), []) + + assert eightball.execute("test", []) == "You must pose the Magic 8-ball a dilemma test!" + + +def test_eightball_question_with_fixed_randomness(): + eightball = EightBall(Bot("/", intents=Intents()), Random(1), ["Yes", "No", "Maybe"]) + + answers = [eightball.execute("test", []) for i in range(0, 7)] + + assert answers == ["Yes", "Maybe", "Yes", "No", "Yes", "No", "No"] \ No newline at end of file