Skip to content

Commit

Permalink
SimultaneousEvent.extend_until: Raise err if empty
Browse files Browse the repository at this point in the history
If 'extend_until' is called on an empty 'SimultaneousEvent' nothing will
happen. This in counter-intuitive and not obvious. It's therefore better
to raise an explicit error that the operation failed.
  • Loading branch information
levinericzimmermann committed Dec 4, 2022
1 parent f591d48 commit 40b89a0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
17 changes: 13 additions & 4 deletions mutwo/core_events/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,15 @@ def extend_until(
duration_to_white_space
or core_events.configurations.DEFAULT_DURATION_TO_WHITE_SPACE
)
# We only append simple events to sequential events, because there
# are many problems with the SimultaneousEvent[SimpleEvent] construct
# ('extend_until' and 'squash_in' will fail on such a container).
# Therefore calling 'extend_until' on an empty SimultaneousEvent is
# in fact ineffective: The user would get a SimultaneousEvent which
# still has duration = 0, which is absolutely unexpected. Therefore
# we raise an error, to avoid confusion by the user.
if not self:
raise core_utilities.IneffectiveExtendUntilError(self)
for event in self:
try:
event.extend_until(
Expand Down Expand Up @@ -881,8 +890,8 @@ def concatenate_by_index(self, other: SimultaneousEvent) -> SimultaneousEvent:
>>> s.concatenate_by_index(s)
SimultaneousEvent([SequentialEvent([SimpleEvent(duration = DirectDuration(duration = 1)), SimpleEvent(duration = DirectDuration(duration = 1))])])
"""
self_duration = self.duration
self.extend_until(self_duration)
if (self_duration := self.duration) > 0:
self.extend_until(self_duration)
for index, event in enumerate(other.copy()):
try:
ancestor = self[index]
Expand Down Expand Up @@ -919,8 +928,8 @@ def concatenate_by_tag(self, other: SimultaneousEvent) -> SimultaneousEvent:
>>> s.concatenate_by_tag(s)
SimultaneousEvent([TaggedSequentialEvent([SimpleEvent(duration = DirectDuration(duration = 1)), SimpleEvent(duration = DirectDuration(duration = 1))])])
"""
self_duration = self.duration
self.extend_until(self_duration)
if (self_duration := self.duration) > 0:
self.extend_until(self_duration)
for tagged_event in other.copy():
if not hasattr(tagged_event, "tag"):
raise core_utilities.NoTagError(tagged_event)
Expand Down
11 changes: 11 additions & 0 deletions mutwo/core_utilities/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"ImpossibleToSquashInError",
"ImpossibleToSlideInError",
"ImpossibleToExtendUntilError",
"IneffectiveExtendUntilError",
"InvalidStartAndEndValueError",
"InvalidCutOutStartAndEndValuesError",
"SplitUnavailableChildError",
Expand Down Expand Up @@ -95,6 +96,16 @@ def __init__(self, event_to_extend_until):
)


class IneffectiveExtendUntilError(ValueError):
def __init__(self, event_to_extend_until):
super().__init__(
f"Can't extend empty event '{event_to_extend_until}' of type"
f"'{type(event_to_extend_until)}'. If you want to extend "
"a SimultaneousEvent you should first append an empty "
"SequentialEvent to your SimultaneousEvent."
)


class InvalidStartAndEndValueError(Exception):
def __init__(self, start, end):
super().__init__(
Expand Down
6 changes: 6 additions & 0 deletions tests/events/basic_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,12 @@ def test_extend_until(self):
prolong_simple_event=False,
)

# We can't call 'extend_until' on an empty SimultaneousEvent: this would
# raise an error.
self.assertRaises(
core_utilities.IneffectiveExtendUntilError, si().extend_until, 10
)

# Extend sequential events inside simultaneous event..
ese = se([s(1), s(2), s(3), s(4)]) # extended sequential event
self.assertEqual(
Expand Down

0 comments on commit 40b89a0

Please sign in to comment.