use super::super::{ArmModel, MemberModel, SearchModel, SeatModel, SharedBinding, SharedBridge};
use crate::index::Dedup;
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn seat(name: &str) -> SeatModel {
SeatModel {
name: ident(name),
ty: syn::parse_quote!(::std::string::String),
}
}
fn arm(name: &str, tower: &str, shared: Option<SharedBinding>) -> ArmModel {
ArmModel {
name: ident(name),
tower: syn::parse_str(tower).expect("a tower type"),
shared,
floor: None,
}
}
fn member(field: &str, arms: Vec<ArmModel>) -> MemberModel {
MemberModel {
field: ident(field),
ty: syn::parse_quote!(Shots),
weight: None,
depth: None,
floor: None,
arms,
}
}
fn recipe(seats: Vec<SeatModel>, members: Vec<MemberModel>) -> SearchModel {
SearchModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: ident("TravelSearch"),
seats,
dedup: Dedup::Hash,
limit: None,
graphql: None,
members,
}
}
fn tight(model: &SearchModel) -> String {
let mut whole = model.seat_markers();
whole.extend(model.bindings());
whole.to_string().split_whitespace().collect()
}
fn total() -> Option<SharedBinding> {
Some(SharedBinding {
seat: ident("text"),
bridge: SharedBridge::Total,
})
}
#[test]
fn every_seat_mints_a_marker_named_for_its_recipe() {
let emitted = tight(&recipe(
vec![seat("text"), seat("point")],
vec![member("subs", Vec::new())],
));
assert!(emitted.contains("structTravelSearchTextSeat;"), "{emitted}");
assert!(
emitted.contains("structTravelSearchPointSeat;"),
"{emitted}"
);
}
#[test]
fn a_recipe_with_no_seat_mints_no_marker() {
let emitted = tight(&recipe(Vec::new(), vec![member("subs", Vec::new())]));
assert!(!emitted.contains("Seat;"), "{emitted}");
}
#[test]
fn a_shared_arm_demands_the_crossing_from_its_towers_answer() {
let emitted = tight(&recipe(
vec![seat("text")],
vec![member(
"shots",
vec![arm("text", "crate::ClipText", total())],
)],
));
assert!(
emitted.contains("fn__ingraph_a_bound_tower_answers_what_its_member_is_asked_with"),
"{emitted}",
);
assert!(
emitted.contains(
"__IgAsked:::ingraph::__private::retrieve::TrySharedQuestion<TravelSearchTextSeat,__IgAnswer>"
),
"{emitted}",
);
assert!(
emitted.contains("<Shotsas::ingraph::__private::retrieve::Fusable>::Question"),
"the member's question is named rather than projected: {emitted}",
);
assert!(
emitted.contains("<crate::ClipTextas::ingraph::__private::retrieve::SearchTower>::Answer"),
"the tower's answer is named rather than projected: {emitted}",
);
}
#[test]
fn an_unshared_arm_raises_no_obligation() {
let emitted = tight(&recipe(
vec![seat("text")],
vec![member("shots", vec![arm("text", "crate::ClipText", None)])],
));
assert!(
!emitted.contains("__ingraph_a_bound_tower_answers"),
"{emitted}",
);
assert!(emitted.contains("structTravelSearchTextSeat;"), "{emitted}");
}
#[test]
fn a_plain_member_raises_no_obligation() {
let emitted = tight(&recipe(
vec![seat("text")],
vec![member("subs", Vec::new())],
));
assert!(
!emitted.contains("__ingraph_a_bound_tower_answers"),
"{emitted}",
);
}
#[test]
fn the_four_crossings_each_state_their_own_bridge() {
let hook: syn::Path = syn::parse_quote!(crate::widen);
let table: [(SharedBridge, &str); 4] = [
(
SharedBridge::Total,
"__ingraph_a_shared_arm_takes_the_seats_own_type",
),
(
SharedBridge::Fallible,
"__ingraph_a_try_shared_arm_takes_the_seats_own_type",
),
(SharedBridge::Hook(hook.clone()), "crate::widen"),
(
SharedBridge::FallibleHook(hook),
"__ingraph_a_try_shared_hook_answers_the_towers_question",
),
];
for (bridge, owed) in table {
let word = bridge.word();
let emitted = tight(&recipe(
vec![seat("text")],
vec![member(
"shots",
vec![arm(
"text",
"crate::ClipText",
Some(SharedBinding {
seat: ident("text"),
bridge,
}),
)],
)],
));
assert!(
emitted.contains(owed),
"`{word}` lost its bridge: {emitted}"
);
}
}
#[test]
fn the_trait_bridges_demand_the_conversions_their_words_name() {
for (bridge, owed) in [
(SharedBridge::Total, "::core::convert::From<__IgSeat>"),
(SharedBridge::Fallible, "::core::convert::TryFrom<__IgSeat>"),
] {
let emitted = tight(&recipe(
vec![seat("text")],
vec![member(
"shots",
vec![arm(
"text",
"crate::ClipText",
Some(SharedBinding {
seat: ident("text"),
bridge,
}),
)],
)],
));
let owed = owed.replace(' ', "");
assert!(emitted.contains(&owed), "{emitted}");
assert!(
emitted.contains(
"<__IgToweras::ingraph::__private::retrieve::SearchTower>::Question:::core::convert"
),
"the bound was stated about the wrong subject: {emitted}",
);
}
}
#[test]
fn an_arm_naming_an_undeclared_seat_states_nothing() {
let emitted = tight(&recipe(
vec![seat("text")],
vec![member(
"shots",
vec![arm(
"image",
"crate::ClipImage",
Some(SharedBinding {
seat: ident("absent"),
bridge: SharedBridge::Total,
}),
)],
)],
));
assert!(
emitted.contains("pubstructTravelSearchTextSeat"),
"the emitter did not run at all, so the absences below say nothing: {emitted}",
);
assert_eq!(
emitted.matches("const_:fn()=").count(),
0,
"an arm whose seat the head never declared spent a binding obligation: {emitted}",
);
assert!(!emitted.contains("AbsentSeat"), "{emitted}");
assert!(
!emitted.contains("__ingraph_a_bound_tower_answers"),
"{emitted}",
);
}