Skip to content

Commit

Permalink
Check for correct number of endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Nov 27, 2023
1 parent fcf4575 commit 0ddfa22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/mind_the_gaps/gaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class Gaps[T: SupportsLessThan]:
endpoints: list[T | Endpoint[T]] = field(default_factory=list)

def __post_init__(self):
if len(self.endpoints) % 2 == 1:
raise ValueError("Need an even number of endpoints.")

for i, endpoint in enumerate(self.endpoints):
if not isinstance(endpoint, Endpoint):
self.endpoints[i] = Endpoint(endpoint, "[" if i % 2 == 0 else "]")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_gap_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ def test_wrong_boundary_right_closed():
def test_wrong_boundary_right_open():
with pytest.raises(ValueError, match="right"):
Gaps([0, Endpoint(1, "[")])


def test_wrong_number_of_endpoints():
with pytest.raises(ValueError, match="endpoints"):
Gaps([0, 1, 2])

0 comments on commit 0ddfa22

Please sign in to comment.