fn parse(input: &syn::DeriveInput) -> syn::Result<ingraph_codegen::graph::GraphModel> {
let (host, arguments) = crate::host::split(input);
super::parse(&host, &arguments)
}
pub(super) fn accepted(input: proc_macro2::TokenStream) -> ingraph_codegen::graph::GraphModel {
parse(&syn::parse2(input).expect("the declaration parses")).expect("the declaration is a graph")
}
pub(super) fn refused(input: proc_macro2::TokenStream) -> String {
parse(&syn::parse2(input).expect("the declaration parses"))
.expect_err("this declaration breaks a rule")
.into_iter()
.map(|error| error.to_string())
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn an_ingraph_is_its_field_list_in_order() {
let model = accepted(quote::quote! {
pub struct App {
videos: media::Video,
scenes: Scene,
}
});
assert_eq!(model.name, "App");
assert!(matches!(model.vis, syn::Visibility::Public(_)));
assert_eq!(model.nodes.len(), 2);
assert_eq!(
quote::ToTokens::to_token_stream(&model.nodes[0]).to_string(),
"media :: Video"
);
assert_eq!(
quote::ToTokens::to_token_stream(&model.nodes[1]).to_string(),
"Scene"
);
}
#[test]
fn the_crate_path_defaults_and_is_moveable() {
let default = accepted(quote::quote! {
struct App {
videos: Video,
}
});
assert_eq!(
quote::ToTokens::to_token_stream(&default.krate).to_string(),
":: ingraph"
);
let moved = accepted(quote::quote! {
#[ingraph(crate = "shim::vendored::ingraph")]
struct App {
videos: Video,
}
});
assert_eq!(
quote::ToTokens::to_token_stream(&moved.krate).to_string(),
"shim :: vendored :: ingraph"
);
}
#[test]
fn an_enum_is_not_an_ingraph() {
assert!(
refused(quote::quote! {
enum App {
Videos(Video),
}
})
.contains("a graph root is a STRUCT")
);
}
#[test]
fn a_positional_field_has_no_mount_name() {
assert!(
refused(quote::quote! {
struct App(Video, Scene);
})
.contains("fields are NAMED")
);
assert!(
refused(quote::quote! {
struct App;
})
.contains("fields are NAMED")
);
}
#[test]
fn an_empty_ingraph_declares_nothing_to_walk() {
assert!(
refused(quote::quote! {
struct App {}
})
.contains("no field declares no node")
);
}
#[test]
fn a_generic_root_names_a_different_ingraph_per_instantiation() {
assert!(
refused(quote::quote! {
struct App<T> {
videos: T,
}
})
.contains("no generic parameter")
);
}
#[test]
fn a_node_listed_twice_is_refused_and_the_first_field_is_named() {
let refusal = refused(quote::quote! {
struct App {
videos: Video,
clips: Scene,
archive: Video,
}
});
assert!(refusal.contains("already in the graph"), "{refusal}");
assert!(refusal.contains("`videos`"), "{refusal}");
}
#[test]
fn a_field_carries_no_declaration_of_its_own() {
assert!(
refused(quote::quote! {
struct App {
#[ingraph(sqlite)]
videos: Video,
}
})
.contains("unknown entry in `#[ingraph(…)]` on a graph root's field")
);
}
#[test]
fn the_root_takes_its_four_entries_and_says_so() {
let refusal = refused(quote::quote! {
#[ingraph(sqlite)]
struct App {
videos: Video,
}
});
assert!(refusal.contains("takes `crate = \"…\"`"), "{refusal}");
assert!(
refusal.contains("engines(<word> = \"path::To::Engine\")"),
"and it names the engine table, which is the second of what a root declares: {refusal}"
);
assert!(
refusal.contains("`retired(<engine>)`"),
"and the ledger, which is the third: {refusal}"
);
assert!(
refusal.contains("`generations(<engine>)`"),
"and the generations seat, which is the fourth — the two of them being the only entries that \
name a durable thing: {refusal}"
);
assert!(!refusal.contains("`dedup(…)`"), "{refusal}");
}
#[test]
fn a_member_may_not_be_mounted_at_the_registry_seat() {
let refusal = refused(quote::quote! {
struct App {
videos: Video,
registry: Registry,
}
});
assert!(
refusal.contains("`registry` is a RESERVED mount"),
"{refusal}"
);
assert!(refusal.contains("[registry]"), "{refusal}");
assert!(refusal.contains("[registry.<family>]"), "{refusal}");
}
#[test]
fn one_compile_reports_every_rule_it_can_still_see() {
let refusal = refused(quote::quote! {
#[ingraph(sqlite)]
struct App<T> {
videos: Video,
archive: Video,
other: T,
}
});
assert!(refusal.contains("nothing else"), "{refusal}");
assert!(refusal.contains("no generic parameter"), "{refusal}");
assert!(refusal.contains("already in the graph"), "{refusal}");
}