use crate::{
model::GraphqlKind,
union::{UnionInput, UnionMember, UnionModel, UnionOutput, UnionSkin},
};
fn found(graphql: Vec<UnionSkin>) -> UnionModel {
UnionModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: ident("Found"),
members: vec![
member("Media", syn::parse_quote!(Media)),
member("r#Tape", syn::parse_quote!(media::Tape)),
member("VideoClip", syn::parse_quote!(Vec<u8>)),
],
error: None,
graphql,
}
}
fn member(name: &str, ty: syn::Type) -> UnionMember {
UnionMember {
ident: ident(name),
ty,
}
}
fn ident(name: &str) -> syn::Ident {
syn::parse_str(name).expect("an identifier")
}
fn skin(output: bool, input: Option<UnionInput>) -> UnionSkin {
UnionSkin {
kind: GraphqlKind::AsyncGraphql,
output: output.then(UnionOutput::default),
input,
}
}
fn renamed(word: &str) -> UnionInput {
UnionInput {
rename: Some(syn::parse_quote!(#word)),
}
}
fn rendered(model: &UnionModel) -> String {
model.graphql_faces().to_string()
}
#[test]
fn a_union_with_no_backend_emits_nothing() {
assert!(rendered(&found(Vec::new())).is_empty());
}
#[test]
fn the_whole_wire_half_is_behind_the_binding_gate() {
let model = found(vec![skin(true, Some(UnionInput::default()))]);
let file: syn::File = syn::parse2(model.graphql_faces()).expect("the face parses");
let syn::Item::Const(anonymous) = file.items.first().expect("one anonymous const per backend")
else {
panic!("the face is an anonymous const");
};
assert_eq!(anonymous.ident, "_");
let rendered = rendered(&model);
assert_eq!(
rendered.matches("if_async_graphql").count(),
1,
"one gate, wrapping the block's whole body: {rendered}"
);
assert!(
rendered.contains(":: ingraph :: __private :: gate :: if_async_graphql"),
"and it is reached through the shelf: {rendered}"
);
}
#[test]
fn every_library_name_is_shelved_and_no_attribute_is_written() {
let rendered = rendered(&found(vec![skin(true, Some(UnionInput::default()))]));
assert!(
!rendered.contains(":: async_graphql")
|| rendered.contains(":: ingraph :: __private :: async_graphql"),
"{rendered}"
);
assert_eq!(
rendered.matches(":: async_graphql").count(),
rendered
.matches(":: ingraph :: __private :: async_graphql")
.count(),
"every mention of the library is the shelved one: {rendered}"
);
assert!(
!rendered.contains("# [:: ingraph :: __private :: async_graphql"),
"no async-graphql attribute is emitted, so none can be missing its `crate` key: {rendered}"
);
}
#[test]
fn a_single_face_publishes_the_unions_own_name() {
let output = skin(true, None);
assert_eq!(output.output_sdl(&ident("Found")), "Found");
let input = skin(false, Some(UnionInput::default()));
assert_eq!(input.input_sdl(&ident("Found")), "Found");
let rendered = rendered(&found(vec![input]));
assert!(rendered.contains(r#"Borrowed ("Found")"#), "{rendered}");
assert!(!rendered.contains("FoundInput"), "{rendered}");
}
#[test]
fn both_faces_split_the_name_between_them() {
let both = skin(true, Some(UnionInput::default()));
assert_eq!(both.output_sdl(&ident("Found")), "Found");
assert_eq!(both.input_sdl(&ident("Found")), "FoundInput");
let rendered = rendered(&found(vec![both]));
assert!(rendered.contains(r#"Borrowed ("Found")"#), "{rendered}");
assert!(
rendered.contains(r#"Borrowed ("FoundInput")"#),
"{rendered}"
);
assert!(rendered.contains(r#"MetaType :: Union"#), "{rendered}");
assert!(
rendered.contains(r#"MetaType :: InputObject"#),
"{rendered}"
);
}
#[test]
fn the_rename_moves_the_input_name_whether_or_not_the_output_is_rendered() {
assert_eq!(
skin(true, Some(renamed("FoundChoice"))).input_sdl(&ident("Found")),
"FoundChoice"
);
assert_eq!(
skin(false, Some(renamed("FoundChoice"))).input_sdl(&ident("Found")),
"FoundChoice"
);
let rendered = rendered(&found(vec![skin(true, Some(renamed("FoundChoice")))]));
assert!(
rendered.contains(r#"Borrowed ("FoundChoice")"#),
"{rendered}"
);
assert!(!rendered.contains("FoundInput"), "{rendered}");
}
#[test]
fn a_raw_union_publishes_the_word_without_its_escape() {
let mut model = found(vec![skin(true, Some(UnionInput::default()))]);
model.name = ident("r#type");
let both = &model.graphql[0];
assert_eq!(both.output_sdl(&model.name), "type");
assert_eq!(both.input_sdl(&model.name), "typeInput");
assert!(
!rendered(&model).contains("r#type\""),
"{}",
rendered(&model)
);
}
#[test]
fn the_input_face_seats_every_member_under_its_own_camel_word() {
let rendered = rendered(&found(vec![skin(false, Some(UnionInput::default()))]));
for word in ["media", "tape", "videoClip"] {
assert_eq!(
rendered.matches(&format!(r#""{word}""#)).count(),
5,
"`{word}` is the seat, its registry name, its two reads and its write: {rendered}"
);
}
assert!(rendered.contains("oneof : true"), "{rendered}");
assert!(
rendered.contains("OneofObjectType for Found"),
"the marker beside `InputType`, which is what a `@oneOf` carries: {rendered}"
);
}
#[test]
fn the_input_seats_are_reached_through_a_marker_that_names_the_refusal() {
let rendered = rendered(&found(vec![skin(true, Some(UnionInput::default()))]));
assert!(
rendered.contains("diagnostic :: on_unimplemented"),
"{rendered}"
);
assert!(
rendered.contains("diagnostic :: do_not_recommend"),
"{rendered}"
);
assert!(
rendered.contains("is a member of `Found`, which declares a GraphQL INPUT face"),
"the sentence names the union: {rendered}"
);
assert_eq!(
rendered.matches("__IngraphSeat").count(),
2 + 3 * 3,
"every member mention goes through the marker: {rendered}"
);
for member in ["Media", "media :: Tape", "Vec < u8 >"] {
assert!(
!rendered.contains(&format!(
"< {member} as :: ingraph :: __private :: async_graphql :: InputType >"
)),
"no member is named beside the library's own trait, or the refusal would be its sentence \
rather than this framework's: {rendered}"
);
}
}
#[test]
fn an_output_only_union_declares_no_seat_marker() {
let rendered = rendered(&found(vec![skin(true, None)]));
assert!(!rendered.contains("__IngraphSeat"), "{rendered}");
assert!(!rendered.contains("InputType"), "{rendered}");
assert!(rendered.contains("UnionType for Found"), "{rendered}");
}
#[test]
fn the_output_face_answers_every_question_off_the_member() {
let rendered = rendered(&found(vec![skin(true, None)]));
assert!(
rendered.contains("ContainerType :: collect_all_fields (__ingraph_arm"),
"{rendered}"
);
assert_eq!(
rendered.matches("OutputType > :: type_name ()").count(),
3 * 2,
"{rendered}"
);
assert_eq!(
rendered.matches("OutputType > :: create_type_info").count(),
3,
"every arm is registered, so a schema reaching the union reaches its members: {rendered}"
);
}