From b4ea1d65118d111e58ca656925e80662971d7cc4 Mon Sep 17 00:00:00 2001 From: ionite34 Date: Tue, 16 Aug 2022 18:31:00 -0400 Subject: Expanded regex testing for discord invites - Added tests for both match and search, previously the fact that `re.match` did not work with http/https links was not made apparent by the tests as only `re.search` is tested. --- tests/botcore/utils/test_regex.py | 69 ++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'tests') diff --git a/tests/botcore/utils/test_regex.py b/tests/botcore/utils/test_regex.py index 2ffd0e46..4a7390a5 100644 --- a/tests/botcore/utils/test_regex.py +++ b/tests/botcore/utils/test_regex.py @@ -4,8 +4,18 @@ from typing import Optional from botcore.utils.regex import DISCORD_INVITE -def use_regex(s: str) -> Optional[str]: - """Helper function to run the Regex on a string. +def match_regex(s: str) -> Optional[str]: + """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.match(s) + return result if result is None else result.group("invite") + + +def search_regex(s: str) -> Optional[str]: + """Helper function to run re.match on a string. Return the invite capture group, if the string matches the pattern else return None @@ -19,32 +29,37 @@ 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(use_regex("discord.gg/python"), "python") - self.assertEqual(use_regex("https://discord.gg/python"), "python") - self.assertEqual(use_regex("discord.com/invite/python"), "python") - self.assertEqual(use_regex("discordapp.com/invite/python"), "python") - self.assertEqual(use_regex("discord.me/python"), "python") - self.assertEqual(use_regex("discord.li/python"), "python") - self.assertEqual(use_regex("discord.io/python"), "python") - self.assertEqual(use_regex(".gg/python"), "python") - - self.assertEqual(use_regex("discord.gg/python/but/extra"), "python/but/extra") - self.assertEqual(use_regex("discord.me/this/isnt/python"), "this/isnt/python") - self.assertEqual(use_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(use_regex("discordapp.com/invite/python/snakescord"), "python/snakescord") - self.assertEqual(use_regex("http://discord.gg/python/%20/notpython"), "python/%20/notpython") - self.assertEqual(use_regex("discord.gg/python?=ts/notpython"), "python?=ts/notpython") - self.assertEqual(use_regex("https://discord.gg/python#fragment/notpython"), "python#fragment/notpython") - self.assertEqual(use_regex("https://discord.gg/python/~/notpython"), "python/~/notpython") - - self.assertEqual(use_regex("https://discord.gg/python with whitespace"), "python") - self.assertEqual(use_regex(" https://discord.gg/python "), "python") + 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("discord.li/python"), "python") + self.assertEqual(match_regex("discord.io/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") def test_discord_invite_negatives(self): """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture.""" - self.assertEqual(use_regex("another string"), None) - self.assertEqual(use_regex("https://pythondiscord.com"), None) - self.assertEqual(use_regex("https://discord.com"), None) - self.assertEqual(use_regex("https://discord.gg"), None) - self.assertEqual(use_regex("https://discord.gg/ python"), None) + self.assertEqual(match_regex("another string"), None) + self.assertEqual(match_regex("https://pythondiscord.com"), 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 with whitespace"), None) + self.assertEqual(search_regex(" https://discord.com "), None) -- cgit v1.2.3 From 8b71cb416a6b27c4e7829a0b4e898c0441c5f84a Mon Sep 17 00:00:00 2001 From: ionite34 Date: Tue, 16 Aug 2022 18:40:56 -0400 Subject: Corrected docstrings --- tests/botcore/utils/test_regex.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/botcore/utils/test_regex.py b/tests/botcore/utils/test_regex.py index 4a7390a5..491e22bd 100644 --- a/tests/botcore/utils/test_regex.py +++ b/tests/botcore/utils/test_regex.py @@ -5,7 +5,7 @@ from botcore.utils.regex import DISCORD_INVITE def match_regex(s: str) -> Optional[str]: - """Helper function to run re.search on a string. + """Helper function to run re.match on a string. Return the invite capture group, if the string matches the pattern else return None @@ -15,7 +15,7 @@ def match_regex(s: str) -> Optional[str]: def search_regex(s: str) -> Optional[str]: - """Helper function to run re.match on a string. + """Helper function to run re.search on a string. Return the invite capture group, if the string matches the pattern else return None -- cgit v1.2.3