diff --git a/pyproject.toml b/pyproject.toml index ea2ca5b5..a5dd50f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "coola" -version = "0.0.25a2" +version = "0.0.25a3" description = "A library to check if two complex/nested objects are equal or not" readme = "README.md" authors = ["Thibaut Durand "] diff --git a/src/coola/utils/format.py b/src/coola/utils/format.py index 3fa96c9a..c9307402 100644 --- a/src/coola/utils/format.py +++ b/src/coola/utils/format.py @@ -1,6 +1,6 @@ from __future__ import annotations -__all__ = ["repr_indent", "str_indent", "str_mapping", "str_sequence"] +__all__ = ["repr_indent", "repr_indent", "str_indent", "str_mapping", "str_sequence"] from collections.abc import Mapping, Sequence from typing import Any @@ -40,6 +40,35 @@ def repr_indent(original: Any, num_spaces: int = 2) -> str: return str_indent(original, num_spaces) +def repr_sequence(sequence: Sequence, num_spaces: int = 2) -> str: + r"""Computes a string representation of a sequence. + + Args: + ---- + sequence (``Sequence``): Specifies the sequence. + num_spaces (int, optional): Specifies the number of spaces + used for the indentation. Default: ``2``. + + Returns: + ------- + str: The string representation of the sequence. + + Example usage: + + .. code-block:: pycon + + >>> from coola.utils.format import repr_indent + >>> print(repr_sequence(["abc", "something\nelse"])) + (0): abc + (1): something + else + """ + lines = [] + for i, item in enumerate(sequence): + lines.append(f"({i}): {repr_indent(item, num_spaces=num_spaces)}") + return "\n".join(lines) + + def str_indent(original: Any, num_spaces: int = 2) -> str: r"""Add indentations if the original string is a multi-lines string. diff --git a/tests/unit/utils/test_format.py b/tests/unit/utils/test_format.py index 5b3e2b57..20d6a93b 100644 --- a/tests/unit/utils/test_format.py +++ b/tests/unit/utils/test_format.py @@ -2,7 +2,13 @@ from pytest import raises -from coola.utils.format import repr_indent, str_indent, str_mapping, str_sequence +from coola.utils.format import ( + repr_indent, + repr_sequence, + str_indent, + str_mapping, + str_sequence, +) ################################# # Tests for repr_indent # @@ -38,6 +44,27 @@ def test_repr_indent_not_a_repring() -> None: assert repr_indent(123) == "123" +################################### +# Tests for repr_sequence # +################################### + + +def test_repr_sequence_empty() -> None: + assert repr_sequence([]) == "" + + +def test_repr_sequence_1_item() -> None: + assert repr_sequence(["abc"]) == "(0): abc" + + +def test_repr_sequence_2_items() -> None: + assert repr_sequence(["abc", 123]) == "(0): abc\n(1): 123" + + +def test_repr_sequence_2_items_multiple_line() -> None: + assert repr_sequence(["abc", "something\nelse"]) == "(0): abc\n(1): something\n else" + + ################################ # Tests for str_indent # ################################