def test_parse_bad_expression(self): """ Should raise if the expression isn't a duration""" try: d = parse_duration("Not a number, Bro") assert False, "Should have raised" except ValueError as e: assert True
def test_parse_negative_value(self): """ Should raise if the expression is negative because a duration can't be negative""" try: d = parse_duration("-1") assert False, "Should have raised" except ValueError as e: assert True
def check_duration(value): if len(value) == 0: raise ArgumentTypeError("Duration can't be empty") try: sec = int(value) if sec < -1: raise ArgumentTypeError("A duration can't be negative (found {})".format(sec)) return sec except ValueError: pass try: return parse_duration(value) except ValueError as e: raise ArgumentTypeError(e.message)
def test_parse_minutes(self): """ A duration can have only minutes """ d = parse_duration("14min") assert d == 14*60, d
def test_parse_minutes_secs(self): """ A duration can have minutes and seconds """ d = parse_duration("14min 12s") assert d == 14*60 + 12, d
def test_parse_seconds(self): """ should interpret s as seconds """ d = parse_duration("12s") assert d == 12, d
def test_parse_days(self): """ A duration can have days """ d = parse_duration("4d 12s") assert d == 4*24*3600 + 12, d
def test_parse_days(self): """ A duration can have days """ d = parse_duration("4d 12s") assert d == 4 * 24 * 3600 + 12, d
def test_parse_several_spaces(self): """ Figures and units also parts of the duration can be separated by any number of spaces """ d = parse_duration("14 min 12 s") assert d == 14 * 60 + 12, d
def test_parse_minutes(self): """ A duration can have only minutes """ d = parse_duration("14min") assert d == 14 * 60, d
def test_parse_minutes_secs(self): """ A duration can have minutes and seconds """ d = parse_duration("14min 12s") assert d == 14 * 60 + 12, d
def test_parse_several_spaces(self): """ Figures and units also parts of the duration can be separated by any number of spaces """ d = parse_duration("14 min 12 s") assert d == 14*60 + 12, d
def test_parse_hours(self): """ A duration can have hours """ d = parse_duration("3 h 12s") assert d == 3*3600 + 12, d
def test_parse_hours(self): """ A duration can have hours """ d = parse_duration("3 h 12s") assert d == 3 * 3600 + 12, d