diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/README.md | 2 | ||||
| -rw-r--r-- | tests/bot/exts/info/doc/__init__.py | 0 | ||||
| -rw-r--r-- | tests/bot/exts/info/doc/test_parsing.py | 66 | ||||
| -rw-r--r-- | tests/bot/test_converters.py | 21 | 
4 files changed, 77 insertions, 12 deletions
| diff --git a/tests/README.md b/tests/README.md index 4f62edd68..092324123 100644 --- a/tests/README.md +++ b/tests/README.md @@ -114,7 +114,7 @@ class BotCogTests(unittest.TestCase):  ### Mocking coroutines -By default, the `unittest.mock.Mock` and `unittest.mock.MagicMock` classes cannot mock coroutines, since the `__call__` method they provide is synchronous. In anticipation of the `AsyncMock` that will be [introduced in Python 3.8](https://docs.python.org/3.9/whatsnew/3.8.html#unittest), we have added an `AsyncMock` helper to [`helpers.py`](/tests/helpers.py). Do note that this drop-in replacement only implements an asynchronous `__call__` method, not the additional assertions that will come with the new `AsyncMock` type in Python 3.8. +By default, the `unittest.mock.Mock` and `unittest.mock.MagicMock` classes cannot mock coroutines, since the `__call__` method they provide is synchronous. The [`AsyncMock`](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.AsyncMock) that has been [introduced in Python 3.8](https://docs.python.org/3.9/whatsnew/3.8.html#unittest) is an asynchronous version of `MagicMock` that can be used anywhere a coroutine is expected.  ### Special mocks for some `discord.py` types diff --git a/tests/bot/exts/info/doc/__init__.py b/tests/bot/exts/info/doc/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/bot/exts/info/doc/__init__.py diff --git a/tests/bot/exts/info/doc/test_parsing.py b/tests/bot/exts/info/doc/test_parsing.py new file mode 100644 index 000000000..1663d8491 --- /dev/null +++ b/tests/bot/exts/info/doc/test_parsing.py @@ -0,0 +1,66 @@ +from unittest import TestCase + +from bot.exts.info.doc import _parsing as parsing + + +class SignatureSplitter(TestCase): + +    def test_basic_split(self): +        test_cases = ( +            ("0,0,0", ["0", "0", "0"]), +            ("0,a=0,a=0", ["0", "a=0", "a=0"]), +        ) +        self._run_tests(test_cases) + +    def test_commas_ignored_in_brackets(self): +        test_cases = ( +            ("0,[0,0],0,[0,0],0", ["0", "[0,0]", "0", "[0,0]", "0"]), +            ("(0,),0,(0,(0,),0),0", ["(0,)", "0", "(0,(0,),0)", "0"]), +        ) +        self._run_tests(test_cases) + +    def test_mixed_brackets(self): +        tests_cases = ( +            ("[0,{0},0],0,{0:0},0", ["[0,{0},0]", "0", "{0:0}", "0"]), +            ("([0],0,0),0,(0,0),0", ["([0],0,0)", "0", "(0,0)", "0"]), +            ("([(0,),(0,)],0),0", ["([(0,),(0,)],0)", "0"]), +        ) +        self._run_tests(tests_cases) + +    def test_string_contents_ignored(self): +        test_cases = ( +            ("'0,0',0,',',0", ["'0,0'", "0", "','", "0"]), +            ("0,[']',0],0", ["0", "[']',0]", "0"]), +            ("{0,0,'}}',0,'{'},0", ["{0,0,'}}',0,'{'}", "0"]), +        ) +        self._run_tests(test_cases) + +    def test_mixed_quotes(self): +        test_cases = ( +            ("\"0',0',\",'0,0',0", ["\"0',0',\"", "'0,0'", "0"]), +            ("\",',\",'\",',0", ["\",',\"", "'\",'", "0"]), +        ) +        self._run_tests(test_cases) + +    def test_quote_escaped(self): +        test_cases = ( +            (r"'\',','\\',0", [r"'\','", r"'\\'", "0"]), +            (r"'0\',0\\\'\\',0", [r"'0\',0\\\'\\'", "0"]), +        ) +        self._run_tests(test_cases) + +    def test_real_signatures(self): +        test_cases = ( +            ("start, stop[, step]", ["start", " stop[, step]"]), +            ("object=b'', encoding='utf-8', errors='strict'", ["object=b''", " encoding='utf-8'", " errors='strict'"]), +            ( +                "typename, field_names, *, rename=False, defaults=None, module=None", +                ["typename", " field_names", " *", " rename=False", " defaults=None", " module=None"] +            ), +        ) +        self._run_tests(test_cases) + +    def _run_tests(self, test_cases): +        for input_string, expected_output in test_cases: +            with self.subTest(input_string=input_string): +                self.assertEqual(list(parsing._split_parameters(input_string)), expected_output) diff --git a/tests/bot/test_converters.py b/tests/bot/test_converters.py index c42111f3f..4af84dde5 100644 --- a/tests/bot/test_converters.py +++ b/tests/bot/test_converters.py @@ -10,9 +10,9 @@ from bot.converters import (      Duration,      HushDurationConverter,      ISODateTime, +    PackageName,      TagContentConverter,      TagNameConverter, -    ValidPythonIdentifier,  ) @@ -78,24 +78,23 @@ class ConverterTests(unittest.IsolatedAsyncioTestCase):                  with self.assertRaisesRegex(BadArgument, re.escape(exception_message)):                      await TagNameConverter.convert(self.context, invalid_name) -    async def test_valid_python_identifier_for_valid(self): -        """ValidPythonIdentifier returns valid identifiers unchanged.""" -        test_values = ('foo', 'lemon') +    async def test_package_name_for_valid(self): +        """PackageName returns valid package names unchanged.""" +        test_values = ('foo', 'le_mon', 'num83r')          for name in test_values:              with self.subTest(identifier=name): -                conversion = await ValidPythonIdentifier.convert(self.context, name) +                conversion = await PackageName.convert(self.context, name)                  self.assertEqual(name, conversion) -    async def test_valid_python_identifier_for_invalid(self): -        """ValidPythonIdentifier raises the proper exception for invalid identifiers.""" -        test_values = ('nested.stuff', '#####') +    async def test_package_name_for_invalid(self): +        """PackageName raises the proper exception for invalid package names.""" +        test_values = ('text_with_a_dot.', 'UpperCaseName', 'dashed-name')          for name in test_values:              with self.subTest(identifier=name): -                exception_message = f'`{name}` is not a valid Python identifier' -                with self.assertRaisesRegex(BadArgument, re.escape(exception_message)): -                    await ValidPythonIdentifier.convert(self.context, name) +                with self.assertRaises(BadArgument): +                    await PackageName.convert(self.context, name)      async def test_duration_converter_for_valid(self):          """Duration returns the correct `datetime` for valid duration strings.""" | 
