Skip to content

Commit

Permalink
ENH: allow enforce single-line arrays (#373)
Browse files Browse the repository at this point in the history
* MAINT: simplify `to_toml_array()` implementation
  • Loading branch information
redeboer authored Aug 12, 2024
1 parent b0d1b22 commit ab251ea
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/compwa_policy/utilities/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from tomlkit.items import Array


def to_toml_array(items: Iterable[Any], multiline: bool = False) -> Array:
def to_toml_array(items: Iterable[Any], multiline: bool | None = None) -> Array:
array = tomlkit.array()
array.extend(items)
if multiline or len(array) > 1:
array.multiline(True)
if multiline is None:
array.multiline(len(array) > 1)
else:
array.multiline(False)
array.multiline(multiline)
return array
51 changes: 41 additions & 10 deletions tests/utilities/test_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,48 @@ def test_to_toml_array_single_item():
assert _dump(array) == expected.strip()


@pytest.mark.parametrize("multiline", [False, True])
def test_to_toml_array_multiple_items(multiline: bool):
lst = [1, 2, 3]
@pytest.mark.parametrize(
("lst", "multiline", "expected"),
[
([0], False, "a = [0]"),
(
[0],
True,
"""
a = [
0,
]
""",
),
([0], None, "a = [0]"),
([1, 2, 3], False, "a = [1, 2, 3]"),
(
[1, 2, 3],
True,
"""
a = [
1,
2,
3,
]
""",
),
(
[1, 2, 3],
None,
"""
a = [
1,
2,
3,
]
""",
),
],
)
def test_to_toml_array_multiple_items(lst: list[int], multiline: bool, expected: str):
array = to_toml_array(lst, multiline)
expected = dedent("""
a = [
1,
2,
3,
]
""")
expected = dedent(expected).strip()
assert _dump(array) == expected.strip()


Expand Down

0 comments on commit ab251ea

Please sign in to comment.