use ingraph_codegen::{model::NodeKind, prelude::*};
fn parse(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, arguments) = crate::host::split(input);
crate::parse::parse(&host, &arguments, NodeKind::Entity)
}
fn model(input: &syn::DeriveInput) -> RecordModel {
parse(input).expect("an accepted declaration")
}
fn refusals(input: &syn::DeriveInput) -> String {
parse(input)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn field<'a>(model: &'a RecordModel, name: &str) -> &'a Field {
model
.fields
.iter()
.find(|field| field.column.source == name)
.expect("a declared field")
}
macro_rules! wrapping {
($($container:tt)*) => {
syn::parse_quote! {
#[ingraph(remote = "upstream::Frame" $($container)*)]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy)]
width: u32,
#[ingraph(optional)]
note: Option<u8>,
}
}
};
}
#[test]
fn a_declaration_that_describes_nothing_carries_the_default_protocol() {
let m = model(&wrapping!());
assert_eq!(m.construction, Construction::default());
assert_eq!(m.construction.entry, "new");
assert!(m.construction.finish.is_none(), "and it is a constructor");
assert!(!m.construction.fallible);
for name in ["id", "width", "note"] {
assert_eq!(
field(&m, name).build,
Build::default(),
"`{name}` took the seat the law gives it"
);
}
}
#[test]
fn the_container_words_reach_the_model() {
let m = model(&wrapping!(, builder(
entry = "builder",
finish = "build",
fallible,
on(u32, into),
call(with = "with_unit", value = "Unit::Pixels"),
call(with = "with_scale", value = "2"),
)));
assert_eq!(m.construction.entry, "builder");
assert_eq!(
m.construction.finish.as_ref().expect("a closing call"),
"build"
);
assert!(m.construction.fallible);
assert!(m.construction.blankets(&syn::parse_quote!(u32)));
assert!(!m.construction.blankets(&syn::parse_quote!(u64)));
let methods: Vec<String> = m
.construction
.calls
.iter()
.map(|call| call.method.to_string())
.collect();
assert_eq!(
methods,
["with_unit", "with_scale"],
"an injection is repeatable, and the order is the declaration's"
);
}
#[test]
fn a_blanket_is_keyed_by_a_type_a_meta_path_could_not_hold() {
let m = model(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame", builder(on(Option<u8>, into)))]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(optional)]
note: Option<u8>,
}
});
assert!(m.construction.blankets(&syn::parse_quote!(Option<u8>)));
}
#[test]
fn the_seat_words_reach_the_field() {
let m = model(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame", builder(finish = "build"))]
pub struct Frame {
#[ingraph(id, builder(arg))]
id: i64,
#[ingraph(copy, builder(finish_arg))]
width: u32,
#[ingraph(copy, builder(with))]
weight: u8,
#[ingraph(copy, builder(with = "maybe_depth"))]
depth: u8,
#[ingraph(copy, builder(set))]
tint: u8,
#[ingraph(copy, builder(set = "put_gamma"))]
gamma: u8,
}
});
assert_eq!(field(&m, "id").build.seat, Some(Seat::Arg));
assert_eq!(field(&m, "width").build.seat, Some(Seat::FinishArg));
assert_eq!(field(&m, "weight").build.seat, Some(Seat::With(None)));
assert_eq!(
field(&m, "depth").build.seat,
Some(Seat::With(Some(syn::parse_quote!(maybe_depth))))
);
assert_eq!(field(&m, "tint").build.seat, Some(Seat::Set(None)));
assert_eq!(
field(&m, "gamma").build.seat,
Some(Seat::Set(Some(syn::parse_quote!(put_gamma))))
);
}
#[test]
fn the_crossing_words_reach_the_field() {
let m = model(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(into))]
width: u32,
#[ingraph(copy, builder(map = "narrow::width"))]
height: u32,
#[ingraph(optional, builder(option))]
note: Option<u8>,
#[ingraph(copy, builder(skip))]
cached: u8,
}
});
assert_eq!(field(&m, "width").build.cross, Some(Cross::Into));
assert_eq!(
field(&m, "height").build.cross,
Some(Cross::Map(syn::parse_quote!(narrow::width)))
);
assert!(field(&m, "note").build.option);
assert!(field(&m, "cached").build.skip);
assert!(!field(&m, "width").build.skip, "and nothing else moved");
}
#[test]
fn a_construction_word_is_refused_where_nothing_is_wrapped() {
let reported = refusals(&syn::parse_quote! {
#[ingraph(builder(entry = "from_parts"))]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(arg))]
width: u32,
}
});
assert_eq!(
reported
.matches("describes how a type SOMEBODY ELSE declared is built")
.count(),
2,
"the declaration's word and the field's word are each refused: {reported}"
);
assert!(reported.contains("`remote = \"…\"`"));
}
#[test]
fn a_finish_argument_with_no_closing_call_is_refused_by_name() {
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(finish_arg))]
width: u32,
}
});
assert!(
reported.contains("`width` in the argument list of the protocol's CLOSING call"),
"{reported}"
);
assert!(reported.contains("builder(finish = \"…\")"));
}
#[test]
fn two_seat_words_on_one_field_are_refused_naming_both() {
for (first, second) in [
("arg", "with"),
("with", "set"),
("arg", "set"),
("arg", "finish_arg"),
] {
let first: syn::Ident = syn::parse_str(first).expect("a word");
let second: syn::Ident = syn::parse_str(second).expect("a word");
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame", builder(finish = "build"))]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(#first, #second))]
width: u32,
}
});
assert!(
reported.contains(&format!(
"`{first}` and `{second}` are both declared on this field"
)),
"{reported}"
);
}
}
#[test]
fn a_skipped_column_may_not_also_name_a_seat() {
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(skip, arg))]
width: u32,
}
});
assert!(
reported.contains("`skip` says this column is NOT handed back"),
"{reported}"
);
}
#[test]
fn a_call_that_names_no_method_or_hands_over_nothing_is_refused() {
let missing_value = refusals(&wrapping!(, builder(call(with = "with_unit"))));
assert!(
missing_value.contains("names a method and hands it nothing"),
"{missing_value}"
);
let missing_method = refusals(&wrapping!(, builder(call(value = "1"))));
assert!(
missing_method.contains("supplies a value and names no method"),
"{missing_method}"
);
let bare = refusals(&wrapping!(, builder(call)));
assert!(
bare.contains("`call(…)` is a link this declaration supplies"),
"{bare}"
);
}
#[test]
fn the_whole_option_word_is_refused_on_a_column_with_no_empty_state() {
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(option))]
width: Option<u32>,
}
});
assert!(
reported.contains("this column has no empty state to take"),
"{reported}"
);
}
#[test]
fn a_column_crosses_one_way() {
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(into, map = "narrow::width"))]
width: u32,
}
});
assert!(reported.contains("a column crosses ONE way"), "{reported}");
}
#[test]
fn a_pinned_target_and_a_crossing_inside_an_option_are_refused_together() {
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(optional, builder(into, option))]
note: Option<u8>,
}
});
assert!(
reported.contains("`into` pins the crossing's target"),
"{reported}"
);
}
#[test]
fn a_position_word_takes_no_name() {
let named_position = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(arg = "first"))]
width: u32,
}
});
assert!(
named_position.contains("names a POSITION in an argument list"),
"{named_position}"
);
}
#[test]
fn every_word_is_declared_once() {
for (words, sentence) in [
(
"entry = \"a\", entry = \"b\"",
"`entry` is declared more than once",
),
(
"finish = \"a\", finish = \"b\"",
"`finish` is declared more than once",
),
(
"fallible, fallible",
"`fallible` is declared more than once",
),
] {
let words: proc_macro2::TokenStream = syn::parse_str(words).expect("the words");
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame", builder(#words))]
pub struct Frame {
#[ingraph(id)]
id: i64,
}
});
assert!(reported.contains(sentence), "{reported}");
}
}
#[test]
fn the_block_itself_is_declared_once_per_seat() {
let container = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame", builder(entry = "a"), builder(entry = "b"))]
pub struct Frame {
#[ingraph(id)]
id: i64,
}
});
assert!(
container.contains("one declaration describes one protocol"),
"{container}"
);
let seat = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(arg), builder(with))]
width: u32,
}
});
assert!(seat.contains("one column fills one seat"), "{seat}");
}
#[test]
fn an_unknown_entry_names_the_seat_its_word_belongs_to() {
let container = refusals(&wrapping!(, builder(arg)));
assert!(
container.contains(
"The per-column words — `arg`, `with`, `set`, `map` and the rest — are \
written in a `builder(…)` on the FIELD"
),
"{container}"
);
let seat = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame")]
pub struct Frame {
#[ingraph(id)]
id: i64,
#[ingraph(copy, builder(entry = "new"))]
width: u32,
}
});
assert!(
seat.contains("are facts about the whole protocol and are written on the DECLARATION"),
"{seat}"
);
}
#[test]
fn a_blanket_takes_a_type_and_the_one_word() {
for written in ["on(u32)", "on(u32, map)", "on"] {
let words: proc_macro2::TokenStream = syn::parse_str(written).expect("the words");
let reported = refusals(&syn::parse_quote! {
#[ingraph(remote = "upstream::Frame", builder(#words))]
pub struct Frame {
#[ingraph(id)]
id: i64,
}
});
assert!(
reported.contains("is written `on(<Type>, into)`"),
"`{written}`: {reported}"
);
}
}