diff --git a/tests/test_timetable.py b/tests/test_timetable.py index 78ba6d8..74bb162 100644 --- a/tests/test_timetable.py +++ b/tests/test_timetable.py @@ -103,3 +103,21 @@ def test_cell_count_mismatch_is_a_parse_error(): ) with pytest.raises(ParseError, match="cells"): parse_page(html, "4", "X") + + +def test_invalid_hour_is_a_parse_error(): + html = HEADER.format( + ths=WORKDAY_TH, + rows='2510 ', + ) + with pytest.raises(ParseError, match="invalid time"): + parse_page(html, "4", "X") + + +def test_invalid_minute_is_a_parse_error(): + html = HEADER.format( + ths=WORKDAY_TH, + rows='0499 ', + ) + with pytest.raises(ParseError, match="invalid time"): + parse_page(html, "4", "X") diff --git a/timetable.py b/timetable.py index aff0470..d4907cd 100644 --- a/timetable.py +++ b/timetable.py @@ -93,7 +93,11 @@ def parse_page(html: str, line: str, stop: str) -> Timetable: raise ParseError(f"hour {hour}: {len(cells)} cells for {len(specs)} columns") for column, cell in zip(per_column, cells): for minute, mark in _MINUTE_RE.findall(cell): - column.append(Departure(line, stop, time(int(hour), int(minute)), mark or None)) + try: + departure_time = time(int(hour), int(minute)) + except ValueError: + raise ParseError(f"hour {hour}: invalid time {hour}:{minute}") + column.append(Departure(line, stop, departure_time, mark or None)) if not any(per_column): raise ParseError("timetable contains no departures")