use quote::ToTokens as _;
use super::{columns, row};
fn compact(krate: &syn::Path) -> String {
rendered(krate).split_whitespace().collect()
}
fn rendered(krate: &syn::Path) -> String {
let emitted = row(
krate,
&syn::parse_quote!(VideoJournal),
&syn::parse_quote!(MediaId),
);
prettyplease::unparse(&syn::parse2(emitted).expect("the expansion parses as a file"))
}
fn shape() -> Vec<(String, String)> {
columns(&syn::parse_quote!(MediaId), &syn::parse_quote!(::ingraph))
.named
.into_iter()
.map(|field| {
(
field.ident.expect("a named field").to_string(),
field.ty.to_token_stream().to_string().replace(' ', ""),
)
})
.collect()
}
fn markers(field: &str) -> String {
columns(&syn::parse_quote!(MediaId), &syn::parse_quote!(::ingraph))
.named
.into_iter()
.find(|declared| declared.ident.as_ref().is_some_and(|name| name == field))
.expect("the canonical column")
.attrs
.iter()
.filter(|attr| attr.path().is_ident("ingraph"))
.map(|attr| attr.to_token_stream().to_string().replace(' ', ""))
.collect()
}
#[test]
fn a_journal_is_its_four_columns() {
assert_eq!(
shape(),
[
("root".to_owned(), "MediaId".to_owned()),
("node".to_owned(), "::std::string::String".to_owned()),
("station".to_owned(), "::std::string::String".to_owned()),
(
"at".to_owned(),
"::ingraph::__private::primitives::Timestamp".to_owned()
),
]
);
}
#[test]
fn the_node_carries_the_id_and_the_instant_does_not() {
assert_eq!(markers("node"), "#[ingraph(id)]");
assert_eq!(markers("at"), "#[ingraph(scalar)]");
}
#[test]
fn the_key_side_columns_carry_no_label() {
assert_eq!(markers("root"), "");
assert_eq!(markers("node"), "#[ingraph(id)]");
}
#[test]
fn the_mint_is_one_shape_on_either_rung() {
assert_eq!(markers("root"), "");
assert_eq!(markers("node"), "#[ingraph(id)]");
assert_eq!(markers("station"), "#[ingraph(string)]");
assert_eq!(markers("at"), "#[ingraph(scalar)]");
}
#[test]
fn the_row_contract_is_emitted_for_the_named_root() {
let emitted = compact(&syn::parse_quote!(::ingraph));
assert!(
emitted.contains("impl::ingraph::__private::drive::JournalRow<MediaId>forVideoJournal"),
"{}",
rendered(&syn::parse_quote!(::ingraph))
);
}
#[test]
fn the_instant_crossing_refuses_rather_than_rounding() {
let emitted = compact(&syn::parse_quote!(::ingraph));
assert!(
emitted.contains("::core::convert::TryFrom<::std::time::SystemTime")
&& emitted.contains("::try_from(at)"),
"{}",
rendered(&syn::parse_quote!(::ingraph))
);
assert!(
emitted.contains("::core::panic!"),
"the refusal arm stops: {}",
rendered(&syn::parse_quote!(::ingraph))
);
}
#[test]
fn every_framework_path_goes_through_the_shelf() {
let krate = syn::parse_quote!(::ingraph);
let emitted = compact(&krate);
for path in emitted.match_indices("::ingraph::").map(|(at, _)| at) {
assert!(
emitted[path..].starts_with("::ingraph::__private::"),
"a framework path skips the shelf: {}",
rendered(&krate)
);
}
}
#[test]
fn a_renamed_crate_moves_the_paths() {
let krate: syn::Path = syn::parse_quote!(crate::shim);
let emitted = compact(&krate);
assert!(
emitted.contains("crate::shim::__private::drive::JournalRow<MediaId>forVideoJournal"),
"{}",
rendered(&krate)
);
assert!(
!emitted.contains("::ingraph::"),
"nothing still names the default crate: {}",
rendered(&krate)
);
let at = columns(&syn::parse_quote!(MediaId), &krate)
.named
.into_iter()
.find(|field| field.ident.as_ref().is_some_and(|name| name == "at"))
.expect("the instant column")
.ty
.to_token_stream()
.to_string()
.replace(' ', "");
assert_eq!(at, "crate::shim::__private::primitives::Timestamp");
}