use ingraph_codegen::prelude::*;
fn record(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, NodeKind::Record)
}
fn addressed(block: proc_macro2::TokenStream) -> syn::Result<RecordModel> {
record(&syn::parse_quote! {
#[ingraph(table = "shots", sqlite, lineage(fields(reel_id)), graphql(async_graphql, #block))]
struct Shot {
#[ingraph(id)]
id: String,
reel_id: String,
body: String,
}
})
}
fn rooted(block: proc_macro2::TokenStream) -> syn::Result<RecordModel> {
record(&syn::parse_quote! {
#[ingraph(table = "shots", sqlite, graphql(async_graphql, #block))]
struct Shot {
#[ingraph(id)]
id: String,
body: String,
}
})
}
fn seats(model: &RecordModel) -> (String, Option<String>, Option<String>) {
let graphql = model.graphql.as_ref().expect("a wire face");
(
graphql.key_output.to_string(),
graphql.key_object.clone(),
graphql.key_argument.clone(),
)
}
fn refusal(block: proc_macro2::TokenStream) -> String {
addressed(block)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join(" | ")
}
#[test]
fn the_base_and_the_two_faces_each_fill_their_own_slot() {
let model = addressed(quote::quote! {
key(rename = "Address", input(rename = "AddressIn"), output(rename = "AddressM"))
})
.expect("the ruling's canonical spelling");
assert_eq!(
seats(&model),
(
"Address".to_owned(),
Some("AddressM".to_owned()),
Some("AddressIn".to_owned()),
),
);
}
#[test]
fn a_face_may_be_named_with_no_base_above_it() {
let model = addressed(quote::quote! { key(output(rename = "AddressM")) })
.expect("one member is a whole declaration");
assert_eq!(
seats(&model),
(
"ShotKeyOutput".to_owned(),
Some("AddressM".to_owned()),
None,
),
"the base slot took a default it was not given",
);
let model = addressed(quote::quote! { key(input(rename = "AddressIn")) })
.expect("its twin, by the same reading");
assert_eq!(
seats(&model),
(
"ShotKeyOutput".to_owned(),
None,
Some("AddressIn".to_owned()),
),
);
}
#[test]
fn the_base_alone_names_the_rust_type_and_leaves_both_members_empty() {
let model = addressed(quote::quote! { key(rename = "Address") }).expect("the shipped spelling");
assert_eq!(seats(&model), ("Address".to_owned(), None, None));
}
#[test]
fn a_member_takes_a_keyword_and_the_base_refuses_one() {
let model = addressed(quote::quote! { key(output(rename = "type")) })
.expect("a schema type may be called `type`");
assert_eq!(seats(&model).1, Some("type".to_owned()));
let refused = refusal(quote::quote! { key(rename = "type") });
assert!(
refused.contains("must be a valid identifier"),
"the base is a Rust name and is fenced as one: {refused}",
);
}
#[test]
fn a_member_is_held_to_the_wire_alphabet() {
for block in [
quote::quote! { key(output(rename = "9lives")) },
quote::quote! { key(input(rename = "9lives")) },
] {
let refused = refusal(block);
assert!(refused.contains("9lives"), "{refused}");
}
}
#[test]
fn an_unknown_entry_is_measured_against_the_three_seats() {
let refused = refusal(quote::quote! { key(outputs(rename = "AddressM")) });
assert!(refused.contains("unknown entry in `key(…)`"), "{refused}");
assert!(
refused.contains("did you mean `output`"),
"a near miss is measured against the seats: {refused}",
);
assert!(
refused.contains("takes it with `Input` on the end"),
"and the sentence states the one composition, which is what an author needs to predict the \
name they did not write: {refused}",
);
}
#[test]
fn an_empty_block_is_told_what_it_could_have_said() {
for block in [quote::quote! { key }, quote::quote! { key() }] {
let refused = refusal(block);
assert!(refused.contains("`key(…)` names the ADDRESS"), "{refused}");
assert!(refused.contains("output(rename"), "{refused}");
assert!(refused.contains("input(rename"), "{refused}");
}
}
#[test]
fn each_seat_is_named_once() {
for (block, sentence) in [
(
quote::quote! { key(rename = "A", rename = "B") },
"declared more than once in `key(…)`",
),
(
quote::quote! { key(output(rename = "A"), output(rename = "B")) },
"declared more than once in `output(…)`",
),
(
quote::quote! { key(input(rename = "A"), input(rename = "B")) },
"declared more than once in `input(…)`",
),
] {
let refused = refusal(block);
assert!(refused.contains(sentence), "{refused}");
}
}
#[test]
fn the_single_faced_blocks_still_take_a_rename_and_nothing_else() {
for what in ["filter", "order_by"] {
let entry: proc_macro2::TokenStream = format!(r#"{what}(output(rename = "X"))"#)
.parse()
.expect("a block");
let refused = refusal(entry);
assert!(
refused.contains(&format!("unknown entry in `{what}(…)`")),
"{refused}",
);
assert!(
refused.contains("it takes `rename = \"…\"` and nothing else"),
"{refused}",
);
}
}
#[test]
fn a_root_has_no_sending_face_for_a_word_to_move() {
let refused = rooted(quote::quote! { key(input(rename = "AddressIn")) })
.expect_err("a root mints no argument object")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join(" | ");
assert!(refused.contains("`Shot` is a ROOT"), "{refused}");
assert!(
refused.contains("no `KeyInput` type is minted"),
"the sentence names the type that is absent: {refused}",
);
assert!(
refused.contains("shot(id: …)"),
"and what a client sends instead: {refused}",
);
assert!(
refused.contains("The word opens where the record has a LINEAGE"),
"and the shape it opens on: {refused}",
);
}
#[test]
fn the_reading_face_is_minted_on_every_shape_including_a_root() {
let model = rooted(quote::quote! { key(output(rename = "AddressM")) })
.expect("every record reads its address back out of an object");
assert_eq!(
seats(&model),
(
"ShotKeyOutput".to_owned(),
Some("AddressM".to_owned()),
None,
),
);
rooted(quote::quote! { key(rename = "Address") }).expect("the base names the face that exists");
}