use ingraph_codegen::prelude::*;
fn operator(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, NodeKind::Operator)
}
fn refusals(input: syn::DeriveInput) -> Vec<String> {
operator(&input)
.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 = operator(&syn::parse_quote! {
#[ingraph(options = "decode::Options")]
struct Decode {
#[ingraph(id)]
id: u64,
}
})
.expect("one word, one type");
let options = model.options.expect("the declaration named one");
assert_eq!(quote::quote!(#options).to_string(), "decode :: Options");
}
#[test]
fn a_declaration_with_no_word_names_no_type() {
let model = operator(&syn::parse_quote! {
struct Keep {
#[ingraph(id)]
id: u64,
}
})
.expect("a node with no knobs is a node");
assert!(model.options.is_none());
}
#[test]
fn the_word_is_written_once() {
let refused = refusals(syn::parse_quote! {
#[ingraph(options = "a::One", options = "b::Other")]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&refused, "one node is configured by one type"),
"{refused:?}"
);
}
#[test]
fn a_value_that_is_not_a_path_is_refused_at_the_word() {
let refused = refusals(syn::parse_quote! {
#[ingraph(options = "not a path!")]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&refused, "must be a path to this node's options type"),
"{refused:?}"
);
}
#[test]
fn the_word_carries_its_type() {
let refused = refusals(syn::parse_quote! {
#[ingraph(options)]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&refused, "must be a path to this node's options type"),
"{refused:?}"
);
}