use ingraph_codegen::{model::NodeKind, prelude::*};
use quote::ToTokens as _;
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")
}
fn manifest() -> syn::DeriveInput {
syn::parse_quote! {
#[ingraph(remote = "upstream::Dimensions")]
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
#[ingraph(copy)]
height: u32,
}
}
}
#[test]
fn the_word_reaches_the_model_as_the_path_it_names() {
let m = parse(&manifest(), NodeKind::Entity).expect("a wrapping entity");
let remote = m.remote.expect("the wrapped type").into_token_stream();
assert_eq!(remote.to_string().replace(' ', ""), "upstream::Dimensions");
}
#[test]
fn the_manifest_is_read_as_ordinary_columns() {
let m = parse(&manifest(), NodeKind::Entity).expect("a wrapping entity");
let names: Vec<String> = m
.fields
.iter()
.map(|field| field.column.source.to_string())
.collect();
assert_eq!(names, ["width", "height"]);
assert!(m.fields[0].identity, "the `id` field bears identity");
assert!(!m.fields[1].identity);
assert_eq!(m.key.id.source, "width");
}
#[test]
fn a_manifest_fields_docs_are_carried_and_an_ordinary_fields_are_not() {
let wrapping = parse(&manifest(), NodeKind::Entity).expect("a wrapping entity");
assert_eq!(wrapping.fields[0].docs.len(), 1, "the `///` is carried");
assert!(wrapping.fields[1].docs.is_empty(), "and only where written");
let plain = parse(
&syn::parse_quote! {
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
}
},
NodeKind::Entity,
)
.expect("an ordinary entity");
assert!(
plain.fields[0].docs.is_empty(),
"an ordinary declaration keeps its own fields, so nothing has to carry their docs"
);
}
#[test]
fn an_operator_publishes_no_address_and_takes_no_wrapper() {
let refused = refusals(&manifest(), NodeKind::Operator);
assert!(
refused.contains("publishes no address"),
"the refusal names the rung's own absence: {refused}"
);
assert!(
refused.contains("`entity`, `record` and `fields`"),
"and names the three that take it: {refused}"
);
}
#[test]
fn a_record_takes_the_word_beside_its_stores() {
let m = parse(
&syn::parse_quote! {
#[ingraph(remote = "upstream::Dimensions", sqlite)]
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
#[ingraph(copy)]
height: u32,
}
},
NodeKind::Record,
)
.expect("a wrapping record");
assert!(m.remote.is_some());
assert!(m.persist.is_some(), "the store words are untouched by it");
}
#[test]
fn one_declaration_wraps_one_type() {
let refused = refusals(
&syn::parse_quote! {
#[ingraph(remote = "a::One", remote = "b::Two")]
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
}
},
NodeKind::Entity,
);
assert!(
refused.contains("`remote` is declared more than once"),
"{refused}"
);
}
#[test]
fn a_wrapper_cannot_also_fan_in() {
let refused = refusals(
&syn::parse_quote! {
#[ingraph(
remote = "upstream::Dimensions",
upstream(node = "a::One"),
upstream(node = "b::Two"),
)]
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
}
},
NodeKind::Entity,
);
assert!(
refused.contains("cannot also draw from several independent upstreams"),
"{refused}"
);
}
#[test]
fn the_representation_is_the_macros_own() {
let refused = refusals(
&syn::parse_quote! {
#[ingraph(remote = "upstream::Dimensions")]
#[repr(C)]
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
}
},
NodeKind::Entity,
);
assert!(
refused.contains("does not choose its own representation"),
"{refused}"
);
}
#[test]
fn a_manifest_field_carries_no_attribute_of_its_own() {
let refused = refusals(
&syn::parse_quote! {
#[ingraph(remote = "upstream::Dimensions")]
pub struct Dimensions {
#[ingraph(id, copy)]
#[serde(rename = "w")]
width: u32,
}
},
NodeKind::Entity,
);
assert!(
refused.contains("is a MANIFEST entry"),
"the refusal names what a manifest field is: {refused}"
);
}
#[test]
fn a_conditional_manifest_field_takes_its_seat() {
for word in [
quote::quote!(#[cfg(feature = "x")]),
quote::quote!(#[cfg_attr(feature = "x", derive(Debug))]),
] {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(remote = "upstream::Dimensions")]
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
#word
height: u32,
}
};
parse(&input, NodeKind::Entity).expect("a manifest field's condition has a seat");
}
}
#[test]
fn an_ordinary_declaration_keeps_every_attribute_it_writes() {
let m = parse(
&syn::parse_quote! {
#[repr(C)]
pub struct Dimensions {
#[ingraph(id, copy)]
#[serde(rename = "w")]
#[cfg_attr(feature = "x", derive(Debug))]
width: u32,
}
},
NodeKind::Entity,
)
.expect("an ordinary entity is refused none of the rewrite's rules");
assert!(m.remote.is_none());
}
#[test]
fn the_host_is_replaced_by_the_transparent_wrapper() {
let input: syn::DeriveInput = syn::parse_quote! {
#[derive(Debug, Clone)]
#[ingraph(remote = "upstream::Dimensions")]
pub struct Dimensions {
#[ingraph(id, copy)]
width: u32,
}
};
let remote: syn::Path = syn::parse_quote!(upstream::Dimensions);
let host = crate::host::transparent(&input, &remote).to_string();
let host = host.replace(' ', "");
assert!(
host.contains("#[repr(transparent)]"),
"the representation is the macro's: {host}"
);
assert!(
host.contains("pubstructDimensions(upstream::Dimensions)"),
"one unnamed position holding the wrapped type: {host}"
);
assert!(
host.contains("#[derive(Debug,Clone)]"),
"the author's derives are forwarded: {host}"
);
assert!(
host.contains("Aframe'sdimensions."),
"and so is their documentation: {host}"
);
assert!(
!host.contains("ingraph("),
"the markers this macro read are consumed: {host}"
);
}