-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bdfae8
commit 32638ad
Showing
6 changed files
with
173 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package gocron | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
ErrMultipleNotSpecified = errors.New("only one `?` is supported") | ||
) | ||
|
||
type TimeUnitError struct { | ||
inner error | ||
kind TimeUnitKind | ||
} | ||
|
||
func newTimeUnitErr(kind TimeUnitKind, inner error) TimeUnitError { | ||
return TimeUnitError{inner: inner, kind: kind} | ||
} | ||
|
||
func (e TimeUnitError) Error() string { | ||
return fmt.Sprintf("time unit `%s` malformed: %s", e.kind, e.inner) | ||
} | ||
|
||
func (e TimeUnitError) Is(err error) bool { | ||
tue, ok := err.(TimeUnitError) | ||
return ok && tue.kind == e.kind && errors.Is(tue.inner, e.inner) | ||
} | ||
|
||
type TimeUnitKind int | ||
|
||
const ( | ||
Seconds TimeUnitKind = iota | ||
Minutes | ||
Hours | ||
Days | ||
Months | ||
WeekDays | ||
) | ||
|
||
var kinds = []string{"seconds", "minutes", "hours", "days", "months", "week days"} | ||
|
||
func (k TimeUnitKind) String() string { | ||
return kinds[k] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package gocron | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestParser_Parse_refusesTwiceNotSpecified(t *testing.T) { | ||
_, err := Parser{}.Parse("* * * ? * ?") | ||
requireErrorIs(t, err, ErrMultipleNotSpecified) | ||
} | ||
|
||
func TestParser_Parse_abortsOnMalformedSeconds(t *testing.T) { | ||
_, err := Parser{}.Parse("a * * * * *") | ||
|
||
var e TimeUnitError | ||
requireErrorAs(t, err, &e) | ||
requireSameKind(t, e.kind, Seconds) | ||
} | ||
|
||
func TestParser_Parse_abortsOnMalformedMinutes(t *testing.T) { | ||
_, err := Parser{}.Parse("* a * * * *") | ||
|
||
var e TimeUnitError | ||
requireErrorAs(t, err, &e) | ||
requireSameKind(t, e.kind, Minutes) | ||
} | ||
|
||
func TestParser_Parse_abortsOnMalformedHours(t *testing.T) { | ||
_, err := Parser{}.Parse("* * a * * *") | ||
|
||
var e TimeUnitError | ||
requireErrorAs(t, err, &e) | ||
requireSameKind(t, e.kind, Hours) | ||
} | ||
|
||
func TestParser_Parse_abortsOnMalformedDays(t *testing.T) { | ||
_, err := Parser{}.Parse("* * * a * *") | ||
|
||
var e TimeUnitError | ||
requireErrorAs(t, err, &e) | ||
requireSameKind(t, e.kind, Days) | ||
} | ||
|
||
func TestParser_Parse_abortsOnMalformedMonths(t *testing.T) { | ||
_, err := Parser{}.Parse("* * * * a *") | ||
|
||
var e TimeUnitError | ||
requireErrorAs(t, err, &e) | ||
requireSameKind(t, e.kind, Months) | ||
} | ||
|
||
func TestParser_Parse_abortsOnMalformedWeekDays(t *testing.T) { | ||
_, err := Parser{}.Parse("* * * * * a") | ||
|
||
var e TimeUnitError | ||
requireErrorAs(t, err, &e) | ||
requireSameKind(t, e.kind, WeekDays) | ||
} | ||
|
||
// --- Utilities | ||
|
||
func requireErrorIs(t testing.TB, err, target error) { | ||
t.Helper() | ||
if !errors.Is(err, target) { | ||
t.Fatalf("expected error: %#v, found %#v", target, err) | ||
} | ||
} | ||
|
||
func requireErrorAs(t testing.TB, err error, target any) { | ||
t.Helper() | ||
if !errors.As(err, target) { | ||
t.Fatalf("expected error of kind: %T, found %#v", target, err) | ||
} | ||
} | ||
|
||
func requireSameKind(t testing.TB, a, b TimeUnitKind) { | ||
t.Helper() | ||
if a != b { | ||
t.Fatalf("%s != %s", a, b) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters