use ingraph_codegen::prelude::*;
use crate::host;
fn record(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = host::split(input);
crate::parse::parse(&host, &options, NodeKind::Record)
}
fn signatures(model: &RecordModel) -> Vec<bool> {
model
.artifacts
.iter()
.map(|artifact| artifact.required)
.collect()
}
fn refusals(input: &syn::DeriveInput) -> Vec<String> {
record(input)
.expect_err("this declaration is refused")
.into_iter()
.map(|error| error.to_string())
.collect()
}
fn names(messages: &[String], rule: &str) -> bool {
messages.iter().any(|message| message.contains(rule))
}
#[test]
fn the_position_carries_its_own_signature() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite,
lineage(fields(media)),
graphql(async_graphql),
locate(cover(family = "app::Cover", required)),
)]
struct Frame { #[ingraph(id)] at: i64, media: String }
})
.expect("the ruling's own declaration");
assert_eq!(signatures(&model), [true]);
assert!(
model.artifacts[0].data.is_none(),
"the position signed for its ADDRESS and asked for no bytes"
);
}
#[test]
fn a_position_that_signs_nothing_is_nullable() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite,
lineage(fields(media)),
graphql(async_graphql),
locate(card = "app::Card", poster(family = "app::Poster", data)),
)]
struct Frame { #[ingraph(id)] at: i64, media: String }
})
.expect("the shorthand and a full form that signs nothing");
assert_eq!(signatures(&model), [false, false]);
}
#[test]
fn the_signature_belongs_to_the_position_that_wrote_it() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite,
lineage(fields(media)),
graphql(async_graphql),
locate(
card = "app::Card",
cover(family = "app::Cover", required),
poster(family = "app::Poster", data),
),
)]
struct Frame { #[ingraph(id)] at: i64, media: String }
})
.expect("three positions, one signature");
assert_eq!(signatures(&model), [false, true, false]);
}
#[test]
fn the_address_and_the_bytes_are_signed_for_separately() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite,
lineage(fields(media)),
graphql(async_graphql),
locate(
neither(family = "app::Neither", data),
address(family = "app::Address", required, data),
bytes(family = "app::Bytes", data(required)),
both(family = "app::Both", required, data(required)),
),
)]
struct Frame { #[ingraph(id)] at: i64, media: String }
})
.expect("the four combinations of two independent words");
assert_eq!(signatures(&model), [false, true, false, true]);
assert_eq!(
model
.artifacts
.iter()
.map(|artifact| artifact
.data
.as_ref()
.expect("every position here asked")
.required())
.collect::<Vec<_>>(),
[false, false, true, true],
"the ask's own signature follows the ask's own word"
);
}
#[test]
fn refuses_a_signature_that_carries_anything() {
for written in [
quote::quote!(locate(cover(family = "app::Cover", required = true))),
quote::quote!(locate(cover(family = "app::Cover", required("yes")))),
] {
let refused = refusals(&syn::parse_quote! {
#[ingraph(#written)]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(&refused, "`required` is a BARE word")
&& names(&refused, "there is no inverse word either"),
"{written}: {refused:?}"
);
}
}
#[test]
fn refuses_a_repeated_signature() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate(cover(family = "app::Cover", required, required)))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"`required` is declared more than once in an artifact position"
),
"{refused:?}"
);
}
#[test]
fn a_typo_of_the_signature_is_suggested() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate(cover(family = "app::Cover", requird)))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(&refused, "unknown entry in an artifact position")
&& names(&refused, "did you mean `required`"),
"{refused:?}"
);
assert!(
names(&refused, "There is no word for the nullable form"),
"and the one-way law is in the sentence: {refused:?}"
);
}
#[test]
fn the_shorthand_carries_no_words_at_all() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate(cover = "app::Cover", required))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"an artifact position is `<name> = \"path::to::Family\"`"
),
"a bare word in the BLOCK is an entry with no family, not a signature: {refused:?}"
);
}