fn parse(input: &syn::DeriveInput) -> syn::Result<ingraph_codegen::resource::ResourceModel> {
let (host, arguments) = crate::host::split(input);
super::parse(&host, &arguments)
}
fn accepted(input: proc_macro2::TokenStream) -> ingraph_codegen::resource::ResourceModel {
parse(&syn::parse2(input).expect("the declaration parses"))
.expect("the declaration is a resource")
}
fn refused(input: proc_macro2::TokenStream) -> String {
parse(&syn::parse2(input).expect("the declaration parses"))
.expect_err("this declaration breaks a rule")
.into_iter()
.map(|error| error.to_string())
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn a_resource_is_its_name_and_the_crate_it_reaches() {
let model = accepted(quote::quote! {
pub struct Egemma {
model: EgemmaModel,
}
});
assert_eq!(model.name, "Egemma");
assert_eq!(
quote::ToTokens::to_token_stream(&model.krate).to_string(),
":: ingraph"
);
}
#[test]
fn a_resource_can_name_the_crate() {
let model = accepted(quote::quote! {
#[ingraph(crate = "crate::shim")]
pub struct Egemma;
});
assert_eq!(
quote::ToTokens::to_token_stream(&model.krate).to_string(),
"crate :: shim"
);
}
#[test]
fn a_resource_may_be_any_shape_at_all() {
assert_eq!(
accepted(quote::quote!(
pub struct Egemma;
))
.name,
"Egemma"
);
assert_eq!(
accepted(quote::quote!(
pub struct Egemma(EgemmaModel);
))
.name,
"Egemma"
);
assert_eq!(
accepted(quote::quote! {
pub enum Backend {
Cpu,
Metal(Device),
}
})
.name,
"Backend"
);
}
#[test]
fn a_resource_takes_no_generic_parameter() {
let refusal = refused(quote::quote! {
pub struct Model<B> {
backend: B,
}
});
assert!(
refusal.contains("no generic parameter"),
"the refusal names the rule: {refusal}"
);
assert!(
refusal.contains("citizen"),
"and says what the registration is: {refusal}"
);
}
#[test]
fn the_options_are_the_attributes_arguments() {
let refusal = refused(quote::quote! {
#[ingraph(crate = "crate::shim")]
#[ingraph(crate = "crate::shim")]
pub struct Egemma;
});
assert!(
refusal.contains("declared more than once"),
"a repeated `crate` is refused: {refusal}"
);
}
#[test]
fn an_unknown_entry_names_where_the_fact_belongs() {
let refusal = refused(quote::quote! {
#[ingraph(options = "EgemmaOptions")]
pub struct Egemma;
});
assert!(
refusal.contains("`AsyncResource` impl"),
"the refusal points at the contract: {refusal}"
);
assert!(
refusal.contains("launch"),
"and at the launch, for the options' source: {refusal}"
);
}