use ingraph_codegen::{model::GraphqlKind, mutation::DeclaredError};
fn parse(input: &syn::DeriveInput) -> syn::Result<ingraph_codegen::mutation::MutationModel> {
let (host, arguments) = crate::host::split(input);
super::parse(&host, &arguments)
}
fn accepted(input: proc_macro2::TokenStream) -> ingraph_codegen::mutation::MutationModel {
parse(&syn::parse2(input).expect("the declaration parses"))
.expect("the declaration is a mutation node")
}
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 a_mutation_node_is_its_fields_and_the_words_beside_them() {
let model = accepted(quote::quote! {
#[ingraph(graphql(async_graphql))]
pub struct WatchOps {
#[ingraph(rename = "addWatch",
handler(path = "crate::watch::add", async,
context(resource("crate::Catalog")),
error(type = "crate::AddWatchError", code = "ADD_FAILED")))]
add: (AddWatch, AddResponse),
#[ingraph(handler(path = "crate::watch::remove",
error(message = "that watch is not ours", code = "NOT_OURS")))]
remove: (RemoveWatch, RemoveResponse),
#[ingraph(handler = "crate::watch::reindex")]
reindex: (Reindex, ()),
}
});
assert_eq!(model.name, "WatchOps");
assert_eq!(model.graphql, vec![GraphqlKind::AsyncGraphql]);
assert_eq!(model.exchanges.len(), 3);
let add = &model.exchanges[0];
assert_eq!(add.field, "add");
assert_eq!(
add.rename.as_ref().map(syn::LitStr::value).as_deref(),
Some("addWatch")
);
assert!(add.handler.awaited);
assert_eq!(
quote::ToTokens::to_token_stream(&add.handler.path).to_string(),
"crate :: watch :: add"
);
assert_eq!(add.handler.context.len(), 1);
assert_eq!(
quote::ToTokens::to_token_stream(&add.handler.context[0]).to_string(),
"crate :: Catalog"
);
match &add.handler.error {
Some(DeclaredError::Rich { ty, code }) => {
assert_eq!(
quote::ToTokens::to_token_stream(ty).to_string(),
"crate :: AddWatchError"
);
assert_eq!(
code.as_ref().map(syn::LitStr::value).as_deref(),
Some("ADD_FAILED")
);
}
other => panic!("a `type` word is the rich shape: {other:?}"),
}
let remove = &model.exchanges[1];
assert!(remove.rename.is_none());
assert!(!remove.handler.awaited);
assert!(remove.handler.context.is_empty());
match &remove.handler.error {
Some(DeclaredError::Canned { message, code }) => {
assert_eq!(message.value(), "that watch is not ours");
assert_eq!(
code.as_ref().map(syn::LitStr::value).as_deref(),
Some("NOT_OURS")
);
}
other => panic!("a `message` word is the canned shape: {other:?}"),
}
let reindex = &model.exchanges[2];
assert_eq!(
quote::ToTokens::to_token_stream(&reindex.handler.path).to_string(),
"crate :: watch :: reindex"
);
assert!(!reindex.handler.awaited);
assert!(reindex.handler.error.is_none());
}
#[test]
fn one_awaited_handler_makes_the_node_awaited() {
let all_blocking = accepted(quote::quote! {
pub struct Ops {
#[ingraph(handler = "crate::a")]
a: (A, ()),
#[ingraph(handler = "crate::b")]
b: (B, ()),
}
});
assert!(all_blocking.all_sync());
let one_awaited = accepted(quote::quote! {
pub struct Ops {
#[ingraph(handler = "crate::a")]
a: (A, ()),
#[ingraph(handler(path = "crate::b", async))]
b: (B, ()),
}
});
assert!(!one_awaited.all_sync());
}
#[test]
fn a_node_needs_no_wire_word() {
let model = accepted(quote::quote! {
pub struct Ops {
#[ingraph(handler = "crate::a")]
a: (A, ()),
}
});
assert!(model.graphql.is_empty());
}
#[test]
fn a_mutation_node_can_name_the_crate() {
let model = accepted(quote::quote! {
#[ingraph(crate = "crate::shim")]
pub struct Ops {
#[ingraph(handler = "crate::a")]
a: (A, ()),
}
});
assert_eq!(
quote::ToTokens::to_token_stream(&model.krate).to_string(),
"crate :: shim"
);
}
#[test]
fn an_exchange_names_the_handler_that_runs_it() {
let refusal = refused(quote::quote! {
pub struct Ops {
add: (AddWatch, AddResponse),
}
});
assert!(
refusal.contains("`add` declares no `handler`"),
"the refusal names the field: {refusal}"
);
assert!(
refusal.contains("handler = \"path::to::fn\""),
"and offers the plain form: {refusal}"
);
}
#[test]
fn a_failure_is_a_type_or_a_message_and_never_both() {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(path = "crate::a", error(type = "crate::E", message = "oops")))]
a: (A, ()),
}
});
assert!(
refusal.contains("two shapes of ONE word"),
"the refusal states the law: {refusal}"
);
let reversed = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(path = "crate::a", error(message = "oops", type = "crate::E")))]
a: (A, ()),
}
});
assert!(reversed.contains("two shapes of ONE word"), "{reversed}");
}
#[test]
fn a_code_alone_heads_nothing() {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(path = "crate::a", error(code = "NOPE")))]
a: (A, ()),
}
});
assert!(
refusal.contains("heads nothing"),
"the refusal names the rule: {refusal}"
);
assert!(
refusal.contains("omits `error(…)`"),
"and names the exit for a field that cannot fail: {refusal}"
);
}
#[test]
fn an_empty_error_word_declares_no_failure() {
for written in [quote::quote!(error()), quote::quote!(error)] {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(path = "crate::a", #written))]
a: (A, ()),
}
});
assert!(
refusal.contains("declares no failure"),
"`{written}` is refused: {refusal}"
);
}
}
#[test]
fn a_rename_is_a_word_in_a_string() {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(rename = addWatch, handler = "crate::a")]
a: (A, ()),
}
});
assert!(
refusal.contains("in a string literal"),
"the refusal names the shape: {refusal}"
);
let fenced = refused(quote::quote! {
pub struct Ops {
#[ingraph(rename = "add watch", handler = "crate::a")]
a: (A, ()),
}
});
assert!(fenced.contains("is not a GraphQL name"), "{fenced}");
}
#[test]
fn a_context_grants_resources_and_nothing_else() {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(path = "crate::a", context(crate::Catalog)))]
a: (A, ()),
}
});
assert!(
refusal.contains("one `resource(\"…\")` line each"),
"the refusal names the only legal line: {refusal}"
);
let bare = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(path = "crate::a", context(resource)))]
a: (A, ()),
}
});
assert!(bare.contains("one `resource(\"…\")` line each"), "{bare}");
let empty = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(path = "crate::a", context()))]
a: (A, ()),
}
});
assert!(empty.contains("grants nothing"), "{empty}");
}
#[test]
fn a_handler_block_names_its_path() {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler(async, error(message = "oops")))]
a: (A, ()),
}
});
assert!(
refusal.contains("names no `path`"),
"the refusal names the missing word: {refusal}"
);
for written in [
quote::quote!(handler = crate::a),
quote::quote!(handler(path = crate::a)),
] {
let bare = refused(quote::quote! {
pub struct Ops {
#[ingraph(#written)]
a: (A, ()),
}
});
assert!(
bare.contains("named in a string literal"),
"`{written}`: {bare}"
);
assert_eq!(bare.lines().count(), 1, "`{written}`: {bare}");
}
}
#[test]
fn a_bad_value_earns_one_error_and_never_its_own_echo() {
for (written, sentence) in [
(
quote::quote!(handler(path = crate::a, error(type = crate::E))),
"`error(type = \"…\")` names the error",
),
(
quote::quote!(handler(path = "crate::a", error(message = oops))),
"`error(message = \"…\")` is the whole sentence",
),
(
quote::quote!(handler(
path = "crate::a",
error(message = "oops", code = NOPE)
)),
"`error(code = \"…\")` is the heading",
),
] {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(#written)]
a: (A, ()),
}
});
assert!(refusal.contains(sentence), "`{written}`: {refusal}");
for echo in ["names no `path`", "declares no failure", "heads nothing"] {
assert!(
!refusal.contains(echo),
"`{written}` echoed `{echo}`: {refusal}"
);
}
}
}
#[test]
fn a_mutation_node_is_a_struct_with_named_fields() {
let positional = refused(quote::quote! {
pub struct Ops(AddWatch, AddResponse);
});
assert!(positional.contains("named fields"), "{positional}");
let variants = refused(quote::quote! {
pub enum Ops {
Add(AddWatch),
}
});
assert!(variants.contains("structs only"), "{variants}");
}
#[test]
fn a_mutation_node_takes_no_generic_parameter() {
let refusal = refused(quote::quote! {
pub struct Ops<R> {
#[ingraph(handler = "crate::a")]
a: (R, ()),
}
});
assert!(refusal.contains("no generic parameter"), "{refusal}");
}
#[test]
fn an_unknown_entry_names_where_the_fact_belongs() {
let head = refused(quote::quote! {
#[ingraph(source = "crate::Watch")]
pub struct Ops {
#[ingraph(handler = "crate::a")]
a: (A, ()),
}
});
assert!(
head.contains("that source's own `mutation(node = \"…\")` word"),
"the head's refusal points at the source: {head}"
);
let field = refused(quote::quote! {
pub struct Ops {
#[ingraph(handler = "crate::a", error(message = "oops"))]
a: (A, ()),
}
});
assert!(
field.contains("lives INSIDE the handler block"),
"a field's refusal points into the block: {field}"
);
}
#[test]
fn every_word_is_written_once() {
for (written, sentence) in [
(
quote::quote!(handler = "crate::a", handler = "crate::b"),
"one field is ONE thing an app can say",
),
(
quote::quote!(rename = "a", rename = "b", handler = "crate::a"),
"one field, one published word",
),
(
quote::quote!(handler(path = "crate::a", async, async)),
"one handler wears one face",
),
(
quote::quote!(handler(path = "crate::a", path = "crate::b")),
"one exchange runs one function",
),
(
quote::quote!(handler(
path = "crate::a",
error(message = "a", code = "A", code = "B")
)),
"one failure renders under one heading",
),
] {
let refusal = refused(quote::quote! {
pub struct Ops {
#[ingraph(#written)]
a: (A, ()),
}
});
assert!(refusal.contains(sentence), "`{written}`: {refusal}");
}
}