From 5386c20d2c50f0f8d844adf6e66ba406161bc3ed Mon Sep 17 00:00:00 2001 From: shiftinv Date: Thu, 25 Aug 2022 00:02:11 +0200 Subject: [PATCH] test: add tests --- tests/ext/commands/test_base_core.py | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/ext/commands/test_base_core.py diff --git a/tests/ext/commands/test_base_core.py b/tests/ext/commands/test_base_core.py new file mode 100644 index 0000000000..ce8293543d --- /dev/null +++ b/tests/ext/commands/test_base_core.py @@ -0,0 +1,82 @@ +import pytest + +from disnake import Permissions +from disnake.ext import commands + + +class TestDefaultPermissions: + def test_decorator(self): + class Cog(commands.Cog): + @commands.slash_command(default_member_permissions=64) + async def cmd(self, _): + ... + + @commands.default_member_permissions(64) + @commands.slash_command() + async def above(self, _): + ... + + @commands.slash_command() + @commands.default_member_permissions(64) + async def below(self, _): + ... + + for c in (Cog, Cog()): + assert c.cmd.default_member_permissions == Permissions(64) + assert c.above.default_member_permissions == Permissions(64) + assert c.below.default_member_permissions == Permissions(64) + + def test_decorator_overwrite(self): + # putting the decorator above should fail + with pytest.raises(ValueError, match="Cannot set `default_member_permissions`"): + + class Cog(commands.Cog): + @commands.default_member_permissions(32) + @commands.slash_command(default_member_permissions=64) + async def above(self, _): + ... + + # putting the decorator below shouldn't fail + # (this is a side effect of how command copying works, + # and while this *should* probably fail, we're just testing + # for regressions for now) + class Cog2(commands.Cog): + @commands.slash_command(default_member_permissions=64) + @commands.default_member_permissions(32) + async def below(self, _): + ... + + for c in (Cog2, Cog2()): + assert c.below.default_member_permissions == Permissions(32) + + def test_attrs(self): + class Cog(commands.Cog, slash_command_attrs={"default_member_permissions": 32}): + @commands.slash_command() + async def no_overwrite(self, _): + ... + + @commands.slash_command(default_member_permissions=64) + async def overwrite(self, _): + ... + + @commands.default_member_permissions(64) + @commands.slash_command() + async def overwrite_decorator_above(self, _): + ... + + @commands.slash_command() + @commands.default_member_permissions(64) + async def overwrite_decorator_below(self, _): + ... + + assert Cog.no_overwrite.default_member_permissions is None + assert Cog().no_overwrite.default_member_permissions == Permissions(32) + + assert Cog.overwrite.default_member_permissions == Permissions(64) + assert Cog().overwrite.default_member_permissions == Permissions(64) + + assert Cog.overwrite_decorator_above.default_member_permissions == Permissions(64) + assert Cog().overwrite_decorator_above.default_member_permissions == Permissions(64) + + assert Cog.overwrite_decorator_below.default_member_permissions == Permissions(64) + assert Cog().overwrite_decorator_below.default_member_permissions == Permissions(64)