use super::{Catalog, ResourceField, ResourceSource};
use crate::graph::GraphModel;
fn app() -> GraphModel {
GraphModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: syn::parse_quote!(App),
nodes: vec![syn::parse_quote!(media::Video)],
mounts: vec![syn::parse_quote!(videos)],
queried: Vec::new(),
interfaces: Vec::new(),
fused: Vec::new(),
resources: vec![
ResourceField {
mount: syn::parse_quote!(egemma),
ty: syn::parse_quote!(models::Egemma),
source: ResourceSource::Section,
teardown: false,
},
ResourceField {
mount: syn::parse_quote!(pool),
ty: syn::parse_quote!(Pool),
source: ResourceSource::Prepare,
teardown: false,
},
ResourceField {
mount: syn::parse_quote!(cache),
ty: syn::parse_quote!(Cache),
source: ResourceSource::Initializer(syn::parse_quote!(crate::boot::cache)),
teardown: false,
},
],
origins: Vec::new(),
engines: Vec::new(),
retired: None,
generations: None,
}
}
fn nodes_only() -> GraphModel {
GraphModel {
queried: Vec::new(),
interfaces: Vec::new(),
fused: Vec::new(),
resources: Vec::new(),
origins: Vec::new(),
engines: Vec::new(),
retired: None,
generations: None,
..app()
}
}
fn compact(model: &GraphModel) -> String {
rendered(model).split_whitespace().collect()
}
fn rendered(model: &GraphModel) -> String {
prettyplease::unparse(&syn::parse2(model.emit()).expect("the expansion parses as a file"))
}
fn packed(claim: &str) -> String {
claim.split_whitespace().collect()
}
fn says(compact: &str, claim: &[&str]) -> Option<usize> {
let mut at = 0;
let mut head = None;
for fragment in claim {
let fragment = packed(fragment);
let found = at + compact[at..].find(&fragment)?;
head.get_or_insert(found);
at = found + fragment.len();
}
head
}
#[test]
fn the_block_holds_a_section_only_for_the_resources_that_have_one() {
let compact = compact(&app());
assert!(
compact.contains(&packed(
"pub (crate) egemma : ::core::option::Option < < models::Egemma as \
::ingraph::__private::resource::AsyncResource > ::Options >"
)),
"{}",
rendered(&app())
);
assert!(!compact.contains("pub(crate)pool:"), "{}", rendered(&app()));
assert!(
!compact.contains("pub(crate)cache:"),
"{}",
rendered(&app())
);
}
#[test]
fn the_spine_carries_the_block_under_the_reserved_key() {
let compact = compact(&app());
assert!(
compact.contains(&packed("pub (crate) resources : AppResources")),
"{}",
rendered(&app())
);
assert!(
compact.contains(&packed(
"#[serde(default)] pub (crate) resources : AppResources"
)),
"{}",
rendered(&app())
);
assert!(
compact.contains(&packed(
"#[serde(crate = \"::ingraph::__private::serde\", deny_unknown_fields)]"
)),
"{}",
rendered(&app())
);
}
#[test]
fn a_root_with_no_sections_carries_no_block() {
let compact = compact(&nodes_only());
assert!(
!compact.contains("AppResources"),
"{}",
rendered(&nodes_only())
);
assert!(
!compact.contains("pub(crate)resources:"),
"{}",
rendered(&nodes_only())
);
assert!(
!compact.contains("fnwith_resources"),
"{}",
rendered(&nodes_only())
);
}
#[test]
fn the_block_and_the_spine_both_take_their_options_in_code() {
let compact = compact(&app());
for door in [
&[
"pub fn with_egemma (mut self , options :",
"< models::Egemma as ::ingraph::__private::resource::AsyncResource > ::Options",
") -> Self",
][..],
&["pub fn with_resources (mut self , resources : AppResources) -> Self"][..],
&["pub const fn resources (& self) -> & AppResources"][..],
] {
assert!(
says(&compact, door).is_some(),
"{door:?}\n{}",
rendered(&app())
);
}
}
#[test]
fn the_walk_declares_all_three_sources_in_order() {
let compact = compact(&app());
let calls: Vec<usize> = [
("models::Egemma", "egemma", "Section"),
("Pool", "pool", "Prepare"),
("Cache", "cache", "Initializer"),
]
.iter()
.map(|&(ty, mount, source)| {
let call = [
format!("next_resource :: < {ty}"),
"visitor".to_owned(),
format!("\"{mount}\""),
format!("::ingraph::__private::resource::ResourceSource::{source}"),
];
let claim: Vec<&str> = call.iter().map(String::as_str).collect();
says(&compact, &claim).unwrap_or_else(|| panic!("{claim:?}\n{}", rendered(&app())))
})
.collect();
assert!(calls[0] < calls[1] && calls[1] < calls[2], "{calls:?}");
}
#[test]
fn the_catalog_is_the_walk_driven_into_the_frameworks_collector() {
let compact = compact(&app());
assert!(
compact.contains(&packed(
"pub fn resources () -> ::ingraph::__private::resource::ResourceCatalog"
)),
"{}",
rendered(&app())
);
assert!(
compact.contains(&packed("Self::resource_iter (& mut declared)")),
"{}",
rendered(&app())
);
}
#[test]
fn the_create_walk_drives_the_two_declared_sources_only() {
let compact = compact(&app());
assert!(
says(
&compact,
&[
"let AppResources { egemma : __ingraph_resource_egemma",
"} = options.resources ;",
]
)
.is_some(),
"{}",
rendered(&app())
);
assert!(
says(
&compact,
&[
"declared_resource :: < models::Egemma",
"(& mut __ingraph_creation , move || ::core::option::Option::or_else \
(__ingraph_resource_egemma ,",
"::ingraph::__private::drive::bare_resource_options ! (models::Egemma)",
]
)
.is_some(),
"{}",
rendered(&app())
);
assert!(
says(
&compact,
&[
"declared_resource :: < Cache",
"(& mut __ingraph_creation ,",
"|| ::core::option::Option::Some (crate::boot::cache ())",
]
)
.is_some(),
"{}",
rendered(&app())
);
assert!(
!compact.contains("declared_resource::<Pool"),
"{}",
rendered(&app())
);
}
#[test]
fn the_declared_resources_are_built_before_prepare_and_prepare_before_the_nodes() {
let compact = compact(&app());
let resource = compact
.find(&packed("declared_resource :: < models::Egemma"))
.expect("the section resource is built");
let prepare = compact
.find(&packed("prepare (& mut __ingraph_creation) . await ?"))
.expect("the hook runs");
let node = compact
.find(&packed("node :: < media::Video"))
.expect("the node pass runs");
assert!(resource < prepare, "{}", rendered(&app()));
assert!(prepare < node, "{}", rendered(&app()));
}
#[test]
fn the_entry_installs_the_roots_own_catalog() {
let compact = compact(&app());
assert!(
says(
&compact,
&[
"::ingraph::__private::drive::Plan::with_resources (",
"::ingraph::__private::drive::Plan::with_enablement (plan , __ingraph_enablement ,)",
", Self::resources () ,)",
]
)
.is_some(),
"{}",
rendered(&app())
);
}
#[test]
fn every_resource_carries_its_error_predicate() {
let compact = compact(&app());
assert!(
compact.contains(&packed(
"< models::Egemma as ::ingraph::__private::resource::AsyncResource > ::Error : \
::core::error::Error"
)),
"{}",
rendered(&app())
);
assert!(
compact.contains(&packed(
"< Pool as ::ingraph::__private::resource::AsyncResource > ::Error : ::core::error::Error"
)),
"{}",
rendered(&app())
);
}
#[test]
fn the_catalog_reads_its_own_list() {
let model = app();
let catalog: Catalog<'_> = model.catalog();
assert!(catalog.has_block());
assert_eq!(catalog.sections().count(), 1);
assert!(!model.catalog().resources[1].source.is_walked());
assert!(model.catalog().resources[2].source.is_walked());
assert!(!nodes_only().catalog().has_block());
}