ktdm test
continuous-integration/drone/push Build is passing

En refactor, want hoe het stond was niet heel testbaar
This commit is contained in:
2022-12-05 02:11:56 +01:00
parent 19d71e3b4c
commit a9de2ab753
2 changed files with 47 additions and 37 deletions
+30 -36
View File
@@ -649,48 +649,42 @@ class Shitpost(commands.Cog):
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
+16
View File
@@ -46,3 +46,19 @@ class TestShitpost(unittest.TestCase):
self.assertIsInstance(activiteit, str)
with self.subTest(msg="Watspelenwe reden does not return a string"):
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)