use quote::ToTokens as _;
use crate::{
aggregate::{AggregateModel, Grouped, Measure, Measured},
model::Projection,
};
fn folding(measures: &[(&str, Measure, Option<&str>)]) -> AggregateModel {
let ident = |name: &str| syn::Ident::new(name, proc_macro2::Span::call_site());
AggregateModel {
aggregate: ident("CodecCensus"),
vis: syn::parse_quote!(pub),
krate: syn::parse_quote!(::ingraph),
krate_spelled: None,
upstream: syn::parse_quote!(demux::Track),
groups: vec![Grouped {
field: ident("codec"),
from: ident("codec_name"),
ty: syn::parse_quote!(Codec),
projection: Projection::Borrowed,
}],
measures: measures
.iter()
.map(|(field, by, from)| Measured {
field: ident(field),
by: by.clone(),
from: from.map(ident),
ty: syn::parse_quote!(u64),
projection: Projection::Borrowed,
})
.collect(),
triggers: proc_macro2::TokenStream::new(),
stores: proc_macro2::TokenStream::new(),
graphql: None,
}
}
fn tight(model: &AggregateModel) -> String {
model.fold().to_string().replace(' ', "")
}
#[test]
fn a_group_column_is_read_off_the_upstream_by_the_name_the_entry_gave() {
let emitted = tight(&folding(&[("count", Measure::Counting, None)]));
assert!(
emitted.contains("demux::Track::codec_name_ref(__ig_row)"),
"the read is the upstream's accessor, qualified: {emitted}"
);
assert!(
!emitted.contains("codec_ref"),
"and never this declaration's own field name: {emitted}"
);
}
#[test]
fn a_measure_is_called_statically_with_its_item_type_written() {
let emitted = tight(&folding(&[("count", Measure::Counting, None)]));
assert!(
emitted.contains("<::ingraph::__private::measure::Countingas::ingraph::__private::measure::Measure<&demux::Track>>::fold"),
"the citizen, the trait and the item type are all named: {emitted}"
);
}
#[test]
fn a_shipped_word_and_a_declared_path_are_both_default_constructed() {
let shipped = tight(&folding(&[("count", Measure::Counting, None)]));
assert!(
shipped
.contains("<::ingraph::__private::measure::Countingas::core::default::Default>::default()"),
"{shipped}"
);
let declared = tight(&folding(&[(
"peak",
Measure::Declared(syn::parse_quote!(app::Largest)),
None,
)]));
assert!(
declared.contains("<app::Largestas::core::default::Default>::default()"),
"the path is emitted verbatim for rustc to resolve: {declared}"
);
}
#[test]
fn the_slice_a_fold_is_handed_is_what_it_owes_its_entry() {
let counted = tight(&folding(&[("count", Measure::Counting, None)]));
assert!(
counted.contains(">::fold(&<::ingraph::__private::measure::Countingas::core::default::Default>::default(),&__ig_held)"),
"counting folds the group's rows: {counted}"
);
let summed = tight(&folding(&[("bytes", Measure::Summing, Some("size"))]));
assert!(
summed.contains("demux::Track::size_ref(__ig_row)"),
"summing reads the column its entry named: {summed}"
);
assert!(
summed.contains("&__ig_column)"),
"and is handed that column's values rather than the rows: {summed}"
);
}
#[test]
fn one_row_per_group_fills_every_column_the_declaration_wrote() {
let emitted = tight(&folding(&[
("count", Measure::Counting, None),
("bytes", Measure::Summing, Some("size")),
]));
assert!(emitted.contains("Self{codec,count:"), "{emitted}");
assert!(emitted.contains("bytes:"), "{emitted}");
assert!(
!emitted.contains("Default::default()}"),
"no struct-update tail hides a column nobody filled: {emitted}"
);
}
#[test]
fn the_groups_are_found_by_equality_and_nothing_heavier() {
let emitted = tight(&folding(&[("count", Measure::Counting, None)]));
assert!(
emitted.contains("__ig_groups.iter_mut().find(|(__ig_seen,_)|*__ig_seen==__ig_key)"),
"{emitted}"
);
for heavier in ["HashMap", "BTreeMap", "sort_by", "Hash", "cmp"] {
assert!(
!emitted.contains(heavier),
"the fold reaches for no `{heavier}`: {emitted}"
);
}
}
#[test]
fn the_fold_is_as_visible_as_the_declaration_and_takes_its_upstream() {
let model = folding(&[("count", Measure::Counting, None)]);
let emitted = tight(&model);
assert!(
emitted.contains("pubfnfold(__ig_rows:&[demux::Track])->::std::vec::Vec<Self>"),
"{emitted}"
);
assert_eq!(
model.vis.to_token_stream().to_string(),
"pub",
"the visibility read back is the one the declaration wrote",
);
}