use crate::graph::GraphModel;
fn root(queried: &[&str]) -> 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: Vec::new(),
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 a_member_is_reached_by_projecting_through_the_transformer_line() {
let tokens = emitted(&root(&["media::Reel"]));
assert!(
tokens.contains(
"<< media :: Reel as :: ingraph :: __private :: transform :: Transformer > :: Graphql as :: \
ingraph :: __private :: transform :: Graphql > :: Query"
),
"{tokens}"
);
assert!(
!tokens.contains("ReelQueries"),
"the unit's NAME is never written — the projection is what names it:\n{tokens}"
);
}
#[test]
fn only_the_members_the_declaration_marked_are_folded() {
let tokens = emitted(&root(&["media::Reel", "media::Take"]));
assert!(tokens.contains("media :: Reel"), "{tokens}");
assert!(tokens.contains("media :: Take"), "{tokens}");
assert!(
!tokens.contains("media :: Cutter"),
"a node with no `query` word is no member of the fold:\n{tokens}"
);
}
#[test]
fn a_root_with_no_marked_field_has_no_query_at_all() {
assert!(emitted(&root(&[])).is_empty());
}
#[test]
fn the_rust_name_is_the_roots_and_the_published_name_is_the_conventional_one() {
let tokens = emitted(&root(&["media::Reel"]));
assert!(tokens.contains("struct MediaQuery"), "{tokens}");
assert!(tokens.contains("name = \"Query\""), "{tokens}");
}
#[test]
fn the_merged_object_derive_carries_both_halves_of_the_shelf_rule() {
let tokens = emitted(&root(&["media::Reel"]));
assert!(
tokens.contains(":: ingraph :: __private :: async_graphql :: MergedObject"),
"the derive's own path is the shelf's:\n{tokens}"
);
assert!(
tokens.contains("crate = \"::ingraph::__private::async_graphql\""),
"and so is what it expands to:\n{tokens}"
);
assert!(
!tokens.contains(":: async_graphql :: MergedObject ,")
|| tokens.contains(":: ingraph :: __private :: async_graphql :: MergedObject"),
"no bare async-graphql path anywhere:\n{tokens}"
);
}
#[test]
fn the_root_query_is_written_inside_the_binding_gate() {
let tokens = emitted(&root(&["media::Reel"]));
assert!(
tokens.starts_with(":: ingraph :: __private :: gate :: if_async_graphql !"),
"{tokens}"
);
}
#[test]
fn the_root_carries_no_description_for_a_derive_to_publish() {
let tokens = emitted(&root(&["media::Reel"]));
assert!(!tokens.contains("# [doc"), "{tokens}");
assert!(
tokens.contains("allow (missing_docs"),
"and the absence is declared, so a crate under `missing_docs` is not warned about a type it \
did not write:\n{tokens}"
);
}
#[test]
fn the_query_takes_the_roots_own_visibility() {
let mut model = root(&["media::Reel"]);
model.vis = syn::parse_quote!(pub(crate));
let tokens = emitted(&model);
assert!(tokens.contains("pub (crate) struct MediaQuery"), "{tokens}");
}
fn retrieving(queried: bool) -> GraphModel {
let mut model = root(&[]);
model.fused = vec![crate::graph::FusedMember {
mount: syn::parse_quote!(shots),
ty: syn::parse_quote!(media::Shots),
queried,
}];
model
}
#[test]
fn a_retrieving_member_joins_through_the_one_hop_projection() {
let tokens = emitted(&retrieving(true));
assert!(
tokens
.contains("< media :: Shots as :: ingraph :: __private :: transform :: Graphql > :: Query"),
"{tokens}"
);
assert!(
!tokens.contains("Transformer > :: Graphql as"),
"a retrieving member took the node's two-hop road: {tokens}"
);
assert!(
!tokens.contains("ShotsQueries"),
"the unit's NAME is written where the projection should be: {tokens}"
);
}
#[test]
fn an_unmounted_retrieving_member_is_not_projected() {
assert!(
emitted(&retrieving(false)).is_empty(),
"a root whose only marked member is unmounted emitted a query",
);
}
#[test]
fn a_retrieving_member_alone_is_enough_to_mint_the_root_query() {
let tokens = emitted(&retrieving(true));
assert!(tokens.contains("MergedObject"), "{tokens}");
assert!(tokens.contains("name = \"Query\""), "{tokens}");
}