github.com/sandwich-go/boost@v1.3.29/xtime/cron/cronexpr_test.go (about) 1 package cron 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 type cronTimes struct { 9 from string 10 next string 11 } 12 13 type cronTest struct { 14 expr string 15 layout string 16 times []cronTimes 17 } 18 19 var cronTests = []cronTest{ 20 // Seconds 21 { 22 "* * * * * *", 23 "2006-01-02 15:04:05", 24 []cronTimes{ 25 {"2013-01-01 00:00:00", "2013-01-01 00:00:01"}, 26 {"2013-01-01 00:00:59", "2013-01-01 00:01:00"}, 27 {"2013-01-01 00:59:59", "2013-01-01 01:00:00"}, 28 {"2013-01-01 23:59:59", "2013-01-02 00:00:00"}, 29 {"2013-02-28 23:59:59", "2013-03-01 00:00:00"}, 30 {"2016-02-28 23:59:59", "2016-02-29 00:00:00"}, 31 {"2012-12-31 23:59:59", "2013-01-01 00:00:00"}, 32 }, 33 }, 34 35 // every 5 Second 36 { 37 "*/5 * * * * *", 38 "2006-01-02 15:04:05", 39 []cronTimes{ 40 {"2013-01-01 00:00:00", "2013-01-01 00:00:05"}, 41 {"2013-01-01 00:00:59", "2013-01-01 00:01:00"}, 42 {"2013-01-01 00:59:59", "2013-01-01 01:00:00"}, 43 {"2013-01-01 23:59:59", "2013-01-02 00:00:00"}, 44 {"2013-02-28 23:59:59", "2013-03-01 00:00:00"}, 45 {"2016-02-28 23:59:59", "2016-02-29 00:00:00"}, 46 {"2012-12-31 23:59:59", "2013-01-01 00:00:00"}, 47 }, 48 }, 49 50 // Minutes 51 { 52 "* * * * *", 53 "2006-01-02 15:04:05", 54 []cronTimes{ 55 {"2013-01-01 00:00:00", "2013-01-01 00:01:00"}, 56 {"2013-01-01 00:00:59", "2013-01-01 00:01:00"}, 57 {"2013-01-01 00:59:00", "2013-01-01 01:00:00"}, 58 {"2013-01-01 23:59:00", "2013-01-02 00:00:00"}, 59 {"2013-02-28 23:59:00", "2013-03-01 00:00:00"}, 60 {"2016-02-28 23:59:00", "2016-02-29 00:00:00"}, 61 {"2012-12-31 23:59:00", "2013-01-01 00:00:00"}, 62 }, 63 }, 64 65 // Minutes with interval 66 { 67 "17-43/5 * * * *", 68 "2006-01-02 15:04:05", 69 []cronTimes{ 70 {"2013-01-01 00:00:00", "2013-01-01 00:17:00"}, 71 {"2013-01-01 00:16:59", "2013-01-01 00:17:00"}, 72 {"2013-01-01 00:30:00", "2013-01-01 00:32:00"}, 73 {"2013-01-01 00:50:00", "2013-01-01 01:17:00"}, 74 {"2013-01-01 23:50:00", "2013-01-02 00:17:00"}, 75 {"2013-02-28 23:50:00", "2013-03-01 00:17:00"}, 76 {"2016-02-28 23:50:00", "2016-02-29 00:17:00"}, 77 {"2012-12-31 23:50:00", "2013-01-01 00:17:00"}, 78 }, 79 }, 80 81 // Minutes interval, list 82 { 83 "15-30/4,55 * * * *", 84 "2006-01-02 15:04:05", 85 []cronTimes{ 86 {"2013-01-01 00:00:00", "2013-01-01 00:15:00"}, 87 {"2013-01-01 00:16:00", "2013-01-01 00:19:00"}, 88 {"2013-01-01 00:30:00", "2013-01-01 00:55:00"}, 89 {"2013-01-01 00:55:00", "2013-01-01 01:15:00"}, 90 {"2013-01-01 23:55:00", "2013-01-02 00:15:00"}, 91 {"2013-02-28 23:55:00", "2013-03-01 00:15:00"}, 92 {"2016-02-28 23:55:00", "2016-02-29 00:15:00"}, 93 {"2012-12-31 23:54:00", "2012-12-31 23:55:00"}, 94 {"2012-12-31 23:55:00", "2013-01-01 00:15:00"}, 95 }, 96 }, 97 98 // Days of week 99 { 100 "0 0 * * 1", 101 "Mon 2006-01-02 15:04", 102 []cronTimes{ 103 {"2013-01-01 00:00:00", "Mon 2013-01-07 00:00"}, 104 {"2013-01-28 00:00:00", "Mon 2013-02-04 00:00"}, 105 {"2013-12-30 00:30:00", "Mon 2014-01-06 00:00"}, 106 }, 107 }, 108 { 109 "0 0 * * 5", 110 "Mon 2006-01-02 15:04", 111 []cronTimes{ 112 {"2013-01-01 00:00:00", "Fri 2013-01-04 00:00"}, 113 {"2013-01-28 00:00:00", "Fri 2013-02-01 00:00"}, 114 {"2013-12-30 00:30:00", "Fri 2014-01-03 00:00"}, 115 }, 116 }, 117 { 118 "0 0 * * 6,0", 119 "Mon 2006-01-02 15:04", 120 []cronTimes{ 121 {"2013-01-01 00:00:00", "Sat 2013-01-05 00:00"}, 122 {"2013-01-28 00:00:00", "Sat 2013-02-02 00:00"}, 123 {"2013-12-30 00:30:00", "Sat 2014-01-04 00:00"}, 124 }, 125 }, 126 // TODO: more tests 127 } 128 129 func TestExpressions(t *testing.T) { 130 for _, test := range cronTests { 131 for _, times := range test.times { 132 from, _ := time.Parse("2006-01-02 15:04:05", times.from) 133 expr := MustParse(test.expr) 134 next := expr.Next(from) 135 nextStr := next.Format(test.layout) 136 if nextStr != times.next { 137 t.Errorf(`("%s").Next("%s") = "%s", got "%s"`, test.expr, times.from, times.next, nextStr) 138 } 139 } 140 } 141 }