use cfait::model::{DateType, Task};
use chrono::NaiveTime;
use std::collections::HashMap;
fn parse(input: &str) -> Task {
Task::new(input, &HashMap::new(), None)
}
#[test]
fn test_floating_time_weekly_recurrence() {
let t = parse("Take vitamins @weekly");
assert!(t.rrule.is_some());
let rrule = t.rrule.clone().unwrap();
assert!(rrule.contains("FREQ=WEEKLY"));
assert!(t.dtstart.is_some());
let next_task = cfait::model::RecurrenceEngine::next_occurrence(&t);
assert!(next_task.is_some());
let next_task = next_task.unwrap();
let original_time = match t.dtstart.unwrap() {
DateType::Specific(dt) => dt.with_timezone(&chrono::Local).time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
let next_time = match next_task.dtstart.unwrap() {
DateType::Specific(dt) => dt.with_timezone(&chrono::Local).time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
assert_eq!(original_time, next_time);
}
#[test]
fn test_floating_time_daily_recurrence() {
let t = parse("Morning walk @daily");
assert!(t.rrule.is_some());
let rrule = t.rrule.clone().unwrap();
assert!(rrule.contains("FREQ=DAILY"));
assert!(t.dtstart.is_some());
let next_task = cfait::model::RecurrenceEngine::next_occurrence(&t);
assert!(next_task.is_some());
let next_task = next_task.unwrap();
let original_time = match t.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
let next_time = match next_task.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
assert_eq!(original_time, next_time);
}
#[test]
fn test_floating_time_monthly_recurrence() {
let t = parse("Pay rent @monthly");
assert!(t.rrule.is_some());
let rrule = t.rrule.clone().unwrap();
assert!(rrule.contains("FREQ=MONTHLY"));
assert!(t.dtstart.is_some());
let next_task = cfait::model::RecurrenceEngine::next_occurrence(&t);
assert!(next_task.is_some());
let next_task = next_task.unwrap();
let original_time = match t.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
let next_time = match next_task.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
assert_eq!(original_time, next_time);
}
#[test]
fn test_floating_time_yearly_recurrence() {
let t = parse("Annual checkup @yearly");
assert!(t.rrule.is_some());
let rrule = t.rrule.clone().unwrap();
assert!(rrule.contains("FREQ=YEARLY"));
assert!(t.dtstart.is_some());
let next_task = cfait::model::RecurrenceEngine::next_occurrence(&t);
assert!(next_task.is_some());
let next_task = next_task.unwrap();
let original_time = match t.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
let next_time = match next_task.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
assert_eq!(original_time, next_time);
}
#[test]
fn test_floating_time_with_explicit_time() {
let t = parse("Take vitamins at 11:00 @weekly");
assert!(t.rrule.is_some());
let rrule = t.rrule.clone().unwrap();
assert!(rrule.contains("FREQ=WEEKLY"));
assert!(t.dtstart.is_some());
let next_task = cfait::model::RecurrenceEngine::next_occurrence(&t);
assert!(next_task.is_some());
let next_task = next_task.unwrap();
let original_time = match t.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
let next_time = match next_task.dtstart.unwrap() {
DateType::Specific(dt) => dt.time(),
DateType::AllDay(_d) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Month(_, _) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
DateType::Year(_) => NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
};
assert_eq!(original_time, next_time);
}