Skip to content

Commit 4f83e58

Browse files
committed
catch ValueError in base tendency if duration is negative
1 parent 7f0b836 commit 4f83e58

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

tests/tendencies/test_base.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
(None, 20, None, 0, 20, 20, False),
1616
(None, None, 30, 0, 30, 30, False),
1717
(None, None, None, 0, 1, 1, False),
18-
(10, 20, 40, None, None, None, True),
19-
(10, None, 5, None, None, None, True),
20-
(10, -5, None, None, None, None, True),
21-
(None, 0, None, None, None, None, True),
18+
(10, 20, 40, 0, 1, 1, True),
19+
(10, None, 5, 10, 1, 11, True),
20+
(10, -5, None, 10, 1, 11, True),
21+
(None, 0, None, 0, 1, 1, True),
2222
],
2323
)
2424
def test_first_base_tendency(
@@ -34,12 +34,12 @@ def test_first_base_tendency(
3434
kwargs = dict(user_start=start, user_duration=duration, user_end=end)
3535
base_tendency = BaseTendency(**kwargs)
3636

37+
assert base_tendency.start == approx(expected_start)
38+
assert base_tendency.duration == approx(expected_duration)
39+
assert base_tendency.end == approx(expected_end)
3740
if has_error:
3841
assert base_tendency.annotations
3942
else:
40-
assert base_tendency.start == approx(expected_start)
41-
assert base_tendency.duration == approx(expected_duration)
42-
assert base_tendency.end == approx(expected_end)
4343
assert not base_tendency.annotations
4444

4545

@@ -54,10 +54,10 @@ def test_first_base_tendency(
5454
(None, 20, None, 10, 20, 30, False),
5555
(None, None, 30, 10, 20, 30, False),
5656
(None, None, None, 10, 1, 11, False),
57-
(10, 20, 40, None, None, None, True),
58-
(10, None, 5, None, None, None, True),
59-
(10, -5, None, None, None, None, True),
60-
(None, 0, None, None, None, None, True),
57+
(10, 20, 40, 10, 1, 11, True),
58+
(10, None, 5, 10, 1, 11, True),
59+
(10, -5, None, 10, 1, 11, True),
60+
(None, 0, None, 10, 1, 11, True),
6161
],
6262
)
6363
def test_second_base_tendency(
@@ -73,21 +73,25 @@ def test_second_base_tendency(
7373
prev_tendency = BaseTendency(user_start=0, user_end=10)
7474
kwargs = dict(user_start=start, user_duration=duration, user_end=end)
7575
base_tendency = BaseTendency(**kwargs)
76+
base_tendency.set_previous_tendency(prev_tendency)
77+
prev_tendency.set_next_tendency(base_tendency)
7678

79+
assert base_tendency.start == approx(expected_start)
80+
assert base_tendency.duration == approx(expected_duration)
81+
assert base_tendency.end == approx(expected_end)
7782
if has_error:
7883
assert base_tendency.annotations
7984
else:
80-
base_tendency.set_previous_tendency(prev_tendency)
81-
assert base_tendency.start == approx(expected_start)
82-
assert base_tendency.duration == approx(expected_duration)
83-
assert base_tendency.end == approx(expected_end)
8485
assert not base_tendency.annotations
8586

8687

8788
def test_suggestion():
8889
"""Test if suggestions are provided for miswritten keywords."""
8990
base_tendency = BaseTendency(user_starrt=0, user_duuration=5, user_ennd=10)
9091
assert base_tendency.annotations
92+
assert base_tendency.start == 0
93+
assert base_tendency.duration == 1
94+
assert base_tendency.end == 1
9195
assert any(
9296
"start" in annotation["text"] for annotation in base_tendency.annotations
9397
)
@@ -106,6 +110,14 @@ def test_gap():
106110
assert not t1.annotations
107111
assert t2.annotations
108112

113+
assert t1.start == 0
114+
assert t1.duration == 5
115+
assert t1.end == 5
116+
117+
assert t2.start == 15
118+
assert t2.duration == 5
119+
assert t2.end == 20
120+
109121

110122
def test_overlap():
111123
"""Test if an overlap between 2 tendencies is encountered."""
@@ -116,6 +128,14 @@ def test_overlap():
116128
assert not t1.annotations
117129
assert t2.annotations
118130

131+
assert t1.start == 0
132+
assert t1.duration == 5
133+
assert t1.end == 5
134+
135+
assert t2.start == 3
136+
assert t2.duration == 5
137+
assert t2.end == 8
138+
119139

120140
def test_declarative_assignments():
121141
t1 = BaseTendency(user_duration=10)

waveform_editor/tendencies/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,9 @@ def _calc_times(self):
255255

256256
# Check if any value has changed
257257
if (self.start, self.duration, self.end) != values:
258-
self.start, self.duration, self.end = values
258+
try:
259+
self.start, self.duration, self.end = values
260+
except Exception as error:
261+
self._handle_error(error)
259262
# Trigger timing event
260263
self.times_changed = True
261-
262-
if self.duration <= 0:
263-
error_msg = "Tendency end time must be greater than its start time."
264-
self.annotations.add(self.line_number, error_msg)

0 commit comments

Comments
 (0)