use core::num::NonZeroU32;
use super::SearchModel;
use crate::{
index::Dedup,
search::{ArmModel, MemberModel, SeatModel, SharedBinding, SharedBridge},
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn member(field: &str, ty: &str) -> MemberModel {
MemberModel {
field: ident(field),
ty: syn::parse_str(ty).expect("a member type"),
weight: None,
depth: None,
floor: None,
arms: Vec::new(),
}
}
fn arm(name: &str, tower: &str) -> ArmModel {
ArmModel {
name: ident(name),
tower: syn::parse_str(tower).expect("a tower type"),
shared: None,
floor: None,
}
}
fn recipe(members: Vec<MemberModel>, dedup: Dedup) -> SearchModel {
SearchModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: ident("TravelSearch"),
seats: Vec::new(),
dedup,
limit: None,
graphql: None,
members,
}
}
fn tight(model: &SearchModel) -> String {
model.record().to_string().split_whitespace().collect()
}
fn travel() -> SearchModel {
let mut subs = member("subs", "Subs");
subs.weight = Some(1.0);
subs.depth = NonZeroU32::new(50);
subs.arms.push(ArmModel {
shared: Some(SharedBinding {
seat: ident("text"),
bridge: SharedBridge::Total,
}),
..arm("zh", "crate::ZhQuery")
});
let mut shots = member("shots", "Shots");
shots.weight = Some(2.0);
shots.depth = NonZeroU32::new(100);
shots.arms.push(ArmModel {
shared: Some(SharedBinding {
seat: ident("text"),
bridge: SharedBridge::Total,
}),
floor: Some(syn::parse_quote!(crate::floors::SEM_TEXT)),
..arm("text", "crate::ClipText")
});
shots.arms.push(ArmModel {
shared: Some(SharedBinding {
seat: ident("text"),
bridge: SharedBridge::Total,
}),
..arm("image", "crate::ClipImage")
});
let mut model = recipe(vec![subs, shots], Dedup::Hash);
model.seats.push(SeatModel {
name: ident("text"),
ty: syn::parse_quote!(String),
});
model.limit = NonZeroU32::new(20);
model
}
#[test]
fn the_locked_recipe_states_the_locked_record() {
let emitted = tight(&travel());
assert!(
emitted.contains("impl::ingraph::__private::retrieve::DeclaredSearchforTravelSearch{"),
"{emitted}",
);
assert!(
emitted.contains("&[::ingraph::__private::retrieve::Seat::of(\"text\"),]"),
"the broadcast seat is missing from the record: {emitted}",
);
assert!(
emitted.contains("Member::of(\"subs\").weighted(1f64).deep(50u32).bound(&["),
"the member's declared numbers are missing: {emitted}",
);
assert!(
emitted.contains("Binding::of(\"zh\").shared(::ingraph::__private::retrieve::Bridge::Total)"),
"the arm's bridge is missing: {emitted}",
);
assert!(
emitted.contains(
"LIMIT:::core::option::Option<::core::num::NonZeroU32>=::core::num::NonZeroU32::new(20u32)"
),
"the recipe's own limit is missing: {emitted}",
);
}
#[test]
fn a_floor_crosses_as_the_fact_and_not_the_const() {
let emitted = tight(&travel());
assert!(
emitted.contains(
"Binding::of(\"text\").shared(::ingraph::__private::retrieve::Bridge::Total).floored()"
),
"the arm's floor did not cross as a fact: {emitted}",
);
assert!(
!emitted.contains("SEM_TEXT"),
"the record carries a value in a currency it cannot type: {emitted}",
);
}
#[test]
fn an_omitted_word_states_nothing() {
let emitted = tight(&recipe(vec![member("subs", "Subs")], Dedup::Hash));
assert!(
emitted.contains("Member::of(\"subs\").bound(&[])"),
"a member that declared nothing carries a value: {emitted}",
);
assert!(
emitted.contains(
"LIMIT:::core::option::Option<::core::num::NonZeroU32>=::core::option::Option::None"
),
"an omitted limit became a number: {emitted}",
);
assert!(
emitted.contains("SEATS:&'static[::ingraph::__private::retrieve::Seat]=&[]"),
"a recipe with no broadcast states one: {emitted}",
);
assert!(!emitted.contains("20u32"), "{emitted}");
}
#[test]
fn each_crossing_states_its_own_word() {
for (bridge, word) in [
(SharedBridge::Total, "Total"),
(SharedBridge::Fallible, "Fallible"),
(SharedBridge::Hook(syn::parse_quote!(app::cross)), "Hook"),
(
SharedBridge::FallibleHook(syn::parse_quote!(app::cross)),
"FallibleHook",
),
] {
let mut subs = member("subs", "Subs");
subs.arms.push(ArmModel {
shared: Some(SharedBinding {
seat: ident("text"),
bridge,
}),
..arm("zh", "crate::ZhQuery")
});
let emitted = tight(&recipe(vec![subs], Dedup::Hash));
assert!(
emitted.contains(&format!("Bridge::{word})")),
"the crossing did not reach its variant: {emitted}",
);
assert!(!emitted.contains("app::cross"), "{emitted}");
}
}