use quote::format_ident;
use crate::{
model::{
Grant, Grants, PollBody, PollCadence, PollEntry, PollMode, Polls, RecordModel,
default_sweeps_name,
},
name::RustSafeName,
tests::{emission, model},
};
fn node(anchored: bool, granting: bool) -> RecordModel {
let mut model = model("Reaper", &["media_id"], "id");
let granted: &[&str] = if granting { &["media::Media"] } else { &[] };
let mut entries = vec![entry("hourly", granted, None)];
if anchored {
entries.push(entry("scrape", &[], Some("app::Scraper")));
}
model.polls = Some(Polls { entries });
model
}
fn entry(name: &str, granted: &[&str], handler: Option<&str>) -> PollEntry {
use heck::ToUpperCamelCase as _;
PollEntry {
name: format_ident!("{name}"),
variant: format_ident!("{}", name.to_upper_camel_case()),
cadence: PollCadence::Every(core::time::Duration::new(600, 0)),
grants: (!granted.is_empty()).then(|| Grants {
grants: granted.iter().copied().map(grant).collect(),
}),
body: handler.map_or(PollBody::Light, |path| {
PollBody::Citizen(syn::parse_str(path).expect("a type path"))
}),
mode: PollMode::AsyncInline,
concurrent: false,
}
}
fn grant(path: &str) -> Grant {
let node: syn::Path = syn::parse_str(path).expect("a type path");
let segment = &node
.segments
.last()
.expect("a path has at least one segment")
.ident;
let mount = RustSafeName::new(segment.to_string(), proc_macro2::Span::call_site());
Grant { node, mount }
}
fn tight(model: &RecordModel) -> String {
model.emit().to_string().replace(' ', "")
}
#[test]
fn the_sweep_bound_carries_its_handles_and_its_pair_in_one_argument_list() {
let pair = "Info=<app::Scraperas::ingraph::__private::drive::AsyncTick<()>>::Info,\
Error=<app::Scraperas::ingraph::__private::drive::AsyncTick<()>>::Error";
for (anchored, granting, arguments) in [
(false, false, String::new()),
(false, true, "<MediaHandle>".to_owned()),
(true, false, format!("<{pair}>")),
(true, true, format!("<MediaHandle,{pair}>")),
] {
let model = node(anchored, granting);
let _ = emission(&model);
let sweeps = default_sweeps_name(&model.record);
let expected = format!("O:{sweeps}{arguments}+::core::marker::Send,");
assert!(
tight(&model).contains(&expected),
"mounted={anchored} granting={granting} wants `{expected}`: {}",
tight(&model),
);
}
}
#[test]
fn no_pairing_reopens_a_second_argument_list_on_the_sweep_trait() {
for anchored in [false, true] {
for granting in [false, true] {
let model = node(anchored, granting);
let sweeps = default_sweeps_name(&model.record);
let emitted = tight(&model);
assert!(
!emitted.contains(&format!("{sweeps}<MediaHandle><")),
"mounted={anchored} granting={granting}: {emitted}"
);
}
}
}