use ingraph_codegen::{model::NodeKind, prelude::*};
fn parse(input: &syn::DeriveInput, kind: NodeKind) -> syn::Result<RecordModel> {
let (host, arguments) = crate::host::split(input);
crate::parse::parse(&host, &arguments, kind)
}
fn refusals(input: &syn::DeriveInput, kind: NodeKind) -> String {
parse(input, kind)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn a_data_fields_condition_takes_its_seat() {
let model = parse(
&syn::parse_quote! {
pub struct Frame {
#[ingraph(id, copy)]
id: u64,
#[cfg(feature = "x")]
#[ingraph(copy)]
width: u32,
#[ingraph(copy)]
height: u32,
}
},
NodeKind::Entity,
)
.expect("a condition on a data field is served");
assert!(
model.conditional(),
"the declaration knows it has a condition"
);
let width = syn::parse_quote!(width);
let height = syn::parse_quote!(height);
assert!(
!model.gate(&width).is_empty(),
"the conditional field carries its gate"
);
assert!(
model.gate(&height).is_empty(),
"an unconditional field costs not one token"
);
}
#[test]
fn two_conditions_are_one_conjunction() {
let model = parse(
&syn::parse_quote! {
pub struct Frame {
#[ingraph(id, copy)]
id: u64,
#[cfg(feature = "x")]
#[cfg(unix)]
#[ingraph(copy)]
width: u32,
}
},
NodeKind::Entity,
)
.expect("two conditions on one field are served");
let width = syn::parse_quote!(width);
let present = model.present(&width).to_string();
assert!(
present.contains("all") && present.contains("not"),
"the presence term is the conjunction and its negation: {present}"
);
}
#[test]
fn an_unconditional_field_pays_nothing() {
let model = parse(
&syn::parse_quote! {
pub struct Frame {
#[ingraph(id, copy)]
id: u64,
#[ingraph(copy)]
width: u32,
}
},
NodeKind::Entity,
)
.expect("a plain declaration is unchanged");
assert!(!model.conditional());
let width = syn::parse_quote!(width);
assert!(model.gate(&width).is_empty());
assert_eq!(model.present(&width).to_string(), "1usize");
}
#[test]
fn a_conditional_identity_is_refused_by_name() {
let refused = refusals(
&syn::parse_quote! {
pub struct Frame {
#[cfg(feature = "x")]
#[ingraph(id, copy)]
id: u64,
#[ingraph(copy)]
width: u32,
}
},
NodeKind::Entity,
);
assert!(
refused.contains("identity is not conditional"),
"the refusal names the rule: {refused}"
);
}
#[test]
fn a_conditional_ingraph_word_is_refused_by_name() {
let refused = refusals(
&syn::parse_quote! {
pub struct Frame {
#[ingraph(id, copy)]
id: u64,
#[cfg_attr(feature = "x", ingraph(copy))]
width: u32,
}
},
NodeKind::Entity,
);
assert!(
refused.contains("cannot carry an `ingraph(…)` word"),
"the refusal names what it cannot decide: {refused}"
);
}
#[test]
fn a_conditional_foreign_word_passes_through() {
parse(
&syn::parse_quote! {
pub struct Frame {
#[ingraph(id, copy)]
id: u64,
#[cfg_attr(feature = "x", serde(rename = "w"))]
#[ingraph(copy)]
width: u32,
}
},
NodeKind::Entity,
)
.expect("an attribute this grammar does not own is not this grammar's to refuse");
}