aboutsummaryrefslogtreecommitdiffstats
path: root/tests/pydis_core/utils/test_regex.py
blob: 26fe23ea649656e0ce6e6f0113c81b5cf51af1e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import unittest

from pydis_core.utils.regex import DISCORD_INVITE


def match_regex(s: str) -> str | None:
    """Helper function to run re.match on a string.

    Return the invite capture group, if the string matches the pattern
        else return None
    """
    result = DISCORD_INVITE.match(s)
    return result if result is None else result.group("invite")


def search_regex(s: str) -> str | None:
    """Helper function to run re.search on a string.

    Return the invite capture group, if the string matches the pattern
        else return None
    """
    result = DISCORD_INVITE.search(s)
    return result if result is None else result.group("invite")


class UtilsRegexTests(unittest.TestCase):

    def test_discord_invite_positives(self):
        """Test the DISCORD_INVITE regex on a set of strings we would expect to capture."""

        self.assertEqual(match_regex("discord.gg/python"), "python")
        self.assertEqual(match_regex("https://discord.gg/python"), "python")
        self.assertEqual(match_regex("https://www.discord.gg/python"), "python")
        self.assertEqual(match_regex("discord.com/invite/python"), "python")
        self.assertEqual(match_regex("www.discord.com/invite/python"), "python")
        self.assertEqual(match_regex("discordapp.com/invite/python"), "python")
        self.assertEqual(match_regex("discord.me/python"), "python")
        self.assertEqual(match_regex(".gg/python"), "python")

        self.assertEqual(match_regex("discord.gg/python/but/extra"), "python/but/extra")
        self.assertEqual(match_regex("discord.me/this/isnt/python"), "this/isnt/python")
        self.assertEqual(match_regex(".gg/a/a/a/a/a/a/a/a/a/a/a"), "a/a/a/a/a/a/a/a/a/a/a")
        self.assertEqual(match_regex("discordapp.com/invite/python/snakescord"), "python/snakescord")
        self.assertEqual(match_regex("http://discord.gg/python/%20/notpython"), "python/%20/notpython")
        self.assertEqual(match_regex("discord.gg/python?=ts/notpython"), "python?=ts/notpython")
        self.assertEqual(match_regex("https://discord.gg/python#fragment/notpython"), "python#fragment/notpython")
        self.assertEqual(match_regex("https://discord.gg/python/~/notpython"), "python/~/notpython")

        self.assertEqual(search_regex("https://discord.gg/python with whitespace"), "python")
        self.assertEqual(search_regex(" https://discord.gg/python "), "python")

        self.assertEqual(search_regex("discord:#@discordapp.com/invite/python"), "python")
        self.assertEqual(search_regex("discord:/#@discordapp.com/invite/python"), "python")
        self.assertEqual(search_regex("discord://#@discordapp.com/invite/python"), "python")
        self.assertEqual(search_regex("discord://@#discordapp.com/invite/python"), "python")

    def test_discord_invite_negatives(self):
        """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture."""

        self.assertEqual(match_regex("another string"), None)
        self.assertEqual(match_regex("https://pythondiscord.com"), None)
        self.assertEqual(match_regex("https://tenor.com/view/cat-scream-cat-rage-gif-8639900204413677493"), None)
        self.assertEqual(match_regex("https://paste.pythondiscord.com/1234"), None)
        self.assertEqual(match_regex("https://discord.com"), None)
        self.assertEqual(match_regex("https://discord.gg"), None)
        self.assertEqual(match_regex("https://discord.gg/ python"), None)
        self.assertEqual(search_regex("https://discord.com/developers/applications"), None)
        self.assertEqual(
            search_regex(
                "https://discord.com/channels/267624335836053506/305126844661760000/1309985927287930892"
            ), None
        )

        self.assertEqual(search_regex("https://discord.com with whitespace"), None)
        self.assertEqual(search_regex(" https://discord.com "), None)