use crate::graph::{GraphModel, InterfaceMount};
fn root(queried: &[&str], interfaces: &[(&str, bool)]) -> GraphModel {
let nodes: Vec<syn::Type> = ["media::Reel", "media::Take", "media::Cutter"]
.iter()
.map(|path| syn::parse_str(path).expect("a node path"))
.collect();
GraphModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: syn::parse_quote!(Media),
nodes,
mounts: vec![
syn::parse_quote!(reels),
syn::parse_quote!(takes),
syn::parse_quote!(cutters),
],
queried: queried
.iter()
.map(|path| syn::parse_str(path).expect("a node path"))
.collect(),
interfaces: interfaces
.iter()
.enumerate()
.map(|(at, (path, queried))| InterfaceMount {
mount: quote::format_ident!("mount{at}"),
ty: syn::parse_str(path).expect("an interface path"),
queried: *queried,
})
.collect(),
fused: Vec::new(),
resources: Vec::new(),
origins: Vec::new(),
engines: Vec::new(),
retired: None,
generations: None,
}
}
fn emitted(model: &GraphModel) -> String {
model.query().to_string()
}
#[test]
fn an_interface_is_projected_through_graphql_alone() {
let tokens = emitted(&root(&[], &[("media::Screened", true)]));
assert!(
tokens.contains(
"< media :: Screened as :: ingraph :: __private :: transform :: Graphql > :: Query"
),
"{tokens}"
);
assert!(
!tokens.contains("Screened as :: ingraph :: __private :: transform :: Transformer"),
"an interface has no `Transformer` to project through:\n{tokens}"
);
assert!(
!tokens.contains("ScreenedQueries"),
"the unit's NAME is never written here either:\n{tokens}"
);
}
#[test]
fn the_nodes_and_the_interfaces_are_folded_by_one_assembly() {
let tokens = emitted(&root(&["media::Reel"], &[("media::Screened", true)]));
assert!(tokens.contains("media :: Reel"), "{tokens}");
assert!(tokens.contains("media :: Screened"), "{tokens}");
assert_eq!(
tokens.matches("MergedObject").count(),
1,
"one fold, not one per tier:\n{tokens}"
);
}
#[test]
fn an_interface_that_did_not_write_the_word_is_not_folded() {
let tokens = emitted(&root(&["media::Reel"], &[("media::Screened", false)]));
assert!(
!tokens.contains("Screened"),
"an unmounted interface is no member of the fold:\n{tokens}"
);
assert!(tokens.contains("media :: Reel"), "{tokens}");
}
#[test]
fn an_interface_mounts_without_any_member_mounting() {
let tokens = emitted(&root(&[], &[("media::Screened", true)]));
assert!(tokens.contains("media :: Screened"), "{tokens}");
assert!(
!tokens.contains("media :: Reel"),
"no member joined, and none was dragged in:\n{tokens}"
);
assert!(tokens.contains("struct MediaQuery"), "{tokens}");
}
#[test]
fn the_query_condition_reads_both_tiers() {
assert!(!emitted(&root(&[], &[("media::Screened", true)])).is_empty());
assert!(emitted(&root(&[], &[("media::Screened", false)])).is_empty());
assert!(emitted(&root(&[], &[])).is_empty());
}