use ingraph_codegen::resource::{ResourceKind, ResourceMode};
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_bare_declaration_is_foreign_and_blocking() {
let model = accepted(quote::quote! {
pub struct MyAnn(MyAnnEngine);
});
assert_eq!(model.kind, None);
assert_eq!(model.mode, ResourceMode::Block);
}
#[test]
fn the_async_word_puts_a_foreign_resource_on_the_awaited_face() {
let model = accepted(quote::quote! {
#[ingraph(async)]
pub struct MyRemote(RemoteQdrant);
});
assert_eq!(model.kind, None);
assert_eq!(model.mode, ResourceMode::Async);
}
#[test]
fn a_kind_word_names_a_builtin_engine() {
let model = accepted(quote::quote! {
#[ingraph(tantivy)]
pub struct Ft;
});
assert_eq!(model.kind, Some(ResourceKind::Tantivy));
}
#[test]
fn a_kind_word_carries_its_own_mode() {
let model = accepted(quote::quote! {
#[ingraph(tantivy)]
pub struct Ft;
});
assert_eq!(
model.mode,
ResourceMode::Block,
"tantivy is a synchronous engine, and the word knows it"
);
}
#[test]
fn a_builtin_takes_no_second_opinion_about_its_mode() {
let refusal = refused(quote::quote! {
#[ingraph(tantivy, async)]
pub struct Ft;
});
assert!(
refusal.contains("already knows which face"),
"the refusal names the rule: {refusal}"
);
assert!(
refusal.contains("FOREIGN form"),
"and says whose word it is: {refusal}"
);
}
#[test]
fn a_builtin_declaration_holds_nothing_of_its_own() {
for declaration in [
quote::quote! {
#[ingraph(tantivy)]
pub struct Ft(TantivyStore);
},
quote::quote! {
#[ingraph(tantivy)]
pub struct Ft { store: TantivyStore }
},
quote::quote! {
#[ingraph(tantivy)]
pub enum Ft { One }
},
] {
let refusal = refused(declaration);
assert!(
refusal.contains("MINTS this resource's engine"),
"the refusal names the rule: {refusal}"
);
assert!(
refusal.contains("FOREIGN form"),
"and names the form that wants the inner type: {refusal}"
);
}
}
#[test]
fn the_foreign_form_still_admits_every_shape() {
for declaration in [
quote::quote!(
pub struct MyAnn;
),
quote::quote!(
pub struct MyAnn(MyAnnEngine);
),
quote::quote! { pub struct MyAnn { engine: MyAnnEngine } },
quote::quote! { pub enum Backend { Cpu, Metal } },
] {
assert_eq!(accepted(declaration).kind, None);
}
}
#[test]
fn an_unknown_kind_word_is_refused_by_the_list() {
let refusal = refused(quote::quote! {
#[ingraph(lance)]
pub struct VectorsA;
});
assert!(
refusal.contains("BUILTIN KIND WORD"),
"the refusal names the axis: {refusal}"
);
for word in ResourceKind::WORDS {
assert!(
refusal.contains(word),
"and lists `{word}`, which this build knows: {refusal}"
);
}
}
#[test]
fn neither_word_is_configured() {
let refusal = refused(quote::quote! {
#[ingraph(tantivy(heap = 200))]
pub struct Ft;
});
assert!(
refusal.contains("bare word"),
"the kind word takes nothing: {refusal}"
);
assert!(
refusal.contains("[resources."),
"and says where an engine's knobs go: {refusal}"
);
let refusal = refused(quote::quote! {
#[ingraph(async = "true")]
pub struct MyRemote;
});
assert!(
refusal.contains("bare word"),
"the mode word takes nothing either: {refusal}"
);
}
#[test]
fn neither_word_is_written_twice() {
let refusal = refused(quote::quote! {
#[ingraph(tantivy, tantivy)]
pub struct Ft;
});
assert!(refusal.contains("one resource is one engine"), "{refusal}");
let refusal = refused(quote::quote! {
#[ingraph(async, async)]
pub struct MyRemote;
});
assert!(
refusal.contains("one declaration wears one face"),
"{refusal}"
);
}
#[test]
fn a_kind_word_composes_with_the_crate_word() {
let model = accepted(quote::quote! {
#[ingraph(crate = "crate::shim", tantivy)]
pub struct Ft;
});
assert_eq!(model.kind, Some(ResourceKind::Tantivy));
assert_eq!(
quote::ToTokens::to_token_stream(&model.krate).to_string(),
"crate :: shim"
);
}