| 138 | } |
| 139 | |
| 140 | func TestParseSchedule(t *testing.T) { |
| 141 | tokyo, _ := time.LoadLocation("Asia/Tokyo") |
| 142 | entries := []struct { |
| 143 | parser Parser |
| 144 | expr string |
| 145 | expected Schedule |
| 146 | }{ |
| 147 | {secondParser, "0 5 * * * *", every5min(time.Local)}, |
| 148 | {standardParser, "5 * * * *", every5min(time.Local)}, |
| 149 | {secondParser, "CRON_TZ=UTC 0 5 * * * *", every5min(time.UTC)}, |
| 150 | {standardParser, "CRON_TZ=UTC 5 * * * *", every5min(time.UTC)}, |
| 151 | {secondParser, "CRON_TZ=Asia/Tokyo 0 5 * * * *", every5min(tokyo)}, |
| 152 | {secondParser, "@every 5m", ConstantDelaySchedule{5 * time.Minute}}, |
| 153 | {secondParser, "@midnight", midnight(time.Local)}, |
| 154 | {secondParser, "TZ=UTC @midnight", midnight(time.UTC)}, |
| 155 | {secondParser, "TZ=Asia/Tokyo @midnight", midnight(tokyo)}, |
| 156 | {secondParser, "@yearly", annual(time.Local)}, |
| 157 | {secondParser, "@annually", annual(time.Local)}, |
| 158 | { |
| 159 | parser: secondParser, |
| 160 | expr: "* 5 * * * *", |
| 161 | expected: &SpecSchedule{ |
| 162 | Second: all(seconds), |
| 163 | Minute: 1 << 5, |
| 164 | Hour: all(hours), |
| 165 | Dom: all(dom), |
| 166 | Month: all(months), |
| 167 | Dow: all(dow), |
| 168 | Location: time.Local, |
| 169 | }, |
| 170 | }, |
| 171 | } |
| 172 | |
| 173 | for _, c := range entries { |
| 174 | actual, err := c.parser.Parse(c.expr) |
| 175 | if err != nil { |
| 176 | t.Errorf("%s => unexpected error %v", c.expr, err) |
| 177 | } |
| 178 | if !reflect.DeepEqual(actual, c.expected) { |
| 179 | t.Errorf("%s => expected %b, got %b", c.expr, c.expected, actual) |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func TestOptionalSecondSchedule(t *testing.T) { |
| 185 | parser := NewParser(SecondOptional | Minute | Hour | Dom | Month | Dow | Descriptor) |