aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar ionite34 <[email protected]>2022-12-20 17:54:48 +0800
committerGravatar ionite34 <[email protected]>2022-12-20 17:54:48 +0800
commitb04143ca971e2f272ab78da808a63b5fd700ab68 (patch)
tree472bf58868c1c3554c8a8673db8ba59a47e3d37f /tests
parentRefactor snekbox tests to module (diff)
Add normalize file name tests
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/exts/utils/snekbox/test_io.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/bot/exts/utils/snekbox/test_io.py b/tests/bot/exts/utils/snekbox/test_io.py
new file mode 100644
index 000000000..36ac720ba
--- /dev/null
+++ b/tests/bot/exts/utils/snekbox/test_io.py
@@ -0,0 +1,34 @@
+import unittest
+
+# noinspection PyProtectedMember
+from bot.exts.utils.snekbox import _io
+
+
+class SnekboxIOTests(unittest.TestCase):
+ # noinspection SpellCheckingInspection
+ def test_normalize_file_name(self):
+ """Invalid file names should be normalized."""
+ cases = [
+ # ANSI escape sequences -> underscore
+ (r"\u001b[31mText", "_Text"),
+ # (Multiple consecutive should be collapsed to one underscore)
+ (r"a\u001b[35m\u001b[37mb", "a_b"),
+ # Backslash escaped chars -> underscore
+ (r"\n", "_"),
+ (r"\r", "_"),
+ (r"A\0\tB", "A__B"),
+ # Any other disallowed chars -> underscore
+ (r"\\.txt", "_.txt"),
+ (r"A!@#$%^&*B, C()[]{}+=D.txt", "A_B_C_D.txt"), # noqa: P103
+ (" ", "_"),
+ # Normal file names should be unchanged
+ ("legal_file-name.txt", "legal_file-name.txt"),
+ ("_-.", "_-."),
+ ]
+ for name, expected in cases:
+ with self.subTest(name=name, expected=expected):
+ # Test function directly
+ self.assertEqual(_io.normalize_discord_file_name(name), expected)
+ # Test FileAttachment.to_file()
+ obj = _io.FileAttachment(name, b"")
+ self.assertEqual(obj.to_file().filename, expected)