use ingraph_codegen::prelude::*;
use quote::ToTokens as _;
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 artifacts(model: &RecordModel) -> Vec<(String, String)> {
model
.artifacts
.iter()
.map(|artifact| (artifact.name.to_string(), rendered(&artifact.family)))
.collect()
}
fn rendered(path: &syn::Path) -> String {
path.to_token_stream().to_string().replace(' ', "")
}
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 a_locate_block_reaches_the_model_as_its_families() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite,
lineage(fields(media)),
graphql(async_graphql),
derived(tracks = "app::Track"),
locate(card = "app::Card", poster = "app::Poster"),
)]
struct Frame { #[ingraph(id)] at: i64, media: String }
})
.expect("the ruling's own declaration is the one this word exists for");
assert_eq!(
artifacts(&model),
[
("card".to_owned(), "app::Card".to_owned()),
("poster".to_owned(), "app::Poster".to_owned()),
],
"one entry per artifact, several allowed, in the order they were written"
);
assert_eq!(
model
.derived
.iter()
.map(|position| position.name.to_string())
.collect::<Vec<_>>(),
["tracks"],
"the reverse container beside it is untouched"
);
assert!(
model.graphql.is_some(),
"and the wire face is still declared"
);
assert_eq!(
model
.lineage
.iter()
.flat_map(|lineage| lineage.path().iter())
.map(|column| column.name.to_string())
.collect::<Vec<_>>(),
["media"],
"and the lineage still names its field"
);
}
#[test]
fn a_record_that_wrote_no_word_locates_nothing() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite,
lineage(fields(media)),
graphql(async_graphql),
derived(tracks = "app::Track"),
)]
struct Frame { #[ingraph(id)] at: i64, media: String }
})
.expect("a record with no artifact family is spelled by omitting the block");
assert!(
model.artifacts.is_empty(),
"absence by omission, with no sentinel"
);
assert_eq!(
model
.derived
.iter()
.map(|position| position.name.to_string())
.collect::<Vec<_>>(),
["tracks"],
"and the sibling that WAS written is still read"
);
assert!(model.graphql.is_some(), "the wire face too");
assert_eq!(
model
.lineage
.iter()
.flat_map(|lineage| lineage.path().iter())
.count(),
1,
"and the lineage"
);
}
#[test]
fn one_family_is_a_whole_block_without_the_sibling() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media)), graphql(async_graphql), locate(thumb = "app::Thumbnail"))]
struct Frame { #[ingraph(id)] at: i64, media: String }
})
.expect("one artifact family is a declaration");
assert_eq!(
artifacts(&model),
[("thumb".to_owned(), "app::Thumbnail".to_owned())]
);
assert!(
model.derived.is_empty(),
"the sibling was not written, and the word did not write one"
);
assert!(model.graphql.is_some(), "the wire face is still declared");
assert!(model.lineage.is_some(), "and so is the lineage");
}
#[test]
fn refuses_a_repeated_family_name() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate(card = "app::Card", card = "app::Poster"))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"`card` is declared more than once in `locate(…)` — one position, one name"
),
"the refusal counts the word: {refused:?}"
);
}
#[test]
fn refuses_a_second_locate_block() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate(card = "app::Card"), locate(poster = "app::Poster"))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"`locate(…)` is declared more than once — one block carries every artifact family"
),
"the refusal is about the block: {refused:?}"
);
}
#[test]
fn refuses_a_family_that_is_not_a_path_in_a_string() {
for written in [
quote::quote!(locate(card = 3)),
quote::quote!(locate(card = "not a path")),
quote::quote!(locate(card)),
] {
let refused = refusals(&syn::parse_quote! {
#[ingraph(#written)]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"an artifact position is `<name> = \"path::to::Family\"`"
) && names(&refused, "the TYPE, not a field inside it"),
"the refusal names the form and the thing the path points at: {refused:?}"
);
}
}
#[test]
fn refuses_a_multi_segment_left_side() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate(app::Card = "app::Card"))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"the left side of an artifact position is a WORD this record mints"
) && names(&refused, "The TYPE goes on the right"),
"the refusal names both sides: {refused:?}"
);
}
#[test]
fn refuses_the_siblings_option_words_by_name() {
for written in [
quote::quote!(locate(card(type = "app::Card"))),
quote::quote!(locate(card(family = "app::Card", field = "at"))),
] {
let refused = refusals(&syn::parse_quote! {
#[ingraph(#written)]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"`type` and `field` are `derived(…)`'s option words"
) && names(&refused, "no second `ref` column for a `field` to select"),
"the refusal names the words it is refusing and why: {refused:?}"
);
}
}
#[test]
fn refuses_a_locate_block_naming_no_family() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate())]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"`locate(…)` names the artifact families this record answers an address for, and it names \
none"
) && names(&refused, "spelled by omitting the block"),
"the refusal is about the record's families: {refused:?}"
);
}
#[test]
fn refuses_a_family_that_takes_a_fields_name() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locate(media = "app::Card"))]
struct Frame { #[ingraph(id)] at: i64, media: String }
});
assert!(
names(&refused, "`media` is already a field of this record")
&& names(
&refused,
"One namespace per face: rename the artifact, or the field"
),
"the refusal names the half the word was already spent on: {refused:?}"
);
}
#[test]
fn refuses_a_family_that_repeats_a_reverse_position() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(derived(cover = "app::CoverRow"), locate(cover = "app::Cover"))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(
&refused,
"`cover` is already a reverse position this record declares"
) && names(
&refused,
"One namespace per face: rename the artifact, or the reverse position"
),
"the refusal names the sibling: {refused:?}"
);
}
#[test]
fn a_dropped_reverse_position_leaves_its_word_free() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(derived(media = "app::Track"), locate(media = "app::Card"))]
struct Frame { #[ingraph(id)] at: i64, media: String }
});
assert!(
names(&refused, "`media` is already a field of this record"),
"the field collision is reported: {refused:?}"
);
assert!(
!names(&refused, "already a reverse position this record declares"),
"and the dropped position raises no second one: {refused:?}"
);
}
#[test]
fn a_near_miss_of_the_word_is_named() {
let refused = refusals(&syn::parse_quote! {
#[ingraph(locat(card = "app::Card"))]
struct Frame { #[ingraph(id)] at: i64 }
});
assert!(
names(&refused, "did you mean `locate`?"),
"the head's suggestion list carries the word: {refused:?}"
);
assert!(
names(&refused, "`derived(…)`, `locate(…)`"),
"and the sentence listing what a record takes names it beside its sibling: {refused:?}"
);
}