From 3c132528e346b85667773b6cd79994ee573a3c99 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 15 May 2024 23:33:38 -0400 Subject: [PATCH] ok that was fun --- pttodoer/src/main.rs | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/pttodoer/src/main.rs b/pttodoer/src/main.rs index 8ed1ed8..59f157a 100644 --- a/pttodoer/src/main.rs +++ b/pttodoer/src/main.rs @@ -11,7 +11,6 @@ fn main() { #[derive(Debug)] struct Task(serde_yaml::Mapping); //pub todo: String, - //pub when: Option, //pub detail: Option, //pub sub: Vec, @@ -27,8 +26,7 @@ impl Task { fn is_due_now(&self, now: TS) -> bool { match self.when() { Some(when) => { - eprintln!("next: {}", when.next(self.ts()).to_string()); - now.unix() <= when.next(self.ts()).unix() + now.unix() >= when.next(self.ts()).unix() }, None => true, } @@ -95,14 +93,42 @@ mod test_task { } #[test] - fn is_due() { + fn is_due_duration() { let mut t = Task::new(); assert!(t.is_due()); - t.set("ts".to_string(), "61".to_string()); - t.set("schedule".to_string(), "* * * * *".to_string()); - assert!(!t.is_due_now(TS::from_unix(119))); - assert!(t.is_due_now(TS::from_unix(120))); - assert!(t.is_due_now(TS::from_unix(121))); + let then = TS::new("2000-01-02T15:00Z".to_string()).unwrap(); + let now = TS::new("2000-01-02T16:00Z".to_string()).unwrap(); + t.set("ts".to_string(), then.to_string()); + t.set("schedule".to_string(), "1h".to_string()); + assert!(!t.is_due_now(TS::from_unix(now.unix()-1))); + assert!(t.is_due_now(TS::from_unix(now.unix()))); + assert!(t.is_due_now(TS::from_unix(now.unix()+1))); + } + + #[test] + fn is_due_schedule() { + let mut t = Task::new(); + assert!(t.is_due()); + let then = TS::new("2000-01-02T15:00Z".to_string()).unwrap(); + let now = TS::new("2000-01-02T16:00Z".to_string()).unwrap(); + t.set("ts".to_string(), then.to_string()); + t.set("schedule".to_string(), "2000-01-02T16:00Z".to_string()); + assert!(!t.is_due_now(TS::from_unix(now.unix()-1))); + assert!(t.is_due_now(TS::from_unix(now.unix()))); + assert!(t.is_due_now(TS::from_unix(now.unix()+1))); + } + + #[test] + fn is_due_cron() { + let mut t = Task::new(); + assert!(t.is_due()); + let then = TS::new("2000-01-02T15:00Z".to_string()).unwrap(); + let now = TS::new("2000-01-02T16:00Z".to_string()).unwrap(); + t.set("ts".to_string(), then.to_string()); + t.set("schedule".to_string(), "0 16 * * *".to_string()); + assert!(!t.is_due_now(TS::from_unix(now.unix()-1))); + assert!(t.is_due_now(TS::from_unix(now.unix()))); + assert!(t.is_due_now(TS::from_unix(now.unix()+1))); } }