use crate::{model::RecordModel, name::RustSafeName};
fn addressed(word: 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 word {
Some(word) => RustSafeName::new(word, proc_macro2::Span::call_site()),
None => RustSafeName::from_ident(&crate::model::default_key_output_name(&record)),
};
assert!(
model.key.compound(),
"`{record}` is the compound fixture this reading needs",
);
model
}
fn tight(model: &RecordModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn quoted_before(emitted: &str, anchor: &str, needle: &str) -> String {
let at = emitted
.find(anchor)
.unwrap_or_else(|| panic!("`{anchor}` is missing: {emitted}"));
let head = &emitted[..at];
let at = head
.rfind(needle)
.unwrap_or_else(|| panic!("`{needle}` is missing before `{anchor}`: {emitted}"));
read(&head[at + needle.len()..])
}
fn quoted_after(emitted: &str, anchor: &str, needle: &str) -> String {
let at = emitted
.find(anchor)
.unwrap_or_else(|| panic!("`{anchor}` is missing: {emitted}"));
let rest = &emitted[at..];
let at = rest
.find(needle)
.unwrap_or_else(|| panic!("`{needle}` is missing after `{anchor}`: {emitted}"));
read(&rest[at + needle.len()..])
}
fn read(rest: &str) -> String {
let end = rest.find('"').expect("a string literal closes");
rest[..end].to_owned()
}
pub(super) fn published(model: &RecordModel) -> (String, String) {
let emitted = tight(model);
let object = model
.graphql
.as_ref()
.expect("the fixture declares a face")
.key_output
.safe()
.to_string();
(
quoted_before(&emitted, &format!(")]impl{object}{{"), "name=\""),
quoted_after(
&emitted,
"InputTypeforVideoTrackKeyInput{",
"Cow::Borrowed(\"",
),
)
}
#[test]
fn an_unrenamed_address_publishes_the_record_stem_at_both_halves() {
assert_eq!(
published(&addressed(None)),
("VideoTrackKey".to_owned(), "VideoTrackKeyInput".to_owned()),
);
}
#[test]
fn a_declared_word_re_bases_the_object_and_the_argument_together() {
assert_eq!(
published(&addressed(Some("Address"))),
("Address".to_owned(), "AddressInput".to_owned()),
);
}
#[test]
fn the_argument_objects_rust_name_stays_where_the_emitter_put_it() {
let file: syn::File =
syn::parse2(addressed(Some("Address")).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(&"VideoTrackKeyInput".to_owned()),
"the schema word moved a Rust name: {landed:?}",
);
assert!(
!landed.iter().any(|name| name == "AddressInput"),
"the schema word minted a Rust name: {landed:?}",
);
}