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_level_is_a_bare_word() {
let model = operator(&syn::parse_quote! {
#[ingraph(tracing(level = debug))]
struct Decode {
#[ingraph(id)]
id: u64,
}
})
.expect("one word, one level");
assert_eq!(model.tracing, Some(TracingLevel::Debug));
}
#[test]
fn every_shipped_level_is_a_word_a_declaration_can_write() {
for level in TracingLevel::ALL {
let word = syn::Ident::new(level.as_str(), proc_macro2::Span::call_site());
let model = operator(&syn::parse_quote! {
#[ingraph(tracing(level = #word))]
struct Decode {
#[ingraph(id)]
id: u64,
}
})
.unwrap_or_else(|_| {
panic!(
"`{word}` is shipped and must be writable",
word = level.as_str()
)
});
assert_eq!(model.tracing, Some(level));
}
}
#[test]
fn a_declaration_with_no_word_carries_no_level() {
let model = operator(&syn::parse_quote! {
struct Keep {
#[ingraph(id)]
id: u64,
}
})
.expect("the word is optional");
assert_eq!(model.tracing, None);
}
#[test]
fn a_level_the_framework_does_not_ship_is_refused_by_name() {
let messages = refusals(syn::parse_quote! {
#[ingraph(tracing(level = verbose))]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&messages, "`verbose` is not a level this framework names"),
"{messages:?}"
);
for level in TracingLevel::ALL {
let word = level.as_str();
assert!(
names(&messages, &format!("`{word}`")),
"the refusal omits `{word}`: {messages:?}"
);
}
}
#[test]
fn a_quoted_level_is_refused_and_the_refusal_names_the_law() {
let messages = refusals(syn::parse_quote! {
#[ingraph(tracing(level = "debug"))]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&messages, "`level` takes a BARE WORD"),
"{messages:?}"
);
assert!(
names(&messages, "REFERS to something declared elsewhere"),
"the refusal does not say why the quoted form is not merely a second spelling: {messages:?}"
);
}
#[test]
fn the_bare_word_is_refused_with_the_form_it_takes() {
let messages = refusals(syn::parse_quote! {
#[ingraph(tracing)]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(
&messages,
"`tracing` is a block — write `tracing(level = debug)`"
),
"{messages:?}"
);
}
#[test]
fn an_empty_block_is_refused_the_way_the_bare_word_is() {
let messages = refusals(syn::parse_quote! {
#[ingraph(tracing())]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(names(&messages, "`tracing` is a block"), "{messages:?}");
}
#[test]
fn an_unknown_entry_in_the_block_is_refused() {
let messages = refusals(syn::parse_quote! {
#[ingraph(tracing(level = debug, target = "media"))]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&messages, "unknown entry in `tracing(…)`"),
"{messages:?}"
);
}
#[test]
fn the_word_is_declared_once() {
let two_blocks = refusals(syn::parse_quote! {
#[ingraph(tracing(level = debug), tracing(level = warn))]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&two_blocks, "`tracing(…)` is declared more than once"),
"{two_blocks:?}"
);
let two_levels = refusals(syn::parse_quote! {
#[ingraph(tracing(level = debug, level = warn))]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&two_levels, "`level` is declared more than once"),
"{two_levels:?}"
);
}
#[test]
fn every_node_kind_takes_the_word() {
for kind in [
NodeKind::Operator,
NodeKind::Record,
NodeKind::Fulltext,
NodeKind::Vector,
NodeKind::Blob,
] {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(tracing(level = error))]
struct Decode {
#[ingraph(id)]
id: u64,
}
};
let (host, options) = crate::host::split(&input);
match crate::parse::parse(&host, &options, kind) {
Ok(model) => assert_eq!(model.tracing, Some(TracingLevel::Error), "{kind:?}"),
Err(refused) => {
let messages: Vec<String> = refused.into_iter().map(|e| e.to_string()).collect();
assert!(
!names(&messages, "tracing"),
"{kind:?} refused the word: {messages:?}"
);
}
}
}
}
#[test]
fn the_unknown_attribute_sentence_lists_the_word() {
let messages = refusals(syn::parse_quote! {
#[ingraph(traicng(level = debug))]
struct Decode {
#[ingraph(id)]
id: u64,
}
});
assert!(
names(&messages, "unknown `ingraph` record attribute"),
"{messages:?}"
);
assert!(
names(&messages, "`tracing(…)`"),
"the vocabulary a misspelling is answered with omits the word: {messages:?}"
);
}