Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement String() for CronRange #4

Merged
merged 1 commit into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement String()
  • Loading branch information
an63 committed Nov 10, 2019
commit a9c188e35d63abb28f13abbd1709e28879f9716f
12 changes: 12 additions & 0 deletions cronrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,18 @@ type CronRange struct {
schedule cron.Schedule
}

func (cr *CronRange) String() string {
sb := strings.Builder{}
if cr.duration > 0 {
sb.WriteString(fmt.Sprintf("DR=%d; ", cr.duration/time.Minute))
}
if len(cr.timeZone) > 0 {
sb.WriteString(fmt.Sprintf("TZ=%s; ", cr.timeZone))
}
sb.WriteString(cr.cronExpression)
return sb.String()
}

// TimeRange represents a time range between starting time and ending time.
type TimeRange struct {
Start time.Time
Expand Down
70 changes: 60 additions & 10 deletions cronrange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 5,12 @@ import (
)

var (
exprEveryMin = "* * * * *"
timeZoneBangkok = "Asia/Bangkok"
emptyString = ""
exprEveryMin = "* * * * *"
exprEveryXmasMorning = "0 8 25 12 *"
exprEveryNewYear = "0 0 1 1 *"
timeZoneBangkok = "Asia/Bangkok"
timeZoneNewYork = "America/New_York"
)

func TestNew(t *testing.T) {
Expand All @@ -21,14 25,14 @@ func TestNew(t *testing.T) {
wantCr bool
wantErr bool
}{
{"Empty cronExpr", args{"", "", 5}, false, true},
{"Invalid cronExpr", args{"h e l l o", "", 5}, false, true},
{"Incomplete cronExpr", args{"* * * *", "", 5}, false, true},
{"Nonexistent timezone", args{exprEveryMin, "Mars", 5}, false, true},
{"Zero durationMin", args{exprEveryMin, "", 0}, false, true},
{"Normal without timezone", args{exprEveryMin, "", 5}, true, false},
{"Normal with local timezone", args{exprEveryMin, " Local ", 5}, true, false},
{"Normal with timezone", args{exprEveryMin, timeZoneBangkok, 5}, true, false},
{"Empty cronExpr", args{emptyString, emptyString, 5}, false, true},
{"Invalid cronExpr", args{"h e l l o", emptyString, 5}, false, true},
{"Incomplete cronExpr", args{"* * * *", emptyString, 5}, false, true},
{"Nonexistent time zone", args{exprEveryMin, "Mars", 5}, false, true},
{"Zero durationMin", args{exprEveryMin, emptyString, 0}, false, true},
{"Normal without time zone", args{exprEveryMin, emptyString, 5}, true, false},
{"Normal with local time zone", args{exprEveryMin, " Local ", 5}, true, false},
{"Normal with time zone", args{exprEveryMin, timeZoneBangkok, 5}, true, false},
{"Normal with large duration", args{exprEveryMin, timeZoneBangkok, 5259500}, true, false},
}
for _, tt := range tests {
Expand All @@ -50,3 54,49 @@ func BenchmarkNew(b *testing.B) {
_, _ = New(exprEveryMin, timeZoneBangkok, 10)
}
}

func TestCronRange_String(t *testing.T) {
type args struct {
cronExpr string
timeZone string
durationMin uint64
}
tests := []struct {
name string
args *args
want string
}{
{"nil struct", nil, ""},
{"1min duration without time zone", &args{exprEveryMin, emptyString, 1}, "DR=1; * * * * *"},
{"5min duration without time zone", &args{exprEveryMin, emptyString, 5}, "DR=5; * * * * *"},
{"10min duration with local time zone", &args{exprEveryMin, "local", 10}, "DR=10; * * * * *"},
{"10min duration with time zone", &args{exprEveryMin, timeZoneBangkok, 10}, "DR=10; TZ=Asia/Bangkok; * * * * *"},
{"every xmas morning in new york city", &args{exprEveryXmasMorning, timeZoneNewYork, 240}, "DR=240; TZ=America/New_York; 0 8 25 12 *"},
{"every new year's day in bangkok", &args{exprEveryNewYear, timeZoneBangkok, 1440}, "DR=1440; TZ=Asia/Bangkok; 0 0 1 1 *"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var cr *CronRange
if tt.args == nil {
cr = &CronRange{}
} else {
var err error
if cr, err = New(tt.args.cronExpr, tt.args.timeZone, tt.args.durationMin); err != nil {
t.Errorf("New() error = %v", err)
return
}
}
if got := cr.String(); got != tt.want {
t.Errorf("String() = %q, want %q", got, tt.want)
}
})
}
}

func BenchmarkString(b *testing.B) {
cr, _ := New(exprEveryMin, timeZoneBangkok, 10)
b.ResetTimer()
for i := 0; i < b.N; i {
_ = cr.String()
}
}