generated from jCloud/repository-template
style: limit line length to 79 characters
Limited the lengths of all lines to 79 characters (if possible). Additionally, some other lines were breaked for consistence.
This commit is contained in:
25 files changed
+6726
-1071
No files matched your search
@@ -16,7 +16,9 @@ import pathlib
|
||||
import pytest
|
||||
from jcloud_docsgen.cli.cli import create_argument_parser, main as cli_main
|
||||
from jcloud_docsgen.cli.languages.python import parse_sections_list
|
||||
from jcloud_docsgen.core.python import PythonDefinitionDocumentationIncludeSections
|
||||
from jcloud_docsgen.core.python import (
|
||||
PythonDefinitionDocumentationIncludeSections
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize('include,exclude,expected', [
|
||||
(
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -13,28 +13,98 @@
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python import PythonDocumentationGenerator
|
||||
from src.jcloud_docsgen.core.python.namespaces import PythonModuleNamespace, PythonPackageNamespace
|
||||
from src.jcloud_docsgen.core.python.namespaces import (
|
||||
PythonModuleNamespace,
|
||||
PythonPackageNamespace
|
||||
)
|
||||
import pytest
|
||||
import pathlib
|
||||
|
||||
@pytest.mark.parametrize('python_documentation_generator,expected', [
|
||||
(PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_1'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_1/docs')), []),
|
||||
(PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/docs')), [PythonPackageNamespace('pkg', [PythonModuleNamespace('module')])]),
|
||||
(PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_3'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_3/docs')), [PythonPackageNamespace('pkg', [PythonModuleNamespace('module'), PythonPackageNamespace('pkg', [PythonModuleNamespace('module2')])])]),
|
||||
(
|
||||
PythonDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_1'
|
||||
),
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_1/docs'
|
||||
)
|
||||
),
|
||||
[]
|
||||
),
|
||||
(
|
||||
PythonDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_2'
|
||||
),
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_2/docs'
|
||||
)
|
||||
),
|
||||
[
|
||||
PythonPackageNamespace(
|
||||
'pkg',
|
||||
[
|
||||
PythonModuleNamespace('module')
|
||||
]
|
||||
)
|
||||
]
|
||||
),
|
||||
(
|
||||
PythonDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_3'
|
||||
),
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_3/docs'
|
||||
)
|
||||
),
|
||||
[
|
||||
PythonPackageNamespace(
|
||||
'pkg',
|
||||
[
|
||||
PythonModuleNamespace('module'),
|
||||
PythonPackageNamespace(
|
||||
'pkg',
|
||||
[
|
||||
PythonModuleNamespace(
|
||||
'module2'
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
),
|
||||
])
|
||||
def test_PythonDocumentationGenerator_collect_modules(python_documentation_generator: PythonDocumentationGenerator, expected):
|
||||
def test_PythonDocumentationGenerator_collect_modules(
|
||||
python_documentation_generator: PythonDocumentationGenerator,
|
||||
expected
|
||||
):
|
||||
assert python_documentation_generator.namespace() == expected
|
||||
|
||||
@pytest.mark.parametrize('dir', [
|
||||
d.name
|
||||
for d in pathlib.Path('tests/unit/core/python/_core/test_PythonDocumentationGenerator/test_generate_documentation/').iterdir()
|
||||
for d in pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/test_PythonDocume'
|
||||
'ntationGenerator/test_generate_documentation/'
|
||||
)
|
||||
).iterdir()
|
||||
])
|
||||
def test_generate_documentation(dir, expectdir):
|
||||
with expectdir('test_PythonDocumentationGenerator/test_generate_documentation/' + dir) as d:
|
||||
with expectdir(
|
||||
'test_PythonDocumentationGenerator/test_generate_documentation/' + dir
|
||||
) as d:
|
||||
PythonDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
str(
|
||||
pathlib.Path('tests/unit/core/python/_core/test_PythonDocumentationGenerator/test_generate_documentation/') /
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/test_PythonDocume'
|
||||
'ntationGenerator/test_generate_documentation/'
|
||||
)
|
||||
) /
|
||||
dir /
|
||||
'project'
|
||||
)
|
||||
@@ -42,5 +112,4 @@ def test_generate_documentation(dir, expectdir):
|
||||
pathlib.Path(str(d)),
|
||||
allow_tables = True,
|
||||
allow_html = False
|
||||
).generate_documentation(
|
||||
) # simplify the tests
|
||||
).generate_documentation() # simplify the tests
|
||||
+7
-1
@@ -12,7 +12,13 @@ class UserService:
|
||||
def __init__(self, storage: InMemoryUserStorage):
|
||||
self.storage = storage
|
||||
|
||||
def create_user(self, user_id: str, name: str, email: str, age: int | None = None) -> User:
|
||||
def create_user(
|
||||
self,
|
||||
user_id: str,
|
||||
name: str,
|
||||
email: str,
|
||||
age: int | None = None
|
||||
) -> User:
|
||||
"""
|
||||
Creates and stores a new user.
|
||||
"""
|
||||
|
||||
@@ -17,29 +17,223 @@ from src.jcloud_docsgen.exceptions import InvalidNamespaceError
|
||||
import pathlib
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('module_path,src_directory,expected_exception,expected_exception_msg', [
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e.f'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e.f'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e.f.g'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e.f.g'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e.f.g.h'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e.f.g.h'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e.f.g.h.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e.f.g.h.py'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e.f.g.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e.f.g.py'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e.f.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e.f.py'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.e.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.e.py'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.d.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.d.py'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.c.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.c.py'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/files_with_more_than_one_dot/a.b.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.py'),
|
||||
(pathlib.Path('tests/unit/core/python/_core/directory.with.more.than.one.dot/a.b.py'), None, InvalidNamespaceError, 'namespace identifier cannot contain more than one dot: a.b.py'),
|
||||
])
|
||||
def test_PythonModuleDocumentationGenerator_exceptions(module_path, src_directory, expected_exception, expected_exception_msg):
|
||||
@pytest.mark.parametrize(
|
||||
'module_path,src_directory,expected_exception,expected_exception_msg',
|
||||
[
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/fil'
|
||||
'es_with_more_than_one_dot/a.b.c'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
'namespace identifier cannot contain more than one dot: a.b.c'
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/file'
|
||||
's_with_more_than_one_dot/a.b.c.d'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
'namespace identifier cannot contain more than one dot: a.b.c.d'
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files'
|
||||
'_with_more_than_one_dot/a.b.c.d.e'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot cont'
|
||||
'ain more than one dot: a.b.c.d.e'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files_'
|
||||
'with_more_than_one_dot/a.b.c.d.e.f'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot conta'
|
||||
'in more than one dot: a.b.c.d.e.f'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files_w'
|
||||
'ith_more_than_one_dot/a.b.c.d.e.f.g'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot contai'
|
||||
'n more than one dot: a.b.c.d.e.f.g'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files_wi'
|
||||
'th_more_than_one_dot/a.b.c.d.e.f.g.h'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot contain'
|
||||
' more than one dot: a.b.c.d.e.f.g.h'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files_wit'
|
||||
'h_more_than_one_dot/a.b.c.d.e.f.g.h.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot contain m'
|
||||
'ore than one dot: a.b.c.d.e.f.g.h.py'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files_wi'
|
||||
'th_more_than_one_dot/a.b.c.d.e.f.g.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot contain '
|
||||
'more than one dot: a.b.c.d.e.f.g.py'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files_w'
|
||||
'ith_more_than_one_dot/a.b.c.d.e.f.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot contain'
|
||||
' more than one dot: a.b.c.d.e.f.py'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files_'
|
||||
'with_more_than_one_dot/a.b.c.d.e.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot contai'
|
||||
'n more than one dot: a.b.c.d.e.py'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/files'
|
||||
'_with_more_than_one_dot/a.b.c.d.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot conta'
|
||||
'in more than one dot: a.b.c.d.py'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/file'
|
||||
's_with_more_than_one_dot/a.b.c.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
(
|
||||
'namespace identifier cannot cont'
|
||||
'ain more than one dot: a.b.c.py'
|
||||
)
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
(
|
||||
'tests/unit/core/python/_core/fil'
|
||||
'es_with_more_than_one_dot/a.b.py'
|
||||
)
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
'namespace identifier cannot contain more than one dot: a.b.py'
|
||||
),
|
||||
(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/direc'
|
||||
'tory.with.more.than.one.dot/a.b.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
InvalidNamespaceError,
|
||||
'namespace identifier cannot contain more than one dot: a.b.py'
|
||||
),
|
||||
]
|
||||
)
|
||||
def test_PythonModuleDocumentationGenerator_exceptions(
|
||||
module_path,
|
||||
src_directory,
|
||||
expected_exception,
|
||||
expected_exception_msg
|
||||
):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
PythonModuleDocumentationGenerator(module_path, src_directory)
|
||||
assert str(exc_info.value) == expected_exception_msg
|
||||
|
||||
@pytest.mark.parametrize('module_documentation_generator,expected', [
|
||||
(PythonModuleDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/src/pkg/module.py'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/src/'), allow_tables = True, allow_html = False), '''# Module `pkg.module`
|
||||
(
|
||||
PythonModuleDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/test_pr'
|
||||
'oject_dirs/pdir_2/src/pkg/module.py'
|
||||
)
|
||||
),
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_2/src/'
|
||||
),
|
||||
allow_tables = True,
|
||||
allow_html = False
|
||||
),
|
||||
'''# Module `pkg.module`
|
||||
|
||||
A test module.
|
||||
|
||||
@@ -206,9 +400,22 @@ Barks.
|
||||
|
||||
### Docstring
|
||||
|
||||
A dog.'''),
|
||||
A dog.'''
|
||||
),
|
||||
|
||||
(PythonModuleDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/src/pkg/module.py'), None, allow_tables = True, allow_html = False), '''# Module `module`
|
||||
(
|
||||
PythonModuleDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/test_pr'
|
||||
'oject_dirs/pdir_2/src/pkg/module.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
allow_tables = True,
|
||||
allow_html = False
|
||||
),
|
||||
'''# Module `module`
|
||||
|
||||
A test module.
|
||||
|
||||
@@ -375,9 +582,23 @@ Barks.
|
||||
|
||||
### Docstring
|
||||
|
||||
A dog.'''),
|
||||
A dog.'''
|
||||
),
|
||||
|
||||
(PythonModuleDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/src/pkg/module.py'), None, allow_tables = True, allow_html = True), '''# Module `module`
|
||||
(
|
||||
PythonModuleDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/test_pr'
|
||||
'oject_dirs/pdir_2/src/pkg/module.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
allow_tables = True,
|
||||
allow_html = True
|
||||
),
|
||||
(
|
||||
'''# Module `module`
|
||||
|
||||
A test module.
|
||||
|
||||
@@ -411,7 +632,8 @@ Return type: `int | float`
|
||||
|
||||
|
||||
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>Adds two numbers.
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>Adds '''
|
||||
'''two numbers.
|
||||
|
||||
:param a: The first number.
|
||||
:type a: int | float
|
||||
@@ -478,7 +700,8 @@ Return type: `str`
|
||||
|
||||
|
||||
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>Returns a greeting.
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>'''
|
||||
'''Returns a greeting.
|
||||
|
||||
:return: The greeting.
|
||||
:rtype: str</details>
|
||||
@@ -487,7 +710,8 @@ Return type: `str`
|
||||
|
||||
|
||||
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A class representing an animal.
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A '''
|
||||
'''class representing an animal.
|
||||
|
||||
:param name: Its name.
|
||||
:type name: str</details>
|
||||
@@ -528,15 +752,32 @@ Return type: `None`
|
||||
|
||||
|
||||
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>Barks.</details>
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>Barks.'''
|
||||
'''</details>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A dog.</details>'''),
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A dog.'''
|
||||
'</details>'
|
||||
)
|
||||
),
|
||||
|
||||
(PythonModuleDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/src/pkg/module.py'), None, allow_tables = False, allow_html = True), '''# Module `module`
|
||||
(
|
||||
PythonModuleDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/test_pr'
|
||||
'oject_dirs/pdir_2/src/pkg/module.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
allow_tables = False,
|
||||
allow_html = True
|
||||
),
|
||||
(
|
||||
'''# Module `module`
|
||||
|
||||
A test module.
|
||||
|
||||
@@ -585,7 +826,8 @@ Return type: `int | float`
|
||||
|
||||
|
||||
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>Adds two numbers.
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>Adds '''
|
||||
'''two numbers.
|
||||
|
||||
:param a: The first number.
|
||||
:type a: int | float
|
||||
@@ -673,7 +915,8 @@ Return type: `str`
|
||||
|
||||
|
||||
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>Returns a greeting.
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>'''
|
||||
'''Returns a greeting.
|
||||
|
||||
:return: The greeting.
|
||||
:rtype: str</details>
|
||||
@@ -682,7 +925,8 @@ Return type: `str`
|
||||
|
||||
|
||||
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A class representing an animal.
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A '''
|
||||
'''class representing an animal.
|
||||
|
||||
:param name: Its name.
|
||||
:type name: str</details>
|
||||
@@ -729,15 +973,31 @@ Return type: `None`
|
||||
|
||||
|
||||
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>Barks.</details>
|
||||
<details><summary><h5 style="display:inline">Docstring</h5></summary>Barks.'''
|
||||
'''</details>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A dog.</details>'''),
|
||||
<details><summary><h3 style="display:inline">Docstring</h3></summary>A '''
|
||||
'dog.</details>'
|
||||
)
|
||||
),
|
||||
|
||||
(PythonModuleDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/src/pkg/module.py'), None, allow_tables = False, allow_html = False), '''# Module `module`
|
||||
(
|
||||
PythonModuleDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
(
|
||||
'tests/unit/core/python/_core/test_pr'
|
||||
'oject_dirs/pdir_2/src/pkg/module.py'
|
||||
)
|
||||
),
|
||||
None,
|
||||
allow_tables = False,
|
||||
allow_html = False
|
||||
),
|
||||
'''# Module `module`
|
||||
|
||||
A test module.
|
||||
|
||||
@@ -946,7 +1206,11 @@ Barks.
|
||||
|
||||
### Docstring
|
||||
|
||||
A dog.'''),
|
||||
A dog.'''
|
||||
),
|
||||
])
|
||||
def test_PythonModuleDocumentationGenerator_exceptions(module_documentation_generator, expected):
|
||||
def test_PythonModuleDocumentationGenerator_exceptions(
|
||||
module_documentation_generator,
|
||||
expected
|
||||
):
|
||||
assert module_documentation_generator.generate_documentation() == expected
|
||||
@@ -13,8 +13,14 @@
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python._core import _collect_definitions
|
||||
from src.jcloud_docsgen.core.python.definitions import PythonClassDefinition, PythonFunctionDefinition
|
||||
from src.jcloud_docsgen.core.python.arguments import PythonFunctionArgument, PythonArgumentKind
|
||||
from src.jcloud_docsgen.core.python.definitions import (
|
||||
PythonClassDefinition,
|
||||
PythonFunctionDefinition
|
||||
)
|
||||
from src.jcloud_docsgen.core.python.arguments import (
|
||||
PythonFunctionArgument,
|
||||
PythonArgumentKind
|
||||
)
|
||||
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
|
||||
import pytest
|
||||
import ast
|
||||
@@ -33,13 +39,33 @@ def module(source_code: str) -> ast.Module:
|
||||
return ast.parse(source_code)
|
||||
|
||||
@pytest.mark.parametrize('module,expected', [
|
||||
(module('def add(a, b):\n\treturn a + b'), [
|
||||
PythonFunctionDefinition('add', [
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, None),
|
||||
], None, None, [])
|
||||
]),
|
||||
(module('''def add(a: int | float, b: int | float) -> int | float:
|
||||
(
|
||||
module('def add(a, b):\n\treturn a + b'),
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'add',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
],
|
||||
None,
|
||||
None,
|
||||
[]
|
||||
)
|
||||
]
|
||||
),
|
||||
(
|
||||
module('''def add(a: int | float, b: int | float) -> int | float:
|
||||
"""Adds two numbers."""
|
||||
return a + b
|
||||
|
||||
@@ -62,26 +88,94 @@ class Something:
|
||||
Returns the value.
|
||||
"""
|
||||
return self._value
|
||||
'''), [
|
||||
PythonFunctionDefinition('add', [
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, 'int | float'),
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, 'int | float'),
|
||||
], 'int | float', PythonDocstring('Adds two numbers.'), []),
|
||||
PythonClassDefinition('Something', [], PythonDocstring('A class.'), [], [
|
||||
PythonFunctionDefinition('__init__', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('value', PythonArgumentKind.NORMAL, '42', 'typing.Any'),
|
||||
], 'None', None, []),
|
||||
PythonFunctionDefinition('value', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.NORMAL, None, None),
|
||||
], 'typing.Any', PythonDocstring('The value.'), ['property']),
|
||||
PythonFunctionDefinition('get_value', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.NORMAL, None, None),
|
||||
], 'typing.Any', PythonDocstring('Returns the value.'), []),
|
||||
])
|
||||
]),
|
||||
(module('''from src.jcloud_docsgen.core.python import PythonDocumentationGenerator
|
||||
from src.jcloud_docsgen.core.python.namespaces import PythonModuleNamespace, PythonPackageNamespace
|
||||
'''),
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'add',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int | float'
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int | float'
|
||||
),
|
||||
],
|
||||
'int | float',
|
||||
PythonDocstring('Adds two numbers.'),
|
||||
[]
|
||||
),
|
||||
PythonClassDefinition(
|
||||
'Something',
|
||||
[],
|
||||
PythonDocstring('A class.'),
|
||||
[],
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'__init__',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'value',
|
||||
PythonArgumentKind.NORMAL,
|
||||
'42',
|
||||
'typing.Any'
|
||||
),
|
||||
],
|
||||
'None',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
PythonFunctionDefinition(
|
||||
'value',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
],
|
||||
'typing.Any',
|
||||
PythonDocstring('The value.'),
|
||||
['property']
|
||||
),
|
||||
PythonFunctionDefinition(
|
||||
'get_value',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
],
|
||||
'typing.Any',
|
||||
PythonDocstring('Returns the value.'),
|
||||
[]
|
||||
),
|
||||
]
|
||||
)
|
||||
]
|
||||
),
|
||||
(
|
||||
module('''from src.jcloud_docsgen.core.python import (
|
||||
PythonDocumentationGenerator
|
||||
)
|
||||
from src.jcloud_docsgen.core.python.namespaces import (
|
||||
PythonModuleNamespace,
|
||||
PythonPackageNamespace
|
||||
)
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('project_directory,docs_directory', [
|
||||
@@ -94,45 +188,186 @@ import pytest
|
||||
(1, pathlib.Path('tests')),
|
||||
(None, pathlib.Path('tests')),
|
||||
])
|
||||
def test_PythonDocumentationGenerator_type_exceptions(project_directory, docs_directory):
|
||||
def test_PythonDocumentationGenerator_type_exceptions(
|
||||
project_directory,
|
||||
docs_directory
|
||||
):
|
||||
with pytest.raises(TypeError):
|
||||
PythonDocumentationGenerator(project_directory, docs_directory)
|
||||
|
||||
@pytest.mark.parametrize('python_documentation_generator,expected', [
|
||||
(PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_1'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_1/docs')), []),
|
||||
(PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/docs')), [PythonPackageNamespace('pkg', [PythonModuleNamespace('module')])]),
|
||||
(PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_3'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_3/docs')), [PythonPackageNamespace('pkg', [PythonModuleNamespace('module'), PythonPackageNamespace('pkg', [PythonModuleNamespace('module2')])])]),
|
||||
(PythonDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_1'
|
||||
),
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_1/docs'
|
||||
)
|
||||
), []),
|
||||
(PythonDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_2'
|
||||
),
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_2/docs'
|
||||
)
|
||||
), [PythonPackageNamespace('pkg', [PythonModuleNamespace('module')])]),
|
||||
(PythonDocumentationGenerator(
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_3'
|
||||
),
|
||||
pathlib.Path(
|
||||
'tests/unit/core/python/_core/test_project_dirs/pdir_3/docs'
|
||||
)
|
||||
), [
|
||||
PythonPackageNamespace(
|
||||
'pkg',
|
||||
[
|
||||
PythonModuleNamespace('module'),
|
||||
PythonPackageNamespace('pkg',
|
||||
[PythonModuleNamespace('module2')]
|
||||
)
|
||||
]
|
||||
)
|
||||
])
|
||||
])
|
||||
def test_PythonDocumentationGenerator_collect_modules(python_documentation_generator: PythonDocumentationGenerator, expected):
|
||||
assert python_documentation_generator.namespace() == expected'''), [
|
||||
PythonFunctionDefinition('test_PythonDocumentationGenerator_type_exceptions', [
|
||||
PythonFunctionArgument('project_directory', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('docs_directory', PythonArgumentKind.NORMAL, None, None),
|
||||
], None, None, ['''pytest.mark.parametrize('project_directory,docs_directory', [(1, 1), (1, None), (42, None), (pathlib.Path('tests'), 1), (pathlib.Path('tests/'), 1), (1, pathlib.Path('tests/')), (1, pathlib.Path('tests')), (None, pathlib.Path('tests'))])''']),
|
||||
PythonFunctionDefinition('test_PythonDocumentationGenerator_collect_modules', [
|
||||
PythonFunctionArgument('python_documentation_generator', PythonArgumentKind.NORMAL, None, 'PythonDocumentationGenerator'),
|
||||
PythonFunctionArgument('expected', PythonArgumentKind.NORMAL, None, None)
|
||||
], None, None, ['''pytest.mark.parametrize('python_documentation_generator,expected', [(PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_1'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_1/docs')), []), (PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_2/docs')), [PythonPackageNamespace('pkg', [PythonModuleNamespace('module')])]), (PythonDocumentationGenerator(pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_3'), pathlib.Path('tests/unit/core/python/_core/test_project_dirs/pdir_3/docs')), [PythonPackageNamespace('pkg', [PythonModuleNamespace('module'), PythonPackageNamespace('pkg', [PythonModuleNamespace('module2')])])])])'''])
|
||||
]),
|
||||
(module('''class C1:
|
||||
def test_PythonDocumentationGenerator_collect_modules(
|
||||
python_documentation_generator: PythonDocumentationGenerator,
|
||||
expected
|
||||
):
|
||||
assert python_documentation_generator.namespace() == expected'''),
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'test_PythonDocumentationGenerator_type_exceptions',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'project_directory',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'docs_directory',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
],
|
||||
None,
|
||||
None,
|
||||
[
|
||||
(
|
||||
'''pytest.mark.parametrize('project_directo'''
|
||||
'''ry,docs_directory', [(1, 1), (1, None), '''
|
||||
'''(42, None), (pathlib.Path('tests'), 1), '''
|
||||
'''(pathlib.Path('tests/'), 1), (1, pathlib'''
|
||||
'''.Path('tests/')), (1, pathlib.Path('test'''
|
||||
'''s')), (None, pathlib.Path('tests'))])'''
|
||||
)
|
||||
]
|
||||
),
|
||||
PythonFunctionDefinition(
|
||||
'test_PythonDocumentationGenerator_collect_modules',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'python_documentation_generator',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'PythonDocumentationGenerator'
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'expected',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
],
|
||||
None,
|
||||
None,
|
||||
[(
|
||||
'''pytest.mark.parametrize('python_documentation_gener'''
|
||||
'''ator,expected', [(PythonDocumentationGenerator(path'''
|
||||
'''lib.Path('tests/unit/core/python/_core/test_project'''
|
||||
'''_dirs/pdir_1'), pathlib.Path('tests/unit/core/pytho'''
|
||||
'''n/_core/test_project_dirs/pdir_1/docs')), []), (Pyt'''
|
||||
'''honDocumentationGenerator(pathlib.Path('tests/unit/'''
|
||||
'''core/python/_core/test_project_dirs/pdir_2'), pathl'''
|
||||
'''ib.Path('tests/unit/core/python/_core/test_project_'''
|
||||
'''dirs/pdir_2/docs')), [PythonPackageNamespace('pkg','''
|
||||
''' [PythonModuleNamespace('module')])]), (PythonDocum'''
|
||||
'''entationGenerator(pathlib.Path('tests/unit/core/pyt'''
|
||||
'''hon/_core/test_project_dirs/pdir_3'), pathlib.Path('''
|
||||
''''tests/unit/core/python/_core/test_project_dirs/pdi'''
|
||||
'''r_3/docs')), [PythonPackageNamespace('pkg', [Python'''
|
||||
'''ModuleNamespace(\'module'), PythonPackageNamespace('''
|
||||
'''\'pkg\', [PythonModuleNamespace(\'module2')])])])])'''
|
||||
)]
|
||||
)
|
||||
]
|
||||
),
|
||||
(
|
||||
module(
|
||||
'''class C1:
|
||||
class C2:
|
||||
class C3:
|
||||
def function(self, value: bool, /, arg) -> bool:
|
||||
def should_be_not_in_the_result(arg: int) -> object:
|
||||
pass
|
||||
return not value'''), [
|
||||
PythonClassDefinition('C1', [], None, [], [
|
||||
PythonClassDefinition('C2', [], None, [], [
|
||||
PythonClassDefinition('C3', [], None, [], [
|
||||
PythonFunctionDefinition('function', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||
PythonFunctionArgument('value', PythonArgumentKind.POSITIONAL_ONLY, None, 'bool'),
|
||||
PythonFunctionArgument('arg', PythonArgumentKind.NORMAL, None, None),
|
||||
], 'bool', None, [])
|
||||
])
|
||||
])
|
||||
])
|
||||
])
|
||||
return not value'''
|
||||
),
|
||||
[
|
||||
PythonClassDefinition(
|
||||
'C1',
|
||||
[],
|
||||
None,
|
||||
[],
|
||||
[
|
||||
PythonClassDefinition(
|
||||
'C2',
|
||||
[],
|
||||
None,
|
||||
[],
|
||||
[
|
||||
PythonClassDefinition(
|
||||
'C3',
|
||||
[],
|
||||
None,
|
||||
[],
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'function',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'value',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
'bool'
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'arg',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
],
|
||||
'bool',
|
||||
None,
|
||||
[]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
])
|
||||
def test___collect_definitions(module, expected):
|
||||
assert list(_collect_definitions(module)) == expected
|
||||
@@ -12,12 +12,20 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python._core import _function_params_info, _FunctionParamInfo
|
||||
from src.jcloud_docsgen.core.python.arguments import PythonFunctionArgument, PythonArgumentKind
|
||||
from src.jcloud_docsgen.core.python._core import (
|
||||
_function_params_info,
|
||||
_FunctionParamInfo
|
||||
)
|
||||
from src.jcloud_docsgen.core.python.arguments import (
|
||||
PythonFunctionArgument,
|
||||
PythonArgumentKind
|
||||
)
|
||||
import pytest
|
||||
import docstring_parser
|
||||
|
||||
def docstring_args_list(docstring: str) -> list[docstring_parser.DocstringParam]:
|
||||
def docstring_args_list(
|
||||
docstring: str
|
||||
) -> list[docstring_parser.DocstringParam]:
|
||||
'''
|
||||
Returns the parameter list of a docstring.
|
||||
|
||||
@@ -101,4 +109,7 @@ def docstring_args_list(docstring: str) -> list[docstring_parser.DocstringParam]
|
||||
([], '', {}),
|
||||
])
|
||||
def test__function_params_types(arg_list, docstring, expected):
|
||||
assert _function_params_info(arg_list, docstring_args_list(docstring)) == expected
|
||||
assert _function_params_info(
|
||||
arg_list,
|
||||
docstring_args_list(docstring)
|
||||
) == expected
|
||||
@@ -15,79 +15,406 @@
|
||||
from src.jcloud_docsgen.core.python._core import _markdown_table
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('header,body,allow_markdown_table,force_row_lengths,one_line_values,missing_value,expected', [
|
||||
([], [], False, False, True, '--', ''),
|
||||
([], [('a',)], False, False, True, '--', ''),
|
||||
(['a'], [], False, False, True, '--', ''),
|
||||
(['a'], [(1,)], True, False, True, '--', '''| a |
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
'header,body,allow_markdown_table,force_row_le'
|
||||
'ngths,one_line_values,missing_value,expected'
|
||||
),
|
||||
[
|
||||
(
|
||||
[],
|
||||
[],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
''
|
||||
),
|
||||
(
|
||||
[],
|
||||
[('a',)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[(1,)],
|
||||
True,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''| a |
|
||||
| --- |
|
||||
| 1 |'''),
|
||||
(['a'], [(1, 2)], True, False, True, '--', '''| a |
|
||||
| 1 |'''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[(1, 2)],
|
||||
True,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''| a |
|
||||
| --- |
|
||||
| 1 |'''),
|
||||
(['a', 'b'], [(1, 2)], True, False, True, '--', '''| a | b |
|
||||
| 1 |'''
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1, 2)],
|
||||
True,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''| a | b |
|
||||
| --- | --- |
|
||||
| 1 | 2 |'''),
|
||||
(['a', 'b'], [(1,)], True, False, True, '--', '''| a | b |
|
||||
| 1 | 2 |'''
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1,)],
|
||||
True,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''| a | b |
|
||||
| --- | --- |
|
||||
| 1 | -- |'''),
|
||||
(['a', 'b'], [()], True, False, True, '--', '''| a | b |
|
||||
| 1 | -- |'''
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[()],
|
||||
True,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''| a | b |
|
||||
| --- | --- |
|
||||
| -- | -- |'''),
|
||||
(['a', 'b'], [(1, 2), (3, 4)], True, False, True, '--', '''| a | b |
|
||||
| -- | -- |'''
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1, 2), (3, 4)],
|
||||
True,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''| a | b |
|
||||
| --- | --- |
|
||||
| 1 | 2 |
|
||||
| 3 | 4 |'''),
|
||||
(['a'], [('line1\nline2',)], True, False, True, '--', '''| a |
|
||||
| 3 | 4 |'''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[('line1\nline2',)],
|
||||
True,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''| a |
|
||||
| --- |
|
||||
| line1 line2 |'''),
|
||||
(['a'], [('line1\nline2',)], True, False, False, '--', '''| a |
|
||||
| line1 line2 |'''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[('line1\nline2',)],
|
||||
True,
|
||||
False,
|
||||
False,
|
||||
'--',
|
||||
'''| a |
|
||||
| --- |
|
||||
| line1\nline2 |'''),
|
||||
| line1\nline2 |'''
|
||||
),
|
||||
|
||||
([], [], False, False, True, '--', ''),
|
||||
([], [('a',)], False, False, True, '--', ''),
|
||||
(['a'], [], False, False, True, '--', ''),
|
||||
(['a'], [(1,)], False, False, True, '--', '''- a: 1'''),
|
||||
(['a'], [(1, 2)], False, False, True, '--', '''- a: 1'''),
|
||||
(['a', 'b'], [(1, 2)], False, False, True, '--', '''- a: 1
|
||||
(
|
||||
[],
|
||||
[],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
''
|
||||
),
|
||||
(
|
||||
[],
|
||||
[('a',)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[(1,)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'- a: 1'
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[(1, 2)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'- a: 1'
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1, 2)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''- a: 1
|
||||
|
||||
b: 2'''),
|
||||
(['a', 'b'], [(1,)], False, False, True, '--', '''- a: 1
|
||||
b: 2'''
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1,)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''- a: 1
|
||||
|
||||
b: --'''),
|
||||
(['a', 'b'], [()], False, False, True, '--', '''- a: --
|
||||
b: --'''
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[()],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''- a: --
|
||||
|
||||
b: --'''),
|
||||
(['a', 'b'], [(1, 2), (3, 4)], False, False, True, '--', '''- a: 1
|
||||
b: --'''
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1, 2), (3, 4)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'''- a: 1
|
||||
|
||||
b: 2
|
||||
|
||||
- a: 3
|
||||
|
||||
b: 4'''),
|
||||
(['a'], [('line1\nline2',)], False, False, True, '--', '- a: line1 line2'),
|
||||
(['a'], [('line1\nline2',)], False, False, False, '--', '- a: line1\nline2'),
|
||||
])
|
||||
def test__markdown_table(header, body, allow_markdown_table, force_row_lengths, one_line_values, missing_value, expected):
|
||||
assert _markdown_table(header, body, allow_markdown_table = allow_markdown_table, force_row_lengths = force_row_lengths, one_line_values = one_line_values, missing_value = missing_value) == expected
|
||||
b: 4'''
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[('line1\nline2',)],
|
||||
False,
|
||||
False,
|
||||
True,
|
||||
'--',
|
||||
'- a: line1 line2'
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[('line1\nline2',)],
|
||||
False,
|
||||
False,
|
||||
False,
|
||||
'--',
|
||||
'- a: line1\nline2'
|
||||
)
|
||||
]
|
||||
)
|
||||
def test__markdown_table(
|
||||
header,
|
||||
body,
|
||||
allow_markdown_table,
|
||||
force_row_lengths,
|
||||
one_line_values,
|
||||
missing_value,
|
||||
expected
|
||||
):
|
||||
assert _markdown_table(
|
||||
header,
|
||||
body,
|
||||
allow_markdown_table = allow_markdown_table,
|
||||
force_row_lengths = force_row_lengths,
|
||||
one_line_values = one_line_values,
|
||||
missing_value = missing_value
|
||||
) == expected
|
||||
|
||||
@pytest.mark.parametrize('header,body,allow_markdown_table,force_row_lengths,one_line_values,missing_value,expected_exception,expected_exception_msg', [
|
||||
(['a'], [(1, 2)], True, True, True, '--', ValueError, 'expected 1 value, got 2'),
|
||||
(['a'], [(1, 2, 3)], True, True, True, '--', ValueError, 'expected 1 value, got 3'),
|
||||
(['a'], [()], True, True, True, '--', ValueError, 'expected 1 value, got 0'),
|
||||
(['a', 'b'], [()], True, True, True, '--', ValueError, 'expected 2 values, got 0'),
|
||||
(['a', 'b'], [(1,)], True, True, True, '--', ValueError, 'expected 2 values, got 1'),
|
||||
(['a', 'b'], [(1, 2, 3)], True, True, True, '--', ValueError, 'expected 2 values, got 3'),
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
'header,body,allow_markdown_table,force_row_lengths,one_line_v'
|
||||
'alues,missing_value,expected_exception,expected_exception_msg'
|
||||
),
|
||||
[
|
||||
(
|
||||
['a'],
|
||||
[(1, 2)],
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 1 value, got 2'
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[(1, 2, 3)],
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 1 value, got 3'
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[()],
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 1 value, got 0'
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[()],
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 2 values, got 0'
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1,)],
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 2 values, got 1'
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1, 2, 3)],
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 2 values, got 3'
|
||||
),
|
||||
|
||||
(['a'], [(1, 2)], False, True, True, '--', ValueError, 'expected 1 value, got 2'),
|
||||
(['a'], [(1, 2, 3)], False, True, True, '--', ValueError, 'expected 1 value, got 3'),
|
||||
(['a'], [()], False, True, True, '--', ValueError, 'expected 1 value, got 0'),
|
||||
(['a', 'b'], [()], False, True, True, '--', ValueError, 'expected 2 values, got 0'),
|
||||
(['a', 'b'], [(1,)], False, True, True, '--', ValueError, 'expected 2 values, got 1'),
|
||||
(['a', 'b'], [(1, 2, 3)], False, True, True, '--', ValueError, 'expected 2 values, got 3'),
|
||||
])
|
||||
def test__markdown_table_exceptions(header, body, allow_markdown_table, force_row_lengths, one_line_values, missing_value, expected_exception, expected_exception_msg):
|
||||
(
|
||||
['a'],
|
||||
[(1, 2)],
|
||||
False,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 1 value, got 2'
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[(1, 2, 3)],
|
||||
False,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 1 value, got 3'
|
||||
),
|
||||
(
|
||||
['a'],
|
||||
[()],
|
||||
False,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 1 value, got 0'
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[()],
|
||||
False,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 2 values, got 0'
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1,)],
|
||||
False,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 2 values, got 1'
|
||||
),
|
||||
(
|
||||
['a', 'b'],
|
||||
[(1, 2, 3)],
|
||||
False,
|
||||
True,
|
||||
True,
|
||||
'--',
|
||||
ValueError,
|
||||
'expected 2 values, got 3'
|
||||
)
|
||||
]
|
||||
)
|
||||
def test__markdown_table_exceptions(
|
||||
header,
|
||||
body,
|
||||
allow_markdown_table,
|
||||
force_row_lengths,
|
||||
one_line_values,
|
||||
missing_value,
|
||||
expected_exception,
|
||||
expected_exception_msg
|
||||
):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
_markdown_table(header, body, allow_markdown_table = allow_markdown_table, force_row_lengths = force_row_lengths, one_line_values = one_line_values, missing_value = missing_value)
|
||||
_markdown_table(
|
||||
header,
|
||||
body,
|
||||
allow_markdown_table = allow_markdown_table,
|
||||
force_row_lengths = force_row_lengths,
|
||||
one_line_values = one_line_values,
|
||||
missing_value = missing_value
|
||||
)
|
||||
assert str(exc_info.value) == expected_exception_msg
|
||||
@@ -13,84 +13,271 @@
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python._core import _signature_argument_list
|
||||
from src.jcloud_docsgen.core.python.arguments import PythonFunctionArgument, PythonArgumentKind
|
||||
from src.jcloud_docsgen.core.python.arguments import (
|
||||
PythonFunctionArgument,
|
||||
PythonArgumentKind
|
||||
)
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('argument_list,expected', [
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, None)
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None)
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '/']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '/', 'b']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'c',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '/', 'b', '*', 'c']),
|
||||
([
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'c',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['b', '*', 'c']),
|
||||
([
|
||||
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||
PythonFunctionArgument(
|
||||
'c',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['*', 'c']),
|
||||
([
|
||||
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'c',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['b', '/', '*', 'c']),
|
||||
([
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['*args']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '*args']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a', PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '/', '*args']),
|
||||
([
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument('a', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['*args', 'a']),
|
||||
([
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'kwargs',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['*args', '**kwargs']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'kwargs',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '/', '*args', '**kwargs']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'kwargs',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '*args', '**kwargs']),
|
||||
([
|
||||
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||
PythonFunctionArgument('a', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||
PythonFunctionArgument(
|
||||
'args',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'kwargs',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['*args', 'a', '**kwargs']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'kwargs',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['*', 'a', '**kwargs']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'kwargs',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '**kwargs']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'kwargs',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
], ['a', '/', '**kwargs']),
|
||||
([
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '1', 'int'),
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
'1',
|
||||
'int'
|
||||
),
|
||||
], ['a: int = 1']),
|
||||
])
|
||||
def test__signature_argument_list(argument_list, expected):
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -12,56 +12,374 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python.arguments import PythonFunctionArgument, PythonArgumentKind
|
||||
from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError, InvalidPythonAnnotationError, PythonArgumentStructureError
|
||||
from src.jcloud_docsgen.core.python.arguments import (
|
||||
PythonFunctionArgument,
|
||||
PythonArgumentKind
|
||||
)
|
||||
from src.jcloud_docsgen.exceptions import (
|
||||
InvalidPythonIdentifierError,
|
||||
InvalidPythonAnnotationError,
|
||||
PythonArgumentStructureError
|
||||
)
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('name,kind,default,annotation,expected_exception,expected_exception_msg', [
|
||||
(None, PythonArgumentKind.NORMAL, None, None, TypeError, 'expected \'str\', got \'NoneType\''),
|
||||
(1, PythonArgumentKind.NORMAL, None, None, TypeError, 'expected \'str\', got \'int\''),
|
||||
(42, PythonArgumentKind.NORMAL, None, None, TypeError, 'expected \'str\', got \'int\''),
|
||||
(4.2, PythonArgumentKind.NORMAL, None, None, TypeError, 'expected \'str\', got \'float\''),
|
||||
(4.2, PythonArgumentKind.NORMAL, None, None, TypeError, 'expected \'str\', got \'float\''),
|
||||
('a', PythonArgumentKind.NORMAL, 1, None, TypeError, 'expected either \'str\' or \'NoneType\', got \'int\''),
|
||||
('a', PythonArgumentKind.NORMAL, None, 1, TypeError, 'expected either \'str\' or \'NoneType\', got \'int\''),
|
||||
('a', 0, None, None, TypeError, 'expected \'PythonArgumentKind\', got \'int\''),
|
||||
('a', PythonArgumentKind.NORMAL, None, '', InvalidPythonAnnotationError, 'invalid annotation'),
|
||||
('', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier'),
|
||||
('.', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: .'),
|
||||
('#', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: #'),
|
||||
('1a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: 1a'),
|
||||
('1_', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: 1_'),
|
||||
('1,', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: 1,'),
|
||||
(',', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: ,'),
|
||||
(':', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: :'),
|
||||
('-', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: -'),
|
||||
(';', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: ;'),
|
||||
('|', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: |'),
|
||||
('<', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: <'),
|
||||
('>', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: >'),
|
||||
('a,', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: a,'),
|
||||
(',a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: ,a'),
|
||||
('a:', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: a:'),
|
||||
(':a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: :a'),
|
||||
('a-', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: a-'),
|
||||
('-a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: -a'),
|
||||
('a;', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: a;'),
|
||||
(';a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: ;a'),
|
||||
('a|', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: a|'),
|
||||
('|a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: |a'),
|
||||
('a<', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: a<'),
|
||||
('<a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: <a'),
|
||||
('a>', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: a>'),
|
||||
('>a', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: >a'),
|
||||
('class', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: class'),
|
||||
('def', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: def'),
|
||||
('lambda', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: lambda'),
|
||||
('None', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: None'),
|
||||
('True', PythonArgumentKind.NORMAL, None, None, InvalidPythonIdentifierError, 'invalid identifier: True'),
|
||||
('a', PythonArgumentKind.VARARG, '1', None, PythonArgumentStructureError, '* or ** arguments cannot have default values: *a = 1'),
|
||||
('a', PythonArgumentKind.KWARGS, '1', None, PythonArgumentStructureError, '* or ** arguments cannot have default values: **a = 1'),
|
||||
])
|
||||
def test_PythonFunctionArgument_exceptions(name, kind, default, annotation, expected_exception, expected_exception_msg):
|
||||
@pytest.mark.parametrize(
|
||||
'name,kind,default,annotation,expected_exception,expected_exception_msg',
|
||||
[
|
||||
(
|
||||
None,
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
TypeError,
|
||||
'expected \'str\', got \'NoneType\''
|
||||
),
|
||||
(
|
||||
1,
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
TypeError,
|
||||
'expected \'str\', got \'int\''
|
||||
),
|
||||
(
|
||||
42,
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
TypeError,
|
||||
'expected \'str\', got \'int\''
|
||||
),
|
||||
(
|
||||
4.2,
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
TypeError,
|
||||
'expected \'str\', got \'float\''
|
||||
),
|
||||
(
|
||||
4.2,
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
TypeError,
|
||||
'expected \'str\', got \'float\''
|
||||
),
|
||||
(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
1,
|
||||
None,
|
||||
TypeError,
|
||||
'expected either \'str\' or \'NoneType\', got \'int\''
|
||||
),
|
||||
(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
1,
|
||||
TypeError,
|
||||
'expected either \'str\' or \'NoneType\', got \'int\''
|
||||
),
|
||||
(
|
||||
'a',
|
||||
0,
|
||||
None,
|
||||
None,
|
||||
TypeError,
|
||||
'expected \'PythonArgumentKind\', got \'int\''
|
||||
),
|
||||
(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'',
|
||||
InvalidPythonAnnotationError,
|
||||
'invalid annotation'
|
||||
),
|
||||
(
|
||||
'',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier'
|
||||
),
|
||||
(
|
||||
'.',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: .'
|
||||
),
|
||||
(
|
||||
'#',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: #'
|
||||
),
|
||||
(
|
||||
'1a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1a'
|
||||
),
|
||||
(
|
||||
'1_',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1_'
|
||||
),
|
||||
(
|
||||
'1,',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1,'
|
||||
),
|
||||
(
|
||||
',',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,'
|
||||
),
|
||||
(
|
||||
':',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :'
|
||||
),
|
||||
(
|
||||
'-',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -'
|
||||
),
|
||||
(
|
||||
';',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;'
|
||||
),
|
||||
(
|
||||
'|',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |'
|
||||
),
|
||||
(
|
||||
'<',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <'
|
||||
),
|
||||
(
|
||||
'>',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >'
|
||||
),
|
||||
(
|
||||
'a,',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a,'
|
||||
),
|
||||
(
|
||||
',a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,a'
|
||||
),
|
||||
(
|
||||
'a:',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a:'
|
||||
),
|
||||
(
|
||||
':a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :a'
|
||||
),
|
||||
(
|
||||
'a-',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a-'
|
||||
),
|
||||
(
|
||||
'-a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -a'
|
||||
),
|
||||
(
|
||||
'a;',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a;'
|
||||
),
|
||||
(
|
||||
';a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;a'
|
||||
),
|
||||
(
|
||||
'a|',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a|'
|
||||
),
|
||||
(
|
||||
'|a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |a'
|
||||
),
|
||||
(
|
||||
'a<',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a<'
|
||||
),
|
||||
(
|
||||
'<a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <a'
|
||||
),
|
||||
(
|
||||
'a>',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a>'
|
||||
),
|
||||
(
|
||||
'>a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >a'
|
||||
),
|
||||
(
|
||||
'class',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: class'
|
||||
),
|
||||
(
|
||||
'def',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: def'
|
||||
),
|
||||
(
|
||||
'lambda',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: lambda'
|
||||
),
|
||||
(
|
||||
'None',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: None'
|
||||
),
|
||||
(
|
||||
'True',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None,
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: True'
|
||||
),
|
||||
(
|
||||
'a',
|
||||
PythonArgumentKind.VARARG,
|
||||
'1',
|
||||
None,
|
||||
PythonArgumentStructureError,
|
||||
'* or ** arguments cannot have default values: *a = 1'
|
||||
),
|
||||
(
|
||||
'a',
|
||||
PythonArgumentKind.KWARGS,
|
||||
'1',
|
||||
None,
|
||||
PythonArgumentStructureError,
|
||||
'* or ** arguments cannot have default values: **a = 1'
|
||||
),
|
||||
]
|
||||
)
|
||||
def test_PythonFunctionArgument_exceptions(
|
||||
name,
|
||||
kind,
|
||||
default,
|
||||
annotation,
|
||||
expected_exception,
|
||||
expected_exception_msg
|
||||
):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
PythonFunctionArgument(name, kind, default, annotation)
|
||||
assert str(exc_info.value) == expected_exception_msg
|
||||
@@ -136,22 +454,150 @@ def test_PythonFunctionArgument_asterik(name, kind, annotation):
|
||||
PythonFunctionArgument(name, kind, None, annotation)
|
||||
|
||||
@pytest.mark.parametrize('argument,expected', [
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, None), 'a'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '1', None), 'a = 1'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, 'int'), 'a: int'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '1', 'int'), 'a: int = 1'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None), 'a'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, '1', None), 'a = 1'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, 'int'), 'a: int'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, '1', 'int'), 'a: int = 1'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.KEYWORD_ONLY, None, None), 'a'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.KEYWORD_ONLY, '1', None), 'a = 1'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.KEYWORD_ONLY, None, 'int'), 'a: int'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.KEYWORD_ONLY, '1', 'int'), 'a: int = 1'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.VARARG, None, None), '*a'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.VARARG, None, 'int'), '*a: int'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.KWARGS, None, None), '**a'),
|
||||
(PythonFunctionArgument('a', PythonArgumentKind.KWARGS, None, 'int'), '**a: int'),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
'a'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
'1',
|
||||
None
|
||||
),
|
||||
'a = 1'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
'a: int'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
'1',
|
||||
'int'
|
||||
),
|
||||
'a: int = 1'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
'a'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
'1',
|
||||
None
|
||||
),
|
||||
'a = 1'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
'a: int'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
'1',
|
||||
'int'
|
||||
),
|
||||
'a: int = 1'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
None
|
||||
),
|
||||
'a'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
'1',
|
||||
None
|
||||
),
|
||||
'a = 1'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
'a: int'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
'1',
|
||||
'int'
|
||||
),
|
||||
'a: int = 1'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
),
|
||||
'*a'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
'*a: int'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
),
|
||||
'**a'
|
||||
),
|
||||
(
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
'**a: int'
|
||||
),
|
||||
])
|
||||
def test_PythonFunctionArgument_signature_repr(argument, expected):
|
||||
assert argument.signature_repr() == expected
|
||||
@@ -12,86 +12,482 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python.definitions import PythonFunctionDefinition, PythonClassDefinition
|
||||
from src.jcloud_docsgen.core.python.arguments import PythonFunctionArgument, PythonArgumentKind
|
||||
from src.jcloud_docsgen.core.python.definitions import (
|
||||
PythonFunctionDefinition,
|
||||
PythonClassDefinition
|
||||
)
|
||||
from src.jcloud_docsgen.core.python.arguments import (
|
||||
PythonFunctionArgument,
|
||||
PythonArgumentKind
|
||||
)
|
||||
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
|
||||
from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError
|
||||
from tests.utils.ast_node import ast_node
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('definition,expected', [
|
||||
(PythonClassDefinition('A', [], None, None, []), 'PythonClassDefinition(\'A\', [], None, None, [])'),
|
||||
(PythonClassDefinition('A', [], None, PythonDocstring('Hello, World!'), []), 'PythonClassDefinition(\'A\', [], None, PythonDocstring(\'Hello, World!\'), [])'),
|
||||
(
|
||||
PythonClassDefinition(
|
||||
'A',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[]
|
||||
),
|
||||
'PythonClassDefinition(\'A\', [], None, None, [])'
|
||||
),
|
||||
(
|
||||
PythonClassDefinition(
|
||||
'A',
|
||||
[],
|
||||
None,
|
||||
PythonDocstring('Hello, World!'),
|
||||
[]
|
||||
),
|
||||
(
|
||||
'PythonClassDefinition(\'A\', [], None, '
|
||||
'PythonDocstring(\'Hello, World!\'), [])'
|
||||
)
|
||||
),
|
||||
])
|
||||
def test_PythonDefinition_string_representation(definition, expected):
|
||||
assert repr(definition) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name,args,returns,doc,decorators,expected_exception,expected_exception_message', [
|
||||
('', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier'),
|
||||
('.', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: .'),
|
||||
('#', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: #'),
|
||||
('1a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: 1a'),
|
||||
('1_', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: 1_'),
|
||||
('1,', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: 1,'),
|
||||
(',', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ,'),
|
||||
(':', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: :'),
|
||||
('-', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: -'),
|
||||
(';', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ;'),
|
||||
('|', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: |'),
|
||||
('<', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: <'),
|
||||
('>', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: >'),
|
||||
('a,', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a,'),
|
||||
(',a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ,a'),
|
||||
('a:', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a:'),
|
||||
(':a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: :a'),
|
||||
('a-', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a-'),
|
||||
('-a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: -a'),
|
||||
('a;', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a;'),
|
||||
(';a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ;a'),
|
||||
('a|', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a|'),
|
||||
('|a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: |a'),
|
||||
('a<', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a<'),
|
||||
('<a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: <a'),
|
||||
('a>', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a>'),
|
||||
('>a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: >a'),
|
||||
('class', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: class'),
|
||||
('def', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: def'),
|
||||
('lambda', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: lambda'),
|
||||
('None', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: None'),
|
||||
('True', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: True'),
|
||||
])
|
||||
def test_PythonFunctionDefinition_exceptions(name, args, returns, doc, decorators, expected_exception, expected_exception_message):
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
'name,args,returns,doc,decorators,expect'
|
||||
'ed_exception,expected_exception_message'
|
||||
),
|
||||
[
|
||||
(
|
||||
'',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier'
|
||||
),
|
||||
(
|
||||
'.',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: .'
|
||||
),
|
||||
(
|
||||
'#',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: #'
|
||||
),
|
||||
(
|
||||
'1a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1a'
|
||||
),
|
||||
(
|
||||
'1_',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1_'
|
||||
),
|
||||
(
|
||||
'1,',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1,'
|
||||
),
|
||||
(
|
||||
',',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,'
|
||||
),
|
||||
(
|
||||
':',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :'
|
||||
),
|
||||
(
|
||||
'-',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -'
|
||||
),
|
||||
(
|
||||
';',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;'
|
||||
),
|
||||
(
|
||||
'|',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |'
|
||||
),
|
||||
(
|
||||
'<',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <'
|
||||
),
|
||||
(
|
||||
'>',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >'
|
||||
),
|
||||
(
|
||||
'a,',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a,'
|
||||
),
|
||||
(
|
||||
',a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,a'
|
||||
),
|
||||
(
|
||||
'a:',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a:'
|
||||
),
|
||||
(
|
||||
':a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :a'
|
||||
),
|
||||
(
|
||||
'a-',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a-'
|
||||
),
|
||||
(
|
||||
'-a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -a'
|
||||
),
|
||||
(
|
||||
'a;',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a;'
|
||||
),
|
||||
(
|
||||
';a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;a'
|
||||
),
|
||||
(
|
||||
'a|',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a|'
|
||||
),
|
||||
(
|
||||
'|a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |a'
|
||||
),
|
||||
(
|
||||
'a<',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a<'
|
||||
),
|
||||
(
|
||||
'<a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <a'
|
||||
),
|
||||
(
|
||||
'a>',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a>'
|
||||
),
|
||||
(
|
||||
'>a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >a'
|
||||
),
|
||||
(
|
||||
'class',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: class'
|
||||
),
|
||||
(
|
||||
'def',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: def'
|
||||
),
|
||||
(
|
||||
'lambda',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: lambda'
|
||||
),
|
||||
(
|
||||
'None',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: None'
|
||||
),
|
||||
(
|
||||
'True',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: True'
|
||||
),
|
||||
]
|
||||
)
|
||||
def test_PythonFunctionDefinition_exceptions(
|
||||
name,
|
||||
args,
|
||||
returns,
|
||||
doc,
|
||||
decorators,
|
||||
expected_exception,
|
||||
expected_exception_message
|
||||
):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
PythonClassDefinition(name, args, returns, doc, decorators)
|
||||
assert str(exc_info.value) == expected_exception_message
|
||||
|
||||
|
||||
@pytest.mark.parametrize('node,expected', [
|
||||
(ast_node('@decorator\nclass Class(BaseClass1, BaseClass2):\n\tdef __init__(self, value: object) -> None:\n\t\tself._value = value\n\n\t@property\n\tdef value(self) -> object:\n\t\treturn self._value'), PythonClassDefinition('Class', [
|
||||
'BaseClass1',
|
||||
'BaseClass2'
|
||||
], None, ['decorator'], [
|
||||
PythonFunctionDefinition('__init__', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.NORMAL, None, None),
|
||||
PythonFunctionArgument('value', PythonArgumentKind.NORMAL, None, 'object')
|
||||
], 'None', None, []),
|
||||
PythonFunctionDefinition('value', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.NORMAL, None, None)
|
||||
], 'object', None, ['property'])
|
||||
])),
|
||||
(ast_node('class Class: ...'), PythonClassDefinition('Class', [], None, [], [])),
|
||||
(ast_node('class Class: \'\'\'docstring\'\'\''), PythonClassDefinition('Class', [], PythonDocstring('docstring'), [], [])),
|
||||
(ast_node('class Class:\n\tdef __init__(self): ...'), PythonClassDefinition('Class', [], None, [], [
|
||||
PythonFunctionDefinition('__init__', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.NORMAL, None, None)
|
||||
], None, None, [])
|
||||
])),
|
||||
(ast_node('class Class:\n\t\'\'\'doc\'\'\'\n\tdef __init__(self): ...'), PythonClassDefinition('Class', [], PythonDocstring('doc'), [], [
|
||||
PythonFunctionDefinition('__init__', [
|
||||
PythonFunctionArgument('self', PythonArgumentKind.NORMAL, None, None)
|
||||
], None, None, [])
|
||||
])),
|
||||
(
|
||||
ast_node(
|
||||
(
|
||||
'@decorator\nclass Class(BaseClass1, BaseClass2)'
|
||||
':\n\tdef __init__(self, value: object) -> None:'
|
||||
'\n\t\tself._value = value\n\n\t@property\n\tdef'
|
||||
' value(self) -> object:\n\t\treturn self._value'
|
||||
)
|
||||
|
||||
),
|
||||
PythonClassDefinition(
|
||||
'Class',
|
||||
['BaseClass1', 'BaseClass2'],
|
||||
None,
|
||||
['decorator'],
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'__init__',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'value',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'object'
|
||||
)
|
||||
],
|
||||
'None',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
PythonFunctionDefinition(
|
||||
'value',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
],
|
||||
'object',
|
||||
None,
|
||||
['property']
|
||||
)
|
||||
]
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node('class Class: ...'),
|
||||
PythonClassDefinition(
|
||||
'Class',
|
||||
[],
|
||||
None,
|
||||
[],
|
||||
[]
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node('class Class: \'\'\'docstring\'\'\''),
|
||||
PythonClassDefinition(
|
||||
'Class',
|
||||
[],
|
||||
PythonDocstring('docstring'),
|
||||
[],
|
||||
[]
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node('class Class:\n\tdef __init__(self): ...'),
|
||||
PythonClassDefinition(
|
||||
'Class',
|
||||
[],
|
||||
None,
|
||||
[],
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'__init__',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
],
|
||||
None,
|
||||
None,
|
||||
[]
|
||||
)
|
||||
]
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node('class Class:\n\t\'\'\'doc\'\'\'\n\tdef __init__(self): ...'),
|
||||
PythonClassDefinition(
|
||||
'Class',
|
||||
[],
|
||||
PythonDocstring('doc'),
|
||||
[],
|
||||
[
|
||||
PythonFunctionDefinition(
|
||||
'__init__',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'self',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
None
|
||||
)
|
||||
],
|
||||
None,
|
||||
None,
|
||||
[]
|
||||
)
|
||||
]
|
||||
)
|
||||
),
|
||||
])
|
||||
def test_PythonDefinition_from_node(node, expected):
|
||||
assert PythonClassDefinition.from_node(node) == expected
|
||||
@@ -18,66 +18,425 @@ from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('definition,expected', [
|
||||
(PythonDefinition('a', None, []), 'PythonDefinition(\'a\', None, [])'),
|
||||
(PythonDefinition('a', PythonDocstring('Hello, World!'), []), 'PythonDefinition(\'a\', PythonDocstring(\'Hello, World!\'), [])'),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
'PythonDefinition(\'a\', None, [])'
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('Hello, World!'),
|
||||
[]
|
||||
),
|
||||
'PythonDefinition(\'a\', PythonDocstring(\'Hello, World!\'), [])'
|
||||
),
|
||||
])
|
||||
def test_PythonDefinition_string_representation(definition, expected):
|
||||
assert repr(definition) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name,doc,decorators,expected_exception,expected_exception_message', [
|
||||
('', None, [], InvalidPythonIdentifierError, 'invalid identifier'),
|
||||
('.', None, [], InvalidPythonIdentifierError, 'invalid identifier: .'),
|
||||
('#', None, [], InvalidPythonIdentifierError, 'invalid identifier: #'),
|
||||
('1a', None, [], InvalidPythonIdentifierError, 'invalid identifier: 1a'),
|
||||
('1_', None, [], InvalidPythonIdentifierError, 'invalid identifier: 1_'),
|
||||
('1,', None, [], InvalidPythonIdentifierError, 'invalid identifier: 1,'),
|
||||
(',', None, [], InvalidPythonIdentifierError, 'invalid identifier: ,'),
|
||||
(':', None, [], InvalidPythonIdentifierError, 'invalid identifier: :'),
|
||||
('-', None, [], InvalidPythonIdentifierError, 'invalid identifier: -'),
|
||||
(';', None, [], InvalidPythonIdentifierError, 'invalid identifier: ;'),
|
||||
('|', None, [], InvalidPythonIdentifierError, 'invalid identifier: |'),
|
||||
('<', None, [], InvalidPythonIdentifierError, 'invalid identifier: <'),
|
||||
('>', None, [], InvalidPythonIdentifierError, 'invalid identifier: >'),
|
||||
('a,', None, [], InvalidPythonIdentifierError, 'invalid identifier: a,'),
|
||||
(',a', None, [], InvalidPythonIdentifierError, 'invalid identifier: ,a'),
|
||||
('a:', None, [], InvalidPythonIdentifierError, 'invalid identifier: a:'),
|
||||
(':a', None, [], InvalidPythonIdentifierError, 'invalid identifier: :a'),
|
||||
('a-', None, [], InvalidPythonIdentifierError, 'invalid identifier: a-'),
|
||||
('-a', None, [], InvalidPythonIdentifierError, 'invalid identifier: -a'),
|
||||
('a;', None, [], InvalidPythonIdentifierError, 'invalid identifier: a;'),
|
||||
(';a', None, [], InvalidPythonIdentifierError, 'invalid identifier: ;a'),
|
||||
('a|', None, [], InvalidPythonIdentifierError, 'invalid identifier: a|'),
|
||||
('|a', None, [], InvalidPythonIdentifierError, 'invalid identifier: |a'),
|
||||
('a<', None, [], InvalidPythonIdentifierError, 'invalid identifier: a<'),
|
||||
('<a', None, [], InvalidPythonIdentifierError, 'invalid identifier: <a'),
|
||||
('a>', None, [], InvalidPythonIdentifierError, 'invalid identifier: a>'),
|
||||
('>a', None, [], InvalidPythonIdentifierError, 'invalid identifier: >a'),
|
||||
('class', None, [], InvalidPythonIdentifierError, 'invalid identifier: class'),
|
||||
('def', None, [], InvalidPythonIdentifierError, 'invalid identifier: def'),
|
||||
('lambda', None, [], InvalidPythonIdentifierError, 'invalid identifier: lambda'),
|
||||
('None', None, [], InvalidPythonIdentifierError, 'invalid identifier: None'),
|
||||
('True', None, [], InvalidPythonIdentifierError, 'invalid identifier: True'),
|
||||
])
|
||||
def test_PythonDefinition_exceptions(name, doc, decorators, expected_exception, expected_exception_message):
|
||||
@pytest.mark.parametrize(
|
||||
'name,doc,decorators,expected_exception,expected_exception_message',
|
||||
[
|
||||
(
|
||||
'',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier'
|
||||
),
|
||||
(
|
||||
'.',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: .'
|
||||
),
|
||||
(
|
||||
'#',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: #'
|
||||
),
|
||||
(
|
||||
'1a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1a'
|
||||
),
|
||||
(
|
||||
'1_',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1_'
|
||||
),
|
||||
(
|
||||
'1,',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1,'
|
||||
),
|
||||
(
|
||||
',',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,'
|
||||
),
|
||||
(
|
||||
':',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :'
|
||||
),
|
||||
(
|
||||
'-',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -'
|
||||
),
|
||||
(
|
||||
';',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;'
|
||||
),
|
||||
(
|
||||
'|',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |'
|
||||
),
|
||||
(
|
||||
'<',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <'
|
||||
),
|
||||
(
|
||||
'>',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >'
|
||||
),
|
||||
(
|
||||
'a,',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a,'
|
||||
),
|
||||
(
|
||||
',a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,a'
|
||||
),
|
||||
(
|
||||
'a:',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a:'
|
||||
),
|
||||
(
|
||||
':a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :a'
|
||||
),
|
||||
(
|
||||
'a-',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a-'
|
||||
),
|
||||
(
|
||||
'-a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -a'
|
||||
),
|
||||
(
|
||||
'a;',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a;'
|
||||
),
|
||||
(
|
||||
';a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;a'
|
||||
),
|
||||
(
|
||||
'a|',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a|'
|
||||
),
|
||||
(
|
||||
'|a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |a'
|
||||
),
|
||||
(
|
||||
'a<',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a<'
|
||||
),
|
||||
(
|
||||
'<a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <a'
|
||||
),
|
||||
(
|
||||
'a>',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a>'
|
||||
),
|
||||
(
|
||||
'>a',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >a'
|
||||
),
|
||||
(
|
||||
'class',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: class'
|
||||
),
|
||||
(
|
||||
'def',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: def'
|
||||
),
|
||||
(
|
||||
'lambda',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: lambda'
|
||||
),
|
||||
(
|
||||
'None',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: None'
|
||||
),
|
||||
(
|
||||
'True',
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: True'
|
||||
),
|
||||
]
|
||||
)
|
||||
def test_PythonDefinition_exceptions(
|
||||
name,
|
||||
doc,
|
||||
decorators,
|
||||
expected_exception,
|
||||
expected_exception_message
|
||||
):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
PythonDefinition(name, doc, decorators)
|
||||
assert str(exc_info.value) == expected_exception_message
|
||||
|
||||
|
||||
@pytest.mark.parametrize('definition1,definition2,expected', [
|
||||
(PythonDefinition('a', None, []), PythonDefinition('a', None, []), True),
|
||||
(PythonDefinition('a', PythonDocstring('doc'), []), PythonDefinition('a', PythonDocstring('doc'), []), True),
|
||||
(PythonDefinition('a', PythonDocstring('doc'), ['a']), PythonDefinition('a', PythonDocstring('doc'), ['a']), True),
|
||||
(PythonDefinition('a', PythonDocstring('doc'), ['a', 'b']), PythonDefinition('a', PythonDocstring('doc'), ['a', 'b']), True),
|
||||
(PythonDefinition('a', None, []), PythonDefinition('b', None, []), False),
|
||||
(PythonDefinition('a', PythonDocstring('doc'), []), PythonDefinition('a', None, []), False),
|
||||
(PythonDefinition('a', PythonDocstring('doc'), []), PythonDefinition('a', PythonDocstring('docs'), []), False),
|
||||
(PythonDefinition('a', None, ['a']), PythonDefinition('a', None, ['b']), False),
|
||||
(PythonDefinition('a', None, ['a']), PythonDefinition('a', None, []), False),
|
||||
(PythonDefinition('a', None, ['a']), PythonDefinition('a', None, ['a', 'b']), False),
|
||||
(PythonDefinition('a', None, ['b', 'a']), PythonDefinition('a', None, ['a', 'b']), False),
|
||||
(PythonDefinition('a', PythonDocstring('doc'), []), PythonDefinition('b', None, ['a', 'b']), False),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
True
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
[]
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
[]
|
||||
),
|
||||
True
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
['a']
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
['a']
|
||||
),
|
||||
True
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
['a', 'b']
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
['a', 'b']
|
||||
),
|
||||
True
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
PythonDefinition(
|
||||
'b',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
False
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
[]
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
False
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
[]
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('docs'),
|
||||
[]
|
||||
),
|
||||
False
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
['a']
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
['b']
|
||||
),
|
||||
False
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
['a']
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
[]
|
||||
),
|
||||
False
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
['a']
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
['a', 'b']
|
||||
),
|
||||
False
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
['b', 'a']
|
||||
),
|
||||
PythonDefinition(
|
||||
'a',
|
||||
None,
|
||||
['a', 'b']
|
||||
),
|
||||
False
|
||||
),
|
||||
(
|
||||
PythonDefinition(
|
||||
'a',
|
||||
PythonDocstring('doc'),
|
||||
[]
|
||||
),
|
||||
PythonDefinition(
|
||||
'b',
|
||||
None,
|
||||
['a', 'b']
|
||||
),
|
||||
False
|
||||
),
|
||||
])
|
||||
def test_PythonDefinition___eq__(definition1, definition2, expected):
|
||||
assert (definition1 == definition2) == expected
|
||||
|
||||
@@ -13,75 +13,447 @@
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python.definitions import PythonFunctionDefinition
|
||||
from src.jcloud_docsgen.core.python.arguments import PythonFunctionArgument, PythonArgumentKind
|
||||
from src.jcloud_docsgen.core.python.arguments import (
|
||||
PythonFunctionArgument,
|
||||
PythonArgumentKind
|
||||
)
|
||||
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
|
||||
from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError
|
||||
from tests.utils.ast_node import ast_node
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('definition,expected', [
|
||||
(PythonFunctionDefinition('a', [], None, None, []), 'PythonFunctionDefinition(\'a\', [], None, None, [])'),
|
||||
(PythonFunctionDefinition('a', [], None, PythonDocstring('Hello, World!'), []), 'PythonFunctionDefinition(\'a\', [], None, PythonDocstring(\'Hello, World!\'), [])'),
|
||||
(
|
||||
PythonFunctionDefinition(
|
||||
'a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[]
|
||||
),
|
||||
'PythonFunctionDefinition(\'a\', [], None, None, [])'
|
||||
),
|
||||
(
|
||||
PythonFunctionDefinition(
|
||||
'a',
|
||||
[],
|
||||
None,
|
||||
PythonDocstring('Hello, World!'),
|
||||
[]
|
||||
),
|
||||
(
|
||||
'PythonFunctionDefinition(\'a\', [], None,'
|
||||
' PythonDocstring(\'Hello, World!\'), [])'
|
||||
)
|
||||
),
|
||||
])
|
||||
def test_PythonDefinition_string_representation(definition, expected):
|
||||
assert repr(definition) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name,args,returns,doc,decorators,expected_exception,expected_exception_message', [
|
||||
('', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier'),
|
||||
('.', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: .'),
|
||||
('#', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: #'),
|
||||
('1a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: 1a'),
|
||||
('1_', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: 1_'),
|
||||
('1,', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: 1,'),
|
||||
(',', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ,'),
|
||||
(':', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: :'),
|
||||
('-', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: -'),
|
||||
(';', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ;'),
|
||||
('|', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: |'),
|
||||
('<', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: <'),
|
||||
('>', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: >'),
|
||||
('a,', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a,'),
|
||||
(',a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ,a'),
|
||||
('a:', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a:'),
|
||||
(':a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: :a'),
|
||||
('a-', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a-'),
|
||||
('-a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: -a'),
|
||||
('a;', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a;'),
|
||||
(';a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: ;a'),
|
||||
('a|', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a|'),
|
||||
('|a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: |a'),
|
||||
('a<', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a<'),
|
||||
('<a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: <a'),
|
||||
('a>', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: a>'),
|
||||
('>a', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: >a'),
|
||||
('class', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: class'),
|
||||
('def', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: def'),
|
||||
('lambda', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: lambda'),
|
||||
('None', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: None'),
|
||||
('True', [], None, None, [], InvalidPythonIdentifierError, 'invalid identifier: True'),
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
'name,args,returns,doc,decorators,expect'
|
||||
'ed_exception,expected_exception_message'
|
||||
),
|
||||
[
|
||||
(
|
||||
'',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier'
|
||||
),
|
||||
(
|
||||
'.',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: .'
|
||||
),
|
||||
(
|
||||
'#',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: #'
|
||||
),
|
||||
(
|
||||
'1a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1a'
|
||||
),
|
||||
(
|
||||
'1_',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1_'
|
||||
),
|
||||
(
|
||||
'1,',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: 1,'
|
||||
),
|
||||
(
|
||||
',',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,'
|
||||
),
|
||||
(
|
||||
':',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :'
|
||||
),
|
||||
(
|
||||
'-',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -'
|
||||
),
|
||||
(
|
||||
';',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;'
|
||||
),
|
||||
(
|
||||
'|',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |'
|
||||
),
|
||||
(
|
||||
'<',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <'
|
||||
),
|
||||
(
|
||||
'>',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >'
|
||||
),
|
||||
(
|
||||
'a,',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a,'
|
||||
),
|
||||
(
|
||||
',a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ,a'
|
||||
),
|
||||
(
|
||||
'a:',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a:'
|
||||
),
|
||||
(
|
||||
':a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: :a'
|
||||
),
|
||||
(
|
||||
'a-',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a-'
|
||||
),
|
||||
(
|
||||
'-a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: -a'
|
||||
),
|
||||
(
|
||||
'a;',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a;'
|
||||
),
|
||||
(
|
||||
';a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: ;a'
|
||||
),
|
||||
(
|
||||
'a|',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a|'
|
||||
),
|
||||
(
|
||||
'|a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: |a'
|
||||
),
|
||||
(
|
||||
'a<',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a<'
|
||||
),
|
||||
(
|
||||
'<a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: <a'
|
||||
),
|
||||
(
|
||||
'a>',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: a>'
|
||||
),
|
||||
(
|
||||
'>a',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: >a'
|
||||
),
|
||||
(
|
||||
'class',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: class'
|
||||
),
|
||||
(
|
||||
'def',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: def'
|
||||
),
|
||||
(
|
||||
'lambda',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: lambda'
|
||||
),
|
||||
(
|
||||
'None',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: None'
|
||||
),
|
||||
(
|
||||
'True',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[],
|
||||
InvalidPythonIdentifierError,
|
||||
'invalid identifier: True'
|
||||
),
|
||||
])
|
||||
def test_PythonFunctionDefinition_exceptions(name, args, returns, doc, decorators, expected_exception, expected_exception_message):
|
||||
def test_PythonFunctionDefinition_exceptions(
|
||||
name,
|
||||
args,
|
||||
returns,
|
||||
doc,
|
||||
decorators,
|
||||
expected_exception,
|
||||
expected_exception_message
|
||||
):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
PythonFunctionDefinition(name, args, returns, doc, decorators)
|
||||
assert str(exc_info.value) == expected_exception_message
|
||||
|
||||
|
||||
@pytest.mark.parametrize('node,expected', [
|
||||
(ast_node('def add(a: int, b: int) -> int: return a + b'), PythonFunctionDefinition('add', [
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, 'int'),
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, 'int'),
|
||||
], 'int', None, [])),
|
||||
(ast_node('@decorator\ndef add(a: int, b: int) -> int: return a + b'), PythonFunctionDefinition('add', [
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, 'int'),
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, 'int'),
|
||||
], 'int', None, ['decorator'])),
|
||||
(ast_node('@decorator1\n@decorator2\ndef add(a: int, b: int) -> int: return a + b'), PythonFunctionDefinition('add', [
|
||||
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, None, 'int'),
|
||||
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, None, 'int'),
|
||||
], 'int', None, ['decorator1', 'decorator2'])),
|
||||
(ast_node('def func(): ...'), PythonFunctionDefinition('func', [], None, None, [])),
|
||||
(ast_node('@decorator\ndef func(): ...'), PythonFunctionDefinition('func', [], None, None, ['decorator'])),
|
||||
(
|
||||
ast_node('def add(a: int, b: int) -> int: return a + b'),
|
||||
PythonFunctionDefinition(
|
||||
'add', [
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
],
|
||||
'int',
|
||||
None,
|
||||
[]
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node('@decorator\ndef add(a: int, b: int) -> int: return a + b'),
|
||||
PythonFunctionDefinition(
|
||||
'add',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
],
|
||||
'int',
|
||||
None,
|
||||
['decorator']
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node(
|
||||
(
|
||||
'@decorator1\n@decorator2\ndef add(a'
|
||||
': int, b: int) -> int: return a + b'
|
||||
)
|
||||
),
|
||||
PythonFunctionDefinition(
|
||||
'add',
|
||||
[
|
||||
PythonFunctionArgument(
|
||||
'a',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
PythonFunctionArgument(
|
||||
'b',
|
||||
PythonArgumentKind.NORMAL,
|
||||
None,
|
||||
'int'
|
||||
),
|
||||
],
|
||||
'int',
|
||||
None,
|
||||
['decorator1', 'decorator2']
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node('def func(): ...'),
|
||||
PythonFunctionDefinition(
|
||||
'func',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
[]
|
||||
)
|
||||
),
|
||||
(
|
||||
ast_node('@decorator\ndef func(): ...'),
|
||||
PythonFunctionDefinition(
|
||||
'func',
|
||||
[],
|
||||
None,
|
||||
None,
|
||||
['decorator']
|
||||
)
|
||||
),
|
||||
])
|
||||
def test_PythonDefinition_from_node(node, expected):
|
||||
assert PythonFunctionDefinition.from_node(node) == expected
|
||||
@@ -12,16 +12,40 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from src.jcloud_docsgen.core.python.namespaces import PythonPackageNamespace, PythonModuleNamespace
|
||||
from src.jcloud_docsgen.core.python.namespaces import (
|
||||
PythonPackageNamespace,
|
||||
PythonModuleNamespace
|
||||
)
|
||||
import pytest
|
||||
from src.jcloud_docsgen.exceptions import NamespaceNotFoundError, InvalidNamespaceError
|
||||
from src.jcloud_docsgen.exceptions import (
|
||||
NamespaceNotFoundError,
|
||||
InvalidNamespaceError
|
||||
)
|
||||
|
||||
class StrSubclass(str): ...
|
||||
|
||||
@pytest.mark.parametrize('namespace,expected_name', [
|
||||
(PythonPackageNamespace('a', []), 'a'),
|
||||
(PythonPackageNamespace('1', []), '1'),
|
||||
(PythonPackageNamespace(StrSubclass('a'), []), StrSubclass('a')),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[]
|
||||
),
|
||||
'a'
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'1',
|
||||
[]
|
||||
),
|
||||
'1'
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
StrSubclass('a'),
|
||||
[]
|
||||
),
|
||||
StrSubclass('a')
|
||||
),
|
||||
])
|
||||
def test_PythonPackageNamespace_name_attribute(namespace, expected_name):
|
||||
assert namespace.name == expected_name
|
||||
@@ -41,68 +65,225 @@ namespace_e = PythonPackageNamespace('e', [namespace_f])
|
||||
namespace_d = PythonPackageNamespace('d', [namespace_e])
|
||||
|
||||
@pytest.mark.parametrize('namespace,sub_namespaces,expected', [
|
||||
(PythonPackageNamespace('name', [
|
||||
namespace_a,
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'name',
|
||||
[namespace_a, namespace_c]
|
||||
),
|
||||
['c'],
|
||||
namespace_c
|
||||
]), ['c'], namespace_c),
|
||||
(PythonPackageNamespace('name', [
|
||||
namespace_a,
|
||||
namespace_c
|
||||
]), ['a'], namespace_a),
|
||||
(PythonPackageNamespace('name', [
|
||||
namespace_a,
|
||||
namespace_c
|
||||
]), ['a', 'b'], namespace_b),
|
||||
(namespace_d, ['e', 'f', 'g', 'h', 'i'], namespace_i),
|
||||
(namespace_d, ['e', 'f', 'g', 'h', 'i', 'j'], namespace_j),
|
||||
(namespace_d, ['e', 'f', 'g', 'h', 'i', 'j', 'k'], namespace_k),
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'name',
|
||||
[namespace_a, namespace_c]
|
||||
),
|
||||
['a'],
|
||||
namespace_a
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'name',
|
||||
[namespace_a, namespace_c]
|
||||
),
|
||||
['a', 'b'],
|
||||
namespace_b
|
||||
),
|
||||
(
|
||||
namespace_d,
|
||||
['e', 'f', 'g', 'h', 'i'],
|
||||
namespace_i
|
||||
),
|
||||
(
|
||||
namespace_d,
|
||||
['e', 'f', 'g', 'h', 'i', 'j'],
|
||||
namespace_j
|
||||
),
|
||||
(
|
||||
namespace_d,
|
||||
['e', 'f', 'g', 'h', 'i', 'j', 'k'],
|
||||
namespace_k
|
||||
),
|
||||
])
|
||||
def test_PythonPackageNamespace_namespace(namespace: PythonPackageNamespace, sub_namespaces, expected):
|
||||
def test_PythonPackageNamespace_namespace(
|
||||
namespace: PythonPackageNamespace,
|
||||
sub_namespaces,
|
||||
expected
|
||||
):
|
||||
assert namespace.namespace(sub_namespaces) == expected
|
||||
|
||||
@pytest.mark.parametrize('namespace,sub_namespaces,expected_exception,expected_exception_msg', [
|
||||
(namespace_a, ['c'], NamespaceNotFoundError, 'no such namespace: c'),
|
||||
(namespace_h, ['i', 'k'], NamespaceNotFoundError, 'no such namespace: k'),
|
||||
(namespace_a, ['b', ''], InvalidNamespaceError, 'invalid namespace'),
|
||||
(namespace_a, ['', 'b'], InvalidNamespaceError, 'invalid namespace'),
|
||||
(namespace_a, ['b', '', 'b'], InvalidNamespaceError, 'invalid namespace'),
|
||||
])
|
||||
def test_PythonPackageNamespace_namespace_exceptions(namespace, sub_namespaces, expected_exception, expected_exception_msg):
|
||||
@pytest.mark.parametrize(
|
||||
'namespace,sub_namespaces,expected_exception,expected_exception_msg',
|
||||
[
|
||||
(
|
||||
namespace_a,
|
||||
['c'],
|
||||
NamespaceNotFoundError,
|
||||
'no such namespace: c'
|
||||
),
|
||||
(
|
||||
namespace_h,
|
||||
['i', 'k'],
|
||||
NamespaceNotFoundError,
|
||||
'no such namespace: k'
|
||||
),
|
||||
(
|
||||
namespace_a,
|
||||
['b', ''],
|
||||
InvalidNamespaceError,
|
||||
'invalid namespace'
|
||||
),
|
||||
(
|
||||
namespace_a,
|
||||
['', 'b'],
|
||||
InvalidNamespaceError,
|
||||
'invalid namespace'
|
||||
),
|
||||
(
|
||||
namespace_a,
|
||||
['b', '', 'b'],
|
||||
InvalidNamespaceError,
|
||||
'invalid namespace'
|
||||
),
|
||||
]
|
||||
)
|
||||
def test_PythonPackageNamespace_namespace_exceptions(
|
||||
namespace,
|
||||
sub_namespaces,
|
||||
expected_exception,
|
||||
expected_exception_msg
|
||||
):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
namespace.namespace(sub_namespaces)
|
||||
assert str(exc_info.value) == expected_exception_msg
|
||||
|
||||
@pytest.mark.parametrize('namespace,expected', [
|
||||
(PythonPackageNamespace('a', [PythonModuleNamespace('b')]), {'b': 'b'}),
|
||||
(PythonPackageNamespace('a', [PythonModuleNamespace('b'), PythonPackageNamespace('c', [PythonModuleNamespace('d')])]), {'b': 'b', 'c': {'d': 'd'}}),
|
||||
(PythonPackageNamespace('a', []), {})
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[PythonModuleNamespace('b')]
|
||||
),
|
||||
{'b': 'b'}
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[
|
||||
PythonModuleNamespace('b'),
|
||||
PythonPackageNamespace(
|
||||
'c',
|
||||
[PythonModuleNamespace('d')]
|
||||
)
|
||||
]
|
||||
),
|
||||
{
|
||||
'b': 'b',
|
||||
'c': {'d': 'd'}
|
||||
}
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace('a', []),
|
||||
{}
|
||||
)
|
||||
])
|
||||
def test_PythonPackageNamespace_sub_namespace_names(namespace, expected):
|
||||
assert namespace.sub_namespace_names() == expected
|
||||
|
||||
@pytest.mark.parametrize('namespace1,namespace2,expected', [
|
||||
(PythonPackageNamespace('a', [PythonModuleNamespace('b')]), PythonPackageNamespace('a', [PythonModuleNamespace('b')]), True),
|
||||
(PythonPackageNamespace('a', [PythonModuleNamespace('b'), PythonPackageNamespace('c', [PythonModuleNamespace('d')])]), PythonPackageNamespace('a', [PythonModuleNamespace('b'), PythonPackageNamespace('c', [PythonModuleNamespace('d')])]), True),
|
||||
(PythonPackageNamespace('a', []), PythonPackageNamespace('a', []), True),
|
||||
(PythonPackageNamespace('a', []), PythonPackageNamespace('b', []), False),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[PythonModuleNamespace('b')]
|
||||
),
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[PythonModuleNamespace('b')]
|
||||
),
|
||||
True
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[
|
||||
PythonModuleNamespace('b'),
|
||||
PythonPackageNamespace(
|
||||
'c',
|
||||
[PythonModuleNamespace('d')]
|
||||
)
|
||||
]
|
||||
),
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[
|
||||
PythonModuleNamespace('b'),
|
||||
PythonPackageNamespace(
|
||||
'c',
|
||||
[PythonModuleNamespace('d')]
|
||||
)
|
||||
]
|
||||
),
|
||||
True
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace('a', []),
|
||||
PythonPackageNamespace('a', []),
|
||||
True
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace('a', []),
|
||||
PythonPackageNamespace('b', []),
|
||||
False
|
||||
),
|
||||
])
|
||||
def test_PythonPackageNamespace___eq__(namespace1, namespace2, expected):
|
||||
assert (namespace1 == namespace2) == expected
|
||||
assert (namespace2 == namespace1) == expected
|
||||
|
||||
@pytest.mark.parametrize('namespace,expected', [
|
||||
(PythonPackageNamespace('a', []), 'PythonPackageNamespace(\'a\', [])'),
|
||||
(PythonPackageNamespace('1', []), 'PythonPackageNamespace(\'1\', [])'),
|
||||
(PythonPackageNamespace(StrSubclass('a'), []), 'PythonPackageNamespace(\'a\', [])'),
|
||||
(namespace_a, 'PythonPackageNamespace(\'a\', [PythonModuleNamespace(\'b\')])'),
|
||||
(namespace_d, 'PythonPackageNamespace(\'d\', [PythonPackageNamespace(\'e\', [PythonPackageNamespace(\'f\', [PythonPackageNamespace(\'g\', [PythonPackageNamespace(\'h\', [PythonPackageNamespace(\'i\', [PythonPackageNamespace(\'j\', [PythonModuleNamespace(\'k\')])])])])])])])')
|
||||
(
|
||||
PythonPackageNamespace('a', []),
|
||||
'PythonPackageNamespace(\'a\', [])'
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace('1', []),
|
||||
'PythonPackageNamespace(\'1\', [])'
|
||||
),
|
||||
(
|
||||
PythonPackageNamespace(StrSubclass('a'), []),
|
||||
'PythonPackageNamespace(\'a\', [])'
|
||||
),
|
||||
(
|
||||
namespace_a,
|
||||
'PythonPackageNamespace(\'a\', [PythonModuleNamespace(\'b\')])'
|
||||
),
|
||||
(
|
||||
namespace_d,
|
||||
(
|
||||
'PythonPackageNamespace(\'d\', [PythonPackageNamespac'
|
||||
'e(\'e\', [PythonPackageNamespace(\'f\', [PythonPacka'
|
||||
'geNamespace(\'g\', [PythonPackageNamespace(\'h\', [P'
|
||||
'ythonPackageNamespace(\'i\', [PythonPackageNamespace'
|
||||
'(\'j\', [PythonModuleNamespace(\'k\')])])])])])])])'
|
||||
)
|
||||
)
|
||||
])
|
||||
def test_PythonPackageNamespace_string_representation(namespace, expected):
|
||||
assert repr(namespace) == expected
|
||||
|
||||
@pytest.mark.parametrize('namespace,new_namespace,expected', [
|
||||
(PythonPackageNamespace('a', []), PythonModuleNamespace('b'), PythonPackageNamespace('a', [PythonModuleNamespace('b')]))
|
||||
(
|
||||
PythonPackageNamespace('a', []),
|
||||
PythonModuleNamespace('b'),
|
||||
PythonPackageNamespace(
|
||||
'a',
|
||||
[PythonModuleNamespace('b')]
|
||||
)
|
||||
)
|
||||
])
|
||||
def test_PythonPackageNamespace___getitem__(namespace, new_namespace, expected):
|
||||
def test_PythonPackageNamespace___getitem__(
|
||||
namespace,
|
||||
new_namespace,
|
||||
expected
|
||||
):
|
||||
namespace.add(new_namespace)
|
||||
assert namespace == expected
|
||||
@@ -31,13 +31,37 @@ def test_assert_that_is_instance(obj, class_or_tuple):
|
||||
assert_that_is_instance(obj, class_or_tuple)
|
||||
|
||||
@pytest.mark.parametrize('obj,class_or_tuple,expected_exception_msg', [
|
||||
(1, str, 'expected \'str\', got \'int\''),
|
||||
(1, (str,), 'expected \'str\', got \'int\''),
|
||||
(1, str | float, 'expected either \'str\' or \'float\', got \'int\''),
|
||||
(1, str | float | list, 'expected either \'str\', \'float\' or \'list\', got \'int\''),
|
||||
(1, TestType, 'expected \'NOT TestType\', got \'int\''),
|
||||
(
|
||||
1,
|
||||
str,
|
||||
'expected \'str\', got \'int\''
|
||||
),
|
||||
(
|
||||
1,
|
||||
(str,),
|
||||
'expected \'str\', got \'int\''
|
||||
),
|
||||
(
|
||||
1,
|
||||
str | float,
|
||||
'expected either \'str\' or \'float\', got \'int\''
|
||||
),
|
||||
(
|
||||
1,
|
||||
str | float | list,
|
||||
'expected either \'str\', \'float\' or \'list\', got \'int\''
|
||||
),
|
||||
(
|
||||
1,
|
||||
TestType,
|
||||
'expected \'NOT TestType\', got \'int\''
|
||||
),
|
||||
])
|
||||
def test_assert_that_is_instance_exceptions(obj, class_or_tuple, expected_exception_msg):
|
||||
def test_assert_that_is_instance_exceptions(
|
||||
obj,
|
||||
class_or_tuple,
|
||||
expected_exception_msg
|
||||
):
|
||||
with pytest.raises(TypeError) as exc_info:
|
||||
assert_that_is_instance(obj, class_or_tuple)
|
||||
assert str(exc_info.value) == expected_exception_msg
|
||||
@@ -48,7 +48,12 @@ import pytest
|
||||
([1, 2, 3], 'and', '\'', '\'1\', \'2\' and \'3\''),
|
||||
([1, 2, 3], '', '\'', '\'1\', \'2\' \'3\''),
|
||||
([1, 2, 3, 42, 'a', 23], 'and', '', '1, 2, 3, 42, a and 23'),
|
||||
([1, 2, 3, 42, 'a', 23], 'and', '\'', '\'1\', \'2\', \'3\', \'42\', \'a\' and \'23\''),
|
||||
(
|
||||
[1, 2, 3, 42, 'a', 23],
|
||||
'and',
|
||||
'\'',
|
||||
'\'1\', \'2\', \'3\', \'42\', \'a\' and \'23\''
|
||||
),
|
||||
])
|
||||
def test_human_readable_list(ls, final_separator, quotation_mark, expected):
|
||||
assert human_readable_list(ls, final_separator, quotation_mark) == expected
|
||||
Reference in new issue
Block a user