diff --git a/src/mind_the_gaps/gaps.py b/src/mind_the_gaps/gaps.py index 8e81c33..fc91b87 100644 --- a/src/mind_the_gaps/gaps.py +++ b/src/mind_the_gaps/gaps.py @@ -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] @@ -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 diff --git a/tests/test_gap_init.py b/tests/test_gap_init.py index d7998f9..a03bbf0 100644 --- a/tests/test_gap_init.py +++ b/tests/test_gap_init.py @@ -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]) @@ -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, "[")])