use super::{ResourceKind, ResourceMode, ResourceModel};
use crate::prefix::EmissionPrefix;
fn foreign(mode: ResourceMode) -> ResourceModel {
ResourceModel {
prefix: EmissionPrefix::default(),
krate: syn::parse_quote!(::ingraph),
name: syn::parse_quote!(MyAnn),
kind: None,
mode,
}
}
fn tantivy() -> ResourceModel {
ResourceModel {
prefix: EmissionPrefix::default(),
krate: syn::parse_quote!(::ingraph),
name: syn::parse_quote!(Ft),
kind: Some(ResourceKind::Tantivy),
mode: ResourceKind::Tantivy.mode(),
}
}
fn compact(emitted: &proc_macro2::TokenStream) -> String {
render(emitted).split_whitespace().collect()
}
fn render(emitted: &proc_macro2::TokenStream) -> String {
prettyplease::unparse(&syn::parse2(emitted.clone()).expect("the expansion parses as a file"))
}
#[test]
fn the_declaration_records_which_face_its_engine_wears() {
assert!(
compact(&foreign(ResourceMode::Block).emit()).contains("constSYNC:bool=true;"),
"{}",
render(&foreign(ResourceMode::Block).emit())
);
assert!(
compact(&foreign(ResourceMode::Async).emit()).contains("constSYNC:bool=false;"),
"{}",
render(&foreign(ResourceMode::Async).emit())
);
}
#[test]
fn the_foreign_form_emits_no_engine_impl() {
let rendered = render(&foreign(ResourceMode::Async).emit());
assert_eq!(
rendered
.lines()
.filter(|line| line.contains("impl"))
.count(),
1,
"{rendered}"
);
assert!(!rendered.contains("Fulltext"), "{rendered}");
assert!(!rendered.contains("AsyncResource"), "{rendered}");
}
#[test]
fn only_a_kind_word_mints_a_field() {
assert!(foreign(ResourceMode::Block).carried_engine().is_none());
let field = tantivy().carried_engine().expect("the kind word mints one");
let rendered = quote::ToTokens::to_token_stream(&field)
.to_string()
.split_whitespace()
.collect::<String>();
assert_eq!(
rendered, "__ig_engine__:::ingraph::__private::store::TantivyStore",
"the injected field is the engine, through the shelf"
);
}
#[test]
fn the_kind_word_emits_the_construction_the_base_and_the_factory() {
let emitted = tantivy().emit();
let compact = compact(&emitted);
for expected in [
"impl::ingraph::__private::resource::AsyncResourceforFt",
"impl::ingraph::__private::resource::DeclaredResourceforFt",
"impl::ingraph::__private::store::FulltextEngineforFt",
"impl::ingraph::__private::store::FulltextStoreforFt",
"implFt",
] {
assert!(
compact.contains(expected),
"the expansion is missing `{expected}`: {}",
render(&emitted)
);
}
}
#[test]
fn the_mount_wears_the_factory_and_not_the_faces() {
let rendered = render(&tantivy().emit());
for face in [
"FulltextRead",
"FulltextWrite",
"AsyncFulltextRead",
"AsyncFulltextWrite",
] {
assert!(
!rendered.contains(&format!("{face} for Ft")),
"the mount claimed `{face}`, which has nowhere to take a slot name: {rendered}"
);
}
}
#[test]
fn a_blocking_kind_word_is_given_no_awaited_face() {
let rendered = render(&tantivy().emit());
assert!(
!rendered.contains("AsyncFulltextStore"),
"a blocking store claimed an awaited construction face: {rendered}"
);
assert!(
compact(&tantivy().emit()).contains("constSYNC:bool=true;"),
"and the record says so: {rendered}"
);
}
#[test]
fn the_base_delegates_every_seat_rather_than_naming_a_type() {
let compact = compact(&tantivy().emit());
for seat in [
"Options",
"SearchOptions",
"Error",
"Score",
"Address",
"Connection",
"WriteBuffer",
"ReadBuffer",
] {
assert!(
compact.contains(&format!(
"type{seat}=<::ingraph::__private::store::TantivyFulltextas::ingraph::__private::store::FulltextEngine>::{seat};"
)),
"the `{seat}` seat is not projected: {}",
render(&tantivy().emit())
);
}
}
#[test]
fn construction_is_the_stores_own() {
let compact = compact(&tantivy().emit());
assert!(
compact.contains(
"typeOptions=<::ingraph::__private::store::TantivyStoreas::ingraph::__private::store::FulltextStore>::Options;"
),
"{}",
render(&tantivy().emit())
);
assert!(
compact.contains(
"<::ingraph::__private::store::TantivyStoreas::ingraph::__private::store::FulltextStore>::open("
),
"{}",
render(&tantivy().emit())
);
assert!(
compact.contains(".map(|__ig_engine__|Self{__ig_engine__})"),
"the opened store lands in the field the injection minted: {}",
render(&tantivy().emit())
);
}
#[test]
fn the_factory_takes_the_corpus_name_and_mints_none() {
let emitted = tantivy().emit();
let compact = compact(&emitted);
assert!(
compact.contains(
"fnfulltext(&self,slot:impl::core::convert::Into<::ingraph::__private::store::Utf8Bytes>,)"
),
"the corpus name is not a parameter: {}",
render(&emitted)
);
assert!(
!compact.contains("constSLOT"),
"the expansion mints a corpus name of its own: {}",
render(&emitted)
);
assert!(
compact.contains("pubfnconnection(&self)->&::ingraph::__private::store::TantivyStore"),
"{}",
render(&emitted)
);
}
#[test]
fn a_kind_words_expansion_rides_its_engines_gate() {
let plain = quote::quote!(
pub struct Ft;
);
let carrying = quote::quote!(
pub struct Ft {
__ig_engine__: Store,
}
);
let expanded = tantivy().expand(&plain, &carrying).to_string();
assert!(
expanded.contains(":: ingraph :: __private :: gate :: if_tantivy !"),
"{expanded}"
);
assert!(
expanded.contains(":: ingraph :: __private :: gate :: if_no_tantivy !"),
"{expanded}"
);
assert!(expanded.contains("compile_error !"), "{expanded}");
assert!(
expanded.contains("`tantivy` feature"),
"the shut half names the feature: {expanded}"
);
assert!(
expanded.matches("struct Ft").count() == 2,
"the declaration survives whichever half is open: {expanded}"
);
}
#[test]
fn the_foreign_form_is_the_host_and_the_citizenship() {
let plain = quote::quote!(
pub struct MyAnn(MyAnnEngine);
);
let expanded = foreign(ResourceMode::Block)
.expand(&plain, "e::quote!(unreachable))
.to_string();
assert!(!expanded.contains("gate"), "{expanded}");
assert!(!expanded.contains("unreachable"), "{expanded}");
assert!(expanded.contains("struct MyAnn"), "{expanded}");
}
#[test]
fn every_framework_path_a_kind_word_names_goes_through_the_shelf() {
let emitted = tantivy().emit();
let compact = compact(&emitted);
for path in compact.match_indices("::ingraph::").map(|(at, _)| at) {
assert!(
compact[path..].starts_with("::ingraph::__private::"),
"a framework path skips the shelf: {}",
render(&emitted)
);
}
}
#[test]
fn a_renamed_crate_moves_a_kind_words_paths() {
let model = ResourceModel {
prefix: EmissionPrefix::default(),
krate: syn::parse_quote!(crate::shim),
name: syn::parse_quote!(Ft),
kind: Some(ResourceKind::Tantivy),
mode: ResourceMode::Block,
};
let expanded = model
.expand(
"e::quote!(
pub struct Ft;
),
"e::quote!(
pub struct Ft {}
),
)
.to_string();
assert!(
expanded.contains("crate :: shim :: __private :: gate :: if_tantivy !"),
"{expanded}"
);
assert!(!expanded.contains(":: ingraph ::"), "{expanded}");
}
#[test]
fn every_kind_word_resolves_to_the_kind_that_spells_it() {
for word in ResourceKind::WORDS {
let kind = ResourceKind::from_word(word).expect("a listed word names a kind");
assert_eq!(kind.word(), *word);
}
assert!(ResourceKind::from_word("lance").is_none());
}
#[test]
fn a_platform_mark_moves_the_injected_engine_field() {
let mut model = tantivy();
model.prefix = EmissionPrefix::new("__mg");
let field = model
.carried_engine()
.expect("a builtin kind injects its engine");
assert_eq!(
field.ident.as_ref().expect("a named field").to_string(),
"__mg_engine__",
);
let rendered = compact(&model.emit());
assert!(
rendered.contains("__mg_engine__"),
"the bodies read the moved field"
);
assert!(
!rendered.contains("__ig_engine__"),
"and no body still reads `ingraph`'s spelling of it",
);
}