ok that was fun

master
Bel LaPointe 2024-05-15 23:33:38 -04:00
parent 1639286189
commit 3c132528e3
1 changed files with 35 additions and 9 deletions

View File

@ -11,7 +11,6 @@ fn main() {
#[derive(Debug)] #[derive(Debug)]
struct Task(serde_yaml::Mapping); struct Task(serde_yaml::Mapping);
//pub todo: String, //pub todo: String,
//pub when: Option<When>,
//pub detail: Option<String>, //pub detail: Option<String>,
//pub sub: Vec<Task>, //pub sub: Vec<Task>,
@ -27,8 +26,7 @@ impl Task {
fn is_due_now(&self, now: TS) -> bool { fn is_due_now(&self, now: TS) -> bool {
match self.when() { match self.when() {
Some(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, None => true,
} }
@ -95,14 +93,42 @@ mod test_task {
} }
#[test] #[test]
fn is_due() { fn is_due_duration() {
let mut t = Task::new(); let mut t = Task::new();
assert!(t.is_due()); assert!(t.is_due());
t.set("ts".to_string(), "61".to_string()); let then = TS::new("2000-01-02T15:00Z".to_string()).unwrap();
t.set("schedule".to_string(), "* * * * *".to_string()); let now = TS::new("2000-01-02T16:00Z".to_string()).unwrap();
assert!(!t.is_due_now(TS::from_unix(119))); t.set("ts".to_string(), then.to_string());
assert!(t.is_due_now(TS::from_unix(120))); t.set("schedule".to_string(), "1h".to_string());
assert!(t.is_due_now(TS::from_unix(121))); 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)));
} }
} }