use ingraph_codegen::prelude::*;
use crate::{host, parse::parse};
fn refused(input: &syn::DeriveInput) -> String {
let (host, options) = host::split(input);
parse(&host, &options, NodeKind::Record)
.err()
.map(|error| {
error
.into_iter()
.map(|error| error.to_string())
.collect::<Vec<_>>()
.join(" | ")
})
.unwrap_or_else(|| panic!("this declaration is refused"))
}
#[test]
fn the_label_spelling_teaches_the_new_seat() {
let message = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(string, sortable))] tags: Vec<String>,
}
});
assert!(
message.contains("is the FIELD's own word now")
&& message.contains("#[ingraph(list(scalar), sortable)]"),
"the refusal names the seat and the spelling: {message}"
);
}
#[test]
fn every_label_seat_gets_the_one_sentence() {
for declaration in [
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(sortable, string))] tags: Vec<String>,
}
},
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(set(string, sortable))] notes: Vec<String>,
}
},
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(string(sortable))] title: String,
}
},
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(sortable))] tags: Vec<String>,
}
},
] {
let declaration: syn::DeriveInput = declaration;
let message = refused(&declaration);
assert!(
message.contains("is the FIELD's own word now"),
"the label's every seat teaches one move: {message}"
);
}
}
#[test]
fn a_database_block_refuses_the_word_into_the_common_scope() {
let message = refused(&syn::parse_quote! {
#[ingraph(postgres, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), postgres(sortable))] marks: Vec<i64>,
}
});
assert!(
message.contains("ONE intent for every database")
&& message.contains("#[ingraph(sortable, postgres(…))]"),
"the refusal names the dialect-free seat: {message}"
);
}
#[test]
fn a_key_side_column_is_refused_the_word() {
let message = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id, sortable)] id: u128,
#[ingraph(list(string))] tags: Vec<String>,
}
});
assert!(
message.contains("a KEY-SIDE column declares no label at all"),
"the refusal names what is missing: {message}"
);
}
#[test]
fn an_unlabelled_data_field_keeps_the_one_sentence() {
let message = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(sortable)] tags: Vec<String>,
}
});
assert!(
message.contains("`sortable` orders a COLLECTION")
&& message.contains("this column names no family at all")
&& message.contains("`#[ingraph(list(scalar), sortable)]`"),
"the word with nothing to order names what would give it something: {message}"
);
assert!(
!message.contains("carries a face and declares no KIND"),
"and the rule it deferred to is gone rather than silent: {message}"
);
assert!(
!message.contains("a KEY-SIDE column declares no label"),
"and it is this field's own sentence — the key-side one is TRUE and about another field, so \
reusing it here would tell an author looking at a `Vec<String>` a rule they are not breaking: \
{message}"
);
}
#[test]
fn a_path_marker_cannot_carry_the_word() {
let message = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(label = "their::RollMarker", sortable)] roll: Roll<Tag>,
}
});
assert!(
message.contains("a path is a name here, not a family"),
"the refusal says why a path cannot answer: {message}"
);
}
#[test]
fn the_marker_is_what_it_was_before_the_word_moved() {
let markers = |declaration: syn::DeriveInput| {
let (host, options) = host::split(&declaration);
let model = parse(&host, &options, NodeKind::Record).expect("a legal declaration");
format!(
"{:?}",
model
.fields
.iter()
.map(|field| &field.marker)
.collect::<Vec<_>>()
)
};
let asked = markers(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(string), sortable)] tags: Vec<String>,
}
});
let plain = markers(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(string))] tags: Vec<String>,
}
});
assert_eq!(asked, plain, "the storage word reaches no interpretation");
}