use super::key_rename_tests::published;
use crate::{model::RecordModel, name::RustSafeName};
fn word(spelled: &str) -> String {
spelled.to_owned()
}
fn addressed(base: Option<&str>, object: Option<&str>, argument: Option<&str>) -> RecordModel {
let mut model = crate::tests::wired();
let record = model.record.clone();
let block = model
.graphql
.as_mut()
.expect("`wired` declares a wire face");
block.key_output = match base {
Some(base) => RustSafeName::new(base, proc_macro2::Span::call_site()),
None => RustSafeName::from_ident(&crate::model::default_key_output_name(&record)),
};
block.key_object = object.map(word);
block.key_argument = argument.map(word);
assert!(
model.key.compound(),
"`{record}` is the compound fixture this reading needs",
);
model
}
#[test]
fn the_object_seat_moves_the_reading_half_and_leaves_the_sending_one() {
assert_eq!(
published(&addressed(None, Some("AddressM"), None)),
("AddressM".to_owned(), "VideoTrackKeyInput".to_owned(),),
);
}
#[test]
fn the_argument_seat_moves_the_sending_half_and_leaves_the_reading_one() {
assert_eq!(
published(&addressed(None, None, Some("AddressIn"))),
("VideoTrackKey".to_owned(), "AddressIn".to_owned()),
);
}
#[test]
fn a_named_object_leaves_the_argument_on_the_bases_own_composition() {
assert_eq!(
published(&addressed(Some("Address"), Some("AddressM"), None)),
("AddressM".to_owned(), "AddressInput".to_owned()),
);
}
#[test]
fn each_face_resolves_member_then_base_then_default() {
assert_eq!(
published(&addressed(
Some("Address"),
Some("AddressM"),
Some("AddressIn"),
)),
("AddressM".to_owned(), "AddressIn".to_owned()),
"a member lost to the base it sits under",
);
assert_eq!(
published(&addressed(Some("Address"), None, None)),
("Address".to_owned(), "AddressInput".to_owned()),
"the base stopped answering where no member was written",
);
assert_eq!(
published(&addressed(None, None, None)),
("VideoTrackKey".to_owned(), "VideoTrackKeyInput".to_owned()),
"the silence stopped publishing what it always published",
);
}
#[test]
fn a_member_with_no_base_publishes_its_word_beside_the_default() {
let (object, argument) = published(&addressed(None, Some("AddressM"), None));
assert_eq!(object, "AddressM");
assert_eq!(argument, "VideoTrackKeyInput");
}
#[test]
fn a_named_face_lands_no_type_in_the_declaring_scope() {
let file: syn::File = syn::parse2(addressed(None, Some("AddressM"), Some("AddressIn")).emit())
.expect("the emission parses");
let landed: Vec<String> = file
.items
.iter()
.filter_map(|item| match item {
syn::Item::Struct(declared) => Some(declared.ident.to_string()),
_ => None,
})
.collect();
assert!(
landed.contains(&"VideoTrackKeyOutput".to_owned())
&& landed.contains(&"VideoTrackKeyInput".to_owned()),
"a schema word moved a Rust name: {landed:?}",
);
assert!(
!landed
.iter()
.any(|name| name == "AddressM" || name == "AddressIn"),
"a schema word minted a Rust name: {landed:?}",
);
}