style: format source code using Ruff

This commit is contained in:
jakob.scheid committed 2026-09-21 20:10:04 +02:00
1 parent d9887f34c9
commit aa4a139878
41 files changed
+6061 -5670

No files matched your search

@@ -1,5 +1,5 @@
def f(a: int = 42) -> str:
'''
"""
Does something.
:param a: A parameter.
@@ -7,5 +7,5 @@ def f(a: int = 42) -> str:
:return: A string.
:rtype: str
'''
"""
...
+16 -8
View File
@@ -15,20 +15,28 @@
import pytest
from jcloud_docsgen.cli.cli import create_argument_parser
@pytest.mark.parametrize('lang', [
'javascript',
'Python',
'PYTHON',
])
@pytest.mark.parametrize(
'lang',
[
'javascript',
'Python',
'PYTHON',
],
)
def test_language_error(lang):
parser = create_argument_parser()
with pytest.raises(SystemExit):
parser.parse_args([lang])
@pytest.mark.parametrize('lang', [
'python',
])
@pytest.mark.parametrize(
'lang',
[
'python',
],
)
def test_language_success(lang):
parser = create_argument_parser()
parser.parse_args([lang])
+140 -123
View File
@@ -17,152 +17,169 @@ 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
PythonDefinitionDocumentationIncludeSections,
)
@pytest.mark.parametrize('include,exclude,expected', [
(
[],
[],
PythonDefinitionDocumentationIncludeSections(
signature = False,
returns = False,
params = False,
exceptions = False,
examples = False,
docstring = False,
bases = False,
decorators = False,
class_body = False,
@pytest.mark.parametrize(
'include,exclude,expected',
[
(
[],
[],
PythonDefinitionDocumentationIncludeSections(
signature=False,
returns=False,
params=False,
exceptions=False,
examples=False,
docstring=False,
bases=False,
decorators=False,
class_body=False,
),
),
),
(
[],
['signature'],
PythonDefinitionDocumentationIncludeSections(
signature = False,
returns = False,
params = False,
exceptions = False,
examples = False,
docstring = False,
bases = False,
decorators = False,
class_body = False,
(
[],
['signature'],
PythonDefinitionDocumentationIncludeSections(
signature=False,
returns=False,
params=False,
exceptions=False,
examples=False,
docstring=False,
bases=False,
decorators=False,
class_body=False,
),
),
),
(
['signature'],
['signature'],
PythonDefinitionDocumentationIncludeSections(
signature = False,
returns = False,
params = False,
exceptions = False,
examples = False,
docstring = False,
bases = False,
decorators = False,
class_body = False,
(
['signature'],
['signature'],
PythonDefinitionDocumentationIncludeSections(
signature=False,
returns=False,
params=False,
exceptions=False,
examples=False,
docstring=False,
bases=False,
decorators=False,
class_body=False,
),
),
),
(
['signature'],
[],
PythonDefinitionDocumentationIncludeSections(
signature = True,
returns = False,
params = False,
exceptions = False,
examples = False,
docstring = False,
bases = False,
decorators = False,
class_body = False,
(
['signature'],
[],
PythonDefinitionDocumentationIncludeSections(
signature=True,
returns=False,
params=False,
exceptions=False,
examples=False,
docstring=False,
bases=False,
decorators=False,
class_body=False,
),
),
),
(
None,
[],
PythonDefinitionDocumentationIncludeSections(),
),
(
['signature', 'exceptions', 'bases', 'class_body'],
['signature', 'bases'],
PythonDefinitionDocumentationIncludeSections(
signature = False,
returns = False,
params = False,
exceptions = True,
examples = False,
docstring = False,
bases = False,
decorators = False,
class_body = True,
(
None,
[],
PythonDefinitionDocumentationIncludeSections(),
),
),
(
None,
['params', 'docstring'],
PythonDefinitionDocumentationIncludeSections(
signature = True,
returns = True,
params = False,
exceptions = True,
examples = True,
docstring = False,
bases = True,
decorators = True,
class_body = True,
(
['signature', 'exceptions', 'bases', 'class_body'],
['signature', 'bases'],
PythonDefinitionDocumentationIncludeSections(
signature=False,
returns=False,
params=False,
exceptions=True,
examples=False,
docstring=False,
bases=False,
decorators=False,
class_body=True,
),
),
)
])
(
None,
['params', 'docstring'],
PythonDefinitionDocumentationIncludeSections(
signature=True,
returns=True,
params=False,
exceptions=True,
examples=True,
docstring=False,
bases=True,
decorators=True,
class_body=True,
),
),
],
)
def test_parse_sections_list(include, exclude, expected):
assert parse_sections_list(include, exclude) == expected
def test_sections_error():
parser = create_argument_parser()
with pytest.raises(SystemExit):
parser.parse_args([
'python',
'-s',
'param', # correct: `params`
])
parser.parse_args(
[
'python',
'-s',
'param', # correct: `params`
]
)
with pytest.raises(SystemExit):
parser.parse_args([
'python',
'-s',
'params',
'docstrng', # correct: `docstring`
])
parser.parse_args(
[
'python',
'-s',
'params',
'docstrng', # correct: `docstring`
]
)
def test_sections_success():
parser = create_argument_parser()
parser.parse_args([
'python',
'-s',
'params',
])
parser.parse_args([
'python',
'-s',
'params',
'docstring',
])
parser.parse_args(
[
'python',
'-s',
'params',
]
)
parser.parse_args(
[
'python',
'-s',
'params',
'docstring',
]
)
def test_docs_generation(expectdir):
with expectdir('python_project') as output_dir:
output_dir: pathlib.Path
input_dir = pathlib.Path(__file__).parent / 'python_project/project'
cli_main([
'python',
'-s',
'params',
'docstring',
'-HTo',
str(output_dir),
str(input_dir),
])
cli_main(
[
'python',
'-s',
'params',
'docstring',
'-HTo',
str(output_dir),
str(input_dir),
]
)
File diff suppressed because it is too large. Load diff
@@ -20,78 +20,82 @@ from src.jcloud_docsgen.core.python.namespaces import (
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'
@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'
(
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')],
),
],
),
[
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'
(
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'),
],
),
],
),
],
),
[
PythonPackageNamespace(
'pkg',
[
PythonModuleNamespace('module'),
PythonPackageNamespace(
'pkg',
[
PythonModuleNamespace(
'module2'
),
],
),
],
),
],
),
])
],
)
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_PythonDocume'
'ntationGenerator/test_generate_documentation/'
),
).iterdir()
])
@pytest.mark.parametrize(
'dir',
[
d.name
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
@@ -104,12 +108,12 @@ def test_generate_documentation(dir, expectdir):
'tests/unit/core/python/_core/test_PythonDocume'
'ntationGenerator/test_generate_documentation/'
),
) /
dir /
'project',
)
/ dir
/ 'project',
),
),
pathlib.Path(str(d)),
allow_tables = True,
allow_html = False,
).generate_documentation() # simplify the tests
allow_tables=True,
allow_html=False,
).generate_documentation() # simplify the tests
@@ -1,8 +1,10 @@
class UserNotFoundError(Exception):
"""User does not exist."""
pass
class InvalidEmailError(Exception):
"""Email address is invalid."""
pass
pass
@@ -7,7 +7,8 @@ class User:
"""
Represents a user.
"""
id: str
name: str
email: str
age: Optional[int] = None
age: Optional[int] = None
@@ -13,23 +13,16 @@ class UserService:
self.storage = storage
def create_user(
self,
user_id: str,
name: str,
email: str,
age: int | None = None
self, user_id: str, name: str, email: str, age: int | None = None
) -> User:
"""
Creates and stores a new user.
"""
if not is_valid_email(email):
raise InvalidEmailError(f"Invalid email: {email}")
raise InvalidEmailError(f'Invalid email: {email}')
user = User(
id=user_id,
name=normalize_name(name),
email=email,
age=age
id=user_id, name=normalize_name(name), email=email, age=age
)
self.storage.add(user)
@@ -51,4 +44,4 @@ class UserService:
try:
self.storage.delete(user_id)
except KeyError:
raise UserNotFoundError(user_id)
raise UserNotFoundError(user_id)
@@ -25,4 +25,4 @@ class InMemoryUserStorage:
def list_all(self) -> List[User]:
"""Returns all users."""
return list(self._users.values())
return list(self._users.values())
@@ -5,7 +5,7 @@ def is_valid_email(email: str) -> bool:
"""
Checks whether an email address is valid.
"""
pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
return re.match(pattern, email) is not None
@@ -13,4 +13,4 @@ def normalize_name(name: str) -> str:
"""
Removes extra spaces and formats the name.
"""
return " ".join(name.strip().split()).title()
return ' '.join(name.strip().split()).title()
@@ -17,6 +17,7 @@ from src.jcloud_docsgen.exceptions import InvalidNamespaceError
import pathlib
import pytest
@pytest.mark.parametrize(
'module_path,src_directory,expected_exception,expected_exception_msg',
[
@@ -216,22 +217,25 @@ def test_PythonModuleDocumentationGenerator_exceptions(
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_pr'
'oject_dirs/pdir_2/src/pkg/module.py'
@pytest.mark.parametrize(
'module_documentation_generator,expected',
[
(
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,
),
pathlib.Path(
'tests/unit/core/python/_core/test_project_dirs/pdir_2/src/'
),
allow_tables = True,
allow_html = False,
),
'''# Module `pkg.module`
"""# Module `pkg.module`
A test module.
@@ -398,205 +402,21 @@ Barks.
### Docstring
A dog.'''
),
(
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.
## Function `add`
Adds two numbers.
### Function signature
```python
add(a: int | float, b: int | float) -> int | float
```
### Returns
The sum.
Return type: `int | float`
### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `a` | `int | float` | normal | -- | The first number. |
| `b` | `int | float` | normal | -- | The second number. |
### Docstring
Adds two numbers.
:param a: The first number.
:type a: int | float
:param b: The second number.
:type b: int | float
:return: The sum.
:rtype: int | float
## Class `Animal`
A class representing an animal.
### Body
#### Constructor (method `__init__`)
##### Method signature
```python
__init__(self, name: str) -> None
```
##### Returns
Return type: `None`
##### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `self` | -- | normal | -- | -- |
| `name` | `str` | normal | -- | -- |
#### Method `greet`
Returns a greeting.
##### Method signature
```python
greet(self)
```
##### Returns
The greeting.
Return type: `str`
##### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `self` | -- | normal | -- | -- |
##### Docstring
Returns a greeting.
:return: The greeting.
:rtype: str
### Docstring
A class representing an animal.
:param name: Its name.
:type name: str
## Class `Dog`
A dog.
### Base classes
- `Animal`
### Body
#### Method `bark`
Barks.
##### Method signature
```python
bark(self) -> None
```
##### Returns
Return type: `None`
##### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `self` | -- | normal | -- | -- |
##### Docstring
Barks.
### Docstring
A dog.'''
),
(
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,
A dog.""",
),
(
'''# 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.
@@ -630,8 +450,190 @@ Return type: `int | float`
<details><summary><h3 style="display:inline">Docstring</h3></summary>Adds '''
'''two numbers.
### Docstring
Adds two numbers.
:param a: The first number.
:type a: int | float
:param b: The second number.
:type b: int | float
:return: The sum.
:rtype: int | float
## Class `Animal`
A class representing an animal.
### Body
#### Constructor (method `__init__`)
##### Method signature
```python
__init__(self, name: str) -> None
```
##### Returns
Return type: `None`
##### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `self` | -- | normal | -- | -- |
| `name` | `str` | normal | -- | -- |
#### Method `greet`
Returns a greeting.
##### Method signature
```python
greet(self)
```
##### Returns
The greeting.
Return type: `str`
##### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `self` | -- | normal | -- | -- |
##### Docstring
Returns a greeting.
:return: The greeting.
:rtype: str
### Docstring
A class representing an animal.
:param name: Its name.
:type name: str
## Class `Dog`
A dog.
### Base classes
- `Animal`
### Body
#### Method `bark`
Barks.
##### Method signature
```python
bark(self) -> None
```
##### Returns
Return type: `None`
##### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `self` | -- | normal | -- | -- |
##### Docstring
Barks.
### Docstring
A dog.""",
),
(
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.
## Function `add`
Adds two numbers.
### Function signature
```python
add(a: int | float, b: int | float) -> int | float
```
### Returns
The sum.
Return type: `int | float`
### Parameters
| Parameter | Type | Kind | Default value | Description |
| --- | --- | --- | --- | --- |
| `a` | `int | float` | normal | -- | The first number. |
| `b` | `int | float` | normal | -- | The second number. |
<details><summary><h3 style="display:inline">Docstring</h3></summary>Adds """
"""two numbers.
:param a: The first number.
:type a: int | float
@@ -698,8 +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>
@@ -708,8 +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>
@@ -750,32 +752,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>'
),
),
(
PythonModuleDocumentationGenerator(
pathlib.Path(
(
'tests/unit/core/python/_core/test_pr'
'oject_dirs/pdir_2/src/pkg/module.py'
),
<details><summary><h3 style="display:inline">Docstring</h3></summary>A dog."""
'</details>'
),
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.
@@ -824,8 +825,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
@@ -913,8 +914,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>
@@ -923,8 +924,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>
@@ -971,31 +972,30 @@ 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>'
),
),
(
PythonModuleDocumentationGenerator(
pathlib.Path(
(
'tests/unit/core/python/_core/test_pr'
'oject_dirs/pdir_2/src/pkg/module.py'
),
<details><summary><h3 style="display:inline">Docstring</h3></summary>A """
'dog.</details>'
),
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.
@@ -1204,9 +1204,10 @@ Barks.
### Docstring
A dog.'''
),
])
A dog.""",
),
],
)
def test_PythonModuleDocumentationGenerator_exceptions(
module_documentation_generator,
expected,
@@ -16,12 +16,16 @@ from src.jcloud_docsgen.core.python._core import _argument_kind_human_readable
from src.jcloud_docsgen.core.python.arguments import PythonArgumentKind
import pytest
@pytest.mark.parametrize('argument_kind,expected', [
(PythonArgumentKind.NORMAL, 'normal'),
(PythonArgumentKind.POSITIONAL_ONLY, 'positional-only'),
(PythonArgumentKind.KEYWORD_ONLY, 'keyword-only'),
(PythonArgumentKind.VARARG, 'vararg'),
(PythonArgumentKind.KWARGS, 'kwargs'),
])
@pytest.mark.parametrize(
'argument_kind,expected',
[
(PythonArgumentKind.NORMAL, 'normal'),
(PythonArgumentKind.POSITIONAL_ONLY, 'positional-only'),
(PythonArgumentKind.KEYWORD_ONLY, 'keyword-only'),
(PythonArgumentKind.VARARG, 'vararg'),
(PythonArgumentKind.KWARGS, 'kwargs'),
],
)
def test__argument_kind_human_readable(argument_kind, expected):
assert _argument_kind_human_readable(argument_kind) == expected
@@ -25,8 +25,9 @@ from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
import pytest
import ast
def module(source_code: str) -> ast.Module:
'''
"""
Returns an AST module of the source code.
:param source_code: The module source code.
@@ -34,38 +35,41 @@ def module(source_code: str) -> ast.Module:
:return: The AST module.
:rtype: 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:
@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:
"""Adds two numbers."""
return a + b
@@ -89,87 +93,87 @@ class Something:
"""
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 (
[
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 (
@@ -235,139 +239,142 @@ 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:
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
@@ -15,41 +15,50 @@
from src.jcloud_docsgen.core.python._core import _fit_row_length
import pytest
@pytest.mark.parametrize('expected_length,row,expected', [
(0, (), ()),
(1, (), (None,)),
(2, (), (None, None)),
(2, (1,), (1, None)),
(0, (1,), ()),
(1, (1, 2), (1,)),
(1, (1, 2), (1,)),
(0, (1, 2), ()),
(0, (1, 2, 3), ()),
(1, (1, 2, 3), (1,)),
(2, (1, 2, 3), (1, 2)),
(1, (1,), (1,)),
(2, (1, 1), (1, 1)),
(2, (1, 2), (1, 2)),
(3, (1, 2, 3), (1, 2, 3)),
])
@pytest.mark.parametrize(
'expected_length,row,expected',
[
(0, (), ()),
(1, (), (None,)),
(2, (), (None, None)),
(2, (1,), (1, None)),
(0, (1,), ()),
(1, (1, 2), (1,)),
(1, (1, 2), (1,)),
(0, (1, 2), ()),
(0, (1, 2, 3), ()),
(1, (1, 2, 3), (1,)),
(2, (1, 2, 3), (1, 2)),
(1, (1,), (1,)),
(2, (1, 1), (1, 1)),
(2, (1, 2), (1, 2)),
(3, (1, 2, 3), (1, 2, 3)),
],
)
def test__fit_row_length(expected_length, row, expected):
assert _fit_row_length(expected_length, row) == expected
@pytest.mark.parametrize('row', [
(),
(1,),
(1, 2),
(1, 2, 3),
(1, 2, 3, 456),
])
@pytest.mark.parametrize('expected_length,expected_exception', [
(-1, ValueError),
(-2, ValueError),
(-3, ValueError),
(-42, ValueError),
])
@pytest.mark.parametrize(
'row',
[
(),
(1,),
(1, 2),
(1, 2, 3),
(1, 2, 3, 456),
],
)
@pytest.mark.parametrize(
'expected_length,expected_exception',
[
(-1, ValueError),
(-2, ValueError),
(-3, ValueError),
(-42, ValueError),
],
)
def test__fit_row_length_exceptions(expected_length, row, expected_exception):
with pytest.raises(expected_exception) as exc_info:
_fit_row_length(expected_length, row)
@@ -23,10 +23,11 @@ from src.jcloud_docsgen.core.python.arguments import (
import pytest
import docstring_parser
def docstring_args_list(
docstring: str,
) -> list[docstring_parser.DocstringParam]:
'''
"""
Returns the parameter list of a docstring.
:param docstring: The docstring
@@ -34,82 +35,164 @@ def docstring_args_list(
:return: The parameter list.
:rtype: list[docstring_parser.DocstringParam]
'''
"""
return docstring_parser.parse(docstring).params
@pytest.mark.parametrize('arg_list,docstring,expected', [
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', 'int'),
], ':param a: a\n:type a: str', {
'a': _FunctionParamInfo('int', 'a'),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
], ':param a: a\n:type a: str', {
'a': _FunctionParamInfo('str', 'a'),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, '', None),
], ':param a: a\n:type a: str', {
'a': _FunctionParamInfo('str', 'a'),
'b': _FunctionParamInfo(None, None),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, '', 'int'),
], ':param a: a\n:type a: str', {
'a': _FunctionParamInfo('str', 'a'),
'b': _FunctionParamInfo('int', None),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
PythonFunctionArgument('b', PythonArgumentKind.NORMAL, '', 'int'),
], ':param a: a\n:type a: str\n:param b: b\n:type b: str', {
'a': _FunctionParamInfo('str', 'a'),
'b': _FunctionParamInfo('int', 'b'),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
], ':param a: a\n:type a: str\n:param b: b\n:type b: int', {
'a': _FunctionParamInfo('str', 'a'),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
], '', {
'a': _FunctionParamInfo(None, None),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', 'str'),
], '', {
'a': _FunctionParamInfo('str', None),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', 'str'),
], ':param a:\n:type a: int', {
'a': _FunctionParamInfo('str', ''),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
], ':param a:\n:type a: int', {
'a': _FunctionParamInfo('int', ''),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', None),
], ':param a: a', {
'a': _FunctionParamInfo(None, 'a'),
}),
([
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '', 'str'),
], ':param a: a', {
'a': _FunctionParamInfo('str', 'a'),
}),
([], ':param a: a\n:type a: str', {}),
([], '', {}),
])
@pytest.mark.parametrize(
'arg_list,docstring,expected',
[
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', 'int'
),
],
':param a: a\n:type a: str',
{
'a': _FunctionParamInfo('int', 'a'),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
],
':param a: a\n:type a: str',
{
'a': _FunctionParamInfo('str', 'a'),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
PythonFunctionArgument(
'b', PythonArgumentKind.NORMAL, '', None
),
],
':param a: a\n:type a: str',
{
'a': _FunctionParamInfo('str', 'a'),
'b': _FunctionParamInfo(None, None),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
PythonFunctionArgument(
'b', PythonArgumentKind.NORMAL, '', 'int'
),
],
':param a: a\n:type a: str',
{
'a': _FunctionParamInfo('str', 'a'),
'b': _FunctionParamInfo('int', None),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
PythonFunctionArgument(
'b', PythonArgumentKind.NORMAL, '', 'int'
),
],
':param a: a\n:type a: str\n:param b: b\n:type b: str',
{
'a': _FunctionParamInfo('str', 'a'),
'b': _FunctionParamInfo('int', 'b'),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
],
':param a: a\n:type a: str\n:param b: b\n:type b: int',
{
'a': _FunctionParamInfo('str', 'a'),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
],
'',
{
'a': _FunctionParamInfo(None, None),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', 'str'
),
],
'',
{
'a': _FunctionParamInfo('str', None),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', 'str'
),
],
':param a:\n:type a: int',
{
'a': _FunctionParamInfo('str', ''),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
],
':param a:\n:type a: int',
{
'a': _FunctionParamInfo('int', ''),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', None
),
],
':param a: a',
{
'a': _FunctionParamInfo(None, 'a'),
},
),
(
[
PythonFunctionArgument(
'a', PythonArgumentKind.NORMAL, '', 'str'
),
],
':param a: a',
{
'a': _FunctionParamInfo('str', 'a'),
},
),
([], ':param a: a\n:type a: str', {}),
([], '', {}),
],
)
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,6 +15,7 @@
from src.jcloud_docsgen.core.python._core import _markdown_table
import pytest
@pytest.mark.parametrize(
(
'header,body,allow_markdown_table,force_row_le'
@@ -55,9 +56,9 @@ import pytest
False,
True,
'--',
'''| a |
"""| a |
| --- |
| 1 |''',
| 1 |""",
),
(
['a'],
@@ -66,9 +67,9 @@ import pytest
False,
True,
'--',
'''| a |
"""| a |
| --- |
| 1 |''',
| 1 |""",
),
(
['a', 'b'],
@@ -77,9 +78,9 @@ import pytest
False,
True,
'--',
'''| a | b |
"""| a | b |
| --- | --- |
| 1 | 2 |''',
| 1 | 2 |""",
),
(
['a', 'b'],
@@ -88,9 +89,9 @@ import pytest
False,
True,
'--',
'''| a | b |
"""| a | b |
| --- | --- |
| 1 | -- |''',
| 1 | -- |""",
),
(
['a', 'b'],
@@ -99,9 +100,9 @@ import pytest
False,
True,
'--',
'''| a | b |
"""| a | b |
| --- | --- |
| -- | -- |''',
| -- | -- |""",
),
(
['a', 'b'],
@@ -110,10 +111,10 @@ import pytest
False,
True,
'--',
'''| a | b |
"""| a | b |
| --- | --- |
| 1 | 2 |
| 3 | 4 |''',
| 3 | 4 |""",
),
(
['a'],
@@ -122,9 +123,9 @@ import pytest
False,
True,
'--',
'''| a |
"""| a |
| --- |
| line1 line2 |''',
| line1 line2 |""",
),
(
['a'],
@@ -133,11 +134,10 @@ import pytest
False,
False,
'--',
'''| a |
"""| a |
| --- |
| line1\nline2 |''',
| line1\nline2 |""",
),
(
[],
[],
@@ -190,9 +190,9 @@ import pytest
False,
True,
'--',
'''- a: 1
"""- a: 1
b: 2''',
b: 2""",
),
(
['a', 'b'],
@@ -201,9 +201,9 @@ import pytest
False,
True,
'--',
'''- a: 1
"""- a: 1
b: --''',
b: --""",
),
(
['a', 'b'],
@@ -212,9 +212,9 @@ import pytest
False,
True,
'--',
'''- a: --
"""- a: --
b: --''',
b: --""",
),
(
['a', 'b'],
@@ -223,13 +223,13 @@ import pytest
False,
True,
'--',
'''- a: 1
"""- a: 1
b: 2
- a: 3
b: 4''',
b: 4""",
),
(
['a'],
@@ -260,14 +260,18 @@ def test__markdown_table(
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
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(
(
@@ -335,7 +339,6 @@ def test__markdown_table(
ValueError,
'expected 2 values, got 3',
),
(
['a'],
[(1, 2)],
@@ -412,9 +415,9 @@ def test__markdown_table_exceptions(
_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,
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
@@ -19,266 +19,328 @@ from src.jcloud_docsgen.core.python.arguments import (
)
import pytest
@pytest.mark.parametrize('argument_list,expected', [
([
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
None,
None,
@pytest.mark.parametrize(
'argument_list,expected',
[
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
None,
None,
),
],
['a'],
),
], ['a']),
([
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
),
],
['a', '/'],
),
], ['a', '/']),
([
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
),
PythonFunctionArgument(
'b',
PythonArgumentKind.NORMAL,
None,
None,
),
],
['a', '/', 'b'],
),
PythonFunctionArgument(
'b',
PythonArgumentKind.NORMAL,
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'],
),
], ['a', '/', 'b']),
([
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'b',
PythonArgumentKind.NORMAL,
None,
None,
),
PythonFunctionArgument(
'c',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
),
],
['b', '*', 'c'],
),
PythonFunctionArgument(
'b',
PythonArgumentKind.NORMAL,
None,
None,
(
[
PythonFunctionArgument(
'c',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
),
],
['*', 'c'],
),
PythonFunctionArgument(
'c',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'b',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
),
PythonFunctionArgument(
'c',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
),
],
['b', '/', '*', 'c'],
),
], ['a', '/', 'b', '*', 'c']),
([
PythonFunctionArgument(
'b',
PythonArgumentKind.NORMAL,
None,
None,
(
[
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
)
],
['*args'],
),
PythonFunctionArgument(
'c',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
None,
None,
),
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
),
],
['a', '*args'],
),
], ['b', '*', 'c']),
([
PythonFunctionArgument(
'c',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
),
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
),
],
['a', '/', '*args'],
),
], ['*', 'c']),
([
PythonFunctionArgument(
'b',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
),
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
),
],
['*args', 'a'],
),
PythonFunctionArgument(
'c',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
),
PythonFunctionArgument(
'kwargs',
PythonArgumentKind.KWARGS,
None,
None,
),
],
['*args', '**kwargs'],
),
], ['b', '/', '*', 'c']),
([
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
)
], ['*args']),
([
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
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(
'args',
PythonArgumentKind.VARARG,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
None,
None,
),
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
),
PythonFunctionArgument(
'kwargs',
PythonArgumentKind.KWARGS,
None,
None,
),
],
['a', '*args', '**kwargs'],
),
], ['a', '*args']),
([
PythonFunctionArgument(
'a', PythonArgumentKind.POSITIONAL_ONLY,
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(
'args',
PythonArgumentKind.VARARG,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
),
PythonFunctionArgument(
'kwargs',
PythonArgumentKind.KWARGS,
None,
None,
),
],
['*', 'a', '**kwargs'],
),
], ['a', '/', '*args']),
([
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
None,
None,
),
PythonFunctionArgument(
'kwargs',
PythonArgumentKind.KWARGS,
None,
None,
),
],
['a', '**kwargs'],
),
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
),
PythonFunctionArgument(
'kwargs',
PythonArgumentKind.KWARGS,
None,
None,
),
],
['a', '/', '**kwargs'],
),
], ['*args', 'a']),
([
PythonFunctionArgument(
'args',
PythonArgumentKind.VARARG,
None,
None,
(
[
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
'1',
'int',
),
],
['a: int = 1'],
),
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,
),
], ['a', '/', '*args', '**kwargs']),
([
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,
),
], ['*args', 'a', '**kwargs']),
([
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,
),
], ['a', '**kwargs']),
([
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
),
PythonFunctionArgument(
'kwargs',
PythonArgumentKind.KWARGS,
None,
None,
),
], ['a', '/', '**kwargs']),
([
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
'1',
'int',
),
], ['a: int = 1']),
])
],
)
def test__signature_argument_list(argument_list, expected):
assert _signature_argument_list(argument_list) == expected
@@ -1,9 +1,10 @@
'''
"""
A test module.
'''
"""
def add(a: int | float, b: int | float) -> int | float:
'''
"""
Adds two numbers.
:param a: The first number.
@@ -13,38 +14,41 @@ def add(a: int | float, b: int | float) -> int | float:
:return: The sum.
:rtype: int | float
'''
"""
return a + b
class Animal:
'''
"""
A class representing an animal.
:param name: Its name.
:type name: str
'''
"""
def __init__(self, name: str) -> None:
self.name = name
def greet(self):
'''
"""
Returns a greeting.
:return: The greeting.
:rtype: str
'''
"""
return f'Hi there! My name is {self.name}'
class Dog(Animal):
'''
"""
A dog.
'''
"""
def bark(self) -> None:
'''
"""
Barks.
'''
"""
print('Woof!')
File diff suppressed because it is too large. Load diff
@@ -23,6 +23,7 @@ from src.jcloud_docsgen.exceptions import (
)
import pytest
@pytest.mark.parametrize(
'name,kind,default,annotation,expected_exception,expected_exception_msg',
[
@@ -32,7 +33,7 @@ import pytest
None,
None,
TypeError,
'expected \'str\', got \'NoneType\'',
"expected 'str', got 'NoneType'",
),
(
1,
@@ -40,7 +41,7 @@ import pytest
None,
None,
TypeError,
'expected \'str\', got \'int\'',
"expected 'str', got 'int'",
),
(
42,
@@ -48,7 +49,7 @@ import pytest
None,
None,
TypeError,
'expected \'str\', got \'int\'',
"expected 'str', got 'int'",
),
(
4.2,
@@ -56,7 +57,7 @@ import pytest
None,
None,
TypeError,
'expected \'str\', got \'float\'',
"expected 'str', got 'float'",
),
(
4.2,
@@ -64,7 +65,7 @@ import pytest
None,
None,
TypeError,
'expected \'str\', got \'float\'',
"expected 'str', got 'float'",
),
(
'a',
@@ -72,7 +73,7 @@ import pytest
1,
None,
TypeError,
'expected either \'str\' or \'NoneType\', got \'int\'',
"expected either 'str' or 'NoneType', got 'int'",
),
(
'a',
@@ -80,7 +81,7 @@ import pytest
None,
1,
TypeError,
'expected either \'str\' or \'NoneType\', got \'int\'',
"expected either 'str' or 'NoneType', got 'int'",
),
(
'a',
@@ -88,7 +89,7 @@ import pytest
None,
None,
TypeError,
'expected \'PythonArgumentKind\', got \'int\'',
"expected 'PythonArgumentKind', got 'int'",
),
(
'a',
@@ -384,220 +385,244 @@ def test_PythonFunctionArgument_exceptions(
PythonFunctionArgument(name, kind, default, annotation)
assert str(exc_info.value) == expected_exception_msg
@pytest.mark.parametrize('name', [
'a',
'_a',
'a1',
'a0',
'a_',
'_',
'_abc',
'abc',
'abc_',
'match',
'case',
'type',
'ä',
'none',
'true',
'false',
])
@pytest.mark.parametrize('kind', [
PythonArgumentKind.NORMAL,
PythonArgumentKind.POSITIONAL_ONLY,
PythonArgumentKind.KEYWORD_ONLY,
])
@pytest.mark.parametrize('default', [
None,
'x',
'1',
None,
])
@pytest.mark.parametrize('annotation', [
None,
'a',
'list[int]',
'_',
])
@pytest.mark.parametrize(
'name',
[
'a',
'_a',
'a1',
'a0',
'a_',
'_',
'_abc',
'abc',
'abc_',
'match',
'case',
'type',
'ä',
'none',
'true',
'false',
],
)
@pytest.mark.parametrize(
'kind',
[
PythonArgumentKind.NORMAL,
PythonArgumentKind.POSITIONAL_ONLY,
PythonArgumentKind.KEYWORD_ONLY,
],
)
@pytest.mark.parametrize(
'default',
[
None,
'x',
'1',
None,
],
)
@pytest.mark.parametrize(
'annotation',
[
None,
'a',
'list[int]',
'_',
],
)
def test_PythonFunctionArgument(name, kind, default, annotation):
PythonFunctionArgument(name, kind, default, annotation)
@pytest.mark.parametrize('name', [
'a',
'_a',
'a1',
'a0',
'a_',
'_',
'_abc',
'abc',
'abc_',
'match',
'case',
'type',
'ä',
'none',
'true',
'false',
])
@pytest.mark.parametrize('kind', [
PythonArgumentKind.VARARG,
PythonArgumentKind.KWARGS,
])
@pytest.mark.parametrize('annotation', [
None,
'a',
'list[int]',
'_',
])
@pytest.mark.parametrize(
'name',
[
'a',
'_a',
'a1',
'a0',
'a_',
'_',
'_abc',
'abc',
'abc_',
'match',
'case',
'type',
'ä',
'none',
'true',
'false',
],
)
@pytest.mark.parametrize(
'kind',
[
PythonArgumentKind.VARARG,
PythonArgumentKind.KWARGS,
],
)
@pytest.mark.parametrize(
'annotation',
[
None,
'a',
'list[int]',
'_',
],
)
def test_PythonFunctionArgument_asterik(name, kind, annotation):
PythonFunctionArgument(name, kind, None, annotation)
@pytest.mark.parametrize('argument,expected', [
(
PythonFunctionArgument(
@pytest.mark.parametrize(
'argument,expected',
[
(
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
None,
None,
),
'a',
PythonArgumentKind.NORMAL,
None,
None,
),
'a',
),
(
PythonFunctionArgument(
(
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',
PythonArgumentKind.NORMAL,
'1',
None,
),
'a = 1',
),
(
PythonFunctionArgument(
(
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',
PythonArgumentKind.NORMAL,
None,
'int'
),
'a: int',
),
(
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
'1',
'int',
(
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
'1',
None,
),
'a = 1',
),
'a: int = 1',
),
(
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
None,
(
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
None,
'int',
),
'a: int',
),
'a',
),
(
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
'1',
None,
(
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
'1',
'int',
),
'a: int = 1',
),
'a = 1',
),
(
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
None,
'int',
(
PythonFunctionArgument(
'a',
PythonArgumentKind.VARARG,
None,
None,
),
'*a',
),
'a: int',
),
(
PythonFunctionArgument(
'a',
PythonArgumentKind.POSITIONAL_ONLY,
'1',
'int',
(
PythonFunctionArgument(
'a',
PythonArgumentKind.VARARG,
None,
'int',
),
'*a: int',
),
'a: int = 1',
),
(
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
None,
None,
(
PythonFunctionArgument(
'a',
PythonArgumentKind.KWARGS,
None,
None,
),
'**a',
),
'a',
),
(
PythonFunctionArgument(
'a',
PythonArgumentKind.KEYWORD_ONLY,
'1',
None,
(
PythonFunctionArgument(
'a',
PythonArgumentKind.KWARGS,
None,
'int',
),
'**a: int',
),
'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
@@ -25,31 +25,35 @@ 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!'),
[],
@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, "
"PythonDocstring('Hello, World!'), [])"
),
),
),
])
],
)
def test_PythonDefinition_string_representation(definition, expected):
assert repr(definition) == expected
@@ -364,129 +368,132 @@ def test_PythonFunctionDefinition_exceptions(
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'
@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'],
),
],
),
),
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: ...'),
PythonClassDefinition(
'Class',
[],
None,
[],
[],
(
ast_node("class Class: '''docstring'''"),
PythonClassDefinition(
'Class',
[],
PythonDocstring('docstring'),
[],
[],
),
),
),
(
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\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('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
@@ -17,24 +17,28 @@ from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError
import pytest
@pytest.mark.parametrize('definition,expected', [
(
PythonDefinition(
'a',
None,
[],
@pytest.mark.parametrize(
'definition,expected',
[
(
PythonDefinition(
'a',
None,
[],
),
"PythonDefinition('a', None, [])",
),
'PythonDefinition(\'a\', None, [])',
),
(
PythonDefinition(
'a',
PythonDocstring('Hello, World!'),
[],
(
PythonDefinition(
'a',
PythonDocstring('Hello, World!'),
[],
),
"PythonDefinition('a', PythonDocstring('Hello, World!'), [])",
),
'PythonDefinition(\'a\', PythonDocstring(\'Hello, World!\'), [])',
),
])
],
)
def test_PythonDefinition_string_representation(definition, expected):
assert repr(definition) == expected
@@ -280,164 +284,167 @@ def test_PythonDefinition_exceptions(
assert str(exc_info.value) == expected_exception_message
@pytest.mark.parametrize('definition1,definition2,expected', [
(
PythonDefinition(
'a',
None,
[],
@pytest.mark.parametrize(
'definition1,definition2,expected',
[
(
PythonDefinition(
'a',
None,
[],
),
PythonDefinition(
'a',
None,
[],
),
True,
),
PythonDefinition(
'a',
None,
[],
(
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
),
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
),
True,
),
True,
),
(
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
(
PythonDefinition(
'a',
PythonDocstring('doc'),
['a'],
),
PythonDefinition(
'a',
PythonDocstring('doc'),
['a'],
),
True,
),
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
(
PythonDefinition(
'a',
PythonDocstring('doc'),
['a', 'b'],
),
PythonDefinition(
'a',
PythonDocstring('doc'),
['a', 'b'],
),
True,
),
True,
),
(
PythonDefinition(
'a',
PythonDocstring('doc'),
['a'],
(
PythonDefinition(
'a',
None,
[],
),
PythonDefinition(
'b',
None,
[],
),
False,
),
PythonDefinition(
'a',
PythonDocstring('doc'),
['a'],
(
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
),
PythonDefinition(
'a',
None,
[],
),
False,
),
True,
),
(
PythonDefinition(
'a',
PythonDocstring('doc'),
['a', 'b'],
(
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
),
PythonDefinition(
'a',
PythonDocstring('docs'),
[],
),
False,
),
PythonDefinition(
'a',
PythonDocstring('doc'),
['a', 'b'],
(
PythonDefinition(
'a',
None,
['a'],
),
PythonDefinition(
'a',
None,
['b'],
),
False,
),
True,
),
(
PythonDefinition(
'a',
None,
[],
(
PythonDefinition(
'a',
None,
['a'],
),
PythonDefinition(
'a',
None,
[],
),
False,
),
PythonDefinition(
'b',
None,
[],
(
PythonDefinition(
'a',
None,
['a'],
),
PythonDefinition(
'a',
None,
['a', 'b'],
),
False,
),
False,
),
(
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
(
PythonDefinition(
'a',
None,
['b', 'a'],
),
PythonDefinition(
'a',
None,
['a', 'b'],
),
False,
),
PythonDefinition(
'a',
None,
[],
(
PythonDefinition(
'a',
PythonDocstring('doc'),
[],
),
PythonDefinition(
'b',
None,
['a', 'b'],
),
False,
),
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
assert (definition2 == definition1) == expected
@@ -22,31 +22,35 @@ 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!'),
[],
@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,"
" PythonDocstring('Hello, World!'), [])"
),
),
),
])
],
)
def test_PythonDefinition_string_representation(definition, expected):
assert repr(definition) == expected
@@ -57,295 +61,296 @@ def test_PythonDefinition_string_representation(definition, expected):
'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',
),
])
(
'',
[],
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,
@@ -360,100 +365,106 @@ def test_PythonFunctionDefinition_exceptions(
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'
@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,
[],
),
),
PythonFunctionDefinition(
'add',
[
PythonFunctionArgument(
'a',
PythonArgumentKind.NORMAL,
None,
'int',
(
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'
),
PythonFunctionArgument(
'b',
PythonArgumentKind.NORMAL,
None,
'int',
),
],
'int',
None,
['decorator1', 'decorator2'],
),
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('def func(): ...'),
PythonFunctionDefinition(
'func',
[],
None,
None,
[],
),
),
),
(
ast_node('@decorator\ndef func(): ...'),
PythonFunctionDefinition(
'func',
[],
None,
None,
['decorator'],
(
ast_node('@decorator\ndef func(): ...'),
PythonFunctionDefinition(
'func',
[],
None,
None,
['decorator'],
),
),
),
])
],
)
def test_PythonDefinition_from_node(node, expected):
assert PythonFunctionDefinition.from_node(node) == expected
@@ -15,33 +15,49 @@
from src.jcloud_docsgen.core.python.namespaces import PythonModuleNamespace
import pytest
class StrSubclass(str): ...
@pytest.mark.parametrize('namespace,expected_name', [
(PythonModuleNamespace('a'), 'a'),
(PythonModuleNamespace('1'), '1'),
(PythonModuleNamespace(StrSubclass('a')), StrSubclass('a')),
])
@pytest.mark.parametrize(
'namespace,expected_name',
[
(PythonModuleNamespace('a'), 'a'),
(PythonModuleNamespace('1'), '1'),
(PythonModuleNamespace(StrSubclass('a')), StrSubclass('a')),
],
)
def test_PythonModuleNamespace_name_attribute(namespace, expected_name):
assert namespace.name == expected_name
@pytest.mark.parametrize('namespace1,namespace2,expected', [
(PythonModuleNamespace('a'), PythonModuleNamespace('a'), True),
(PythonModuleNamespace('b'), PythonModuleNamespace('b'), True),
(PythonModuleNamespace('1'), PythonModuleNamespace('1'), True),
(PythonModuleNamespace('42'), PythonModuleNamespace('42'), True),
(PythonModuleNamespace('a'), PythonModuleNamespace('b'), False),
(PythonModuleNamespace('0'), PythonModuleNamespace('-0'), False),
(PythonModuleNamespace('1'), PythonModuleNamespace('a'), False),
])
@pytest.mark.parametrize(
'namespace1,namespace2,expected',
[
(PythonModuleNamespace('a'), PythonModuleNamespace('a'), True),
(PythonModuleNamespace('b'), PythonModuleNamespace('b'), True),
(PythonModuleNamespace('1'), PythonModuleNamespace('1'), True),
(PythonModuleNamespace('42'), PythonModuleNamespace('42'), True),
(PythonModuleNamespace('a'), PythonModuleNamespace('b'), False),
(PythonModuleNamespace('0'), PythonModuleNamespace('-0'), False),
(PythonModuleNamespace('1'), PythonModuleNamespace('a'), False),
],
)
def test_PythonModuleNamespace___eq__(namespace1, namespace2, expected):
assert (namespace1 == namespace2) == expected
assert (namespace2 == namespace1) == expected
@pytest.mark.parametrize('namespace,expected', [
(PythonModuleNamespace('a'), 'PythonModuleNamespace(\'a\')'),
(PythonModuleNamespace('1'), 'PythonModuleNamespace(\'1\')'),
(PythonModuleNamespace(StrSubclass('a')), 'PythonModuleNamespace(\'a\')'),
])
@pytest.mark.parametrize(
'namespace,expected',
[
(PythonModuleNamespace('a'), "PythonModuleNamespace('a')"),
(PythonModuleNamespace('1'), "PythonModuleNamespace('1')"),
(
PythonModuleNamespace(StrSubclass('a')),
"PythonModuleNamespace('a')",
),
],
)
def test_PythonModuleNamespace_string_representation(namespace, expected):
assert repr(namespace) == expected
@@ -15,20 +15,29 @@
from src.jcloud_docsgen.core.python.namespaces import PythonNamespace
import pytest
class StrSubclass(str): ...
@pytest.mark.parametrize('namespace,expected_name', [
(PythonNamespace('a'), 'a'),
(PythonNamespace('1'), '1'),
(PythonNamespace(StrSubclass('a')), StrSubclass('a')),
])
@pytest.mark.parametrize(
'namespace,expected_name',
[
(PythonNamespace('a'), 'a'),
(PythonNamespace('1'), '1'),
(PythonNamespace(StrSubclass('a')), StrSubclass('a')),
],
)
def test_PythonNamespace_name_attribute(namespace, expected_name):
assert namespace.name == expected_name
@pytest.mark.parametrize('namespace,expected', [
(PythonNamespace('a'), 'PythonNamespace(\'a\')'),
(PythonNamespace('1'), 'PythonNamespace(\'1\')'),
(PythonNamespace(StrSubclass('a')), 'PythonNamespace(\'a\')'),
])
@pytest.mark.parametrize(
'namespace,expected',
[
(PythonNamespace('a'), "PythonNamespace('a')"),
(PythonNamespace('1'), "PythonNamespace('1')"),
(PythonNamespace(StrSubclass('a')), "PythonNamespace('a')"),
],
)
def test_PythonNamespace_string_representation(namespace, expected):
assert repr(namespace) == expected
@@ -22,38 +22,47 @@ from src.jcloud_docsgen.exceptions import (
InvalidNamespaceError,
)
class StrSubclass(str): ...
@pytest.mark.parametrize('namespace,expected_name', [
(
PythonPackageNamespace(
@pytest.mark.parametrize(
'namespace,expected_name',
[
(
PythonPackageNamespace(
'a',
[],
),
'a',
[],
),
'a',
),
(
PythonPackageNamespace(
(
PythonPackageNamespace(
'1',
[],
),
'1',
[],
),
'1',
),
(
PythonPackageNamespace(
(
PythonPackageNamespace(
StrSubclass('a'),
[],
),
StrSubclass('a'),
[],
),
StrSubclass('a'),
),
])
],
)
def test_PythonPackageNamespace_name_attribute(namespace, expected_name):
assert namespace.name == expected_name
namespace_b = PythonModuleNamespace('b')
namespace_a = PythonPackageNamespace('a', [
namespace_b,
])
namespace_a = PythonPackageNamespace(
'a',
[
namespace_b,
],
)
namespace_c = PythonModuleNamespace('c')
namespace_k = PythonModuleNamespace('k')
namespace_j = PythonPackageNamespace('j', [namespace_k])
@@ -64,47 +73,51 @@ namespace_f = PythonPackageNamespace('f', [namespace_g])
namespace_e = PythonPackageNamespace('e', [namespace_f])
namespace_d = PythonPackageNamespace('d', [namespace_e])
@pytest.mark.parametrize('namespace,sub_namespaces,expected', [
(
PythonPackageNamespace(
'name',
[namespace_a, namespace_c],
@pytest.mark.parametrize(
'namespace,sub_namespaces,expected',
[
(
PythonPackageNamespace(
'name',
[namespace_a, namespace_c],
),
['c'],
namespace_c,
),
['c'],
namespace_c,
),
(
PythonPackageNamespace(
'name',
[namespace_a, namespace_c],
(
PythonPackageNamespace(
'name',
[namespace_a, namespace_c],
),
['a'],
namespace_a,
),
['a'],
namespace_a,
),
(
PythonPackageNamespace(
'name',
[namespace_a, namespace_c],
(
PythonPackageNamespace(
'name',
[namespace_a, namespace_c],
),
['a', 'b'],
namespace_b,
),
['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,
),
])
(
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,
@@ -112,6 +125,7 @@ def test_PythonPackageNamespace_namespace(
):
assert namespace.namespace(sub_namespaces) == expected
@pytest.mark.parametrize(
'namespace,sub_namespaces,expected_exception,expected_exception_msg',
[
@@ -157,129 +171,145 @@ def test_PythonPackageNamespace_namespace_exceptions(
namespace.namespace(sub_namespaces)
assert str(exc_info.value) == expected_exception_msg
@pytest.mark.parametrize('namespace,expected', [
(
PythonPackageNamespace(
'a',
[PythonModuleNamespace('b')],
@pytest.mark.parametrize(
'namespace,expected',
[
(
PythonPackageNamespace(
'a',
[PythonModuleNamespace('b')],
),
{'b': 'b'},
),
{'b': 'b'},
),
(
PythonPackageNamespace(
'a',
[
PythonModuleNamespace('b'),
PythonPackageNamespace(
'c',
[PythonModuleNamespace('d')],
),
],
(
PythonPackageNamespace(
'a',
[
PythonModuleNamespace('b'),
PythonPackageNamespace(
'c',
[PythonModuleNamespace('d')],
),
],
),
{
'b': 'b',
'c': {'d': 'd'},
},
),
{
'b': 'b',
'c': {'d': 'd'},
},
),
(
PythonPackageNamespace('a', []),
{},
),
])
(
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')],
@pytest.mark.parametrize(
'namespace1,namespace2,expected',
[
(
PythonPackageNamespace(
'a',
[PythonModuleNamespace('b')],
),
PythonPackageNamespace(
'a',
[PythonModuleNamespace('b')],
),
True,
),
PythonPackageNamespace(
'a',
[PythonModuleNamespace('b')],
(
PythonPackageNamespace(
'a',
[
PythonModuleNamespace('b'),
PythonPackageNamespace(
'c',
[PythonModuleNamespace('d')],
),
],
),
PythonPackageNamespace(
'a',
[
PythonModuleNamespace('b'),
PythonPackageNamespace(
'c',
[PythonModuleNamespace('d')],
),
],
),
True,
),
True,
),
(
PythonPackageNamespace(
'a',
[
PythonModuleNamespace('b'),
PythonPackageNamespace(
'c',
[PythonModuleNamespace('d')],
),
],
(
PythonPackageNamespace('a', []),
PythonPackageNamespace('a', []),
True,
),
PythonPackageNamespace(
'a',
[
PythonModuleNamespace('b'),
PythonPackageNamespace(
'c',
[PythonModuleNamespace('d')],
),
],
(
PythonPackageNamespace('a', []),
PythonPackageNamespace('b', []),
False,
),
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,
@pytest.mark.parametrize(
'namespace,expected',
[
(
'PythonPackageNamespace(\'d\', [PythonPackageNamespac'
'e(\'e\', [PythonPackageNamespace(\'f\', [PythonPacka'
'geNamespace(\'g\', [PythonPackageNamespace(\'h\', [P'
'ythonPackageNamespace(\'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')],
@pytest.mark.parametrize(
'namespace,new_namespace,expected',
[
(
PythonPackageNamespace('a', []),
PythonModuleNamespace('b'),
PythonPackageNamespace(
'a',
[PythonModuleNamespace('b')],
),
),
),
])
],
)
def test_PythonPackageNamespace___getitem__(
namespace,
new_namespace,
@@ -16,47 +16,58 @@ from src.jcloud_docsgen.utils import assert_that_is_instance
import pytest
import types
class TestType: ...
TestType.__name__ = 'NOT TestType'
@pytest.mark.parametrize('obj,class_or_tuple', [
(1, int),
(1, (int,)),
(1, str | int),
(1, int | str | list),
(TestType(), TestType),
(None, types.NoneType),
])
@pytest.mark.parametrize(
'obj,class_or_tuple',
[
(1, int),
(1, (int,)),
(1, str | int),
(1, int | str | list),
(TestType(), TestType),
(None, types.NoneType),
],
)
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\'',
),
])
@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'",
),
],
)
def test_assert_that_is_instance_exceptions(
obj,
class_or_tuple,
@@ -16,61 +16,62 @@ from src.jcloud_docsgen.utils import get_relative_path_segments
import pytest
import pathlib
@pytest.mark.parametrize('path,root,expected', [
(pathlib.Path('/'), pathlib.Path('/'), []),
(pathlib.Path('/a'), pathlib.Path('/'), ['a']),
(pathlib.Path('/a/'), pathlib.Path('/'), ['a']),
(pathlib.Path('/a'), pathlib.Path('/a'), []),
(pathlib.Path('/a/'), pathlib.Path('/a'), []),
(pathlib.Path('/a'), pathlib.Path('/a/'), []),
(pathlib.Path('/a/'), pathlib.Path('/a/'), []),
(pathlib.Path('/a/b'), pathlib.Path('/'), ['a', 'b']),
(pathlib.Path('/a/b/'), pathlib.Path('/'), ['a', 'b']),
(pathlib.Path('/a/b'), pathlib.Path('/a'), ['b']),
(pathlib.Path('/a/b/'), pathlib.Path('/a'), ['b']),
(pathlib.Path('/a/b'), pathlib.Path('/a/'), ['b']),
(pathlib.Path('/a/b/'), pathlib.Path('/a/'), ['b']),
(pathlib.Path('/a/b'), pathlib.Path('/a/b'), []),
(pathlib.Path('/a/b/'), pathlib.Path('/a/b'), []),
(pathlib.Path('/a/b'), pathlib.Path('/a/b/'), []),
(pathlib.Path('/a/b/'), pathlib.Path('/a/b/'), []),
(pathlib.Path('./'), pathlib.Path('./'), []),
(pathlib.Path('./a'), pathlib.Path('./'), ['a']),
(pathlib.Path('./a/'), pathlib.Path('./'), ['a']),
(pathlib.Path('./a'), pathlib.Path('./a'), []),
(pathlib.Path('./a/'), pathlib.Path('./a'), []),
(pathlib.Path('./a'), pathlib.Path('./a/'), []),
(pathlib.Path('./a/'), pathlib.Path('./a/'), []),
(pathlib.Path('./a/b'), pathlib.Path('./'), ['a', 'b']),
(pathlib.Path('./a/b/'), pathlib.Path('./'), ['a', 'b']),
(pathlib.Path('./a/b'), pathlib.Path('./a'), ['b']),
(pathlib.Path('./a/b/'), pathlib.Path('./a'), ['b']),
(pathlib.Path('./a/b'), pathlib.Path('./a/'), ['b']),
(pathlib.Path('./a/b/'), pathlib.Path('./a/'), ['b']),
(pathlib.Path('./a/b'), pathlib.Path('./a/b'), []),
(pathlib.Path('./a/b/'), pathlib.Path('./a/b'), []),
(pathlib.Path('./a/b'), pathlib.Path('./a/b/'), []),
(pathlib.Path('./a/b/'), pathlib.Path('./a/b/'), []),
(pathlib.Path(''), pathlib.Path(''), []),
(pathlib.Path('a'), pathlib.Path(''), ['a']),
(pathlib.Path('a/'), pathlib.Path(''), ['a']),
(pathlib.Path('a'), pathlib.Path('a'), []),
(pathlib.Path('a/'), pathlib.Path('a'), []),
(pathlib.Path('a'), pathlib.Path('a/'), []),
(pathlib.Path('a/'), pathlib.Path('a/'), []),
(pathlib.Path('a/b'), pathlib.Path(''), ['a', 'b']),
(pathlib.Path('a/b/'), pathlib.Path(''), ['a', 'b']),
(pathlib.Path('a/b'), pathlib.Path('a'), ['b']),
(pathlib.Path('a/b/'), pathlib.Path('a'), ['b']),
(pathlib.Path('a/b'), pathlib.Path('a/'), ['b']),
(pathlib.Path('a/b/'), pathlib.Path('a/'), ['b']),
(pathlib.Path('a/b'), pathlib.Path('a/b'), []),
(pathlib.Path('a/b/'), pathlib.Path('a/b'), []),
(pathlib.Path('a/b'), pathlib.Path('a/b/'), []),
(pathlib.Path('a/b/'), pathlib.Path('a/b/'), []),
])
@pytest.mark.parametrize(
'path,root,expected',
[
(pathlib.Path('/'), pathlib.Path('/'), []),
(pathlib.Path('/a'), pathlib.Path('/'), ['a']),
(pathlib.Path('/a/'), pathlib.Path('/'), ['a']),
(pathlib.Path('/a'), pathlib.Path('/a'), []),
(pathlib.Path('/a/'), pathlib.Path('/a'), []),
(pathlib.Path('/a'), pathlib.Path('/a/'), []),
(pathlib.Path('/a/'), pathlib.Path('/a/'), []),
(pathlib.Path('/a/b'), pathlib.Path('/'), ['a', 'b']),
(pathlib.Path('/a/b/'), pathlib.Path('/'), ['a', 'b']),
(pathlib.Path('/a/b'), pathlib.Path('/a'), ['b']),
(pathlib.Path('/a/b/'), pathlib.Path('/a'), ['b']),
(pathlib.Path('/a/b'), pathlib.Path('/a/'), ['b']),
(pathlib.Path('/a/b/'), pathlib.Path('/a/'), ['b']),
(pathlib.Path('/a/b'), pathlib.Path('/a/b'), []),
(pathlib.Path('/a/b/'), pathlib.Path('/a/b'), []),
(pathlib.Path('/a/b'), pathlib.Path('/a/b/'), []),
(pathlib.Path('/a/b/'), pathlib.Path('/a/b/'), []),
(pathlib.Path('./'), pathlib.Path('./'), []),
(pathlib.Path('./a'), pathlib.Path('./'), ['a']),
(pathlib.Path('./a/'), pathlib.Path('./'), ['a']),
(pathlib.Path('./a'), pathlib.Path('./a'), []),
(pathlib.Path('./a/'), pathlib.Path('./a'), []),
(pathlib.Path('./a'), pathlib.Path('./a/'), []),
(pathlib.Path('./a/'), pathlib.Path('./a/'), []),
(pathlib.Path('./a/b'), pathlib.Path('./'), ['a', 'b']),
(pathlib.Path('./a/b/'), pathlib.Path('./'), ['a', 'b']),
(pathlib.Path('./a/b'), pathlib.Path('./a'), ['b']),
(pathlib.Path('./a/b/'), pathlib.Path('./a'), ['b']),
(pathlib.Path('./a/b'), pathlib.Path('./a/'), ['b']),
(pathlib.Path('./a/b/'), pathlib.Path('./a/'), ['b']),
(pathlib.Path('./a/b'), pathlib.Path('./a/b'), []),
(pathlib.Path('./a/b/'), pathlib.Path('./a/b'), []),
(pathlib.Path('./a/b'), pathlib.Path('./a/b/'), []),
(pathlib.Path('./a/b/'), pathlib.Path('./a/b/'), []),
(pathlib.Path(''), pathlib.Path(''), []),
(pathlib.Path('a'), pathlib.Path(''), ['a']),
(pathlib.Path('a/'), pathlib.Path(''), ['a']),
(pathlib.Path('a'), pathlib.Path('a'), []),
(pathlib.Path('a/'), pathlib.Path('a'), []),
(pathlib.Path('a'), pathlib.Path('a/'), []),
(pathlib.Path('a/'), pathlib.Path('a/'), []),
(pathlib.Path('a/b'), pathlib.Path(''), ['a', 'b']),
(pathlib.Path('a/b/'), pathlib.Path(''), ['a', 'b']),
(pathlib.Path('a/b'), pathlib.Path('a'), ['b']),
(pathlib.Path('a/b/'), pathlib.Path('a'), ['b']),
(pathlib.Path('a/b'), pathlib.Path('a/'), ['b']),
(pathlib.Path('a/b/'), pathlib.Path('a/'), ['b']),
(pathlib.Path('a/b'), pathlib.Path('a/b'), []),
(pathlib.Path('a/b/'), pathlib.Path('a/b'), []),
(pathlib.Path('a/b'), pathlib.Path('a/b/'), []),
(pathlib.Path('a/b/'), pathlib.Path('a/b/'), []),
],
)
def test_get_relative_path_segments(path, root, expected):
assert get_relative_path_segments(path, root) == expected
+44 -40
View File
@@ -15,45 +15,49 @@
from src.jcloud_docsgen.utils import human_readable_list
import pytest
@pytest.mark.parametrize('ls,final_separator,quotation_mark,expected', [
([], '', '', ''),
([], 'and', '', ''),
([], 'and', '\'', ''),
([1], '', '', '1'),
([-1], '', '', '-1'),
([0], '', '', '0'),
([0.0], '', '', '0.0'),
([-0.0], '', '', '-0.0'),
([4.2], '', '', '4.2'),
([-4.2], '', '', '-4.2'),
(['a'], '', '', 'a'),
([1], 'and', '', '1'),
(['a'], 'and', '', 'a'),
([1], 'and', '\'', '\'1\''),
(['a'], 'and', '\'', '\'a\''),
([1, 2], 'and', '', '1 and 2'),
([1, 2], 'and', '\'', '\'1\' and \'2\''),
([1, 'a'], 'and', '', '1 and a'),
([1, 'a'], 'and', '\'', '\'1\' and \'a\''),
([1, 'a'], 'and', '', '1 and a'),
([1, 'a'], 'and', '\'', '\'1\' and \'a\''),
([1, 'a'], 'and', '\'', '\'1\' and \'a\''),
([1, 'a'], '', '', '1 a'),
([1, 'a'], '', '\'', '\'1\' \'a\''),
([1, 'a'], '', '', '1 a'),
([1, 'a'], '', '\'', '\'1\' \'a\''),
([1, 'a'], '', '\'', '\'1\' \'a\''),
([1, 2, 3], 'and', '', '1, 2 and 3'),
([1, 2, 3], '', '', '1, 2 3'),
([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\'',
),
])
@pytest.mark.parametrize(
'ls,final_separator,quotation_mark,expected',
[
([], '', '', ''),
([], 'and', '', ''),
([], 'and', "'", ''),
([1], '', '', '1'),
([-1], '', '', '-1'),
([0], '', '', '0'),
([0.0], '', '', '0.0'),
([-0.0], '', '', '-0.0'),
([4.2], '', '', '4.2'),
([-4.2], '', '', '-4.2'),
(['a'], '', '', 'a'),
([1], 'and', '', '1'),
(['a'], 'and', '', 'a'),
([1], 'and', "'", "'1'"),
(['a'], 'and', "'", "'a'"),
([1, 2], 'and', '', '1 and 2'),
([1, 2], 'and', "'", "'1' and '2'"),
([1, 'a'], 'and', '', '1 and a'),
([1, 'a'], 'and', "'", "'1' and 'a'"),
([1, 'a'], 'and', '', '1 and a'),
([1, 'a'], 'and', "'", "'1' and 'a'"),
([1, 'a'], 'and', "'", "'1' and 'a'"),
([1, 'a'], '', '', '1 a'),
([1, 'a'], '', "'", "'1' 'a'"),
([1, 'a'], '', '', '1 a'),
([1, 'a'], '', "'", "'1' 'a'"),
([1, 'a'], '', "'", "'1' 'a'"),
([1, 2, 3], 'and', '', '1, 2 and 3'),
([1, 2, 3], '', '', '1, 2 3'),
([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'",
),
],
)
def test_human_readable_list(ls, final_separator, quotation_mark, expected):
assert human_readable_list(ls, final_separator, quotation_mark) == expected
+15 -11
View File
@@ -15,16 +15,20 @@
from src.jcloud_docsgen.utils import non_empty_str
import pytest
@pytest.mark.parametrize('value,expected', [
(None, None),
('', None),
(' ', None),
(' ', None),
(' ', None),
(' a ', ' a '),
(' a', ' a'),
('a ', 'a '),
('a', 'a'),
])
@pytest.mark.parametrize(
'value,expected',
[
(None, None),
('', None),
(' ', None),
(' ', None),
(' ', None),
(' a ', ' a '),
(' a', ' a'),
('a ', 'a '),
('a', 'a'),
],
)
def test_non_empty_str(value, expected):
assert non_empty_str(value) == expected