generated from jCloud/repository-template
Add string representation for python namespace objects
This commit is contained in:
+2
-1
@@ -12,4 +12,5 @@
|
||||
- Add type checking for the `name` attribute at the constructor of `core.python.namespaces.PythonNamespace`
|
||||
- Add check whether sub-namespaces are not empty in `core.python.namespaces.PythonPackageNamespace.namespace`
|
||||
- Add feature to get the names of all sub-namespaces of a `core.python.namespaces.PythonPackageNamespace`
|
||||
- Add feature to compare `core.python.namespaces.PythonPackageNamespace` instances or `core.python.namespaces.PythonModuleNamespace` instances
|
||||
- Add feature to compare `core.python.namespaces.PythonPackageNamespace` instances or `core.python.namespaces.PythonModuleNamespace` instances
|
||||
- Add string representation for python namespace objects
|
||||
@@ -37,6 +37,9 @@ class PythonNamespace:
|
||||
|
||||
self.name = name
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'{type(self).__name__}({self.name!r})'
|
||||
|
||||
class PythonPackageNamespace(PythonNamespace):
|
||||
'''
|
||||
A subclass of ``PythonNamespace`` for python package namespaces.
|
||||
@@ -117,6 +120,9 @@ class PythonPackageNamespace(PythonNamespace):
|
||||
|
||||
return value.sub_namespace_names() == self.sub_namespace_names()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return type(self).__name__ + repr((self.name, self.sub_namespaces))
|
||||
|
||||
class PythonModuleNamespace(PythonNamespace):
|
||||
'''
|
||||
A subclass of ``PythonNamespace`` for python module namespaces.
|
||||
|
||||
@@ -22,4 +22,12 @@ def test_PythonModuleNamespace_name_attribute(namespace, expected_name):
|
||||
])
|
||||
def test_PythonModuleNamespace___eq__(namespace1, namespace2, expected):
|
||||
assert (namespace1 == namespace2) == expected
|
||||
assert (namespace2 == namespace1) == expected
|
||||
assert (namespace2 == namespace1) == expected
|
||||
|
||||
@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
|
||||
@@ -9,4 +9,12 @@ class StrSubclass(str): ...
|
||||
(PythonNamespace(StrSubclass('a')), StrSubclass('a')),
|
||||
])
|
||||
def test_PythonNamespace_name_attribute(namespace, expected_name):
|
||||
assert namespace.name == 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\')'),
|
||||
])
|
||||
def test_PythonNamespace_string_representation(namespace, expected):
|
||||
assert repr(namespace) == expected
|
||||
@@ -75,4 +75,14 @@ def test_PythonPackageNamespace_sub_namespace_names(namespace, expected):
|
||||
])
|
||||
def test_PythonPackageNamespace___eq__(namespace1, namespace2, expected):
|
||||
assert (namespace1 == namespace2) == expected
|
||||
assert (namespace2 == namespace1) == 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\', {\'b\': PythonModuleNamespace(\'b\')})'),
|
||||
(namespace_d, 'PythonPackageNamespace(\'d\', {\'e\': PythonPackageNamespace(\'e\', {\'f\': PythonPackageNamespace(\'f\', {\'g\': PythonPackageNamespace(\'g\', {\'h\': PythonPackageNamespace(\'h\', {\'i\': PythonPackageNamespace(\'i\', {\'j\': PythonPackageNamespace(\'j\', {\'k\': PythonModuleNamespace(\'k\')})})})})})})})')
|
||||
])
|
||||
def test_PythonPackageNamespace_string_representation(namespace, expected):
|
||||
assert repr(namespace) == expected
|
||||
Reference in New Issue
Block a user