use super::parse;
fn refused(input: syn::DeriveInput) -> String {
match parse(&input) {
Ok(_) => panic!("the declaration was admitted"),
Err(refusal) => refusal
.into_iter()
.map(|one| one.to_string())
.collect::<Vec<_>>()
.join("\n"),
}
}
#[test]
fn a_transparent_tuple_wrapper_is_admitted() {
let model = parse(&syn::parse_quote! {
#[repr(transparent)]
pub struct TrackId(uuid::Uuid);
})
.expect("the shape is the one this device equips");
assert_eq!(model.subject, "TrackId");
assert_eq!(
quote::ToTokens::to_token_stream(&model.inner).to_string(),
"uuid :: Uuid"
);
assert_eq!(
quote::ToTokens::to_token_stream(&model.krate).to_string(),
":: ingraph"
);
}
#[test]
fn transparent_is_found_beside_another_repr_entry() {
parse(&syn::parse_quote! {
#[repr(align(16), transparent)]
pub struct TrackId(uuid::Uuid);
})
.expect("`transparent` is present, whatever else the representation says");
}
#[test]
fn a_declaration_without_the_representation_is_refused_by_name() {
let refusal = refused(syn::parse_quote! {
pub struct TrackId(uuid::Uuid);
});
assert!(
refusal.contains("the Id derive's admission requires `#[repr(transparent)]`"),
"{refusal}"
);
assert!(refusal.contains("from_ref"), "{refusal}");
}
#[test]
fn another_representation_is_not_the_one_asked_for() {
let refusal = refused(syn::parse_quote! {
#[repr(C)]
pub struct TrackId(uuid::Uuid);
});
assert!(
refusal.contains("requires `#[repr(transparent)]`"),
"{refusal}"
);
}
#[test]
fn a_named_field_is_refused_with_the_rewrite() {
let refusal = refused(syn::parse_quote! {
#[repr(transparent)]
pub struct TrackId {
id: uuid::Uuid,
}
});
assert!(refusal.contains("equips a TUPLE wrapper"), "{refusal}");
assert!(refusal.contains("struct Name(Inner);"), "{refusal}");
}
#[test]
fn a_second_field_is_refused_with_the_count() {
let refusal = refused(syn::parse_quote! {
#[repr(transparent)]
pub struct TrackId(uuid::Uuid, u32);
});
assert!(
refusal.contains("EXACTLY ONE value, and this one holds 2"),
"{refusal}"
);
assert!(refusal.contains("KEY STRUCT"), "{refusal}");
}
#[test]
fn an_empty_wrapper_is_refused() {
let refusal = refused(syn::parse_quote! {
#[repr(transparent)]
pub struct TrackId;
});
assert!(refusal.contains("holds NOTHING"), "{refusal}");
}
#[test]
fn an_enum_is_refused_and_pointed_at_the_vocabulary_door() {
let refusal = refused(syn::parse_quote! {
#[repr(transparent)]
pub enum TrackId {
One(uuid::Uuid),
}
});
assert!(refusal.contains("this declaration is an ENUM"), "{refusal}");
assert!(refusal.contains("#[derive(ingraph::Enum)]"), "{refusal}");
}
#[test]
fn a_union_is_refused() {
let refusal = refused(syn::parse_quote! {
#[repr(transparent)]
pub union TrackId {
one: u64,
}
});
assert!(refusal.contains("this declaration is a UNION"), "{refusal}");
}
#[test]
fn a_generic_wrapper_is_refused_with_the_three_rows() {
let refusal = refused(syn::parse_quote! {
#[repr(transparent)]
pub struct Wrap<T>(T);
});
assert!(refusal.contains("equips a CONCRETE wrapper"), "{refusal}");
assert!(refusal.contains("E0210"), "{refusal}");
assert!(refusal.contains("E0392"), "{refusal}");
}
#[test]
fn the_laws_report_together() {
let refusal = refused(syn::parse_quote! {
pub struct Wrap<T>(T);
});
assert!(refusal.contains("equips a CONCRETE wrapper"), "{refusal}");
assert!(
refusal.contains("requires `#[repr(transparent)]`"),
"{refusal}"
);
}
#[test]
fn the_crate_word_moves_the_root() {
let model = parse(&syn::parse_quote! {
#[repr(transparent)]
#[ingraph(crate = "other::place")]
pub struct TrackId(uuid::Uuid);
})
.expect("`crate = \"…\"` is this device's only option");
assert_eq!(
quote::ToTokens::to_token_stream(&model.krate).to_string(),
"other :: place"
);
}
#[test]
fn a_newtype_word_is_refused_by_name() {
for (word, said) in [
(
quote::quote!(#[ingraph(sqlite)]),
"the dialect words are `#[derive(ingraph::NewType)]`'s",
),
(
quote::quote!(#[ingraph(cursor)]),
"`cursor` is `#[derive(ingraph::NewType)]`'s word",
),
(
quote::quote!(#[ingraph(marker(scalar))]),
"`marker(<family>)` and `filter` are `#[derive(ingraph::NewType)]`'s words",
),
(
quote::quote!(#[ingraph(graphql(async_graphql(scalar)))]),
"`graphql(<backend>(…))` is `#[derive(ingraph::NewType)]`'s word",
),
(
quote::quote!(#[ingraph(mongo)]),
"`mongo` names the document kit",
),
] {
let declaration: syn::DeriveInput = syn::parse_quote! {
#[repr(transparent)]
#word
pub struct TrackId(uuid::Uuid);
};
let refusal = refused(declaration);
assert!(refusal.contains(said), "missing {said} in {refusal}");
}
}
#[test]
fn an_unknown_word_is_refused_as_one() {
let refusal = refused(syn::parse_quote! {
#[repr(transparent)]
#[ingraph(crat = "x")]
pub struct TrackId(uuid::Uuid);
});
assert!(
refusal.contains("unknown entry in `#[ingraph(…)]` on an id wrapper"),
"{refusal}"
);
}