|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from botcore.utils.regex import DISCORD_INVITE |
| 4 | + |
| 5 | + |
| 6 | +def use_regex(s: str) -> Optional[str]: |
| 7 | + """Helper function to run the Regex on a string. |
| 8 | +
|
| 9 | + Return the invite capture group, if the string matches the pattern |
| 10 | + else return None |
| 11 | + """ |
| 12 | + result = DISCORD_INVITE.search(s) |
| 13 | + return result if result is None else result.group("invite") |
| 14 | + |
| 15 | + |
| 16 | +def test_discord_invite_positive(): |
| 17 | + """Test the DISCORD_INVITE regex on a set of strings we would expect to capture.""" |
| 18 | + |
| 19 | + assert use_regex("discord.gg/python") == "python" |
| 20 | + assert use_regex("https://discord.gg/python") == "python" |
| 21 | + assert use_regex("discord.com/invite/python") == "python" |
| 22 | + assert use_regex("discordapp.com/invite/python") == "python" |
| 23 | + assert use_regex("discord.me/python") == "python" |
| 24 | + assert use_regex("discord.li/python") == "python" |
| 25 | + assert use_regex("discord.io/python") == "python" |
| 26 | + assert use_regex(".gg/python") == "python" |
| 27 | + |
| 28 | + assert use_regex("discord.gg/python/but/extra") == "python/but/extra" |
| 29 | + assert use_regex("discord.me/this/isnt/python") == "this/isnt/python" |
| 30 | + assert 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" |
| 31 | + assert use_regex("discordapp.com/invite/python/snakescord") == "python/snakescord" |
| 32 | + assert use_regex("http://discord.gg/python/%20/notpython") == "python/%20/notpython" |
| 33 | + assert use_regex("discord.gg/python?=ts/notpython") == "python?=ts/notpython" |
| 34 | + assert use_regex("https://discord.gg/python#fragment/notpython") == "python#fragment/notpython" |
| 35 | + assert use_regex("https://discord.gg/python/~/notpython") == "python/~/notpython" |
| 36 | + |
| 37 | + assert use_regex("https://discord.gg/python with whitespace") == "python" |
| 38 | + assert use_regex(" https://discord.gg/python ") == "python" |
| 39 | + |
| 40 | + |
| 41 | +def test_discord_invite_negatives(): |
| 42 | + """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture.""" |
| 43 | + |
| 44 | + assert use_regex("another string") is None |
| 45 | + assert use_regex("https://pythondiscord.com") is None |
| 46 | + assert use_regex("https://discord.com") is None |
| 47 | + assert use_regex("https://discord.gg") is None |
| 48 | + assert use_regex("https://discord.gg/ python") is None |
0 commit comments