diff --git a/job.go b/job.go index 890cc12..2ea8e91 100644 --- a/job.go +++ b/job.go @@ -311,8 +311,7 @@ type Weekdays func() []time.Weekday // NewWeekdays provide the days of the week the job should run. func NewWeekdays(weekday time.Weekday, weekdays ...time.Weekday) Weekdays { return func() []time.Weekday { - weekdays = append(weekdays, weekday) - return weekdays + return append([]time.Weekday{weekday}, weekdays...) } } @@ -400,8 +399,7 @@ type DaysOfTheMonth func() days // -5 == 5 days before the end of the month. func NewDaysOfTheMonth(day int, moreDays ...int) DaysOfTheMonth { return func() days { - moreDays = append(moreDays, day) - return moreDays + return append([]int{day}, moreDays...) } } @@ -439,8 +437,7 @@ type AtTimes func() []AtTime // the job should be run func NewAtTimes(atTime AtTime, atTimes ...AtTime) AtTimes { return func() []AtTime { - atTimes = append(atTimes, atTime) - return atTimes + return append([]AtTime{atTime}, atTimes...) } } diff --git a/job_test.go b/job_test.go index d60c2a4..b729486 100644 --- a/job_test.go +++ b/job_test.go @@ -724,3 +724,59 @@ func TestTimeFromAtTime(t *testing.T) { }) } } + +func TestNewAtTimes(t *testing.T) { + at := NewAtTimes( + NewAtTime(1, 1, 1), + NewAtTime(2, 2, 2), + ) + + var times []string + for _, att := range at() { + timeStr := TimeFromAtTime(att, time.UTC).Format("15:04") + times = append(times, timeStr) + } + + var timesAgain []string + for _, att := range at() { + timeStr := TimeFromAtTime(att, time.UTC).Format("15:04") + timesAgain = append(timesAgain, timeStr) + } + + assert.Equal(t, times, timesAgain) +} + +func TestNewWeekdays(t *testing.T) { + wd := NewWeekdays( + time.Monday, + time.Tuesday, + ) + + var dayStrings []string + for _, w := range wd() { + dayStrings = append(dayStrings, w.String()) + } + + var dayStringsAgain []string + for _, w := range wd() { + dayStringsAgain = append(dayStringsAgain, w.String()) + } + + assert.Equal(t, dayStrings, dayStringsAgain) +} + +func TestNewDaysOfTheMonth(t *testing.T) { + dom := NewDaysOfTheMonth(1, 2, 3) + + var domInts []int + for _, d := range dom() { + domInts = append(domInts, d) + } + + var domIntsAgain []int + for _, d := range dom() { + domIntsAgain = append(domIntsAgain, d) + } + + assert.Equal(t, domInts, domIntsAgain) +}