use std::collections::BTreeMap;
use quote::ToTokens as _;
use crate::enumeration::{EnumModel, EnumVariant, Namespace};
fn codec() -> EnumModel {
EnumModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: ident("Codec"),
width: None,
variants: vec![renamed("Vp9", "VP_9"), variant("H264"), variant("r#Tape")],
forms: BTreeMap::new(),
wire_name: None,
error: None,
remote: None,
}
}
fn variant(name: &str) -> EnumVariant {
EnumVariant {
ident: ident(name),
rename: None,
names: BTreeMap::new(),
upstream: None,
escape: None,
carrier: None,
}
}
fn renamed(name: &str, wire: &str) -> EnumVariant {
let mut names = BTreeMap::new();
names.insert(Namespace::Graphql, wire.to_owned());
EnumVariant {
ident: ident(name),
rename: None,
names,
upstream: None,
escape: None,
carrier: None,
}
}
fn ident(name: &str) -> syn::Ident {
syn::parse_str(name).expect("an identifier")
}
fn rendered(model: &EnumModel) -> String {
model.graphql_face().to_string()
}
fn face(model: &EnumModel) -> syn::File {
syn::parse2(model.graphql_face()).expect("the face parses")
}
#[test]
fn the_whole_face_is_behind_the_binding_gate() {
let file = face(&codec());
let syn::Item::Const(anonymous) = file.items.first().expect("the face is one anonymous const")
else {
panic!("the face is one anonymous const");
};
assert_eq!(anonymous.ident, "_");
assert_eq!(file.items.len(), 1, "and nothing stands beside it");
let rendered = rendered(&codec());
assert!(
rendered.contains("__private :: gate :: if_async_graphql"),
"{rendered}"
);
assert_eq!(
rendered.matches("if_async_graphql").count(),
1,
"{rendered}"
);
}
#[test]
fn the_values_shout_and_a_rename_moves_one() {
let rendered = rendered(&codec());
for word in ["\"VP_9\"", "\"H264\"", "\"TAPE\""] {
assert!(rendered.contains(word), "`{word}` is missing:\n{rendered}");
}
assert!(
!rendered.contains("\"VP9\""),
"the rename did not move the value:\n{rendered}"
);
assert!(
!rendered.contains("\"r#Tape\""),
"the raw escape is Rust's and no part of the name:\n{rendered}"
);
}
#[test]
fn the_type_publishes_the_enums_own_name() {
let mut model = codec();
model.name = ident("r#Match");
let rendered = rendered(&model);
assert!(rendered.contains("\"Match\""), "{rendered}");
assert!(!rendered.contains("\"r#Match\""), "{rendered}");
}
#[test]
fn every_description_is_none() {
let rendered = rendered(&codec());
assert_eq!(
rendered
.matches("description : :: core :: option :: Option :: None")
.count(),
4,
"one for the type and one for each of the three values:\n{rendered}"
);
assert!(
!rendered.contains("get_rustdoc") && !rendered.contains("doc ="),
"{rendered}"
);
}
#[test]
fn the_face_carries_no_backend_attribute() {
let attributes: Vec<String> = face(&codec())
.into_token_stream()
.to_string()
.split("# [")
.skip(1)
.filter_map(|tail| tail.split(']').next().map(str::to_owned))
.collect();
assert!(
attributes
.iter()
.all(|attribute| attribute == "automatically_derived" || attribute == "inline"),
"{attributes:?}"
);
}
#[test]
fn every_framework_path_goes_through_the_shelf() {
let rendered = rendered(&codec());
assert!(
!rendered.contains(":: async_graphql :: async_graphql"),
"{rendered}"
);
assert_eq!(
rendered.matches(":: async_graphql").count(),
rendered
.matches(":: ingraph :: __private :: async_graphql")
.count(),
"an async-graphql name escaped the shelf:\n{rendered}"
);
}
#[test]
fn the_face_rides_the_one_emission_inside_the_gate() {
let emitted = codec().emit().to_string();
assert!(emitted.contains("if_async_graphql"), "{emitted}");
let (before, gated) = emitted
.split_once("if_async_graphql")
.expect("the gate is in the emission");
for face in ["InputType", "EnumItem"] {
assert!(gated.contains(face), "`{face}` is missing:\n{emitted}");
assert!(
!before.contains(face),
"`{face}` escaped the gate:\n{emitted}"
);
}
}
#[test]
fn the_shelf_follows_the_declared_crate() {
let mut model = codec();
model.krate = syn::parse_quote!(crate::shim);
let rendered = rendered(&model);
assert!(
rendered.contains("crate :: shim :: __private :: async_graphql"),
"{rendered}"
);
assert!(!rendered.contains(":: ingraph :: __private"), "{rendered}");
}
#[test]
fn the_output_leg_is_emitted_as_an_async_fn() {
let rendered = rendered(&codec());
assert!(
rendered.contains(":: async_graphql :: OutputType"),
"{rendered}"
);
assert!(
rendered.contains("async fn resolve"),
"the output leg takes the RPITIT road:\n{rendered}"
);
assert!(
rendered.contains(":: async_graphql :: resolver_utils :: enum_value"),
"the value a resolver renders comes off the same table the parse reads:\n{rendered}"
);
}
#[test]
fn the_output_face_registers_through_the_input_one() {
let rendered = rendered(&codec());
assert_eq!(
rendered.matches("create_input_type").count(),
1,
"one registration, reached from both positions:\n{rendered}"
);
assert_eq!(
rendered.matches("MetaTypeId :: Enum").count(),
1,
"{rendered}"
);
}
#[test]
fn the_output_crossing_is_emitted_for_the_value() {
let rendered = rendered(&codec());
assert!(
rendered.contains(":: graphql :: ToGraphqlOutput for Codec"),
"the value's row:\n{rendered}"
);
assert!(
!rendered.contains("VecToGraphqlOutput"),
"the retired element-keyed trait is named nowhere:\n{rendered}"
);
assert!(
rendered.contains("type Output < '__ingraph > = & '__ingraph Self"),
"a vocabulary crosses as itself, by reference:\n{rendered}"
);
assert!(
!rendered.contains("type Output < '__ingraph > = & '__ingraph [Self]"),
"and nothing here states what a collection of them crosses as:\n{rendered}"
);
}