diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b0da146..6981d01 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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 \ No newline at end of file +- Add feature to compare `core.python.namespaces.PythonPackageNamespace` instances or `core.python.namespaces.PythonModuleNamespace` instances +- Add string representation for python namespace objects \ No newline at end of file diff --git a/src/jcloud_docsgen/core/python/namespaces.py b/src/jcloud_docsgen/core/python/namespaces.py index a4896d8..998a3eb 100644 --- a/src/jcloud_docsgen/core/python/namespaces.py +++ b/src/jcloud_docsgen/core/python/namespaces.py @@ -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. diff --git a/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py b/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py index f22bdd9..ce42baa 100644 --- a/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py +++ b/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py @@ -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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/tests/unit/core/python/namespaces/test_PythonNamespace.py b/tests/unit/core/python/namespaces/test_PythonNamespace.py index e6ab861..6fc0373 100644 --- a/tests/unit/core/python/namespaces/test_PythonNamespace.py +++ b/tests/unit/core/python/namespaces/test_PythonNamespace.py @@ -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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py b/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py index d9917c9..6ac021f 100644 --- a/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py +++ b/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py @@ -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 \ No newline at end of file + 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 \ No newline at end of file