Skip to content

Commit

Permalink
Check for incorrect boundaries on init.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Nov 27, 2023
1 parent d04a416 commit 307d36d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/mind_the_gaps/gaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def __post_init__(self):
for i, endpoint in enumerate(self.endpoints):
if not isinstance(endpoint, Endpoint):
self.endpoints[i] = Endpoint(endpoint, "[" if i % 2 == 0 else "]")
elif i % 2 == 0 and endpoint.boundary in ")]":
raise ValueError(f"Expected left boundary, got {endpoint!r}.")
elif i % 2 == 1 and endpoint.boundary in "([":
raise ValueError(f"Expected right boundary, got {endpoint!r}.")

for i in range(len(self.endpoints) - 1):
a = self.endpoints[i]
Expand All @@ -186,7 +190,7 @@ def __post_init__(self):
raise ValueError("Intervals unsorted.")
if a.value == b.value and a.boundary + b.boundary in {"](", ")["}:
raise ValueError(
f"Intervals not minimally expressed. Endpoints {a} and {b} can be removed."
f"Intervals not minimally expressed. Endpoints {a!r} and {b!r} can be removed."
)

@classmethod
Expand Down
40 changes: 30 additions & 10 deletions tests/test_gap_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
from mind_the_gaps import Endpoint, Gaps


def test_singleton_ok():
Gaps([1, 1])


def test_missing_singleton_ok():
Gaps([0, Endpoint(1, ")"), Endpoint(1, "("), 2])


def test_unsorted_closed():
with pytest.raises(ValueError, match="unsorted"):
Gaps([1, 0])
Expand All @@ -12,19 +20,31 @@ def test_unsorted_open():
Gaps([Endpoint(1, "("), Endpoint(0, ")")])


def test_singleton_ok():
Gaps([1, 1])


def test_missing_singleton_ok():
Gaps([0, Endpoint(1, ")"), Endpoint(1, "("), 2])


def test_open_closed():
def test_not_minimal_open_closed():
with pytest.raises(ValueError, match="not minimally expressed"):
Gaps([0, Endpoint(1, ")"), 1, 2])


def test_closed_open():
def test_not_minimal_closed_open():
with pytest.raises(ValueError, match="not minimally expressed"):
Gaps([0, 1, Endpoint(1, "("), 2])


def test_wrong_boundary_left_closed():
with pytest.raises(ValueError, match="left"):
Gaps([Endpoint(0, "]"), 1])


def test_wrong_boundary_left_open():
with pytest.raises(ValueError, match="left"):
Gaps([Endpoint(0, ")"), 1])


def test_wrong_boundary_right_closed():
with pytest.raises(ValueError, match="right"):
Gaps([0, Endpoint(1, "[")])


def test_wrong_boundary_right_open():
with pytest.raises(ValueError, match="right"):
Gaps([0, Endpoint(1, "[")])

0 comments on commit 307d36d

Please sign in to comment.