use super::{Face, Mode};
fn refused(read: syn::Result<Mode>) -> String {
match read {
Ok(_) => panic!("the declaration is refused"),
Err(errors) => errors
.into_iter()
.map(|error| error.to_string())
.collect::<Vec<_>>()
.join("\n"),
}
}
fn element(read: syn::Result<Mode>) -> ingraph_codegen::element::ElementModel {
match read.expect("a marked declaration reads as an element") {
Mode::Element(model) => *model,
Mode::Composed(_) | Mode::Struct(_) => panic!("`operator(…)` is the element mode's watershed"),
}
}
#[test]
fn the_marker_decides_the_identity_and_the_shape_does_not() {
let unmarked: syn::DeriveInput = syn::parse_quote! {
pub struct Caption {
words: String,
}
};
assert!(matches!(
super::parse(&unmarked, Face::Filter).expect("an unmarked struct is the struct mode"),
Mode::Struct(_)
));
let marked: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(string))]
pub struct Caption {
words: String,
}
};
let model = element(super::parse(&marked, Face::Filter));
assert_eq!(model.filter.safe().to_string(), "CaptionFilter");
assert_eq!(
model.family,
ingraph_codegen::element::ElementFamily::String
);
}
#[test]
fn a_bare_marker_is_the_scalar_family() {
let bare: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator)]
pub struct Weight(i64);
};
let spelled: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(scalar))]
pub struct Weight(i64);
};
assert_eq!(
element(super::parse(&bare, Face::Filter)).family,
element(super::parse(&spelled, Face::Filter)).family
);
assert_eq!(
element(super::parse(&bare, Face::Filter)).family,
ingraph_codegen::element::ElementFamily::Scalar
);
}
#[test]
fn each_family_word_resolves_to_its_own_family() {
for family in ingraph_codegen::element::ElementFamily::ALL {
let word = syn::Ident::new_raw(family.word(), proc_macro2::Span::call_site());
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(#word))]
pub struct Value(String);
};
assert_eq!(element(super::parse(&input, Face::Filter)).family, family);
}
}
#[test]
fn a_family_with_no_operand_parameter_refuses_by_name() {
for word in ["bool", "duration"] {
let ident = syn::Ident::new(word, proc_macro2::Span::call_site());
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(#ident))]
pub struct Value(i64);
};
let sentence = refused(super::parse(&input, Face::Filter));
assert!(sentence.contains(word), "{sentence}");
assert!(sentence.contains("no operand parameter"), "{sentence}");
}
}
#[test]
fn a_collection_word_refuses_as_a_position_rather_than_as_a_kind() {
for word in ["list", "set", "quantified"] {
let ident = syn::Ident::new(word, proc_macro2::Span::call_site());
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(#ident))]
pub struct Value(i64);
};
let sentence = refused(super::parse(&input, Face::Filter));
assert!(sentence.contains("collection POSITIONS"), "{sentence}");
}
}
#[test]
fn an_unknown_family_names_the_four() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(colour))]
pub struct Value(i64);
};
let sentence = refused(super::parse(&input, Face::Filter));
for family in ingraph_codegen::element::ElementFamily::ALL {
assert!(sentence.contains(family.word()), "{sentence}");
}
}
#[test]
fn a_marked_declaration_that_is_not_one_value_refuses_by_name() {
let two: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(string))]
pub struct Span {
from: String,
to: String,
}
};
let sentence = refused(super::parse(&two, Face::Filter));
assert!(sentence.contains("holds ONE value"), "{sentence}");
assert!(sentence.contains("holds 2"), "{sentence}");
let unit: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(string))]
pub struct Nothing;
};
let sentence = refused(super::parse(&unit, Face::Filter));
assert!(sentence.contains("holds 0"), "{sentence}");
}
#[test]
fn a_marked_enum_names_the_vocabulary_door() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(r#enum))]
pub enum Codec {
H264,
}
};
let sentence = refused(super::parse(&input, Face::Filter));
assert!(sentence.contains("ingraph::Enum"), "{sentence}");
}
#[test]
fn the_marker_refuses_at_the_two_doors_that_face_a_row() {
for face in [Face::Comparison, Face::OrderBy] {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(string))]
pub struct Tag(String);
};
let sentence = refused(super::parse(&input, face));
assert!(sentence.contains("declares an ELEMENT"), "{sentence}");
assert!(
sentence.contains("#[derive(ingraph::Filter)]"),
"{sentence}"
);
}
}
#[test]
fn the_container_words_an_element_does_not_spend_refuse_by_name() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(
operator(string),
sqlite,
comparison(rename = "TagPredicate"),
order_by(rename = "TagOrder"),
filter = "TagWhere"
)]
pub struct Tag(String);
};
let sentence = refused(super::parse(&input, Face::Filter));
for word in [
"`sqlite`",
"comparison(rename",
"order_by(rename",
"filter = ",
] {
assert!(sentence.contains(word), "{sentence}");
}
}
#[test]
fn a_word_on_the_elements_own_field_refuses_by_name() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(string))]
pub struct Tag(#[ingraph(string)] String);
};
let sentence = refused(super::parse(&input, Face::Filter));
assert!(sentence.contains("takes no `#[ingraph(…)]`"), "{sentence}");
}
#[test]
fn the_rename_word_moves_the_minted_filters_name() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(string), filter(rename = "LabelPredicate"))]
pub struct Label(String);
};
assert_eq!(
element(super::parse(&input, Face::Filter))
.filter
.safe()
.to_string(),
"LabelPredicate"
);
}
#[test]
fn a_second_marker_refuses() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(string), operator(scalar))]
pub struct Tag(String);
};
let sentence = refused(super::parse(&input, Face::Filter));
assert!(sentence.contains("more than once"), "{sentence}");
}
#[test]
fn the_element_scalar_takes_the_word_the_declaration_gives_it() {
let model = element(super::parse(
&syn::parse_quote! {
#[ingraph(operator(string), graphql(rename = "Label"))]
pub struct Tag(String);
},
Face::Filter,
));
assert_eq!(model.wire_name.as_deref(), Some("Label"));
assert_eq!(model.subject, "Tag", "the Rust name does not move");
assert_eq!(
model.filter.external(),
"TagFilter",
"and neither does the minted filter's, which has a word of its own"
);
let bare = element(super::parse(
&syn::parse_quote! {
#[ingraph(operator(string))]
pub struct Tag(String);
},
Face::Filter,
));
assert_eq!(bare.wire_name, None);
}
#[test]
fn the_element_scalars_word_rides_the_name_grammar() {
let refused = refused(super::parse(
&syn::parse_quote! {
#[ingraph(operator(string), graphql(rename = "Not A Name"))]
pub struct Tag(String);
},
Face::Filter,
));
assert!(refused.contains("is not a GraphQL name"), "{refused}");
let keyword = element(super::parse(
&syn::parse_quote! {
#[ingraph(operator(string), graphql(rename = "type"))]
pub struct Tag(String);
},
Face::Filter,
));
assert_eq!(keyword.wire_name.as_deref(), Some("type"));
}
#[test]
fn refuses_the_scalars_word_where_no_scalar_is_minted() {
let composed = refused(super::parse(
&syn::parse_quote! {
#[ingraph(composed, graphql(rename = "Rate"))]
pub struct RateFilter { eq: Option<u32> }
},
Face::Filter,
));
assert!(
composed.contains("mints no scalar for a schema to name"),
"{composed}"
);
let plain = refused(super::parse(
&syn::parse_quote! {
#[ingraph(graphql(rename = "Rate"))]
pub struct Rate { hz: u32 }
},
Face::Filter,
));
assert!(
plain.contains("names the SCALAR an element publishes"),
"{plain}"
);
assert!(
plain.contains("`operator(<family>)`"),
"and it names the reading that would give it one: {plain}"
);
}