use quote::ToTokens as _;
use crate::{
construct::{Blanket, Cross, Seat},
model::{Field, NodeKind, RecordModel},
tests::{emission, model},
};
fn wrapping() -> RecordModel {
let mut model = model("Video", &["media_id"], "id");
model.kind = NodeKind::Entity;
model.persist = None;
model.remote = Some(syn::parse_quote!(upstream::Video));
model
}
fn rendered(model: &RecordModel) -> String {
emission(model).into_token_stream().to_string()
}
#[test]
fn an_ordinary_declaration_emits_not_one_token_of_the_device() {
let ordinary = rendered(&model("Video", &["media_id"], "id"));
for absent in [
"__IngraphRemoteBridge",
"__ingraph_cross",
"repr (transparent)",
"Deref",
"BorrowMut",
] {
assert!(
!ordinary.contains(absent),
"an ordinary declaration emitted `{absent}`"
);
}
assert!(
ordinary.contains("& self . media_id"),
"and its readers are the field reads they always were"
);
}
#[test]
fn the_reference_family_is_eight_impls_over_one_field() {
let wrapped = rendered(&wrapping());
for expected in [
"impl :: core :: convert :: AsRef < upstream :: Video > for Video",
"impl :: core :: convert :: AsMut < upstream :: Video > for Video",
"impl :: core :: ops :: Deref for Video",
"impl :: core :: ops :: DerefMut for Video",
"impl :: core :: borrow :: Borrow < upstream :: Video > for Video",
"impl :: core :: borrow :: BorrowMut < upstream :: Video > for Video",
"impl :: core :: convert :: From < upstream :: Video > for Video",
"impl :: core :: convert :: From < Video > for upstream :: Video",
] {
assert!(
wrapped.contains(expected),
"the emission is missing `{expected}`"
);
}
assert!(
!wrapped.contains("transmute"),
"and it conjures nothing — `self.0` is a field access"
);
}
#[test]
fn the_bridge_is_a_marker_that_refuses_in_the_frameworks_own_words() {
let wrapped = rendered(&wrapping());
assert!(wrapped.contains("diagnostic :: on_unimplemented"));
assert!(wrapped.contains("diagnostic :: do_not_recommend"));
assert!(
wrapped.contains("upstream :: Video"),
"and the note names the remote a reader has to go and look at"
);
}
#[test]
fn a_data_column_crosses_and_an_identity_column_borrows() {
let wrapped = rendered(&wrapping());
assert!(
wrapped.contains("fn checksum (& self) -> u64 { __ingraph_cross (self . 0 . checksum ()) }"),
"a data column is read through the accessor and crossed"
);
assert!(
wrapped.contains("fn id_ref (& self) -> & Uuid7 { self . 0 . id () }"),
"an identity column is borrowed straight off the remote"
);
}
#[test]
fn a_wrapper_declares_no_mutator_and_no_const_reader() {
let wrapped = rendered(&wrapping());
assert!(
!wrapped.contains("fn set_checksum"),
"nothing assigns into a remote"
);
assert!(!wrapped.contains("fn with_checksum"));
assert!(
!wrapped.contains("const fn checksum"),
"a foreign accessor is not this emission's to declare `const`"
);
}
#[test]
fn the_name_enumeration_names_exactly_the_wrappers_readers() {
let model = wrapping();
let wrapped = rendered(&model);
for emitted in model
.emitted_methods()
.into_iter()
.filter(|emitted| emitted.owner_name == model.record)
{
assert!(
wrapped.contains(&format!("fn {} (", emitted.name)),
"`{}` is enumerated and not emitted",
emitted.name
);
}
}
#[test]
fn a_manifest_fields_docs_ride_its_reader() {
let mut model = wrapping();
model.fields[2].docs =
vec![syn::parse_quote!(#[doc = " The checksum, as the upstream holds it."])];
let wrapped = rendered(&model);
assert!(
wrapped.contains("The checksum, as the upstream holds it."),
"the sentence the author wrote survives the item that carried it"
);
}
#[test]
fn the_default_law_emits_what_it_always_emitted() {
let plain = wrapping();
assert_eq!(
raise(&plain).to_string(),
"Self (upstream :: Video :: new (__ingraph_cross (row . media_id) , __ingraph_cross (row . \
id) , __ingraph_cross (row . checksum) , __ingraph_cross (row . chapters)) . with_cover_id \
(__ingraph_cross (row . cover_id)))",
);
let mut gated = wrapping();
let last = gated.fields.len() - 1;
gated.fields[last].condition =
crate::model::Condition::of(vec![syn::parse_quote!(#[cfg(feature = "x")])], Vec::new());
assert_eq!(
raise(&gated).to_string(),
"Self ({ let mut __ingraph_built = upstream :: Video :: new (__ingraph_cross (row . media_id) \
, __ingraph_cross (row . id) , __ingraph_cross (row . checksum) , __ingraph_cross (row . \
chapters)) ; # [cfg (feature = \"x\")] { __ingraph_built = __ingraph_built . with_cover_id \
(__ingraph_cross (row . cover_id)) ; } __ingraph_built })",
);
}
fn raise(model: &RecordModel) -> proc_macro2::TokenStream {
let values: Vec<(syn::Ident, proc_macro2::TokenStream)> = model
.fields
.iter()
.map(|field| {
let name = field.column.source.clone();
(name.clone(), quote::quote!(row.#name))
})
.collect();
model.remote_build(&values)
}
fn column<'a>(model: &'a mut RecordModel, name: &str) -> &'a mut Field {
model
.fields
.iter_mut()
.find(|field| field.column.source == name)
.expect("a declared column")
}
#[test]
fn each_crossing_word_writes_the_argument_it_is_written_on() {
let bridged = raise(&wrapping()).to_string();
assert!(
bridged.contains("__ingraph_cross (row . checksum)"),
"the default is the bridge with its target inferred: {bridged}"
);
let mut pinned = wrapping();
column(&mut pinned, "checksum").build.cross = Some(Cross::Into);
assert!(
raise(&pinned)
.to_string()
.contains("__ingraph_cross :: < u64 , _ > (row . checksum)"),
"`into` pins the bridge's target to the column's own type"
);
let mut blanketed = wrapping();
blanketed.construction.blanket = vec![Blanket {
ty: syn::parse_quote!(u64),
}];
let claimed = raise(&blanketed).to_string();
assert!(
claimed.contains("__ingraph_cross :: < u64 , _ > (row . checksum)"),
"a blanket reaches the same place, keyed by the type: {claimed}"
);
assert!(
claimed.contains("__ingraph_cross (row . chapters)"),
"and claims only the columns of that type"
);
let mut overridden = blanketed;
column(&mut overridden, "checksum").build.cross =
Some(Cross::Map(syn::parse_quote!(narrow::checksum)));
assert!(
raise(&overridden)
.to_string()
.contains("narrow :: checksum (row . checksum)"),
"`map` replaces the bridge outright"
);
let mut whole = wrapping();
column(&mut whole, "cover_id").build.option = true;
assert!(
raise(&whole)
.to_string()
.contains(":: core :: option :: Option :: map (row . cover_id , __ingraph_cross)"),
"`option` hands the seat the option and crosses the item"
);
}
#[test]
fn each_seat_word_moves_the_column_it_is_written_on() {
let mut linked = wrapping();
column(&mut linked, "checksum").build.seat = Some(Seat::With(None));
let written = raise(&linked).to_string();
assert!(
written.contains(". with_checksum (__ingraph_cross (row . checksum))"),
"a required column reaches a link: {written}"
);
assert!(
!written.contains("new (__ingraph_cross (row . media_id) , __ingraph_cross (row . id) , __ingraph_cross (row . checksum)"),
"and leaves the argument list"
);
let mut positional = wrapping();
column(&mut positional, "cover_id").build.seat = Some(Seat::Arg);
let written = raise(&positional).to_string();
assert!(
written.contains("__ingraph_cross (row . cover_id))"),
"an optional column reaches the argument list: {written}"
);
assert!(
!written.contains("with_cover_id"),
"and leaves the link it had by default"
);
let mut mutated = wrapping();
column(&mut mutated, "checksum").build.seat = Some(Seat::Set(None));
assert!(
raise(&mutated)
.to_string()
.contains("{ __ingraph_built . set_checksum (__ingraph_cross (row . checksum)) ; }"),
"a setter unrolls the chain and mutates the device's own binding"
);
let mut skipped = wrapping();
column(&mut skipped, "checksum").build.skip = true;
let written = raise(&skipped).to_string();
assert!(
!written.contains("row . checksum"),
"a skipped column reaches no seat at all: {written}"
);
assert!(
written.contains("__ingraph_cross (row . chapters)"),
"and nothing else moved"
);
}