package main import ( "testing" "time" "github.com/stretchr/testify/assert" ) func TestMatchTime(t *testing.T) { tts := []struct { name string pattern string currentValue int exptMatch bool exptError string }{ { name: "just a number matching", pattern: "5", currentValue: 5, exptMatch: true, exptError: "", }, { name: "just a number mismtaching", pattern: "5", currentValue: 6, exptMatch: false, exptError: "", }, { name: "errors because of letters", pattern: "abc", currentValue: 5, exptMatch: false, exptError: "cant parse time pattern", }, { name: "every minute", pattern: "*", currentValue: 5, exptMatch: true, exptError: "", }, { name: "two numbers, matching", pattern: "10, 27", currentValue: 27, exptMatch: true, exptError: "", }, { name: "two numbers, matching", pattern: "10, 27", currentValue: 56, exptMatch: false, exptError: "", }, { name: "single range, match", pattern: "10-20", currentValue: 14, exptMatch: true, exptError: "", }, { name: "range in wrong direction", pattern: "30-20", currentValue: 14, exptMatch: false, exptError: "cant parse time pattern: smaler number should be first", }, { name: "range with letter", pattern: "b-20", currentValue: 14, exptMatch: false, exptError: "cant parse time pattern: strconv.Atoi: parsing \"b\": invalid syntax", }, { name: "2 ranges, matching one", pattern: "10-20,40-50", currentValue: 13, exptMatch: true, exptError: "", }, { name: "invalide pattern 3 -", pattern: "10-20-30", currentValue: 4, exptMatch: false, exptError: "cant parse time pattern: range include more than 2 parameter", }, { name: "every secound matching", pattern: "*/2", currentValue: 4, exptMatch: true, exptError: "", }, { name: "both cases", pattern: "5-59/20", currentValue: 25, exptMatch: true, exptError: "", }, { name: "both cases", pattern: "5-59/20", currentValue: 20, exptMatch: false, exptError: "", }, { name: "both cases", pattern: "5-59/20", currentValue: 0, exptMatch: false, exptError: "", }, } for _, tt := range tts { t.Run(tt.name, func(t *testing.T) { res, err := matchTime(tt.pattern, tt.currentValue) assert.Equal(t, tt.exptMatch, res) if tt.exptError == "" { assert.Nil(t, err) } else { assert.Equal(t, tt.exptError, err.Error()) } }) } } func TestMatchJobTime(t *testing.T) { ti := time.Date(2025, time.October, 15, 14, 14, 15, 0, time.UTC) tts := []struct { name string job jobconfig expRes bool expErrMsg string }{ { name: "failed-houre", job: jobconfig{Hour: "15", Minute: "*", Weekday: "*"}, expRes: false, expErrMsg: "", }, { name: "failed-minute", job: jobconfig{Hour: "*", Minute: "46", Weekday: "*"}, expRes: false, expErrMsg: "", }, { name: "pass", job: jobconfig{Hour: "*", Minute: "*", Weekday: "*"}, expRes: true, expErrMsg: "", }, { name: "failed-with-error", job: jobconfig{Hour: "abs", Minute: "*", Weekday: "*"}, expRes: false, expErrMsg: "cant parse time pattern", }, { name: "pass-with-timezone", job: jobconfig{Hour: "17", Minute: "*", Weekday: "*", Timezone: "Africa/Nairobi"}, expRes: true, expErrMsg: "", }, } for _, tt := range tts { t.Run(tt.name, func(t *testing.T) { res, err := tt.job.MatchCurrentTime(ti) if tt.expErrMsg == "" { assert.Nil(t, err) } else { assert.Equal(t, tt.expErrMsg, err.Error()) } assert.Equal(t, tt.expRes, res) }) } }