use ingraph_codegen::catalog::ResourceSource;
fn parse(input: proc_macro2::TokenStream) -> syn::Result<ingraph_codegen::graph::GraphModel> {
let input: syn::DeriveInput = syn::parse2(input).expect("the declaration parses");
let (host, arguments) = crate::host::split(&input);
crate::graph::parse(&host, &arguments)
}
pub(super) fn accepted(input: proc_macro2::TokenStream) -> ingraph_codegen::graph::GraphModel {
parse(input).expect("the declaration is a graph")
}
pub(super) fn refused(input: proc_macro2::TokenStream) -> String {
parse(input)
.expect_err("this declaration breaks a rule")
.into_iter()
.map(|error| error.to_string())
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn the_word_takes_its_three_forms_and_they_stay_in_order() {
let model = accepted(quote::quote! {
pub struct App {
videos: media::Video,
#[ingraph(resource)]
egemma: models::Egemma,
#[ingraph(resource(factory))]
pool: Pool,
#[ingraph(resource(factory = "crate::boot::cache"))]
cache: Cache,
}
});
assert_eq!(model.nodes.len(), 1, "one field of four is a node");
assert_eq!(model.mounts.len(), 1);
assert_eq!(model.resources.len(), 3);
assert_eq!(model.resources[0].mount, "egemma");
assert_eq!(model.resources[0].source, ResourceSource::Section);
assert_eq!(
quote::ToTokens::to_token_stream(&model.resources[0].ty).to_string(),
"models :: Egemma"
);
assert_eq!(model.resources[1].mount, "pool");
assert_eq!(model.resources[1].source, ResourceSource::Prepare);
assert_eq!(model.resources[2].mount, "cache");
let ResourceSource::Initializer(path) = &model.resources[2].source else {
panic!("the assigned form names a function");
};
assert_eq!(
quote::ToTokens::to_token_stream(path).to_string(),
"crate :: boot :: cache"
);
}
#[test]
fn an_empty_block_is_the_bare_word() {
let model = accepted(quote::quote! {
struct App {
videos: Video,
#[ingraph(resource())]
egemma: Egemma,
}
});
assert_eq!(model.resources[0].source, ResourceSource::Section);
}
#[test]
fn a_member_may_not_be_mounted_at_the_grouping_key() {
for declaration in [
quote::quote! {
struct App {
videos: Video,
resources: Registry,
}
},
quote::quote! {
struct App {
videos: Video,
#[ingraph(resource)]
resources: Registry,
}
},
] {
let refusal = refused(declaration);
assert!(
refusal.contains("`resources` is a RESERVED mount"),
"{refusal}"
);
assert!(refusal.contains("[resources.<field>]"), "{refusal}");
}
}
#[test]
fn an_unknown_field_word_names_what_a_field_takes() {
let refusal = refused(quote::quote! {
struct App {
#[ingraph(sqlite)]
videos: Video,
}
});
assert!(
refusal.contains("unknown entry in `#[ingraph(…)]` on a graph root's field"),
"{refusal}"
);
assert!(refusal.contains("`resource(factory)`"), "{refusal}");
}
#[test]
fn an_unknown_entry_in_the_block_names_what_the_block_takes() {
let refusal = refused(quote::quote! {
struct App {
#[ingraph(resource(lazy))]
egemma: Egemma,
}
});
assert!(
refusal.contains("unknown entry in `resource(…)`"),
"{refusal}"
);
}
#[test]
fn the_named_factory_is_a_quoted_path() {
let refusal = refused(quote::quote! {
struct App {
#[ingraph(resource(factory = boot::cache))]
cache: Cache,
}
});
assert!(refusal.contains("in a string literal"), "{refusal}");
}
#[test]
fn the_word_is_written_once_per_field() {
let refusal = refused(quote::quote! {
struct App {
videos: Video,
#[ingraph(resource)]
#[ingraph(resource(factory))]
egemma: Egemma,
}
});
assert!(
refusal.contains("`resource` is declared more than once"),
"{refusal}"
);
}
#[test]
fn one_type_is_one_member_across_both_tiers() {
let refusal = refused(quote::quote! {
struct App {
#[ingraph(resource)]
egemma: Egemma,
#[ingraph(resource(factory))]
second: Egemma,
}
});
assert!(refusal.contains("already in the graph"), "{refusal}");
assert!(
refusal.contains("declare it twice under two mounts"),
"{refusal}"
);
}