use std::path::PathBuf;
use printobserver_core::listing::{JOB_IDENTITY_TOLERANCE_S, REPLACED_REASON};
const PHRASE: &str = " seconds (the identity tolerance)";
const DOCUMENTS: [&str; 3] = [
"README.md",
"skills/printobserver/reference/api-and-clients.md",
"skills/printobserver/reference/common-operations.md",
];
fn document(path: &str) -> String {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..");
let text = std::fs::read_to_string(root.join(path))
.unwrap_or_else(|error| panic!("{path} could not be read: {error}"));
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
fn stated_tolerances(text: &str) -> Vec<String> {
let mut stated = Vec::new();
let mut rest = text;
while let Some(at) = rest.find(PHRASE) {
let before = &rest[..at];
let number = before.rsplit(' ').next().unwrap_or_default().to_owned();
stated.push(number);
rest = &rest[at + PHRASE.len()..];
}
stated
}
#[test]
fn every_document_states_the_tolerance_the_matching_uses() {
let expected = JOB_IDENTITY_TOLERANCE_S.to_string();
for path in DOCUMENTS {
let stated = stated_tolerances(&document(path));
assert!(
!stated.is_empty(),
"{path} no longer states the identity tolerance as `<N>{PHRASE}`"
);
for number in stated {
assert_eq!(
number, expected,
"{path} states the identity tolerance as {number} seconds, and the \
matching uses JOB_IDENTITY_TOLERANCE_S = {expected}"
);
}
}
}
#[test]
fn every_document_names_the_reason_a_replaced_print_ends_with() {
for path in DOCUMENTS {
assert!(
document(path).contains(REPLACED_REASON),
"{path} does not name `{REPLACED_REASON}`, the reason a print a later job \
of its file replaced is ended with"
);
}
}
#[test]
fn a_statement_of_another_tolerance_is_read_as_that_number() {
let stated = stated_tolerances(
"less than 90 seconds (the identity tolerance) and no more than 120 seconds \
(the identity tolerance) before",
);
assert_eq!(stated, ["90", "120"]);
}