use quote::{format_ident, quote};
use super::{Construction, Filled, Injection, Seat, Supplied};
fn host() -> syn::Path {
syn::parse_quote!(upstream::Frame)
}
fn at(seat: &Seat, column: &str, value: &str) -> Supplied {
let column = format_ident!("{column}");
let value: syn::Expr = syn::parse_str(value).expect("an expression");
Supplied {
fill: seat.filled(&column),
gate: proc_macro2::TokenStream::new(),
value: quote!(#value),
}
}
fn written(construction: &Construction, supplied: &[Supplied]) -> String {
construction
.call(&host(), supplied)
.to_string()
.replace(' ', "")
}
#[test]
fn a_protocol_with_no_closing_call_is_a_chain_on_the_entry() {
let call = written(
&Construction::default(),
&[
at(&Seat::Arg, "id", "row.id"),
at(&Seat::With(None), "note", "row.note"),
],
);
assert_eq!(call, "upstream::Frame::new(row.id).with_note(row.note)");
}
#[test]
fn the_entry_word_moves_the_first_call_only() {
let construction = Construction {
entry: format_ident!("from_ratio"),
..Construction::default()
};
let call = written(&construction, &[at(&Seat::Arg, "id", "row.id")]);
assert_eq!(call, "upstream::Frame::from_ratio(row.id)");
}
#[test]
fn a_closing_call_gives_the_protocol_a_second_argument_list() {
let construction = Construction {
entry: format_ident!("builder"),
finish: Some(format_ident!("build")),
..Construction::default()
};
let call = written(
&construction,
&[
at(
&Seat::With(Some(format_ident!("width"))),
"width",
"row.width",
),
at(&Seat::FinishArg, "unit", "row.unit"),
],
);
assert_eq!(
call,
"upstream::Frame::builder().width(row.width).build(row.unit)"
);
}
#[test]
fn the_fallible_word_marks_the_terminal_step_and_only_that_one() {
let constructor = Construction {
fallible: true,
..Construction::default()
};
assert_eq!(
written(&constructor, &[at(&Seat::Arg, "id", "row.id")]),
"upstream::Frame::new(row.id)?"
);
let builder = Construction {
entry: format_ident!("builder"),
finish: Some(format_ident!("build")),
fallible: true,
..Construction::default()
};
assert_eq!(
written(&builder, &[at(&Seat::FinishArg, "id", "row.id")]),
"upstream::Frame::builder().build(row.id)?",
"the opening is not the step that can fail"
);
}
#[test]
fn an_in_place_setter_binds_the_value_and_mutates_it() {
let call = written(
&Construction::default(),
&[
at(&Seat::Arg, "id", "row.id"),
at(&Seat::Set(None), "tint", "row.tint"),
],
);
assert_eq!(
call,
"{letmut__ingraph_built=upstream::Frame::new(row.id);\
{__ingraph_built.set_tint(row.tint);}__ingraph_built}"
);
}
#[test]
fn a_gated_link_unrolls_and_keeps_its_gate_on_a_statement() {
let call = written(
&Construction::default(),
&[
at(&Seat::Arg, "id", "row.id"),
Supplied {
gate: quote!(#[cfg(feature = "x")]),
..at(&Seat::With(None), "note", "row.note")
},
],
);
assert_eq!(
call,
"{letmut__ingraph_built=upstream::Frame::new(row.id);\
#[cfg(feature=\"x\")]{__ingraph_built=__ingraph_built.with_note(row.note);}__ingraph_built}"
);
}
#[test]
fn a_gated_argument_rides_the_argument_list() {
let call = written(
&Construction::default(),
&[
at(&Seat::Arg, "id", "row.id"),
Supplied {
gate: quote!(#[cfg(unix)]),
..at(&Seat::Arg, "width", "row.width")
},
],
);
assert_eq!(call, "upstream::Frame::new(row.id,#[cfg(unix)]row.width)");
}
#[test]
fn an_injection_lands_after_the_columns_in_the_order_it_was_written() {
let construction = Construction {
calls: vec![
Injection {
method: format_ident!("with_unit"),
value: syn::parse_quote!(Unit::Pixels),
},
Injection {
method: format_ident!("with_scale"),
value: syn::parse_quote!(2),
},
],
..Construction::default()
};
assert_eq!(
written(&construction, &[at(&Seat::With(None), "note", "row.note")]),
"upstream::Frame::new().with_note(row.note).with_unit(Unit::Pixels).with_scale(2)"
);
}
#[test]
fn an_injection_survives_the_statement_form() {
let construction = Construction {
calls: vec![Injection {
method: format_ident!("with_unit"),
value: syn::parse_quote!(Unit::Pixels),
}],
..Construction::default()
};
let call = written(&construction, &[at(&Seat::Set(None), "tint", "row.tint")]);
assert_eq!(
call,
"{letmut__ingraph_built=upstream::Frame::new();\
{__ingraph_built.set_tint(row.tint);}\
__ingraph_built=__ingraph_built.with_unit(Unit::Pixels);__ingraph_built}"
);
}
#[test]
fn a_renamed_seat_spells_the_name_it_was_given() {
let renamed = Seat::With(Some(format_ident!("maybe_note")));
assert!(
matches!(renamed.filled(&format_ident!("note")), Filled::Link(name) if name == "maybe_note")
);
assert!(
matches!(Seat::With(None).filled(&format_ident!("note")), Filled::Link(name) if name == "with_note")
);
assert!(
matches!(Seat::Set(None).filled(&format_ident!("note")), Filled::Setter(name) if name == "set_note")
);
}
#[test]
fn a_blanket_claims_the_type_it_names_and_no_other() {
let construction = Construction {
blanket: vec![super::Blanket {
ty: syn::parse_quote!(Option<u8>),
}],
..Construction::default()
};
assert!(construction.blankets(&syn::parse_quote!(Option<u8>)));
assert!(!construction.blankets(&syn::parse_quote!(Option<u16>)));
assert!(!construction.blankets(&syn::parse_quote!(u8)));
}