From a9de2ab7538c13a913a62949a84d0c2958018216 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Mon, 5 Dec 2022 02:11:56 +0100 Subject: [PATCH] ktdm test En refactor, want hoe het stond was niet heel testbaar --- Shitpost.py | 66 +++++++++++++++++++++-------------------------- tests/shitpost.py | 18 ++++++++++++- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/Shitpost.py b/Shitpost.py index 886a767..0ec60b0 100644 --- a/Shitpost.py +++ b/Shitpost.py @@ -648,49 +648,43 @@ class Shitpost(commands.Cog): return -1.0 return max(1.0, min(10.0, grade)) + + def get_ktdm(self, wit = []): + if not wit: + zwarte_kaart = random.choice(self.zwarte_lijst) + aantal_wit = zwarte_kaart.count("___") + if aantal_wit == 0: # Soms staat er geen ___ in de kaart + zwarte_kaart = zwarte_kaart + " ___" + aantal_wit = 1 + wit = random.sample(self.witte_lijst, k=aantal_wit) + else: + if len(wit) > 3: + return None + zwarte_kaart = None + random.shuffle(self.zwarte_lijst) + for zwart in self.zwarte_lijst: + if zwart.count("___") == len(wit): + zwarte_kaart = zwart + break + if not zwarte_kaart: + return None + + return zwarte_kaart.replace("___", "**{}**").format(*wit) @commands.command(pass_context=True) @asyncio.coroutine def ktdm(self, ctx, *args): '''Speel Kaarten Tegen De Mensheid! Genereer een kaart of geef zelf opties. Bijvoorbeeld: !ktdm <"1"><"2"><"3"> (aanbevolen 1 of 2, max 3)''' - random.shuffle(self.witte_lijst) # schudden voor gebruik - zwarte_kaart = random.choice(self.zwarte_lijst).replace("___", "**{}**") # kies en maak het juiste format - aantal_wit = zwarte_kaart.count("{}") # tel hoeveel witte kaarten er nodig zijn - if aantal_wit == 0: # Soms staat er geen ___ in de kaart - zwarte_kaart = zwarte_kaart + " **{}**" - aantal_wit = 1 - arg_count = len(args) - if arg_count > 3: - yield from ctx.channel.send(ctx.author.mention + ", " + random.choice(insult) + ", 3 is het maximum.") - elif arg_count > 0: - for elem in args: - self.witte_lijst.append(elem) - - if arg_count != 0: # Indien extra args gegeven - while arg_count != aantal_wit: # Match het aantal args aan het aantal dat nodig is - zwarte_kaart = random.choice(self.zwarte_lijst).replace("___", "**{}**") - aantal_wit = zwarte_kaart.count("{}") - - wit={} # maak dynamisch dict aan met de witte kaarten die ingevuld worden - for x in range(aantal_wit, 0, -1): - key = "wit" + str(x) - wit[key] = self.witte_lijst.pop() - - # Dit gedeelte print de zwarte kaart mooi af - if aantal_wit == 1: - print(zwarte_kaart.format(wit['wit1'])) - yield from ctx.channel.send(ctx.author.mention + "\n" + zwarte_kaart.format(wit['wit1'])) - elif aantal_wit == 2: - print(zwarte_kaart.format(wit['wit1'],wit['wit2'])) - yield from ctx.channel.send(ctx.author.mention + "\n" + zwarte_kaart.format(wit['wit1'],wit['wit2'])) - elif aantal_wit == 3: - print(zwarte_kaart.format(wit['wit1'],wit['wit2'],wit['wit3'])) - yield from ctx.channel.send(ctx.author.mention + "\n" + zwarte_kaart.format(wit['wit1'],wit['wit2'],wit['wit3'])) + try: + yield from ctx.message.delete() + except: + print("No permission to delete message") + resultaat = self.get_ktdm(args) + if resultaat: + yield from ctx.channel.send(f"{ctx.author.mention}\n{resultaat}") else: - print("Dit is een vreemd aantal witte kaarten: " + str(aantal_wit) + "\nDe boosdoener: " + zwarte_kaart) - yield from ctx.channel.send("Arme " + random.choice(insult) + ", er is iets foutgegaan!") - yield from ctx.message.delete() + yield from ctx.channel.send(f"{ctx.author.mention}\nIets ging niet helemaal lekker") @commands.command(pass_context=True) @asyncio.coroutine diff --git a/tests/shitpost.py b/tests/shitpost.py index 75e0db2..37d7af3 100644 --- a/tests/shitpost.py +++ b/tests/shitpost.py @@ -45,4 +45,20 @@ class TestShitpost(unittest.TestCase): with self.subTest(msg="Watspelenwe activiteit does not return a string"): self.assertIsInstance(activiteit, str) with self.subTest(msg="Watspelenwe reden does not return a string"): - self.assertIsInstance(reden, str) \ No newline at end of file + self.assertIsInstance(reden, str) + + def test_ktdm(self): + with self.subTest(): + wit = ["qwerty", "yuiop"] + kaart = self.cog.get_ktdm(wit) + self.assertIn(wit[0], kaart) + self.assertIn(wit[1], kaart) + with self.subTest(): + wit = ["a", "b", "c", "d"] + kaart = self.cog.get_ktdm(wit) + self.assertEqual(kaart, None) + with self.subTest(): + wit = ["a", "b", "c"] + self.cog.zwarte_lijst = ["___"] + kaart = self.cog.get_ktdm(wit) + self.assertEqual(kaart, None) \ No newline at end of file