use std::cell::RefCell;
use std::time::{Duration, Instant};
use super::{wait_warning, Due};
#[derive(Default)]
struct Calls {
steps: Vec<&'static str>,
until: Option<Instant>,
warned_after: Option<Duration>,
}
fn run(due: Option<Due>, ended: bool) -> (Calls, &'static str) {
let calls = RefCell::new(Calls::default());
let result = wait_warning(
(),
due,
|(), until| {
let mut calls = calls.borrow_mut();
calls.steps.push("look");
calls.until = Some(until);
ended
},
|after| {
let mut calls = calls.borrow_mut();
calls.steps.push("warn");
calls.warned_after = Some(after);
},
|()| {
calls.borrow_mut().steps.push("wait");
"waited"
},
);
(calls.into_inner(), result)
}
fn due() -> Due {
Due::in_(Some(Duration::from_secs(7))).expect("a threshold")
}
#[skuld::test]
fn without_a_threshold_only_the_wait_runs() {
let (calls, result) = run(None, false);
assert_eq!(calls.steps, ["wait"]);
assert_eq!(result, "waited");
}
#[skuld::test]
fn a_wait_that_ended_in_time_does_not_warn() {
let (calls, result) = run(Some(due()), true);
assert_eq!(calls.steps, ["look", "wait"]);
assert_eq!(result, "waited");
}
#[skuld::test]
fn a_wait_still_going_at_the_threshold_warns_once_before_it_blocks() {
let due = due();
let (calls, result) = run(Some(due), false);
assert_eq!(calls.steps, ["look", "warn", "wait"]);
assert_eq!(calls.until, Some(due.until));
assert_eq!(calls.warned_after, Some(due.after));
assert_eq!(result, "waited");
}