use ingraph_codegen::prelude::*;
fn parse(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) -> String {
parse(&input)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn receipts(input: syn::DeriveInput) -> Vec<(String, bool, bool)> {
parse(&input)
.expect("the declaration is accepted")
.subscriptions
.expect("the container was written")
.upstreams
.iter()
.map(|upstream| {
(
upstream.variant.to_string(),
upstream.keyed,
upstream.notify,
)
})
.collect()
}
#[test]
fn the_receipt_is_per_entry_and_the_default_is_the_message() {
let receipts = receipts(syn::parse_quote! {
#[ingraph(subscribe(
upstream(node = "asr::Segment", notify),
upstream(node = "media::Ocr"),
))]
pub struct Summaries {
#[ingraph(id)]
id: u32,
#[ingraph(copy)]
media_id: u64,
}
});
assert_eq!(
receipts,
vec![
("Segment".to_owned(), false, true),
("Ocr".to_owned(), false, false),
]
);
}
#[test]
fn the_two_bare_words_compose_on_one_entry() {
let receipts = receipts(syn::parse_quote! {
#[ingraph(subscribe(
upstream(node = "asr::Segment", key, notify),
upstream(node = "media::Ocr", key),
))]
pub struct Summaries {
#[ingraph(id)]
id: u32,
#[ingraph(copy)]
media_id: u64,
}
});
assert_eq!(
receipts,
vec![
("Segment".to_owned(), true, true),
("Ocr".to_owned(), true, false),
]
);
}
#[test]
fn the_word_said_twice_on_one_entry_is_refused() {
let refusal = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "asr::Segment", notify, notify)))]
pub struct Summaries {
#[ingraph(id)]
id: u32,
#[ingraph(copy)]
media_id: u64,
}
});
assert!(
refusal.contains("`notify` is written twice in one `upstream(…)`"),
"{refusal}"
);
}
#[test]
fn a_notify_that_names_a_payload_is_refused() {
let refusal = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "asr::Segment", notify = "SegmentKey")))]
pub struct Summaries {
#[ingraph(id)]
id: u32,
#[ingraph(copy)]
media_id: u64,
}
});
assert!(refusal.contains("`notify` is written bare"), "{refusal}");
assert!(refusal.contains("the upstream's own answer"), "{refusal}");
}
#[test]
fn the_unknown_entry_sentence_names_the_receipt_word() {
let refusal = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "asr::Segment", notifies)))]
pub struct Summaries {
#[ingraph(id)]
id: u32,
#[ingraph(copy)]
media_id: u64,
}
});
assert!(
refusal.contains("the bare words `key` and `notify`"),
"{refusal}"
);
}
#[test]
fn the_word_is_not_a_poll_entrys() {
let refusal = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", notify, light, async(inline)))]
pub struct Summaries {
#[ingraph(id)]
id: u32,
#[ingraph(copy)]
media_id: u64,
}
});
assert!(refusal.contains("a poll entry has NO receipt"), "{refusal}");
assert!(
refusal.contains("`upstream(…)` entry inside `subscribe(…)`"),
"{refusal}"
);
}