use ingraph_codegen::tower::TowerModel;
fn parse(input: &syn::DeriveInput) -> syn::Result<TowerModel> {
let (host, arguments) = crate::host::split(input);
super::parse(&host, &arguments)
}
fn accepted(input: proc_macro2::TokenStream) -> TowerModel {
parse(&syn::parse2(input).expect("the declaration parses")).expect("the declaration is a tower")
}
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")
}
fn spelled(ty: &syn::Type) -> String {
quote::quote!(#ty).to_string().replace(' ', "")
}
fn wrote(ty: Option<&syn::Type>) -> String {
spelled(ty.expect("the word was written"))
}
#[test]
fn a_tower_declares_its_question_and_its_steps_are_its_fields() {
let model = accepted(quote::quote! {
#[ingraph(question(type = "String"), crate = "other::ingraph")]
pub struct ClipText {
#[ingraph(answer = "Vec<String>")]
tokenize: Tokenize,
#[ingraph(answer = "Vec<f32>", async)]
encode: ClipTextTower,
}
});
assert_eq!(model.name, "ClipText");
assert_eq!(wrote(model.question.as_ref()), "String");
let krate = &model.krate;
assert_eq!(
quote::quote!(#krate).to_string().replace(' ', ""),
"other::ingraph"
);
assert_eq!(model.steps.len(), 2);
assert_eq!(model.steps[0].field, "tokenize");
assert_eq!(spelled(&model.steps[0].ty), "Tokenize");
assert_eq!(wrote(model.steps[0].answer.as_ref()), "Vec<String>");
assert!(!model.steps[0].awaited);
assert_eq!(model.steps[1].field, "encode");
assert!(model.steps[1].awaited);
assert!(model.awaited(), "the OR law reads the steps' own words");
}
#[test]
fn each_end_words_sugar_is_its_full_form() {
let sugar = accepted(quote::quote! {
#[ingraph(question = "crate::ask::Text", answer = "Vec<f32>")]
pub struct T { #[ingraph(answer = "Vec<f32>")] one: One }
});
assert_eq!(wrote(sugar.question.as_ref()), "crate::ask::Text");
assert_eq!(wrote(sugar.answer.as_ref()), "Vec<f32>");
let full = accepted(quote::quote! {
#[ingraph(question(type = "crate::ask::Text"), answer(type = "Vec<f32>"))]
pub struct T { #[ingraph(answer = "Vec<f32>")] one: One }
});
assert_eq!(wrote(full.question.as_ref()), "crate::ask::Text");
assert_eq!(wrote(full.answer.as_ref()), "Vec<f32>");
}
#[test]
fn a_tower_may_write_no_head_answer() {
let model = accepted(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = "Vec<f32>")] one: One }
});
assert!(model.answer.is_none());
}
#[test]
fn the_head_answer_and_a_steps_answer_are_separate_seats() {
let model = accepted(quote::quote! {
#[ingraph(answer = "Vec<f32>")]
pub struct T { #[ingraph(answer = "Vec<f64>")] one: One }
});
assert_eq!(wrote(model.answer.as_ref()), "Vec<f32>");
assert_eq!(wrote(model.steps[0].answer.as_ref()), "Vec<f64>");
}
#[test]
fn a_tower_with_no_face_word_is_wholly_blocking() {
let model = accepted(quote::quote! {
#[ingraph(question = "String")]
pub struct Rewrite {
#[ingraph(answer = "String")] fold: Fold,
#[ingraph(answer = "String")] expand: Expand,
}
});
assert!(!model.awaited());
}
#[test]
fn the_declarations_visibility_is_carried() {
let model = accepted(quote::quote! {
#[ingraph(question = "String")]
pub(crate) struct T { #[ingraph(answer = "String")] one: One }
});
let vis = &model.vis;
assert_eq!(
quote::quote!(#vis).to_string().replace(' ', ""),
"pub(crate)"
);
}
#[test]
fn a_tower_may_write_neither_type_word() {
let model = accepted(quote::quote! {
pub struct Words {
tokenize: Tokenize,
#[ingraph(async)]
encode: Encode,
}
});
assert!(model.question.is_none(), "absence is a road, not a gap");
assert_eq!(model.steps.len(), 2);
assert!(model.steps.iter().all(|step| step.answer.is_none()));
assert!(model.steps[1].awaited, "the face word still selects");
}
#[test]
fn each_type_word_is_omitted_on_its_own() {
let half = accepted(quote::quote! {
#[ingraph(question = "String")]
pub struct T {
one: One,
two: Two,
}
});
assert_eq!(wrote(half.question.as_ref()), "String");
assert!(half.steps.iter().all(|step| step.answer.is_none()));
let mixed = accepted(quote::quote! {
pub struct T {
#[ingraph(answer = "Vec<String>")] one: One,
two: Two,
}
});
assert!(mixed.question.is_none());
assert_eq!(wrote(mixed.steps[0].answer.as_ref()), "Vec<String>");
assert!(mixed.steps[1].answer.is_none());
}
#[test]
fn an_empty_tower_is_refused() {
for input in [
quote::quote! {
#[ingraph(question = "String")]
pub struct T {}
},
quote::quote! {
#[ingraph(question = "String")]
pub struct T;
},
] {
let refusal = refused(input);
assert!(refusal.contains("a tower declares no step"), "{refusal}");
}
}
#[test]
fn a_tuple_struct_is_refused() {
let refusal = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T(One, Two);
});
assert!(refusal.contains("NAMED fields"), "{refusal}");
}
#[test]
fn an_enum_is_refused() {
let refusal = refused(quote::quote! {
#[ingraph(question = "String")]
pub enum T { A, B }
});
assert!(refusal.contains("supports structs only"), "{refusal}");
}
#[test]
fn a_generic_tower_is_refused() {
for input in [
quote::quote! {
#[ingraph(question = "String")]
pub struct T<S> { #[ingraph(answer = "String")] one: S }
},
quote::quote! {
#[ingraph(question = "String")]
pub struct T where One: Sized { #[ingraph(answer = "String")] one: One }
},
] {
let refusal = refused(input);
assert!(refusal.contains("no generic parameter"), "{refusal}");
}
}
#[test]
fn a_configured_face_word_is_refused() {
for word in [quote::quote!(async = "true"), quote::quote!(async(spawn))] {
let refusal = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = "String", #word)] one: One }
});
assert!(refusal.contains("`async` is a bare word here"), "{refusal}");
}
}
#[test]
fn a_second_word_in_one_seat_is_refused() {
let question = refused(quote::quote! {
#[ingraph(question = "String", question(type = "usize"))]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(
question.contains("`question` is declared more than once"),
"{question}"
);
let answer = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = "String", answer = "usize")] one: One }
});
assert!(
answer.contains("`answer` is declared more than once on this step"),
"{answer}"
);
let pin = refused(quote::quote! {
#[ingraph(answer = "String", answer(type = "usize"))]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(
pin.contains("`answer` is declared more than once at the tower"),
"{pin}"
);
let face = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = "String", async, async)] one: One }
});
assert!(
face.contains("`async` is declared more than once"),
"{face}"
);
}
#[test]
fn an_end_word_naming_no_type_is_refused() {
for (word, head) in [
("question", quote::quote!(question)),
("question", quote::quote!(question())),
("question", quote::quote!(question(crate = "x"))),
("answer", quote::quote!(answer)),
("answer", quote::quote!(answer())),
("answer", quote::quote!(answer(crate = "x"))),
] {
let refusal = refused(quote::quote! {
#[ingraph(#head)]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(
refusal.contains(&format!("`{word}` names no type"))
|| refusal.contains(&format!("unknown entry in `{word}")),
"{refusal}"
);
}
}
#[test]
fn the_head_pins_sentences_say_it_checks_rather_than_decides() {
let empty = refused(quote::quote! {
#[ingraph(answer)]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(
empty.contains("whatever its last step arrives at"),
"{empty}"
);
let inner = refused(quote::quote! {
#[ingraph(answer(rename = "x"))]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(inner.contains("unknown entry in `answer(…)`"), "{inner}");
assert!(inner.contains("DECIDES nothing"), "{inner}");
}
#[test]
fn an_unknown_entry_is_refused_with_this_positions_words() {
let tower = refused(quote::quote! {
#[ingraph(question = "String", steps = "one")]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(
tower.contains("unknown entry in `#[ingraph::tower(…)]`"),
"{tower}"
);
let step = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = "String", question = "usize")] one: One }
});
assert!(
step.contains("unknown entry in `#[ingraph(…)]` on a step"),
"{step}"
);
}
#[test]
fn the_retired_inline_form_is_refused_by_name() {
for word in [quote::quote!(arm(text)), quote::quote!(chain(a, b))] {
let refusal = refused(quote::quote! {
#[ingraph(question = "String", #word)]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(refusal.contains("RETIRED inline tower form"), "{refusal}");
assert!(refusal.contains("search recipe"), "{refusal}");
}
}
#[test]
fn the_retired_naming_word_is_refused_by_name() {
let refusal = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = "String", name = "first")] one: One }
});
assert!(refusal.contains("retired inline form's word"), "{refusal}");
assert!(
refusal.contains("`one` already IS this step's name"),
"{refusal}"
);
}
#[test]
fn an_options_word_on_a_step_is_refused_with_the_seat_law() {
let refusal = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = "String", options(limit = 5))] one: One }
});
assert!(refusal.contains("`Serve::Options`"), "{refusal}");
assert!(
refusal.contains("carries one row per step already"),
"{refusal}"
);
assert!(
refusal.contains("seats `()` takes no knobs at all"),
"{refusal}"
);
assert!(refusal.contains("naming knobs nothing reads"), "{refusal}");
assert!(refusal.contains("`one`"), "{refusal}");
assert!(
!refusal.contains("unknown entry"),
"refused with the LAW, not as an unknown: {refusal}"
);
}
#[test]
fn an_unquoted_type_is_refused() {
let question = refused(quote::quote! {
#[ingraph(question = String)]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(question.contains("quoted"), "{question}");
let answer = refused(quote::quote! {
#[ingraph(question = "String")]
pub struct T { #[ingraph(answer = String)] one: One }
});
assert!(answer.contains("quoted"), "{answer}");
let pin = refused(quote::quote! {
#[ingraph(answer = Vec<f32>)]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(
pin.contains("`answer` names the TYPE the whole tower is pinned to answer"),
"{pin}"
);
let unparsable = refused(quote::quote! {
#[ingraph(answer = "not a type at all")]
pub struct T { #[ingraph(answer = "String")] one: One }
});
assert!(
unparsable.contains("`answer` names the TYPE the whole tower is pinned to answer"),
"{unparsable}"
);
}
#[test]
fn one_compile_reports_every_rule_it_can_still_see() {
let refusal = refused(quote::quote! {
#[ingraph(question)]
pub struct T<S> {
#[ingraph(name = "first")] one: One,
#[ingraph(async = "true")] two: S,
}
});
assert!(refusal.contains("`question` names no type"), "{refusal}");
assert!(
refusal.contains("`one` already IS this step's name"),
"{refusal}"
);
assert!(refusal.contains("`async` is a bare word here"), "{refusal}");
assert!(refusal.contains("no generic parameter"), "{refusal}");
}
fn fan(model: &TowerModel, at: usize) -> &ingraph_codegen::tower::Fan {
model.steps[at]
.fan
.as_ref()
.expect("this step opened a segment")
}
fn path(written: &syn::Path) -> String {
quote::quote!(#written).to_string().replace(' ', "")
}
#[test]
fn a_widening_names_the_expander_it_defaults_to() {
for written in [
quote::quote! {
pub struct T { #[ingraph(expand = "app::Synonyms", answer = "String")] widen: Widen }
},
quote::quote! {
pub struct T { #[ingraph(expand(type = "app::Synonyms"), answer = "String")] widen: Widen }
},
] {
let model = accepted(written);
let ingraph_codegen::tower::Fan::Expand { expander } = fan(&model, 0) else {
panic!("the step opened a WIDENING");
};
assert_eq!(path(expander), "app::Synonyms");
assert_eq!(fan(&model, 0).word(), "expand");
assert_eq!(
fan(&model, 0).stage(),
ingraph_codegen::tower::Stage::Expand
);
assert!(model.fanned());
}
}
#[test]
fn a_crossing_names_its_router_and_the_slot_it_is_wired_to() {
let model = accepted(quote::quote! {
pub struct T {
#[ingraph(route(router = script_weighted_fan, slot = "app::BodySlot"), answer = "String")]
fan: Fan
}
});
let ingraph_codegen::tower::Fan::Route { router, slot } = fan(&model, 0) else {
panic!("the step opened a CROSSING");
};
assert_eq!(router.as_written(), "script_weighted_fan");
assert_eq!(path(slot), "app::BodySlot");
assert_eq!(fan(&model, 0).word(), "route");
assert_eq!(fan(&model, 0).stage(), ingraph_codegen::tower::Stage::Route);
let declared = accepted(quote::quote! {
pub struct T {
#[ingraph(route(slot = "app::BodySlot", router = "app::MyRouter"))]
fan: Fan
}
});
let ingraph_codegen::tower::Fan::Route { router, .. } = fan(&declared, 0) else {
panic!("the step opened a CROSSING");
};
assert!(
matches!(router, ingraph_codegen::tower::Router::Declared(_)),
"a quoted router is the declaring crate's",
);
assert_eq!(router.as_written().replace(' ', ""), "app::MyRouter");
}
#[test]
fn every_shipped_router_has_its_bare_word() {
for word in ingraph_codegen::tower::Router::SHIPPED {
let written: proc_macro2::TokenStream =
format!("pub struct T {{ #[ingraph(route(router = {word}, slot = \"app::S\"))] fan: Fan }}")
.parse()
.expect("the declaration parses");
let model = accepted(written);
let ingraph_codegen::tower::Fan::Route { router, .. } = fan(&model, 0) else {
panic!("the step opened a CROSSING");
};
assert_eq!(router.as_written(), word);
}
}
#[test]
fn a_tower_with_no_fan_word_is_the_degenerate_case() {
let model = accepted(quote::quote! {
#[ingraph(question = "String")]
pub struct ClipText {
#[ingraph(answer = "Vec<String>")] tokenize: Tokenize,
#[ingraph(answer = "Vec<f32>", async)] encode: Encode,
}
});
assert!(!model.fanned());
assert_eq!(model.fans().count(), 0);
assert!(model.steps.iter().all(|step| step.fan.is_none()));
}
#[test]
fn a_crossing_has_no_short_spelling() {
let refusal = refused(quote::quote! {
pub struct T { #[ingraph(route = script_weighted_fan)] fan: Fan }
});
assert!(refusal.contains("has no short spelling"), "{refusal}");
assert!(refusal.contains("EQUALITY"), "{refusal}");
assert!(
!refusal.contains("unknown"),
"refused with the LAW, not as an unknown: {refusal}"
);
}
#[test]
fn a_crossing_names_both_halves_or_is_refused() {
let bare = refused(quote::quote! {
pub struct T { #[ingraph(route)] fan: Fan }
});
assert!(bare.contains("`route` names no property"), "{bare}");
let empty = refused(quote::quote! {
pub struct T { #[ingraph(route())] fan: Fan }
});
assert!(empty.contains("`route` names no property"), "{empty}");
let no_router = refused(quote::quote! {
pub struct T { #[ingraph(route(slot = "app::S"))] fan: Fan }
});
assert!(no_router.contains("names no `router`"), "{no_router}");
let no_slot = refused(quote::quote! {
pub struct T { #[ingraph(route(router = fan_all))] fan: Fan }
});
assert!(no_slot.contains("names no `slot`"), "{no_slot}");
}
#[test]
fn an_unshipped_router_names_the_roster_and_the_escape() {
let refusal = refused(quote::quote! {
pub struct T { #[ingraph(route(router = script_wieghted_fan, slot = "app::S"))] fan: Fan }
});
assert!(
refusal.contains("is not a router this framework ships"),
"{refusal}"
);
assert!(refusal.contains("`script_weighted_fan`"), "{refusal}");
assert!(refusal.contains("arrives QUOTED"), "{refusal}");
assert!(
refusal.contains("did you mean `script_weighted_fan`"),
"the near-miss list is the shipped roster: {refusal}"
);
}
#[test]
fn an_unknown_crossing_property_names_this_positions_words() {
let refusal = refused(quote::quote! {
pub struct T { #[ingraph(route(router = fan_all, slt = "app::S"))] fan: Fan }
});
assert!(
refusal.contains("unknown property in `route(…)`"),
"{refusal}"
);
assert!(refusal.contains("did you mean `slot`"), "{refusal}");
assert!(
!refusal.contains("names no `slot`"),
"an absence check must not run over a word the reader could not place: {refusal}"
);
}
#[test]
fn one_of_each_word_and_one_of_each_property() {
let expand = refused(quote::quote! {
pub struct T { #[ingraph(expand = "app::A", expand = "app::B")] widen: Widen }
});
assert!(
expand.contains("`expand` is declared more than once on this step"),
"{expand}"
);
let route = refused(quote::quote! {
pub struct T {
#[ingraph(route(router = fan_all, slot = "app::S"), route(router = fan_all, slot = "app::S"))]
fan: Fan
}
});
assert!(
route.contains("`route` is declared more than once on `fan`"),
"{route}"
);
let property = refused(quote::quote! {
pub struct T { #[ingraph(route(router = fan_all, router = fan_all, slot = "app::S"))] fan: Fan }
});
assert!(
property.contains("`router` is declared more than once in `route(…)`"),
"{property}"
);
}
#[test]
fn a_step_opens_one_segment() {
let refusal = refused(quote::quote! {
pub struct T {
#[ingraph(expand = "app::A", route(router = fan_all, slot = "app::S"))]
both: Both
}
});
assert!(
refusal.contains("`both` writes `expand` and `route`"),
"{refusal}"
);
let enriched = refused(quote::quote! {
pub struct T {
#[ingraph(annotate = "app::Tag", expand = "app::A")]
both: Both
}
});
assert!(
enriched.contains("`both` writes `annotate` and `expand`"),
"{enriched}"
);
let all = refused(quote::quote! {
pub struct T {
#[ingraph(annotate = "app::Tag", expand = "app::A", route(router = fan_all, slot = "app::S"))]
every: Every
}
});
assert_eq!(
all.matches("opens ONE segment").count(),
1,
"one law, said once, however many words a step wrote: {all}"
);
assert!(
all.contains("`every` writes `annotate` and `expand` and `route`"),
"{all}"
);
}
#[test]
fn a_tower_opens_one_segment_of_each_kind() {
let widened = refused(quote::quote! {
pub struct T {
#[ingraph(expand = "app::A")] first: One,
#[ingraph(expand = "app::B")] second: Two,
}
});
assert!(
widened.contains("`expand` is already open at `first`"),
"{widened}"
);
assert!(widened.contains("ONE question"), "{widened}");
let crossed = refused(quote::quote! {
pub struct T {
#[ingraph(route(router = fan_all, slot = "app::S"))] first: One,
#[ingraph(route(router = fan_all, slot = "app::S"))] second: Two,
}
});
assert!(
crossed.contains("`route` is already open at `first`"),
"{crossed}"
);
assert!(crossed.contains("ONE lane fan"), "{crossed}");
}
#[test]
fn the_stages_run_in_the_order_the_pipeline_has() {
let refusal = refused(quote::quote! {
pub struct T {
#[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
#[ingraph(expand = "app::A")] widen: Widen,
}
});
assert!(
refusal.contains("`route` on `fan` comes before `expand` on `widen`"),
"{refusal}"
);
assert!(refusal.contains("Move the step"), "{refusal}");
let ordered = accepted(quote::quote! {
pub struct T {
#[ingraph(expand = "app::A")] widen: Widen,
#[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
}
});
assert_eq!(ordered.fans().count(), 2);
assert_eq!(
ordered.fans().map(|(at, ..)| at).collect::<Vec<_>>(),
vec![0, 1],
"the position IS the step's own index",
);
}
#[test]
fn a_widening_names_a_type_and_never_a_word() {
let bare = refused(quote::quote! {
pub struct T { #[ingraph(expand)] widen: Widen }
});
assert!(bare.contains("`expand` names no type"), "{bare}");
assert!(bare.contains("ships no expander"), "{bare}");
let unquoted = refused(quote::quote! {
pub struct T { #[ingraph(expand = synonyms)] widen: Widen }
});
assert!(unquoted.contains("quoted"), "{unquoted}");
let inner = refused(quote::quote! {
pub struct T { #[ingraph(expand(typ = "app::A"))] widen: Widen }
});
assert!(inner.contains("unknown entry in `expand(…)`"), "{inner}");
assert!(inner.contains("did you mean `type`"), "{inner}");
}
#[test]
fn a_type_word_claims_no_absence_it_could_not_check() {
for written in [
quote::quote! {
#[ingraph(question(typ = "String"))]
pub struct T { #[ingraph(answer = "String")] one: One }
},
quote::quote! {
#[ingraph(answer(typ = "String"))]
pub struct T { #[ingraph(answer = "String")] one: One }
},
quote::quote! {
pub struct T { #[ingraph(expand(typ = "app::A"))] widen: Widen }
},
] {
let refusal = refused(written);
assert!(refusal.contains("unknown entry"), "{refusal}");
assert!(!refusal.contains("names no type"), "{refusal}");
}
}
#[test]
fn a_slot_is_a_quoted_path() {
let refusal = refused(quote::quote! {
pub struct T { #[ingraph(route(router = fan_all, slot = BodySlot))] fan: Fan }
});
assert!(refusal.contains("`slot = \"path::to::Slot\"`"), "{refusal}");
assert!(refusal.contains("QUOTED"), "{refusal}");
}
#[test]
fn a_router_word_is_the_whole_of_what_it_carries() {
let refusal = refused(quote::quote! {
pub struct T { #[ingraph(route(router = fan_all(2), slot = "app::S"))] fan: Fan }
});
assert!(
refusal.contains("is a shipped router's BARE word or the declaring crate's own QUOTED path"),
"{refusal}"
);
}
#[test]
fn a_steps_vocabulary_names_the_two_fan_words() {
let refusal = refused(quote::quote! {
pub struct T { #[ingraph(rout(router = fan_all, slot = "app::S"))] fan: Fan }
});
assert!(refusal.contains("did you mean `route`"), "{refusal}");
let widen = refused(quote::quote! {
pub struct T { #[ingraph(expnd = "app::A")] widen: Widen }
});
assert!(widen.contains("did you mean `expand`"), "{widen}");
let annotated = refused(quote::quote! {
pub struct T { #[ingraph(anotate = "app::Tag")] detect: Detect }
});
assert!(annotated.contains("did you mean `annotate`"), "{annotated}");
}
#[test]
fn a_fan_segment_wears_no_face() {
let widening = refused(quote::quote! {
pub struct T { #[ingraph(expand = "app::Synonyms", async)] widen: Widen }
});
assert!(widening.contains("opens the WIDENING"), "{widening}");
assert!(widening.contains("through the lift bridge"), "{widening}");
assert!(widening.contains("Take the word off"), "{widening}");
let crossing = refused(quote::quote! {
pub struct T {
#[ingraph(route(router = fan_all, slot = "app::S"), async)] fan: Fan
}
});
assert!(crossing.contains("opens the CROSSING"), "{crossing}");
assert!(
crossing.contains("one face and it is the blocking one"),
"{crossing}"
);
let plain = accepted(quote::quote! {
pub struct T { #[ingraph(async)] encode: Encode }
});
assert!(plain.steps[0].awaited);
assert!(plain.steps[0].fan.is_none());
}
#[test]
fn an_annotation_names_the_type_it_writes_into_the_question() {
let sugar = accepted(quote::quote! {
pub struct T { #[ingraph(annotate = "app::LanguageTag")] detect: Detect }
});
assert_eq!(
wrote(sugar.steps[0].annotation.as_ref()),
"app::LanguageTag"
);
assert_eq!(
sugar.steps[0].stage(),
Some(ingraph_codegen::tower::Stage::Annotate)
);
assert!(
sugar.steps[0].fan.is_none(),
"an annotation opens no FAN — the model's two seats say two different things"
);
assert_eq!(sugar.annotate_at(), Some(0));
let full = accepted(quote::quote! {
pub struct T { #[ingraph(annotate(type = "app::LanguageTag"))] detect: Detect }
});
assert_eq!(wrote(full.steps[0].annotation.as_ref()), "app::LanguageTag");
let plain = accepted(quote::quote! {
pub struct T { encode: Encode }
});
assert!(plain.steps[0].annotation.is_none());
assert!(plain.steps[0].stage().is_none());
assert!(plain.annotate_at().is_none());
}
#[test]
fn an_annotation_that_names_no_type_is_refused() {
let bare = refused(quote::quote! {
pub struct T { #[ingraph(annotate)] detect: Detect }
});
assert!(bare.contains("`annotate` names no type"), "{bare}");
assert!(
bare.contains("the declaring crate's own type"),
"no bare vocabulary here, and the sentence says why: {bare}"
);
let unquoted = refused(quote::quote! {
pub struct T { #[ingraph(annotate = LanguageTag)] detect: Detect }
});
assert!(unquoted.contains("quoted"), "{unquoted}");
let twice = refused(quote::quote! {
pub struct T { #[ingraph(annotate = "app::A", annotate(type = "app::B"))] detect: Detect }
});
assert!(
twice.contains("`annotate` is declared more than once"),
"the sugar and the full form fill ONE seat: {twice}"
);
let inner = refused(quote::quote! {
pub struct T { #[ingraph(annotate(typ = "app::A"))] detect: Detect }
});
assert!(inner.contains("unknown entry in `annotate(…)`"), "{inner}");
assert!(inner.contains("did you mean `type`"), "{inner}");
}
#[test]
fn an_annotation_step_wears_the_face_its_server_does() {
let awaited = accepted(quote::quote! {
pub struct T { #[ingraph(annotate = "app::LanguageTag", async)] detect: Detect }
});
assert!(awaited.steps[0].awaited);
assert!(awaited.steps[0].annotation.is_some());
let blocking = accepted(quote::quote! {
pub struct T { #[ingraph(annotate = "app::LanguageTag")] detect: Detect }
});
assert!(!blocking.steps[0].awaited);
}
#[test]
fn an_annotation_step_answers_the_question_it_was_given() {
let refusal = refused(quote::quote! {
pub struct T {
#[ingraph(annotate = "app::LanguageTag", answer = "app::Ask")] detect: Detect
}
});
assert!(
refusal.contains("`answer` decides nothing on `detect`"),
"{refusal}"
);
assert!(
refusal.contains("hands back the question it was given"),
"{refusal}"
);
let reversed = refused(quote::quote! {
pub struct T {
#[ingraph(answer = "app::Ask", annotate = "app::LanguageTag")] detect: Detect
}
});
assert!(
reversed.contains("`answer` decides nothing on `detect`"),
"{reversed}"
);
}
#[test]
fn the_annotation_stage_runs_before_every_other() {
let after_widening = refused(quote::quote! {
pub struct T {
#[ingraph(expand = "app::A")] widen: Widen,
#[ingraph(annotate = "app::Tag")] detect: Detect,
}
});
assert!(
after_widening.contains("`expand` on `widen` comes before `annotate` on `detect`"),
"{after_widening}"
);
assert!(
after_widening.contains("the stages after it READ what it wrote"),
"the reason belongs to the stage that was written late: {after_widening}"
);
let after_crossing = refused(quote::quote! {
pub struct T {
#[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
#[ingraph(annotate = "app::Tag")] detect: Detect,
}
});
assert!(
after_crossing.contains("`route` on `fan` comes before `annotate` on `detect`"),
"{after_crossing}"
);
let ordered = accepted(quote::quote! {
pub struct T {
#[ingraph(annotate = "app::Tag")] detect: Detect,
#[ingraph(expand = "app::A")] widen: Widen,
#[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
}
});
assert_eq!(ordered.annotate_at(), Some(0));
assert_eq!(ordered.expand_at(), Some(1));
assert_eq!(ordered.route_at(), Some(2));
}
#[test]
fn a_tower_annotates_at_one_stage() {
let refusal = refused(quote::quote! {
pub struct T {
#[ingraph(annotate = "app::Tag")] first: One,
#[ingraph(annotate = "app::Intent")] second: Two,
}
});
assert!(
refusal.contains("`annotate` is already open at `first`"),
"{refusal}"
);
assert!(refusal.contains("which is nowhere"), "{refusal}");
}