Files
2026-10-03 14:07:36 -07:00

95 lines
2.8 KiB
Rust

//! Tests for the five-field UTC cron matcher. Do not edit.
use bxcore::Timestamp;
use loopd::cron::{self, CronError};
fn ts(s: &str) -> Timestamp {
Timestamp::parse(s).unwrap()
}
fn due(schedule: &str, at: &str) -> bool {
cron::due(schedule, ts(at)).unwrap()
}
#[test]
fn a_daily_hour_matches_that_minute_only() {
assert!(due("0 7 * * *", "2026-10-03T07:00:00.000Z"));
assert!(!due("0 7 * * *", "2026-10-03T07:01:00.000Z"));
assert!(!due("0 7 * * *", "2026-10-03T06:00:00.000Z"));
}
#[test]
fn every_minute_matches() {
assert!(due("* * * * *", "2026-10-03T00:00:00.000Z"));
assert!(due("* * * * *", "2026-10-03T23:59:00.000Z"));
}
#[test]
fn a_step_matches_multiples_from_zero() {
assert!(due("*/5 * * * *", "2026-10-03T00:00:00.000Z"));
assert!(due("*/5 * * * *", "2026-10-03T00:05:00.000Z"));
assert!(!due("*/5 * * * *", "2026-10-03T00:06:00.000Z"));
}
#[test]
fn a_range_is_inclusive() {
assert!(due("0-10 * * * *", "2026-10-03T00:00:00.000Z"));
assert!(due("0-10 * * * *", "2026-10-03T00:10:00.000Z"));
assert!(!due("0-10 * * * *", "2026-10-03T00:11:00.000Z"));
}
#[test]
fn sunday_is_zero() {
// 2026-10-04 is a Sunday.
assert!(due("0 7 * * 0", "2026-10-04T07:00:00.000Z"));
// 2026-10-03 is a Saturday.
assert!(!due("0 7 * * 0", "2026-10-03T07:00:00.000Z"));
}
#[test]
fn the_first_of_the_month() {
assert!(due("0 7 1 * *", "2026-10-01T07:00:00.000Z"));
assert!(!due("0 7 1 * *", "2026-10-02T07:00:00.000Z"));
}
#[test]
fn day_of_month_and_day_of_week_are_both_required() {
// 2026-10-04 is a Sunday, not the 1st. 2026-10-01 is a Thursday.
assert!(!due("0 7 1 * 0", "2026-10-04T07:00:00.000Z"));
assert!(!due("0 7 1 * 0", "2026-10-01T07:00:00.000Z"));
// 2026-11-01 is a Sunday and the 1st.
assert!(due("0 7 1 * 0", "2026-11-01T07:00:00.000Z"));
}
#[test]
fn four_or_six_fields_are_errors() {
assert!(matches!(
cron::due("* * * *", ts("2026-10-03T00:00:00.000Z")),
Err(CronError::Fields(_))
));
assert!(matches!(
cron::due("* * * * * *", ts("2026-10-03T00:00:00.000Z")),
Err(CronError::Fields(_))
));
}
#[test]
fn an_out_of_range_number_is_an_error() {
assert!(cron::due("60 * * * *", ts("2026-10-03T00:00:00.000Z")).is_err());
assert!(cron::due("* 24 * * *", ts("2026-10-03T00:00:00.000Z")).is_err());
assert!(cron::due("* * * * 7", ts("2026-10-03T00:00:00.000Z")).is_err());
}
#[test]
fn nicknames_are_errors() {
let err = cron::due("@daily", ts("2026-10-03T00:00:00.000Z")).unwrap_err();
assert!(matches!(err, CronError::Fields(_)), "{err:?}");
}
#[test]
fn display_names_the_schedule() {
let err = cron::due("x x x x x", ts("2026-10-03T00:00:00.000Z")).unwrap_err();
let s = err.to_string();
assert!(s.contains("x x x x x"), "{s}");
}