use ingraph_codegen::prelude::*;
fn entity(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, NodeKind::Entity)
}
fn refusals(input: syn::DeriveInput) -> Vec<String> {
entity(&input)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect()
}
fn refusals_at(kind: NodeKind, input: syn::DeriveInput) -> Vec<String> {
let (host, options) = crate::host::split(&input);
crate::parse::parse(&host, &options, kind)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect()
}
fn names(messages: &[String], rule: &str) -> bool {
messages.iter().any(|message| message.contains(rule))
}
#[test]
fn the_word_is_a_quoted_path() {
let model = entity(&syn::parse_quote! {
#[ingraph(receipt = "media::SceneId")]
struct SceneOpen {
#[ingraph(id)]
id: u64,
}
})
.expect("one word, one type");
let receipt = model.receipt.expect("the declaration named one");
assert_eq!(quote::quote!(#receipt).to_string(), "media :: SceneId");
}
#[test]
fn a_declaration_with_no_word_names_no_type() {
let model = entity(&syn::parse_quote! {
struct Keep {
#[ingraph(id)]
id: u64,
}
})
.expect("a node that receipts nothing is a node");
assert!(model.receipt.is_none());
}
#[test]
fn the_word_is_written_once() {
let refused = refusals(syn::parse_quote! {
#[ingraph(receipt = "a::One", receipt = "b::Other")]
struct SceneOpen {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&refused, "one message is answered with one type"),
"{refused:?}"
);
}
#[test]
fn a_value_that_is_not_a_path_is_refused_at_the_word() {
let refused = refusals(syn::parse_quote! {
#[ingraph(receipt = "not a path!")]
struct SceneOpen {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(
&refused,
"must be a path to the type a consumer answers this message with"
),
"{refused:?}"
);
}
#[test]
fn the_word_carries_its_type() {
let refused = refusals(syn::parse_quote! {
#[ingraph(receipt)]
struct SceneOpen {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(
&refused,
"must be a path to the type a consumer answers this message with"
),
"{refused:?}"
);
}
#[test]
fn a_typo_of_the_word_is_named() {
let refused = refusals(syn::parse_quote! {
#[ingraph(reciept = "media::SceneId")]
struct SceneOpen {
#[ingraph(id)]
id: u64,
}
});
assert!(names(&refused, "did you mean `receipt`"), "{refused:?}");
}
#[test]
fn a_declaration_that_mints_no_message_is_refused_by_name() {
for kind in [
NodeKind::Operator,
NodeKind::Fulltext,
NodeKind::Vector,
NodeKind::Blob,
] {
let refused = refusals_at(
kind,
syn::parse_quote! {
#[ingraph(receipt = "media::SceneId")]
struct Scene {
#[ingraph(id)]
id: u64,
}
},
);
assert!(
names(&refused, "mints no MESSAGE"),
"{kind:?} took a word it mints no subject for: {refused:?}"
);
assert!(
names(
&refused,
"an `entity` (the message node), a `record`, or an `aggregate`"
),
"{kind:?}'s refusal does not say where the word went: {refused:?}"
);
assert!(
names(&refused, "`Emitter::receipt`"),
"{kind:?}'s refusal closes the hand road it must leave open: {refused:?}"
);
}
}
#[test]
fn every_message_rung_takes_the_word() {
for kind in [NodeKind::Entity, NodeKind::Record, NodeKind::Aggregate] {
assert!(
kind.mints_a_message(),
"the roster and the predicate disagree about {kind:?}"
);
}
}